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

gap-report.test.ts

520 lines14.5 KB

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',
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 } },
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 },
56​
57const ledger = parseLedger(
58 '# Ledger\n\n- quiet: maxOutput — not published (#75)\n',
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
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.activities.chat?.facts?.priced).toEqual({ have: 1, need: 1 })
72 expect(full.activities.chat?.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.activities.chat?.facts?.priced).toEqual({
80 have: 0,
81 need: 1,
82 })
83 expect(borrowed.activities.chat?.facts?.cacheRead).toEqual({
84 have: 0,
85 need: 1,
86 })
87 expect(borrowed.score).toBeCloseTo(9 / 11)
88 })
89​
90 it('reads both rate key spellings', () => {
91 const own = buildReport([
92 { ...complete, pricing: { tables: listingTables } },
93 ])
94 expect(provider('full', own).score).toBe(1)
95 })
96​
97 it('counts a card with no base row only when every row prices the lever', () => {
98 const rate = {
99 off_peak: {
100 input_tokens: 1e-7,
101 output_tokens: 4e-7,
102 cache_read_tokens: 1e-9,
103 },
104 peak: { input_tokens: 2e-7, output_tokens: 8e-7 },
105 }
106 const periods = provider(
107 'full',
108 buildReport([{ ...complete, pricing: { tables: { rate } } }]),
109 )
110 expect(periods.activities.chat?.facts?.priced).toEqual({ have: 1, need: 1 })
111 expect(periods.activities.chat?.facts?.cacheRead).toEqual({
112 have: 0,
113 need: 1,
114 })
115 const both = {
116 off_peak: rate.off_peak,
117 peak: { ...rate.peak, cache_read_tokens: 2e-9 },
118 }
119 const fullPeriods = provider(
120 'full',
121 buildReport([{ ...complete, pricing: { tables: { rate: both } } }]),
122 )
123 expect(fullPeriods.activities.chat?.facts?.priced).toEqual({
124 have: 1,
125 need: 1,
126 })
127 expect(fullPeriods.activities.chat?.facts?.cacheRead).toEqual({
128 have: 1,
129 need: 1,
130 })
131 })
132​
133 it('counts a card in another currency as priced', () => {
134 const yuan = buildReport([
135 {
136 ...complete,
137 pricing: {
138 ...complete.pricing,
139 price: { currency: ['CNY', 0] },
140 } as ModelRow['pricing'],
141 },
142 ])
143 expect(provider('full', yuan).activities.chat?.facts?.priced).toEqual({
144 have: 1,
145 need: 1,
146 })
147 expect(provider('full', yuan).activities.chat?.facts?.cacheRead).toEqual({
148 have: 1,
149 need: 1,
150 })
151 })
152​
153 it('leaves a ledger fact out of the score', () => {
154 const quiet = provider('quiet')
155 expect(quiet.silent).toEqual(['maxOutput'])
156 expect(quiet.activities.chat?.facts?.maxOutput).toEqual({
157 have: 0,
158 need: 1,
159 })
160 expect(quiet.score).toBe(1)
161 })
162​
163 it('scores chat rows 1 when every needed fact is on the ledger', () => {
164 const all = parseLedger(
165 [
166 'contextWindow',
167 'maxOutput',
168 'modalities',
169 'priced',
170 'cacheRead',
171 'capabilities',
172 'requestMap',
173 'endpoint',
174 ]
175 .map((fact) => `- bare: ${fact} — not published`)
176 .join('\n'),
177 )
178 const bare = buildReport(
179 [{ provider: 'bare', activity: 'chat' }],
180 all,
181 new Date(0),
182 )
183 expect(bare.providers[0]).toMatchObject({ rows: 1, score: 1 })
184 })
185​
186 it('treats unclassified rows as a gap', () => {
187 const images = provider('images')
188 expect(images).toMatchObject({ rows: 2, noActivity: 1, score: 0 })
189 expect(report.providers[0]?.provider).toBe('images')
190 })
191​
192 it('prints n/a when no row is scored', () => {
193 const speech = buildReport(
194 [{ provider: 'speech', activity: 'embeddings' }],
195 new Map(),
196 new Date(0),
197 )
198 expect(speech.providers[0]?.score).toBeNull()
199 expect(formatTable(speech)).toContain('n/a')
200 expect(formatTable(speech)).toMatch(
201 /unscored.*originLink.*media\.outputModalities/,
202 )
203 expect(failing(speech, 1, ['speech', 'absent'])).toEqual(['absent'])
204 })
205​
206 it('needs reasoning only where a row claims it', () => {
207 const plain = provider(
208 'full',
209 buildReport([
210 {
211 ...complete,
212 capabilities: { tools: true, reasoning: false },
213 reasoning: null,
214 },
215 { ...complete, reasoning: null },
216 ]),
217 )
218 expect(plain.activities.chat?.facts?.reasoning).toEqual({
219 have: 0,
220 need: 1,
221 })
222 expect(plain.activities.chat?.facts?.efforts).toEqual({ have: 0, need: 0 })
223 })
224​
225 it('asks a toggle row for reasoning and for no efforts', () => {
226 const toggles = provider(
227 'full',
228 buildReport([
229 { ...complete, reasoning: { mode: 'toggle', mandatory: false } },
230 { ...complete, reasoning: { mode: 'toggle', mandatory: true } },
231 ]),
232 )
233 expect(toggles.activities.chat?.facts?.reasoning).toEqual({
234 have: 2,
235 need: 2,
236 })
237 expect(toggles.activities.chat?.facts?.efforts).toEqual({
238 have: 0,
239 need: 0,
240 })
241 expect(toggles.score).toBe(1)
242 })
243​
244 it('counts efforts with an unstated mandatory as filled', () => {
245 const unstated = provider(
246 'full',
247 buildReport([
248 {
249 ...complete,
250 reasoning: { mode: 'effort', mandatory: null, efforts: ['low'] },
251 },
252 ]),
253 )
254 expect(unstated.activities.chat?.facts?.reasoning).toEqual({
255 have: 1,
256 need: 1,
257 })
258 expect(unstated.activities.chat?.facts?.efforts).toEqual({
259 have: 1,
260 need: 1,
261 })
262 expect(unstated.score).toBe(1)
263 })
264​
265 it('flags no shape on a complete row', () => {
266 expect(provider('full').flags).toEqual({
267 mandatoryUnknown: [],
268 reasoningUnknown: [],
269 outputModalitiesEmpty: [],
270 mandatoryWithNone: [],
271 })
272 })
273​
274 it('flags the shapes the catalog cannot read, apart from the score', () => {
275 const flagged = provider(
276 'full',
277 buildReport([
278 {
279 ...complete,
280 rawId: 'unstated',
281 reasoning: { mode: 'effort', mandatory: null, efforts: ['low'] },
282 },
283 {
284 ...complete,
285 rawId: 'adaptive-unstated',
286 reasoning: { mode: 'adaptive', mandatory: null },
287 },
288 { ...complete, rawId: 'no-mode', reasoning: null },
289 {
290 ...complete,
291 rawId: 'not-reasoning',
292 capabilities: { reasoning: false },
293 reasoning: null,
294 },
295 {
296 ...complete,
297 rawId: 'no-output',
298 modalities: { input: ['text'], output: [] },
299 },
300 {
301 ...complete,
302 rawId: 'unknown-output',
303 modalities: { input: ['text'], output: null },
304 },
305 {
306 ...complete,
307 rawId: 'forced-none',
308 reasoning: {
309 mode: 'effort',
310 mandatory: true,
311 efforts: ['none', 'high'],
312 },
313 },
314 {
315 ...complete,
316 rawId: 'image',
317 activity: 'image',
318 modalities: { output: [] },
319 },
320 ]),
321 )
322 expect(flagged.flags).toEqual({
323 mandatoryUnknown: ['unstated'],
324 reasoningUnknown: ['no-mode'],
325 outputModalitiesEmpty: ['no-output'],
326 mandatoryWithNone: ['forced-none'],
327 })
328 expect(flagged.activities.chat?.facts?.efforts).toEqual({
329 have: 4,
330 need: 5,
331 })
332 expect(formatTable({ generatedAt: '', providers: [flagged] })).toContain(
333 'mandatoryWithNone',
334 )
335 })
336​
337 it('clears reasoningUnknown only for a reasoning ledger entry', () => {
338 const hostRows: Array<ModelRow> = [
339 { ...complete, provider: 'host', rawId: 'a', reasoning: null },
340 {
341 ...complete,
342 provider: 'host',
343 rawId: 'b',
344 reasoning: null,
345 modalities: { input: ['text'], output: [] },
346 },
347 { ...complete, provider: 'host', rawId: 'c', reasoning: null },
348 ]
349 const scoped = provider(
350 'host',
351 buildReport(
352 hostRows,
353 parseLedger(
354 '- host/a: reasoning — own docs silent\n- host/b: modalities — own docs silent',
355 ),
356 ),
357 )
358 expect(scoped.flags.reasoningUnknown).toEqual(['b', 'c'])
359 expect(scoped.flags.outputModalitiesEmpty).toEqual(['b'])
360 const wide = provider(
361 'host',
362 buildReport(hostRows, parseLedger('- host: reasoning — not published')),
363 )
364 expect(wide.flags.reasoningUnknown).toEqual([])
365 })
366​
367 it('picks the providers below target', () => {
368 expect(failing(report, 1)).toEqual(['images', 'borrowed'])
369 expect(failing(report, 1, ['full', 'images', 'absent'])).toEqual([
370 'images',
371 'absent',
372 ])
373 expect(failing(report, 0.8)).toEqual(['images'])
374 })
375​
376 it('rejects an unknown ledger fact', () => {
377 expect(() => parseLedger('- grok: maxOutputs — typo')).toThrow(
378 /unknown fact/,
379 )
380 expect(() => parseLedger('- fal: activity — unclassified')).toThrow(
381 /classify the row/,
382 )
383 })
384})
385​
386describe('source-silent ledger', () => {
387 it('is the same bundled into the Worker as read from disk', () => {
388 const onDisk = readLedger()
389 expect(onDisk?.size).toBeGreaterThan(0)
390 expect(sourceSilentLedger).toEqual(onDisk)
391 })
392})
393​
394describe('model-scoped source silence', () => {
395 it('matches exact native ids with embedded slashes and keeps published facts counted', () => {
396 const scoped = parseLedger(
397 '- host/maker/model: maxOutput — own docs silent\n- host/maker/filled: maxOutput — no longer silent',
398 )
399 const result = provider(
400 'host',
401 buildReport(
402 [
403 {
404 ...complete,
405 provider: 'host',
406 rawId: 'maker/model',
407 maxOutput: null,
408 },
409 {
410 ...complete,
411 provider: 'host',
412 rawId: 'maker/model-snapshot',
413 maxOutput: null,
414 },
415 { ...complete, provider: 'host', rawId: 'maker/filled' },
416 ],
417 scoped,
418 ),
419 )
420 expect(result.silent).toEqual([])
421 expect(result.activities.chat?.facts?.maxOutput).toEqual({
422 have: 1,
423 need: 3,
424 })
425 expect(result.activities.chat?.modelSilent).toEqual({ maxOutput: 1 })
426 expect(result.score).toBeCloseTo(31 / 32)
427 expect(formatTable({ generatedAt: '', providers: [result] })).toContain(
428 '1/3 (1 model-silent)',
429 )
430 })
431 it('counts both explicit replay booleans, not null or missing fields', () => {
432 const replay = provider(
433 'host',
434 buildReport(
435 [
436 {
437 ...complete,
438 provider: 'host',
439 rawId: 'a',
440 requestMap: { replayReasoningContent: true },
441 },
442 {
443 ...complete,
444 provider: 'host',
445 rawId: 'b',
446 requestMap: { replayReasoningContent: false },
447 },
448 {
449 ...complete,
450 provider: 'host',
451 rawId: 'c',
452 requestMap: { replayReasoningContent: null },
453 },
454 { ...complete, provider: 'host', rawId: 'd', requestMap: null },
455 {
456 ...complete,
457 provider: 'host',
458 rawId: 'e',
459 reasoning: null,
460 capabilities: { reasoning: false },
461 },
462 ],
463 parseLedger('- host/c: replayReasoningContent — checked own source'),
464 ),
465 )
466 expect(replay.activities.chat?.facts?.replayReasoningContent).toEqual({
467 have: 2,
468 need: 4,
469 })
470 expect(replay.activities.chat?.modelSilent).toEqual({
471 replayReasoningContent: 1,
472 })
473 })
474 it('does not turn a model-scoped entry into a provider exemption or guess missing raw ids', () => {
475 const scoped = parseLedger('- host/maker/model: maxOutput — absent')
476 const result = provider(
477 'host',
478 buildReport([{ ...complete, provider: 'host', maxOutput: null }], scoped),
479 )
480 expect(result.activities.chat?.modelSilent).toEqual({})
481 expect(result.score).toBeLessThan(1)
482 })
483 it('preserves provider-wide grammar and avoids double exclusions', () => {
484 const scoped = parseLedger(
485 '- host: maxOutput — absent\n- `host/@cf/a/model:variant`: maxOutput — absent',
486 )
487 expect(scoped.get('host/@cf/a/model:variant')).toEqual(
488 new Set(['maxOutput']),
489 )
490 const result = provider(
491 'host',
492 buildReport(
493 [
494 {
495 ...complete,
496 provider: 'host',
497 rawId: '@cf/a/model:variant',
498 maxOutput: null,
499 },
500 ],
501 scoped,
502 ),
503 )
504 expect(result.silent).toEqual(['maxOutput'])
505 expect(result.activities.chat?.modelSilent).toEqual({})
506 expect(result.score).toBe(1)
507 expect(() => parseLedger('- host/: maxOutput — missing model')).toThrow(
508 'invalid scope',
509 )
510 expect(() => parseLedger('- host/model: ')).toThrow('malformed entry')
511 expect(() => parseLedger('- host/model: `maxOutput')).toThrow(
512 'malformed entry',
513 )
514 expect(() =>
515 parseLedger(
516 '- host/id: requestMap.replayReasoningContent — unsupported spelling',
517 ),
518 ).toThrow('unknown fact')
519 })
520})