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

openrouter-native-toggle.test.ts

222 lines7.3 KB

1import { afterEach, expect, it, vi } from 'vitest'
2import {
3 openRouterGatewayEffortWire,
4 openRouterGatewayToggle,
5} from './openrouter-reasoning.ts'
6import { openRouterReasoning } from './reasoning-config.ts'
7import docs from './fixtures/openrouter-native-toggle.json'
8import { openrouterProvider } from './openrouter.ts'
9import { sha256Text } from './types.ts'
10​
11it('uses host normalized toggle only with explicit native nonmandatory metadata', () => {
12 const toggle = openRouterGatewayToggle(docs.markdown)
13 expect(
14 openRouterReasoning(
15 { reasoning: { mandatory: false } },
16 undefined,
17 !!toggle,
18 ),
19 ).toEqual({ mode: 'toggle', mandatory: false })
20 expect(
21 openRouterReasoning(
22 { reasoning: { mandatory: true } },
23 undefined,
24 !!toggle,
25 ),
26 ).toBeNull()
27 expect(openRouterReasoning({ reasoning: {} }, undefined, !!toggle)).toBeNull()
28 expect(openRouterReasoning({}, undefined, !!toggle)).toBeNull()
29 expect(openRouterReasoning({ reasoning: { mandatory: false } })).toBeNull()
30 expect(
31 openRouterReasoning(
32 { reasoning: { mandatory: false, supports_max_tokens: true } },
33 undefined,
34 !!toggle,
35 ),
36 ).toEqual({ mode: 'budget', mandatory: false })
37 expect(
38 openRouterReasoning(
39 { reasoning: { mandatory: false, supported_efforts: ['low'] } },
40 undefined,
41 !!toggle,
42 ),
43 ).toEqual({ mode: 'effort', mandatory: false, efforts: ['low'] })
44})
45it('rejects fenced, restricted or negated native declarations', () => {
46 expect(() =>
47 openRouterGatewayToggle('```\n' + docs.markdown + '\n```'),
48 ).toThrow()
49 expect(() =>
50 openRouterGatewayToggle(
51 docs.markdown.replace(
52 'so they work on every reasoning model, not only Claude',
53 'so they work only on Claude',
54 ),
55 ),
56 ).toThrow()
57 expect(() =>
58 openRouterGatewayToggle(
59 docs.markdown.replace(
60 '`thinking: { type: "disabled" }` disables reasoning even when',
61 'Do not assume `thinking: { type: "disabled" }` disables reasoning even when',
62 ),
63 ),
64 ).toThrow()
65 expect(() =>
66 openRouterGatewayToggle(
67 docs.markdown.replace('enabled: false', 'enabled: true'),
68 ),
69 ).toThrow()
70})
71​
72afterEach(() => {
73 vi.restoreAllMocks()
74 vi.unstubAllGlobals()
75})
76it('lists actual native nonmandatory toggle with exact guide provenance, preserving mandatory unknown mode', async () => {
77 vi.stubGlobal(
78 'fetch',
79 vi.fn(async (input: string | URL | Request) => {
80 const url = String(input)
81 if (url === 'https://openrouter.ai/api/v1/models')
82 return Response.json(docs.nativeModels)
83 if (url === docs.sourceUrl) return new Response(docs.markdown)
84 throw new Error('unexpected source URL ' + url)
85 }),
86 )
87 const listed = await openrouterProvider.listModels({})
88 expect(listed.models[0]?.reasoning).toEqual({
89 mode: 'toggle',
90 mandatory: false,
91 })
92 expect(listed.models[0]?.factSources?.reasoning).toMatchObject({
93 derivation: 'docs-derived',
94 sourceUrl: docs.sourceUrl,
95 sourceHash: await sha256Text(docs.markdown),
96 })
97 expect(listed.models[0]?.requestMap?.thinking).toEqual({
98 on: { reasoning: { enabled: true } },
99 off: { reasoning: { enabled: false } },
100 levels: null,
101 })
102 expect(listed.models[0]?.requestMap?.strictTools).toBeNull()
103 expect(
104 listed.models[0]?.factSources?.requestMapFields?.thinking,
105 ).toMatchObject({
106 sourceUrl: docs.sourceUrl,
107 sourceHash: await sha256Text(docs.markdown),
108 })
109 expect(listed.models[1]?.reasoning).toBeNull()
110})
111​
112it('separates old shapes and effort-only warm cache from a newly toggle-capable catalog', async () => {
113 const cache = new Map<string, string>()
114 const kv = {
115 get: async (key: string) =>
116 key.endsWith(docs.sourceUrl)
117 ? JSON.stringify({ efforts: ['old'], hash: 'old' })
118 : (cache.get(key) ?? null),
119 put: async (key: string, value: string) => {
120 cache.set(key, value)
121 },
122 } as unknown as KVNamespace
123 let phase = 0,
124 guideFetches = 0
125 vi.stubGlobal(
126 'fetch',
127 vi.fn(async (input: string | URL | Request) => {
128 const url = String(input)
129 if (url === 'https://openrouter.ai/api/v1/models')
130 return Response.json(
131 phase === 0
132 ? {
133 data: [
134 {
135 ...docs.nativeModels.data[0],
136 reasoning: { mandatory: true, supported_efforts: null },
137 },
138 ],
139 }
140 : docs.nativeModels,
141 )
142 if (url === docs.sourceUrl) {
143 guideFetches++
144 return new Response(docs.markdown)
145 }
146 throw new Error('unexpected source ' + url)
147 }),
148 )
149 const before = await openrouterProvider.listModels({}, kv)
150 expect(before.models[0]?.reasoning?.efforts).not.toContain('old')
151 phase = 1
152 const after = await openrouterProvider.listModels({}, kv)
153 expect(after.models[0]?.reasoning).toEqual({
154 mode: 'toggle',
155 mandatory: false,
156 })
157 const warm = await openrouterProvider.listModels({}, kv)
158 expect(warm.models[0]?.requestMap?.thinking?.on).toEqual({
159 reasoning: { enabled: true },
160 })
161 expect(guideFetches).toBe(2)
162})
163​
164it('rejects present malformed native metadata while null and absent facts stay unknown', () => {
165 expect(() =>
166 openRouterReasoning({ reasoning: { mandatory: 'false' } }),
167 ).toThrow('malformed native mandatory')
168 expect(() =>
169 openRouterReasoning({
170 reasoning: { mandatory: false, supports_max_tokens: 'true' },
171 }),
172 ).toThrow('malformed native supports_max_tokens')
173 expect(() =>
174 openRouterReasoning({
175 reasoning: { mandatory: false, supported_efforts: [42] },
176 } as never),
177 ).toThrow('malformed native supported_efforts')
178 expect(() =>
179 openRouterReasoning({
180 reasoning: { mandatory: false, supported_efforts: 'high' },
181 } as never),
182 ).toThrow('malformed native supported_efforts')
183 expect(openRouterReasoning({ reasoning: { mandatory: null } })).toBeNull()
184 expect(openRouterReasoning({ reasoning: {} })).toBeNull()
185})
186​
187it('sources high only from an actual accepted native enum and leaves another native model wire unknown', async () => {
188 vi.stubGlobal(
189 'fetch',
190 vi.fn(async (input: string | URL | Request) => {
191 const url = String(input)
192 if (url === 'https://openrouter.ai/api/v1/models')
193 return Response.json(docs.nativeEffortModels)
194 if (url === docs.sourceUrl) return new Response(docs.markdown)
195 throw new Error('unexpected URL ' + url)
196 }),
197 )
198 const listed = await openrouterProvider.listModels({})
199 expect(listed.models[0]?.requestMap?.thinking?.on).toEqual({
200 reasoning: { effort: 'high' },
201 })
202 expect(listed.models[0]?.requestMap?.thinking?.off).toBeNull()
203 expect(
204 listed.models[0]?.factSources?.requestMapFields?.thinking,
205 ).toMatchObject({
206 sourceUrl: docs.sourceUrl,
207 sourceHash: await sha256Text(docs.markdown),
208 })
209 expect(listed.models[1]?.reasoning?.efforts).toEqual(['xhigh', 'medium'])
210 expect(listed.models[1]?.requestMap).toBeNull()
211 expect(() =>
212 openRouterGatewayEffortWire('```\n' + docs.markdown + '\n```'),
213 ).toThrow()
214 expect(() =>
215 openRouterGatewayEffortWire(
216 docs.markdown.replace(
217 'so they work on every reasoning model, not only Claude',
218 'Do not assume so they work on every reasoning model, not only Claude',
219 ),
220 ),
221 ).toThrow()
222})