Open History to see the last commit
1import { test, expect } from './fixtures'
2import type { APIRequestContext } from '@playwright/test'
3
4/**
5 * An ordinary function whose public name collides with a provider-native tool
6 * must reach the provider as a plain function tool, keeping its schema.
7 *
8 * Before the runtime-discriminator fix the Gemini converter dispatched on
9 * `tool.name`, so a user function called `google_search` was swallowed into
10 * `{googleSearch: {...}}` and its `parameters` were lost entirely.
11 *
12 * Reading the assertion: aimock's `/v1/_requests` journal does NOT store the
13 * raw provider body — the Gemini handler records the request already normalized
14 * into the Chat-Completions envelope (`geminiToCompletionRequest`), which is why
15 * the tool appears as `{type:'function', function:{...}}` rather than Gemini's
16 * `functionDeclarations`. Do not "correct" this to the provider-native shape;
17 * the journal cannot produce it.
18 *
19 * That normalization is exactly what gives the assertion teeth: only tools that
20 * survive as functions appear in the journal, so a misrouted tool vanishes.
21 * Verified against the pre-fix converter — the captured `tools` array comes
22 * back empty and this expectation fails.
23 *
24 * The OpenAI leg of the same fix cannot be covered from this app; see the note
25 * in `api.provider-tool-dispatch-wire.ts` for why, and
26 * `packages/openai-base/tests/provider-tool-dispatch.test.ts` for where it is
27 * covered instead.
28 */
29test.describe('provider tools — custom-name wire dispatch', () => {
30 test('Gemini preserves google_search as a function declaration', async ({
31 request,
32 aimockPort,
33 testId,
34 }) => {
35 const response = await request.post(
36 `/api/provider-tool-dispatch-wire?provider=gemini&testId=${encodeURIComponent(testId)}`,
37 )
38 expect(response.ok()).toBe(true)
39 const result = (await response.json()) as { ok: boolean; error?: string }
40 expect(result, result.error).toMatchObject({ ok: true })
41
42 const tools = await readCapturedTools(request, aimockPort, testId)
43
44 expect(tools).toContainEqual(
45 expect.objectContaining({
46 type: 'function',
47 // Schema shape is deliberately not asserted here: the adapter sends it
48 // as `parametersJsonSchema`, and aimock's journal copies only
49 // `parameters`, so it never appears. The schema leg is covered by the
50 // raw mount in `arktype-tool-wire.spec.ts`.
51 function: expect.objectContaining({ name: 'google_search' }),
52 }),
53 )
54 })
55})
56
57/**
58 * Reads the tools this test's own request carried.
59 *
60 * aimock is a singleton shared by every Playwright worker, so neither clearing
61 * the journal nor taking `entries[0]` is safe: a parallel spec can wipe the
62 * journal between our write and our read, or land its own entry first.
63 * `createTextAdapter` tags every provider call with an `X-Test-Id` header and
64 * aimock stores request headers next to the body, so select this test's entry by
65 * that id rather than mutating state other workers depend on.
66 */
67async function readCapturedTools(
68 request: APIRequestContext,
69 aimockPort: number,
70 testId: string,
71): Promise<Array<Record<string, unknown>>> {
72 const response = await request.get(
73 `http://127.0.0.1:${aimockPort}/v1/_requests`,
74 )
75 const entries = (await response.json()) as Array<{
76 headers?: Record<string, string>
77 body: { tools?: Array<Record<string, unknown>> } | null
78 }>
79
80 const mine = entries.filter((entry) =>
81 Object.entries(entry.headers ?? {}).some(
82 ([key, value]) => key.toLowerCase() === 'x-test-id' && value === testId,
83 ),
84 )
85
86 expect(
87 mine,
88 `no aimock journal entry carried X-Test-Id "${testId}"`,
89 ).not.toHaveLength(0)
90
91 return mine.flatMap((entry) => entry.body?.tools ?? [])
92}