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