Open History to see the last commit
1import { test, expect } from './fixtures'
2
3/**
4 * Wire-format verification for `{ type: 'file' }` content sources (#909).
5 *
6 * Uploads can't run against aimock, so the route at `/api/file-source-wire`
7 * drives `chat()` with a synthetic provider handle and this spec inspects
8 * aimock's journal (`GET /v1/_requests`) for the provider's native file
9 * reference. Aimock normalises every request body to an OpenAI-compatible
10 * chat form before journalling, and that normalisation strips user-message
11 * image parts for all three providers — so the positive assertions here are
12 * the end-to-end ok:true round-trip (the mapping ran to completion and the
13 * request reached the provider endpoint), with the structural wire proof in
14 * the per-package unit tests (packages/ai-openai/tests/files-source.test.ts,
15 * packages/ai-anthropic/tests/files-source.test.ts,
16 * packages/ai-gemini/tests/files-source.test.ts). The cross-provider
17 * rejection path IS asserted end-to-end, for both a RUN_ERROR-yielding
18 * adapter (OpenAI) and a throwing one (Gemini).
19 */
20// Serial mode: each test clears then re-populates the aimock journal.
21test.describe.configure({ mode: 'serial' })
22
23test.describe('file content source — wire format', () => {
24 test.beforeEach(async ({ request, aimockPort }) => {
25 await request.delete(`http://127.0.0.1:${aimockPort}/v1/_requests`)
26 })
27
28 test('openai: an own-provider handle completes the round-trip (input_image.file_id covered by unit test)', async ({
29 request,
30 aimockPort,
31 testId,
32 }) => {
33 const res = await request.post(
34 `/api/file-source-wire?provider=openai&testId=${encodeURIComponent(testId)}`,
35 )
36 const { ok } = (await res.json()) as { ok: boolean; error?: string }
37 expect(ok).toBe(true)
38 // The request must actually have reached the mock endpoint — ok:true with
39 // an empty journal would mean the call never left the adapter.
40 const journal = await request.get(
41 `http://127.0.0.1:${aimockPort}/v1/_requests`,
42 )
43 const entries = (await journal.json()) as Array<{ body: any }>
44 expect(entries.length).toBeGreaterThan(0)
45 })
46
47 test('openai: a gemini-only reference is rejected — no openai entry in the record', async ({
48 request,
49 testId,
50 }) => {
51 const res = await request.post(
52 `/api/file-source-wire?provider=openai&handleProvider=gemini&testId=${encodeURIComponent(testId)}`,
53 )
54 const { ok, error } = (await res.json()) as { ok: boolean; error?: string }
55 expect(ok).toBe(false)
56 expect(error).toMatch(/openai/)
57 expect(error).toMatch(/issued by gemini/)
58 })
59
60 test('anthropic: an own-provider handle completes the round-trip (file_id block covered by unit test)', async ({
61 request,
62 testId,
63 }) => {
64 const res = await request.post(
65 `/api/file-source-wire?provider=anthropic&testId=${encodeURIComponent(testId)}`,
66 )
67 const { ok } = (await res.json()) as { ok: boolean; error?: string }
68 // Structural proof that the handle becomes a { type: 'file', file_id }
69 // source with the files-api beta lives in
70 // packages/ai-anthropic/tests/files-source.test.ts — aimock's journal
71 // normalisation strips the block so it can't be asserted here.
72 expect(ok).toBe(true)
73 })
74
75 test('gemini: an own-provider handle completes the round-trip (fileData.fileUri covered by unit test)', async ({
76 request,
77 testId,
78 }) => {
79 const res = await request.post(
80 `/api/file-source-wire?provider=gemini&testId=${encodeURIComponent(testId)}`,
81 )
82 const { ok } = (await res.json()) as { ok: boolean; error?: string }
83 expect(ok).toBe(true)
84 })
85
86 test('gemini: an openai-only reference is rejected before any request', async ({
87 request,
88 testId,
89 }) => {
90 const res = await request.post(
91 `/api/file-source-wire?provider=gemini&handleProvider=openai&testId=${encodeURIComponent(testId)}`,
92 )
93 const { ok, error } = (await res.json()) as { ok: boolean; error?: string }
94 expect(ok).toBe(false)
95 expect(error).toMatch(/gemini/)
96 expect(error).toMatch(/issued by openai/)
97 })
98})