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

pull.test.ts

264 lines8.7 KB

1import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
2import { tmpdir } from 'node:os'
3import { join } from 'node:path'
4import { afterEach, beforeEach, describe, expect, it } from 'vitest'
5​
6import { readManifest } from './manifest.ts'
7import { checkUpdates, pull, verify } from './pull.ts'
8import type { PullConfig } from './pull.ts'
9​
10const HASH_A = 'a'.repeat(64)
11const HASH_B = 'b'.repeat(64)
12​
13const TYPES_BODY = [
14 '// @generated by modelschemas typegen v1',
15 'export type JsonSchema = { readonly [key: string]: unknown }',
16 'export const anthropicV1MessagesRequestSchema: JsonSchema = {}',
17 'export interface AnthropicV1MessagesRequest { model: string }',
18 '',
19].join('\n')
20​
21interface Call {
22 url: string
23 ifNoneMatch: string | null
24 authorization: string | null
26​
27/** Fake service: provider index + one chat endpoint with input-only schema. */
28function fakeService(options: { hash?: string } = {}) {
29 const hash = options.hash ?? HASH_A
30 const calls: Array<Call> = []
31 const fetchImpl: typeof fetch = (input, init) => {
32 const url = String(input)
33 const headers = new Headers(init?.headers)
34 calls.push({
35 url,
36 ifNoneMatch: headers.get('if-none-match'),
37 authorization: headers.get('authorization'),
38 })
39 const { pathname, searchParams } = new URL(url)
40​
41 if (pathname === '/v1/schemas/anthropic') {
42 return Promise.resolve(
43 Response.json({
44 provider: 'anthropic',
45 activities: { chat: ['v1/messages'] },
46 }),
47 )
48 }
49 if (pathname === '/v1/schemas/anthropic/chat/v1/messages') {
50 // No output schema for this endpoint.
51 if (searchParams.get('kind') === 'output') {
52 return Promise.resolve(
53 Response.json(
54 { error: { code: 'unknown_schema', message: 'no output' } },
55 { status: 404 },
56 ),
57 )
58 }
59 const etag =
60 searchParams.get('format') === 'types'
61 ? `"${hash}-types-v1-${searchParams.get('optional') ?? 'exact'}"`
62 : `"${hash}"`
63 if (headers.get('if-none-match') === etag) {
64 return Promise.resolve(new Response(null, { status: 304 }))
65 }
66 if (searchParams.get('format') === 'types') {
67 return Promise.resolve(
68 new Response(TYPES_BODY, {
69 headers: {
70 etag,
71 'content-type': 'text/typescript; charset=utf-8',
72 },
73 }),
74 )
75 }
76 return Promise.resolve(
77 Response.json(
78 { contentHash: hash, schema: { type: 'object' } },
79 { headers: { etag } },
80 ),
81 )
82 }
83 if (pathname === '/v1/changes') {
84 return Promise.resolve(
85 Response.json({
86 changes: [
87 {
88 type: 'schema.updated',
89 subjectId: 'anthropic/v1/messages',
90 summary: 'Updated input schema for /v1/messages',
91 payload: { kind: 'input', contentHash: HASH_B },
92 },
93 ],
94 nextCursor: null,
95 }),
96 )
97 }
98 return Promise.resolve(
99 Response.json(
100 { error: { code: 'not_found', message: pathname } },
101 { status: 404 },
102 ),
103 )
104 }
105 return { fetchImpl, calls }
107​
108let outDir: string
109​
110beforeEach(async () => {
111 outDir = await mkdtemp(join(tmpdir(), 'modelschemas-codegen-'))
112})
113​
114afterEach(async () => {
115 await rm(outDir, { recursive: true, force: true })
116})
117​
118function config(overrides: Partial<PullConfig> = {}): PullConfig {
119 return {
120 baseUrl: 'https://svc.test',
121 outDir,
122 selections: ['anthropic'],
123 ...overrides,
124 }
126​
127describe('pull', () => {
128 it('writes generated modules + manifest; implicit missing kinds are skipped', async () => {
129 const { fetchImpl } = fakeService()
130 const summary = await pull(config({ fetchImpl }), { now: () => 1000 })
131​
132 expect(summary.written).toEqual(['anthropic/chat/v1/messages#request'])
133 expect(summary.skipped).toEqual(['anthropic/chat/v1/messages#response'])
134 expect(summary.failed).toEqual([])
135​
136 const file = await readFile(
137 join(outDir, 'anthropic', 'v1-messages.request.ts'),
138 'utf8',
139 )
140 expect(file).toContain('AnthropicV1MessagesRequest')
141​
142 const manifest = await readManifest(outDir)
143 expect(manifest?.fetchedAt).toBe(1000)
144 expect(manifest?.entries['anthropic/chat/v1/messages#request']).toEqual({
145 path: join('anthropic', 'v1-messages.request.ts'),
146 contentHash: HASH_A,
147 etag: `"${HASH_A}-types-v1-exact"`,
148 })
149 })
150​
151 it('an explicit kind with no schema is a failure, not a skip', async () => {
152 const { fetchImpl } = fakeService()
153 const summary = await pull(
154 config({ fetchImpl, selections: ['anthropic/v1/messages#response'] }),
155 )
156 expect(summary.failed).toEqual([
157 expect.objectContaining({ key: 'anthropic/chat/v1/messages#response' }),
158 ])
159 })
160​
161 it('replays ETags: second pull sends If-None-Match and short-circuits on 304', async () => {
162 const { fetchImpl, calls } = fakeService()
163 await pull(config({ fetchImpl }), { now: () => 1000 })
164 const again = await pull(config({ fetchImpl }), { now: () => 2000 })
165​
166 expect(again.written).toEqual([])
167 expect(again.unchanged).toEqual(['anthropic/chat/v1/messages#request'])
168 const conditional = calls.filter((c) => c.ifNoneMatch !== null)
169 expect(conditional).toHaveLength(1)
170 // fetchedAt is only bumped when something changed.
171 expect((await readManifest(outDir))?.fetchedAt).toBe(1000)
172 })
173​
174 it('missingOnly trusts existing files without any schema request', async () => {
175 const { fetchImpl } = fakeService()
176 await pull(config({ fetchImpl }))
177 const { fetchImpl: second, calls } = fakeService()
178 const summary = await pull(config({ fetchImpl: second }), {
179 missingOnly: true,
180 })
181 expect(summary.unchanged).toEqual(['anthropic/chat/v1/messages#request'])
182 // Only the catalog index (+ the still-missing response side) is fetched.
183 expect(calls.map((c) => new URL(c.url).searchParams.get('kind'))).toEqual([
184 null,
185 'output',
186 ])
187 })
188​
189 it('types:false wraps the JSON schema in a const-only module locally', async () => {
190 const { fetchImpl } = fakeService()
191 await pull(config({ fetchImpl, types: false }))
192 const file = await readFile(
193 join(outDir, 'anthropic', 'v1-messages.request.ts'),
194 'utf8',
195 )
196 expect(file).toContain('anthropicV1MessagesRequestSchema')
197 // The user's own prettier may reformat (here: the repo config) — assert
198 // content, not formatting.
199 expect(file).toMatch(/["']?type["']?:\s*["']object["']/)
200 expect(file).not.toContain('interface')
201 })
202​
203 it('sends the api key as a bearer header', async () => {
204 const { fetchImpl, calls } = fakeService()
205 await pull(config({ fetchImpl, apiKey: 'msk_test' }))
206 expect(calls.every((c) => c.authorization === 'Bearer msk_test')).toBe(true)
207 })
208})
209​
210describe('verify', () => {
211 it('fails without a manifest, with remediation', async () => {
212 const result = await verify(config())
213 expect(result.ok).toBe(false)
214 expect(result.problems[0]).toContain('modelschemas pull')
215 })
216​
217 it('passes after a pull; fails when a file vanishes or selections grow', async () => {
218 const { fetchImpl } = fakeService()
219 await pull(config({ fetchImpl }))
220 expect((await verify(config())).ok).toBe(true)
221​
222 expect(
223 (await verify(config({ selections: ['anthropic', 'openai'] }))).problems,
224 ).toEqual([expect.stringContaining("Selection 'openai'")])
225​
226 await rm(join(outDir, 'anthropic', 'v1-messages.request.ts'))
227 const result = await verify(config())
228 expect(result.ok).toBe(false)
229 expect(result.problems[0]).toContain('v1-messages.request.ts')
230 })
231})
232​
233describe('checkUpdates', () => {
234 it('reports entries whose upstream hash moved, and nothing otherwise', async () => {
235 const { fetchImpl } = fakeService()
236 await pull(config({ fetchImpl }))
237​
238 const updates = await checkUpdates(config({ fetchImpl }))
239 expect(updates).toEqual([
240 {
241 key: 'anthropic/chat/v1/messages#request',
242 contentHash: HASH_B,
243 summary: 'Updated input schema for /v1/messages',
244 },
245 ])
246​
247 // Manifest already at HASH_B → silence.
248 const manifest = await readManifest(outDir)
249 if (manifest === null) throw new Error('manifest missing')
250 const entry = manifest.entries['anthropic/chat/v1/messages#request']
251 if (entry === undefined) throw new Error('entry missing')
252 entry.contentHash = HASH_B
253 await writeFile(
254 join(outDir, '.manifest.json'),
255 JSON.stringify(manifest, null, 2),
256 )
257 expect(await checkUpdates(config({ fetchImpl }))).toEqual([])
258 })
259​
260 it('returns [] with no manifest', async () => {
261 const { fetchImpl } = fakeService()
262 expect(await checkUpdates(config({ fetchImpl }))).toEqual([])
263 })
264})