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

integrity.test.ts

123 lines3.6 KB

1import { mkdtemp, 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 { schemaContentHash, verifyIntegrity } from './integrity.ts'
7​
8const SCHEMA = { type: 'object', properties: { model: { type: 'string' } } }
9const HASH = schemaContentHash(SCHEMA)
10const KEY = 'anthropic/chat/v1/messages#request'
11​
12const PROVENANCE = {
13 sourceUrl: 'https://example.com/spec.yml',
14 sourceHash: 'f'.repeat(64),
15 fetchedAt: 1_781_150_000,
16 extractorVersion: '1',
18​
19let outDir: string
20​
21beforeEach(async () => {
22 outDir = await mkdtemp(join(tmpdir(), 'modelschemas-integrity-'))
23 await writeFile(
24 join(outDir, '.manifest.json'),
25 JSON.stringify({
26 version: 1,
27 baseUrl: 'https://svc.test',
28 selections: ['anthropic/v1/messages#request'],
29 types: false,
30 optionalStyle: 'exact',
31 fetchedAt: 1_781_150_000,
32 entries: {
33 [KEY]: {
34 path: 'anthropic/v1-messages.request.ts',
35 contentHash: HASH,
36 etag: `"${HASH}"`,
37 },
38 },
39 }),
40 )
41})
42​
43afterEach(async () => {
44 await rm(outDir, { recursive: true, force: true })
45})
46​
47function service(body: unknown, status = 200): typeof fetch {
48 return (input) => {
49 const { pathname, searchParams } = new URL(String(input))
50 expect(pathname).toBe('/v1/schemas/anthropic/chat/v1/messages')
51 expect(searchParams.get('kind')).toBe('input')
52 expect(searchParams.get('version')).toBe(HASH)
53 return Promise.resolve(Response.json(body, { status }))
54 }
56​
57describe('schemaContentHash', () => {
58 it('is key-order independent (matches the server contentHash)', () => {
59 expect(schemaContentHash({ b: 1, a: { d: [2], c: 3 } })).toBe(
60 schemaContentHash({ a: { c: 3, d: [2] }, b: 1 }),
61 )
62 })
63})
64​
65describe('verifyIntegrity', () => {
66 it('passes when the served schema re-hashes to the pinned address', async () => {
67 const result = await verifyIntegrity({
68 baseUrl: 'https://svc.test',
69 outDir,
70 selections: [],
71 fetchImpl: service({
72 contentHash: HASH,
73 schema: SCHEMA,
74 provenance: PROVENANCE,
75 }),
76 })
77 expect(result.ok).toBe(true)
78 expect(result.checks).toHaveLength(1)
79 expect(result.checks[0]?.provenance).toEqual(PROVENANCE)
80 })
81​
82 it('fails when served bytes do not match the pinned hash', async () => {
83 const result = await verifyIntegrity({
84 baseUrl: 'https://svc.test',
85 outDir,
86 selections: [],
87 fetchImpl: service({
88 contentHash: HASH, // service *claims* the right hash…
89 schema: { type: 'object', tampered: true }, // …but serves other bytes
90 }),
91 })
92 expect(result.ok).toBe(false)
93 expect(result.checks[0]?.problem).toContain('mismatch')
94 })
95​
96 it('fails when the pinned version is gone', async () => {
97 const result = await verifyIntegrity({
98 baseUrl: 'https://svc.test',
99 outDir,
100 selections: [],
101 fetchImpl: service(
102 { error: { code: 'unknown_schema', message: 'gone' } },
103 404,
104 ),
105 })
106 expect(result.ok).toBe(false)
107 expect(result.checks[0]?.problem).toContain('pinned version')
108 })
109​
110 it('fails with a remediation problem when the manifest is missing', async () => {
111 await rm(join(outDir, '.manifest.json'))
112 const result = await verifyIntegrity({
113 baseUrl: 'https://svc.test',
114 outDir,
115 selections: [],
116 fetchImpl: () => {
117 throw new Error('must not fetch')
118 },
119 })
120 expect(result.ok).toBe(false)
121 expect(result.problems[0]).toContain('modelschemas pull')
122 })
123})