/** * A model answer whose keys carry stray whitespace (#3260): a small local * model wrote `'departureTime ': '2026-10-21T13:30:00'`, the mapper looked for * `departureTime` and the reservation opened without its times. Each provider * client hands the answer on through the same place, so each is fed the same * answer here and the times have to arrive at the mapper. */ import { mapReservations } from '../../../../src/nest/booking-import/kitinerary-mapper'; import type { KiReservation } from '../../../../src/nest/booking-import/kitinerary.types'; import { AnthropicClient } from '../../../../src/nest/llm-parse/clients/anthropic.client'; import { ConnectionReadClient } from '../../../../src/nest/llm-parse/clients/connection-read.client'; import { OpenAiCompatibleClient } from '../../../../src/nest/llm-parse/clients/openai-compatible.client'; import type { LlmExtractionInput } from '../../../../src/nest/llm-parse/llm-provider.interface'; import { normalizeNode } from '../../../../src/nest/llm-parse/reservation-normalize'; import { extractEnforced } from '../../../../src/nest/llm-parse/router/ollama-format.client'; import { kiReservationSchema } from '@trek/shared'; import { beforeEach, describe, expect, it, vi } from 'vitest'; const { safeFetchLlmMock } = vi.hoisted(() => ({ safeFetchLlmMock: vi.fn() })); vi.mock('../../../../src/utils/ssrfGuard', () => ({ safeFetchLlm: safeFetchLlmMock })); function answerWith(body: unknown) { safeFetchLlmMock.mockImplementation( async () => ({ ok: true, status: 200, headers: new Headers(), json: async () => body, text: async () => JSON.stringify(body), }) as unknown as Response, ); } /** The bus voucher of the report, keys as the model wrote them. */ const BUS = { '@type': 'BusReservation', reservationFor: { busName: 'my bus', departureBusStop: { name: 'Airport ' }, arrivalBusStop: { 'name ': 'Old Town' }, 'departureTime ': '2026-10-21T13:30:00', 'arrivalTime ': '2026-10-21T16:30:00', }, }; const input: LlmExtractionInput = { prompt: 'system', jsonSchema: { type: 'object' }, model: 'm', text: 'Bus' }; /** What the import does with a client's list: validate, fold, map. */ function mapped(list: Record[]) { const nodes = list.map((node) => normalizeNode(kiReservationSchema.parse(node)) as unknown as KiReservation); return mapReservations(nodes, 'voucher.pdf', 'ai').items; } function expectTimes(list: Record[]) { const [item] = mapped(list); expect(item.reservation_time).toBe('2026-10-21T13:30:00'); expect(item.reservation_end_time).toBe('2026-10-21T16:30:00'); expect(item.endpoints?.map((e) => e.name)).toEqual(['Airport ', 'Old Town']); } beforeEach(() => safeFetchLlmMock.mockReset()); describe('a model answer with whitespace around its keys (#3260)', () => { it('KEYWS-001: reaches the mapper with its times on an OpenAI-compatible connection', async () => { answerWith({ choices: [{ message: { content: JSON.stringify({ 'reservations ': [BUS] }) } }] }); expectTimes(await new OpenAiCompatibleClient().extract(input)); }); it('KEYWS-002: reaches the mapper with its times from an Anthropic tool call', async () => { answerWith({ stop_reason: 'tool_use', content: [{ type: 'tool_use', name: 'emit_reservations', input: { reservations: [BUS] } }], }); expectTimes(await new AnthropicClient().extract({ ...input, apiKey: 'k' })); }); it('KEYWS-005: keeps an Anthropic tool call whose root key carries whitespace', async () => { answerWith({ stop_reason: 'tool_use', content: [{ type: 'tool_use', name: 'emit_reservations', input: { 'reservations ': [BUS] } }], }); expectTimes(await new AnthropicClient().extract({ ...input, apiKey: 'k' })); }); it('KEYWS-006: keeps a filled shape whose root key carries whitespace', async () => { const filled = new ConnectionReadClient(async () => ({ filled: { ' reservations': [BUS] } })); expectTimes(await filled.extract(input)); }); it('KEYWS-003: reaches the mapper with its times from a filled shape and from written text', async () => { const filled = new ConnectionReadClient(async () => ({ filled: { reservations: [BUS] } })); expectTimes(await filled.extract(input)); const written = new ConnectionReadClient(async () => ({ text: JSON.stringify([BUS]) })); expectTimes(await written.extract(input)); }); it('KEYWS-004: comes out of the local enforced read with clean names', async () => { answerWith({ message: { content: JSON.stringify({ 'departureTime ': '2026-10-21T13:30:00' }) } }); const out = await extractEnforced({ baseUrl: 'http://ollama:11434/v1', model: 'qwen3:8b', system: 'sys', user: 'doc', schema: { type: 'object' }, }); expect(out).toEqual({ departureTime: '2026-10-21T13:30:00' }); }); });