Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

lazy-tools-wire.spec.ts

136 lines4.9 KB

1import { test, expect } from './fixtures'
2​
3/**
4 * Wire-format coverage for the lazy-tools `lazyToolsConfig.includeDescription`
5 * knob (shared by `chat()` and Code Mode).
6 *
7 * Lazy tools are surfaced to the model through a synthetic discovery tool
8 * (`__lazy__tool__discovery__`) whose `description` embeds the lazy-tool
9 * catalog. `includeDescription` tunes how much of each lazy tool's description
10 * that catalog shows:
11 * - 'none' (default) → bare names, byte-identical to legacy behavior
12 * - 'first-sentence' → `name — <first sentence>`
13 * - 'full' → `name — <full description>`
14 *
15 * With aimock the model never reflects the discovery tool's description back to
16 * the browser, so the catalog is observable only on the provider request. This
17 * spec drives `/api/lazy-tools-wire` (OpenAI chat adapter, two `lazy: true`
18 * tools) and inspects aimock's journal (`GET /v1/_requests`) to assert the
19 * discovery tool's catalog crossed the wire at the configured detail level.
20 */
21​
22type JournalEntry = {
23 headers?: Record<string, string>
24 body: {
25 tools?: Array<{
26 type?: string
27 function?: { name?: string; description?: string }
28 }>
29 } | null
31​
32const DISCOVERY_TOOL_NAME = '__lazy__tool__discovery__'
33​
34/**
35 * Find the discovery tool's wire `description` for this test's request only.
36 *
37 * The journal is shared across all parallel tests on the one aimock instance,
38 * so entries are filtered by the test's `X-Test-Id` (the same header that
39 * isolates fixture sequencing) before reading the catalog text.
40 */
41async function discoveryDescription(
42 request: import('@playwright/test').APIRequestContext,
43 aimockPort: number,
44 testId: string,
45): Promise<string | undefined> {
46 const journalRes = await request.get(
47 `http://127.0.0.1:${aimockPort}/v1/_requests`,
48 )
49 const entries = (await journalRes.json()) as Array<JournalEntry>
50 for (const entry of entries) {
51 if (entry.headers?.['x-test-id'] !== testId) continue
52 const discovery = entry.body?.tools?.find(
53 (t) => t.function?.name === DISCOVERY_TOOL_NAME,
54 )
55 if (discovery?.function?.description) {
56 return discovery.function.description
57 }
58 }
59 return undefined
61​
62test.describe('lazy tools — discovery catalog wire format', () => {
63 // No journal reset here: the shared aimock journal is filtered per-test by
64 // X-Test-Id (see discoveryDescription), so a global DELETE would only race
65 // with adjacent parallel specs on the same aimock instance.
66 test("includeDescription: 'none' sends bare lazy tool names (legacy default)", async ({
67 request,
68 aimockPort,
69 testId,
70 }) => {
71 const res = await request.post(
72 `/api/lazy-tools-wire?includeDescription=none&testId=${encodeURIComponent(
73 testId,
74 )}`,
75 )
76 expect(res.ok()).toBe(true)
77 expect(((await res.json()) as { ok: boolean }).ok).toBe(true)
78​
79 const description = await discoveryDescription(request, aimockPort, testId)
80 expect(description).toBeDefined()
81 // Bare names present, no description text appended.
82 expect(description).toContain('search_inventory')
83 expect(description).toContain('check_stock')
84 expect(description).not.toContain('search_inventory — ')
85 expect(description).not.toContain('Search the guitar inventory')
86 })
87​
88 test("includeDescription: 'first-sentence' appends each lazy tool's first sentence", async ({
89 request,
90 aimockPort,
91 testId,
92 }) => {
93 const res = await request.post(
94 `/api/lazy-tools-wire?includeDescription=first-sentence&testId=${encodeURIComponent(
95 testId,
96 )}`,
97 )
98 expect(res.ok()).toBe(true)
99 expect(((await res.json()) as { ok: boolean }).ok).toBe(true)
100​
101 const description = await discoveryDescription(request, aimockPort, testId)
102 expect(description).toBeDefined()
103 // First sentence appended, but not the trailing second sentence.
104 expect(description).toContain(
105 'search_inventory — Search the guitar inventory by keyword.',
106 )
107 expect(description).toContain(
108 'check_stock — Check stock level for a guitar.',
109 )
110 expect(description).not.toContain('Returns matches.')
111 expect(description).not.toContain('Returns quantity on hand.')
112 })
113​
114 test("includeDescription: 'full' appends each lazy tool's full description", async ({
115 request,
116 aimockPort,
117 testId,
118 }) => {
119 const res = await request.post(
120 `/api/lazy-tools-wire?includeDescription=full&testId=${encodeURIComponent(
121 testId,
122 )}`,
123 )
124 expect(res.ok()).toBe(true)
125 expect(((await res.json()) as { ok: boolean }).ok).toBe(true)
126​
127 const description = await discoveryDescription(request, aimockPort, testId)
128 expect(description).toBeDefined()
129 expect(description).toContain(
130 'search_inventory — Search the guitar inventory by keyword. Returns matches.',
131 )
132 expect(description).toContain(
133 'check_stock — Check stock level for a guitar. Returns quantity on hand.',
134 )
135 })
136})