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

openrouter-web-tools-wire.spec.ts

93 lines3.2 KB

1import { test, expect } from './fixtures'
2​
3/**
4 * Wire-format verification for OpenRouter's `webSearchTool()` and
5 * `webFetchTool()` factories.
6 *
7 * Both factories now emit the SDK's canonical shapes
8 * (`OpenRouterWebSearchServerTool` / `WebFetchServerTool`):
9 * `{type: 'openrouter:web_*', parameters: {...config...}}`. The SDK's
10 * outbound Zod schema preserves `parameters` on the wire, so caller-passed
11 * `engine` / `maxResults` / `maxContentTokens` / `allowedDomains` etc.
12 * actually reach OpenRouter.
13 *
14 * This spec drives the OpenRouter chat adapter against aimock and then
15 * inspects aimock's journal endpoint (`GET /v1/_requests`) to assert the
16 * bytes received over the wire. It pins the fix that landed alongside
17 * #603 — previously the request body contained only `{type: 'web_*'}` with
18 * all options silently dropped by the SDK serializer.
19 */
20test.describe('openrouter — web tool wire format', () => {
21 test.beforeEach(async ({ request, aimockPort }) => {
22 // Clear the aimock journal so we only assert against the request this
23 // test triggers — adjacent specs share the same aimock instance.
24 await request.delete(`http://127.0.0.1:${aimockPort}/v1/_requests`)
25 })
26​
27 test('SDK accepts webSearchTool() + webFetchTool() without throwing', async ({
28 request,
29 testId,
30 }) => {
31 const res = await request.post(
32 `/api/openrouter-web-tools-wire?testId=${encodeURIComponent(testId)}`,
33 )
34 expect(res.ok()).toBe(true)
35 const { ok } = (await res.json()) as { ok: boolean; error?: string }
36 expect(ok).toBe(true)
37 })
38​
39 test('webSearchTool({engine, maxResults, allowedDomains}) preserves config on the wire', async ({
40 request,
41 aimockPort,
42 testId,
43 }) => {
44 await request.post(
45 `/api/openrouter-web-tools-wire?testId=${encodeURIComponent(testId)}`,
46 )
47 const journalRes = await request.get(
48 `http://127.0.0.1:${aimockPort}/v1/_requests`,
49 )
50 const entries = (await journalRes.json()) as Array<{
51 body: { tools?: Array<Record<string, unknown>> } | null
52 }>
53 const captured = entries[0]?.body?.tools?.find(
54 (t) => t['type'] === 'openrouter:web_search',
55 )
56 expect(captured).toMatchObject({
57 type: 'openrouter:web_search',
58 parameters: {
59 engine: 'exa',
60 max_results: 10,
61 allowed_domains: ['example.com'],
62 },
63 })
64 })
65​
66 test('webFetchTool({engine, maxContentTokens, allowedDomains, blockedDomains}) preserves config on the wire', async ({
67 request,
68 aimockPort,
69 testId,
70 }) => {
71 await request.post(
72 `/api/openrouter-web-tools-wire?testId=${encodeURIComponent(testId)}`,
73 )
74 const journalRes = await request.get(
75 `http://127.0.0.1:${aimockPort}/v1/_requests`,
76 )
77 const entries = (await journalRes.json()) as Array<{
78 body: { tools?: Array<Record<string, unknown>> } | null
79 }>
80 const captured = entries[0]?.body?.tools?.find(
81 (t) => t['type'] === 'openrouter:web_fetch',
82 )
83 expect(captured).toMatchObject({
84 type: 'openrouter:web_fetch',
85 parameters: {
86 engine: 'openrouter',
87 max_content_tokens: 4000,
88 allowed_domains: ['example.com'],
89 blocked_domains: ['evil.example'],
90 },
91 })
92 })
93})