Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { sourceSilentLedger } from '../src/server/source-silent.ts'
4import {
5 buildReport,
6 failing,
7 parseLedger,
8 readLedger,
9 formatTable,
10} from './gap-report.ts'
11import type { ModelRow } from './gap-report.ts'
12
13const complete: ModelRow = {
14 provider: 'full',
15 activity: 'chat',
16 contextWindow: 200000,
17 maxOutput: 64000,
18 modalities: { input: ['text'] },
19 pricing: {
20 tables: {
21 rate: {
22 base: {
23 input_tokens: 1e-6,
24 output_tokens: 2e-6,
25 cache_read_tokens: 1e-7,
26 },
27 },
28 },
29 source: { url: 'https://docs.example.com/pricing' },
30 },
31 capabilities: { reasoning: true, tools: true },
32 reasoning: { mode: 'effort', efforts: ['low', 'high'] },
33 requestMap: { maxTokensField: 'max_tokens', replayReasoningContent: false },
34 schemaEndpointId: 'full/chat/completions',
35}
36
37// Listing-compiled cards carry OpenRouter's key names.
38const listingTables = {
39 rate: { base: { prompt: 1e-6, completion: 2e-6, input_cache_read: 1e-7 } },
40}
41
42const rows: Array<ModelRow> = [
43 complete,
44 {
45 ...complete,
46 provider: 'borrowed',
47 pricing: {
48 tables: listingTables,
49 source: { url: 'https://models.dev/api.json' },
50 },
51 },
52 { ...complete, provider: 'quiet', maxOutput: null },
53 { provider: 'images', activity: 'image' },
54 { provider: 'images', activity: null },
55]
56
57const ledger = parseLedger(
58 '# Ledger\n\n- quiet: maxOutput — not published (#75)\n',
59)
60const report = buildReport(rows, ledger, new Date(0))
61const provider = (id: string, from = report) => {
62 const found = from.providers.find((p) => p.provider === id)
63 if (!found) throw new Error(`no provider ${id}`)
64 return found
65}
66
67describe('gap report scoring', () => {
68 it('scores a complete row 1', () => {
69 const full = provider('full')
70 expect(full.score).toBe(1)
71 expect(full.facts.priced).toEqual({ have: 1, need: 1 })
72 expect(full.facts.efforts).toEqual({ have: 1, need: 1 })
73 expect(report.generatedAt).toBe('1970-01-01T00:00:00.000Z')
74 })
75
76 it('does not count a models.dev price as filled', () => {
77 const borrowed = provider('borrowed')
78 expect(borrowed.fromModelsDev).toBe(1)
79 expect(borrowed.facts.priced).toEqual({ have: 0, need: 1 })
80 expect(borrowed.facts.cacheRead).toEqual({ have: 0, need: 1 })
81 expect(borrowed.score).toBeCloseTo(9 / 11)
82 })
83
84 it('reads both rate key spellings', () => {
85 const own = buildReport([
86 { ...complete, pricing: { tables: listingTables } },
87 ])
88 expect(provider('full', own).score).toBe(1)
89 })
90
91 it('counts a card with no base row only when every row prices the lever', () => {
92 const rate = {
93 off_peak: {
94 input_tokens: 1e-7,
95 output_tokens: 4e-7,
96 cache_read_tokens: 1e-9,
97 },
98 peak: { input_tokens: 2e-7, output_tokens: 8e-7 },
99 }
100 const periods = provider(
101 'full',
102 buildReport([{ ...complete, pricing: { tables: { rate } } }]),
103 )
104 expect(periods.facts.priced).toEqual({ have: 1, need: 1 })
105 expect(periods.facts.cacheRead).toEqual({ have: 0, need: 1 })
106 const both = {
107 off_peak: rate.off_peak,
108 peak: { ...rate.peak, cache_read_tokens: 2e-9 },
109 }
110 const fullPeriods = provider(
111 'full',
112 buildReport([{ ...complete, pricing: { tables: { rate: both } } }]),
113 )
114 expect(fullPeriods.facts.priced).toEqual({ have: 1, need: 1 })
115 expect(fullPeriods.facts.cacheRead).toEqual({ have: 1, need: 1 })
116 })
117
118 it('counts a card in another currency as priced', () => {
119 const yuan = buildReport([
120 {
121 ...complete,
122 pricing: {
123 ...complete.pricing,
124 price: { currency: ['CNY', 0] },
125 } as ModelRow['pricing'],
126 },
127 ])
128 expect(provider('full', yuan).facts.priced).toEqual({ have: 1, need: 1 })
129 expect(provider('full', yuan).facts.cacheRead).toEqual({ have: 1, need: 1 })
130 })
131
132 it('leaves a ledger fact out of the score', () => {
133 const quiet = provider('quiet')
134 expect(quiet.silent).toEqual(['maxOutput'])
135 expect(quiet.facts.maxOutput).toEqual({ have: 0, need: 1 })
136 expect(quiet.score).toBe(1)
137 })
138
139 it('scores chat rows 1 when every needed fact is on the ledger', () => {
140 const all = parseLedger(
141 [
142 'contextWindow',
143 'maxOutput',
144 'modalities',
145 'priced',
146 'cacheRead',
147 'capabilities',
148 'requestMap',
149 'endpoint',
150 ]
151 .map((fact) => `- bare: ${fact} — not published`)
152 .join('\n'),
153 )
154 const bare = buildReport(
155 [{ provider: 'bare', activity: 'chat' }],
156 all,
157 new Date(0),
158 )
159 expect(bare.providers[0]).toMatchObject({ chat: 1, score: 1 })
160 })
161
162 it('treats unclassified rows with no chat rows as a gap', () => {
163 const images = provider('images')
164 expect(images).toMatchObject({ rows: 2, chat: 0, noActivity: 1, score: 0 })
165 expect(report.providers[0]?.provider).toBe('images')
166 })
167
168 it('prints n/a when the provider does not serve chat', () => {
169 const speech = buildReport(
170 [{ provider: 'speech', activity: 'audio' }],
171 new Map(),
172 new Date(0),
173 )
174 expect(speech.providers[0]?.score).toBeNull()
175 expect(formatTable(speech)).toContain('n/a')
176 expect(failing(speech, 1, ['speech', 'absent'])).toEqual(['absent'])
177 })
178
179 it('needs reasoning only where a row claims it', () => {
180 const plain = provider(
181 'full',
182 buildReport([
183 {
184 ...complete,
185 capabilities: { tools: true, reasoning: false },
186 reasoning: null,
187 },
188 { ...complete, reasoning: null },
189 ]),
190 )
191 expect(plain.facts.reasoning).toEqual({ have: 0, need: 1 })
192 expect(plain.facts.efforts).toEqual({ have: 0, need: 0 })
193 })
194
195 it('asks a toggle row for reasoning and for no efforts', () => {
196 const toggles = provider(
197 'full',
198 buildReport([
199 { ...complete, reasoning: { mode: 'toggle', mandatory: false } },
200 { ...complete, reasoning: { mode: 'toggle', mandatory: true } },
201 ]),
202 )
203 expect(toggles.facts.reasoning).toEqual({ have: 2, need: 2 })
204 expect(toggles.facts.efforts).toEqual({ have: 0, need: 0 })
205 expect(toggles.score).toBe(1)
206 })
207
208 it('counts efforts with an unstated mandatory as filled', () => {
209 const unstated = provider(
210 'full',
211 buildReport([
212 {
213 ...complete,
214 reasoning: { mode: 'effort', mandatory: null, efforts: ['low'] },
215 },
216 ]),
217 )
218 expect(unstated.facts.reasoning).toEqual({ have: 1, need: 1 })
219 expect(unstated.facts.efforts).toEqual({ have: 1, need: 1 })
220 expect(unstated.score).toBe(1)
221 })
222
223 it('flags no shape on a complete row', () => {
224 expect(provider('full').flags).toEqual({
225 mandatoryUnknown: [],
226 reasoningUnknown: [],
227 outputModalitiesEmpty: [],
228 mandatoryWithNone: [],
229 })
230 })
231
232 it('flags the shapes the catalog cannot read, apart from the score', () => {
233 const flagged = provider(
234 'full',
235 buildReport([
236 {
237 ...complete,
238 rawId: 'unstated',
239 reasoning: { mode: 'effort', mandatory: null, efforts: ['low'] },
240 },
241 {
242 ...complete,
243 rawId: 'adaptive-unstated',
244 reasoning: { mode: 'adaptive', mandatory: null },
245 },
246 { ...complete, rawId: 'no-mode', reasoning: null },
247 {
248 ...complete,
249 rawId: 'not-reasoning',
250 capabilities: { reasoning: false },
251 reasoning: null,
252 },
253 {
254 ...complete,
255 rawId: 'no-output',
256 modalities: { input: ['text'], output: [] },
257 },
258 {
259 ...complete,
260 rawId: 'unknown-output',
261 modalities: { input: ['text'], output: null },
262 },
263 {
264 ...complete,
265 rawId: 'forced-none',
266 reasoning: {
267 mode: 'effort',
268 mandatory: true,
269 efforts: ['none', 'high'],
270 },
271 },
272 {
273 ...complete,
274 rawId: 'image',
275 activity: 'image',
276 modalities: { output: [] },
277 },
278 ]),
279 )
280 expect(flagged.flags).toEqual({
281 mandatoryUnknown: ['unstated'],
282 reasoningUnknown: ['no-mode'],
283 outputModalitiesEmpty: ['no-output'],
284 mandatoryWithNone: ['forced-none'],
285 })
286 expect(flagged.facts.efforts).toEqual({ have: 4, need: 5 })
287 expect(formatTable({ generatedAt: '', providers: [flagged] })).toContain(
288 'mandatoryWithNone',
289 )
290 })
291
292 it('clears reasoningUnknown only for a reasoning ledger entry', () => {
293 const hostRows: Array<ModelRow> = [
294 { ...complete, provider: 'host', rawId: 'a', reasoning: null },
295 {
296 ...complete,
297 provider: 'host',
298 rawId: 'b',
299 reasoning: null,
300 modalities: { input: ['text'], output: [] },
301 },
302 { ...complete, provider: 'host', rawId: 'c', reasoning: null },
303 ]
304 const scoped = provider(
305 'host',
306 buildReport(
307 hostRows,
308 parseLedger(
309 '- host/a: reasoning — own docs silent\n- host/b: modalities — own docs silent',
310 ),
311 ),
312 )
313 expect(scoped.flags.reasoningUnknown).toEqual(['b', 'c'])
314 expect(scoped.flags.outputModalitiesEmpty).toEqual(['b'])
315 const wide = provider(
316 'host',
317 buildReport(hostRows, parseLedger('- host: reasoning — not published')),
318 )
319 expect(wide.flags.reasoningUnknown).toEqual([])
320 })
321
322 it('picks the providers below target', () => {
323 expect(failing(report, 1)).toEqual(['borrowed'])
324 expect(failing(report, 1, ['full', 'images', 'absent'])).toEqual([
325 'images',
326 'absent',
327 ])
328 expect(failing(report, 0.8)).toEqual([])
329 })
330
331 it('rejects an unknown ledger fact', () => {
332 expect(() => parseLedger('- grok: maxOutputs — typo')).toThrow(
333 /unknown fact/,
334 )
335 })
336})
337
338describe('source-silent ledger', () => {
339 it('is the same bundled into the Worker as read from disk', () => {
340 const onDisk = readLedger()
341 expect(onDisk?.size).toBeGreaterThan(0)
342 expect(sourceSilentLedger).toEqual(onDisk)
343 })
344})
345
346describe('model-scoped source silence', () => {
347 it('matches exact native ids with embedded slashes and keeps published facts counted', () => {
348 const scoped = parseLedger(
349 '- host/maker/model: maxOutput — own docs silent\n- host/maker/filled: maxOutput — no longer silent',
350 )
351 const result = provider(
352 'host',
353 buildReport(
354 [
355 {
356 ...complete,
357 provider: 'host',
358 rawId: 'maker/model',
359 maxOutput: null,
360 },
361 {
362 ...complete,
363 provider: 'host',
364 rawId: 'maker/model-snapshot',
365 maxOutput: null,
366 },
367 { ...complete, provider: 'host', rawId: 'maker/filled' },
368 ],
369 scoped,
370 ),
371 )
372 expect(result.silent).toEqual([])
373 expect(result.facts.maxOutput).toEqual({ have: 1, need: 3 })
374 expect(result.modelSilent).toEqual({ maxOutput: 1 })
375 expect(result.score).toBeCloseTo(31 / 32)
376 expect(formatTable({ generatedAt: '', providers: [result] })).toContain(
377 '1/3 (1 model-silent)',
378 )
379 })
380 it('counts both explicit replay booleans, not null or missing fields', () => {
381 const replay = provider(
382 'host',
383 buildReport(
384 [
385 {
386 ...complete,
387 provider: 'host',
388 rawId: 'a',
389 requestMap: { replayReasoningContent: true },
390 },
391 {
392 ...complete,
393 provider: 'host',
394 rawId: 'b',
395 requestMap: { replayReasoningContent: false },
396 },
397 {
398 ...complete,
399 provider: 'host',
400 rawId: 'c',
401 requestMap: { replayReasoningContent: null },
402 },
403 { ...complete, provider: 'host', rawId: 'd', requestMap: null },
404 {
405 ...complete,
406 provider: 'host',
407 rawId: 'e',
408 reasoning: null,
409 capabilities: { reasoning: false },
410 },
411 ],
412 parseLedger('- host/c: replayReasoningContent — checked own source'),
413 ),
414 )
415 expect(replay.facts.replayReasoningContent).toEqual({ have: 2, need: 4 })
416 expect(replay.modelSilent).toEqual({ replayReasoningContent: 1 })
417 })
418 it('does not turn a model-scoped entry into a provider exemption or guess missing raw ids', () => {
419 const scoped = parseLedger('- host/maker/model: maxOutput — absent')
420 const result = provider(
421 'host',
422 buildReport([{ ...complete, provider: 'host', maxOutput: null }], scoped),
423 )
424 expect(result.modelSilent).toEqual({})
425 expect(result.score).toBeLessThan(1)
426 })
427 it('preserves provider-wide grammar and avoids double exclusions', () => {
428 const scoped = parseLedger(
429 '- host: maxOutput — absent\n- `host/@cf/a/model:variant`: maxOutput — absent',
430 )
431 expect(scoped.get('host/@cf/a/model:variant')).toEqual(
432 new Set(['maxOutput']),
433 )
434 const result = provider(
435 'host',
436 buildReport(
437 [
438 {
439 ...complete,
440 provider: 'host',
441 rawId: '@cf/a/model:variant',
442 maxOutput: null,
443 },
444 ],
445 scoped,
446 ),
447 )
448 expect(result.silent).toEqual(['maxOutput'])
449 expect(result.modelSilent).toEqual({})
450 expect(result.score).toBe(1)
451 expect(() => parseLedger('- host/: maxOutput — missing model')).toThrow(
452 'invalid scope',
453 )
454 expect(() => parseLedger('- host/model: ')).toThrow('malformed entry')
455 expect(() => parseLedger('- host/model: `maxOutput')).toThrow(
456 'malformed entry',
457 )
458 expect(() =>
459 parseLedger(
460 '- host/id: requestMap.replayReasoningContent — unsupported spelling',
461 ),
462 ).toThrow('unknown fact')
463 })
464})