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

source-silent-facts.test.ts

236 lines7.7 KB

1import { describe, expect, it } from 'vitest'
2import {
3 parseSourceSilentEvidence,
4 withSourceSilentEvidence,
5} from './source-silent-facts.ts'
6​
7const row = {
8 providerId: 'synthetic',
9 activity: 'chat',
10 contextWindow: null,
11 maxOutput: null,
12 modalities: null,
13 pricing: null,
14 capabilities: null,
15 reasoning: null,
16 requestMap: null,
18const ledger = parseSourceSilentEvidence(
19 '- synthetic: maxOutput — absent, https://example.com/docs, checked 2026-10-09\n- synthetic: capabilities — absent, https://example.com/docs, checked 2026-10-09',
21​
22describe('source-silent API evidence', () => {
23 it('copies recorded metadata and leaves absent metadata absent', () => {
24 expect(ledger.get('synthetic')?.get('maxOutput')).toEqual({
25 derivation: 'source-silent',
26 sourceUrl: 'https://example.com/docs',
27 checkedAt: '2026-10-09',
28 })
29 expect(
30 parseSourceSilentEvidence('- synthetic: maxOutput — absent')
31 .get('synthetic')
32 ?.get('maxOutput'),
33 ).toEqual({ derivation: 'source-silent' })
34 })
35 it('rejects typo facts and duplicate evidence instead of choosing an arbitrary source', () => {
36 expect(() =>
37 parseSourceSilentEvidence('- synthetic: maxOutputs — absent'),
38 ).toThrow(/unknown fact/)
39 expect(() =>
40 parseSourceSilentEvidence(
41 '- synthetic: maxOutput — absent\n- synthetic: maxOutput — absent',
42 ),
43 ).toThrow(/duplicate/)
44 })
45 it('rejects malformed recorded dates instead of dropping or normalizing them', () => {
46 for (const date of ['2026-02-30', '2026-13-01', '2026-1-09', 'unknown']) {
47 expect(() =>
48 parseSourceSilentEvidence(
49 `- synthetic: maxOutput — absent, checked ${date}`,
50 ),
51 ).toThrow(/invalid checked date/)
52 }
53 })
54 it('adds evidence only for ledgered null facts, with whole-field capability silence', () => {
55 const sources = withSourceSilentEvidence(row, null, null, ledger)
56 expect(sources?.maxOutput?.derivation).toBe('source-silent')
57 expect(sources?.capabilities).toEqual(
58 ledger.get('synthetic')?.get('capabilities'),
59 )
60 expect(sources).not.toHaveProperty('contextWindow')
61 expect(
62 withSourceSilentEvidence(
63 { ...row, providerId: 'unledgered' },
64 null,
65 null,
66 ledger,
67 ),
68 ).toBeNull()
69 })
70 it('preserves populated facts and sameAs evidence without mutating stored sources', () => {
71 const stored = {
72 maxOutput: {
73 derivation: 'listing' as const,
74 sourceUrl: 'https://example.com/list',
75 },
76 capabilities: { tools: { derivation: 'listing' as const } },
77 sameAs: { derivation: 'listing' as const, normalized: true as const },
78 schemaEndpointId: {
79 derivation: 'docs-derived' as const,
80 sourceUrl: 'https://example.com/routes',
81 },
82 }
83 expect(
84 withSourceSilentEvidence(
85 { ...row, maxOutput: 1024, capabilities: { tools: true } },
86 null,
87 stored,
88 ledger,
89 ),
90 ).toEqual(stored)
91 const missing = withSourceSilentEvidence(row, null, stored, ledger)
92 expect(missing?.sameAs).toEqual(stored.sameAs)
93 expect(missing?.schemaEndpointId).toEqual(stored.schemaEndpointId)
94 expect(stored.maxOutput.derivation).toBe('listing')
95 })
96 it('keeps evidence for missing subfacts separate from populated parent provenance', () => {
97 const subfacts = parseSourceSilentEvidence(
98 '- synthetic: priced — absent\n- synthetic: cacheRead — absent\n- synthetic: efforts — absent\n- synthetic: endpoint — absent\n- synthetic: requestMap — absent',
99 )
100 const stored = {
101 pricing: { derivation: 'listing' as const },
102 reasoning: { derivation: 'listing' as const },
103 }
104 const model = {
105 ...row,
106 pricing: {
107 tables: { rate: { base: { input_tokens: 1, output_tokens: 2 } } },
108 },
109 reasoning: { mode: 'effort', mandatory: null },
110 requestMap: { replayReasoningContent: null },
111 }
112 const sources = withSourceSilentEvidence(model, null, stored, subfacts)
113 expect(sources).toMatchObject({
114 ...stored,
115 cacheRead: { derivation: 'source-silent' },
116 efforts: { derivation: 'source-silent' },
117 schemaEndpointId: { derivation: 'source-silent' },
118 })
119 expect(sources).not.toHaveProperty('requestMap')
120 expect(
121 withSourceSilentEvidence(
122 model,
123 null,
124 {
125 ...stored,
126 cacheRead: { derivation: 'docs-derived', path: 'not-offered' },
127 },
128 subfacts,
129 )?.cacheRead,
130 ).toEqual({ derivation: 'docs-derived', path: 'not-offered' })
131 expect(
132 withSourceSilentEvidence(
133 {
134 ...model,
135 pricing: { tables: { rate: { base: { cache_read_tokens: 0 } } } },
136 reasoning: { mode: 'effort', efforts: ['low'] },
137 },
138 '/v1/chat',
139 stored,
140 subfacts,
141 ),
142 ).toEqual(stored)
143 })
144 it('does not apply chat ledger evidence to unrelated activities', () => {
145 expect(
146 withSourceSilentEvidence(
147 { ...row, activity: 'image' },
148 null,
149 null,
150 ledger,
151 ),
152 ).toBeNull()
153 })
154})
155​
156describe('model-scoped replay silence evidence', () => {
157 const scoped = parseSourceSilentEvidence(
158 '- synthetic/maker/model: replayReasoningContent — absent, https://example.com/model, checked 2026-10-09\n- synthetic/maker/model: maxOutput — absent',
159 )
160 it('adds only exact native-model leaf evidence without inventing a value or parent source', () => {
161 const model = {
162 ...row,
163 rawId: 'maker/model',
164 requestMap: {
165 maxTokensField: 'max_tokens',
166 replayReasoningContent: null,
167 },
168 }
169 const source = {
170 derivation: 'docs-derived' as const,
171 sourceUrl: 'https://example.com/body',
172 }
173 const stored = {
174 requestMap: source,
175 requestMapFields: { maxTokensField: source },
176 sameAs: { derivation: 'listing' as const },
177 }
178 expect(withSourceSilentEvidence(model, null, stored, scoped)).toMatchObject(
179 {
180 requestMap: source,
181 requestMapFields: {
182 maxTokensField: source,
183 replayReasoningContent: {
184 derivation: 'source-silent',
185 sourceUrl: 'https://example.com/model',
186 checkedAt: '2026-10-09',
187 },
188 },
189 sameAs: stored.sameAs,
190 maxOutput: { derivation: 'source-silent' },
191 },
192 )
193 expect(model.requestMap.replayReasoningContent).toBeNull()
194 expect(stored.requestMapFields).not.toHaveProperty('replayReasoningContent')
195 expect(
196 withSourceSilentEvidence(
197 { ...model, rawId: 'maker/model-snapshot' },
198 null,
199 stored,
200 scoped,
201 ),
202 ).toEqual(stored)
203 })
204 it('preserves true and false replay facts and their original provenance', () => {
205 const source = {
206 derivation: 'docs-derived' as const,
207 sourceUrl: 'https://example.com/known',
208 }
209 const stored = { requestMapFields: { replayReasoningContent: source } }
210 for (const replayReasoningContent of [true, false]) {
211 const result = withSourceSilentEvidence(
212 {
213 ...row,
214 rawId: 'maker/model',
215 maxOutput: 1,
216 requestMap: { replayReasoningContent },
217 },
218 null,
219 stored,
220 scoped,
221 )
222 expect(result).toEqual(stored)
223 }
224 })
225 it('adds leaf evidence for missing request maps without fabricating a request map', () => {
226 const model = { ...row, rawId: 'maker/model' }
227 expect(
228 withSourceSilentEvidence(model, null, null, scoped)?.requestMapFields
229 ?.replayReasoningContent?.derivation,
230 ).toBe('source-silent')
231 expect(model.requestMap).toBeNull()
232 expect(
233 withSourceSilentEvidence(model, null, null, scoped),
234 ).not.toHaveProperty('requestMap')
235 })
236})