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

validate.worker.test.ts

206 lines5.9 KB

1import { beforeAll, describe, expect, it } from 'vitest'
2import { env } from 'cloudflare:test'
3​
4import { getDb } from '../db/index.ts'
5import type { Db } from '../db/index.ts'
6import { endpoints, models, providers, schemaVersions } from '../db/schema.ts'
7import { parseValidateBody, validatePayload } from './validate.ts'
8​
9const NOW = 1_781_150_000
10​
11// Trimmed-down Anthropic /v1/messages input schema (the real one is synced
12// from the spec; this captures its load-bearing constraints).
13const ANTHROPIC_MESSAGES_INPUT = {
14 type: 'object',
15 required: ['model', 'max_tokens', 'messages'],
16 properties: {
17 model: { type: 'string' },
18 max_tokens: { type: 'integer', minimum: 1 },
19 messages: {
20 type: 'array',
21 minItems: 1,
22 items: { $ref: '#/$defs/InputMessage' },
23 },
24 },
25 $defs: {
26 InputMessage: {
27 type: 'object',
28 required: ['role', 'content'],
29 properties: {
30 role: { type: 'string', enum: ['user', 'assistant'] },
31 content: { type: 'string' },
32 },
33 },
34 },
36​
37let db: Db
38​
39beforeAll(async () => {
40 db = getDb(env)
41 await db.insert(providers).values({
42 id: 'val-anthropic',
43 displayName: 'Anthropic (validate fixture)',
44 specSourceUrl: 'https://example.com/spec.yml',
45 })
46 await db.insert(endpoints).values({
47 id: 'val-anthropic/v1/messages',
48 providerId: 'val-anthropic',
49 activity: 'chat',
50 method: 'POST',
51 path: '/v1/messages',
52 })
53 await db.insert(schemaVersions).values({
54 id: 'val-anthropic/v1/messages:input:1',
55 endpointId: 'val-anthropic/v1/messages',
56 kind: 'input',
57 contentHash: 'b'.repeat(64),
58 schema: JSON.stringify(ANTHROPIC_MESSAGES_INPUT),
59 createdAt: NOW,
60 })
61})
62​
63describe('validatePayload', () => {
64 it('accepts a valid Anthropic messages payload', async () => {
65 const outcome = await validatePayload(db, {
66 provider: 'val-anthropic',
67 endpointId: 'v1/messages',
68 payload: {
69 model: 'claude-fable-5',
70 max_tokens: 1024,
71 messages: [{ role: 'user', content: 'Hello' }],
72 },
73 })
74 expect(outcome.ok).toBe(true)
75 if (outcome.ok) {
76 expect(outcome.result.valid).toBe(true)
77 expect(outcome.result.errors).toEqual([])
78 expect(outcome.result.kind).toBe('input')
79 expect(outcome.result.contentHash).toBe('b'.repeat(64))
80 }
81 })
82​
83 it('rejects an invalid payload with structured errors', async () => {
84 const outcome = await validatePayload(db, {
85 provider: 'val-anthropic',
86 endpointId: 'v1/messages',
87 payload: {
88 model: 'claude-fable-5',
89 // max_tokens missing entirely; bad role inside $defs-referenced item
90 messages: [{ role: 'system', content: 'Hello' }],
91 },
92 })
93 expect(outcome.ok).toBe(true)
94 if (outcome.ok) {
95 expect(outcome.result.valid).toBe(false)
96 expect(outcome.result.errors.length).toBeGreaterThan(0)
97 for (const error of outcome.result.errors) {
98 expect(typeof error.path).toBe('string')
99 expect(typeof error.message).toBe('string')
100 expect(typeof error.keyword).toBe('string')
101 }
102 const keywords = outcome.result.errors.map((e) => e.keyword)
103 expect(keywords).toContain('required')
104 }
105 })
106​
107 it('404s with remediation for unknown endpoints or missing schemas', async () => {
108 const unknown = await validatePayload(db, {
109 provider: 'val-anthropic',
110 endpointId: 'nope',
111 payload: {},
112 })
113 expect(unknown).toMatchObject({
114 ok: false,
115 status: 404,
116 code: 'unknown_endpoint',
117 })
118​
119 const noOutput = await validatePayload(db, {
120 provider: 'val-anthropic',
121 endpointId: 'v1/messages',
122 kind: 'output',
123 payload: {},
124 })
125 expect(noOutput).toMatchObject({
126 ok: false,
127 status: 404,
128 code: 'no_schema',
129 })
130 })
131​
132 it('resolves a grok model rawId onto the bound generation route', async () => {
133 await db
134 .insert(providers)
135 .values({
136 id: 'grok',
137 displayName: 'xAI Grok',
138 specSourceUrl: 'https://example.com/g.json',
139 })
140 .onConflictDoNothing()
141 await db.insert(endpoints).values({
142 id: 'grok/v1/images/generations',
143 providerId: 'grok',
144 activity: 'image',
145 method: 'POST',
146 path: '/v1/images/generations',
147 })
148 await db.insert(schemaVersions).values({
149 id: 'grok/v1/images/generations:input:1',
150 endpointId: 'grok/v1/images/generations',
151 kind: 'input',
152 contentHash: 'd'.repeat(64),
153 schema: JSON.stringify({
154 type: 'object',
155 properties: { model: { type: 'string' }, prompt: { type: 'string' } },
156 }),
157 createdAt: NOW,
158 })
159 await db.insert(models).values({
160 id: 'grok-grok-imagine-image-2-0',
161 providerId: 'grok',
162 rawId: 'grok-imagine-image-2.0',
163 activity: 'image',
164 firstSeenAt: NOW,
165 lastSeenAt: NOW,
166 })
167​
168 const outcome = await validatePayload(db, {
169 provider: 'grok',
170 endpointId: 'grok-imagine-image-2.0',
171 payload: { model: 'grok-imagine-image-2.0', prompt: 'a cat' },
172 })
173 expect(outcome.ok).toBe(true)
174 if (outcome.ok) {
175 expect(outcome.result.valid).toBe(true)
176 expect(outcome.result.endpointId).toBe('v1/images/generations')
177 }
178 })
179})
180​
181describe('parseValidateBody', () => {
182 it('accepts well-formed bodies and rejects malformed ones', () => {
183 expect(
184 parseValidateBody({ provider: 'p', endpointId: 'e', payload: {} }),
185 ).not.toBeNull()
186 expect(
187 parseValidateBody({
188 provider: 'p',
189 endpointId: 'e',
190 kind: 'output',
191 payload: null,
192 }),
193 ).not.toBeNull()
194 expect(parseValidateBody({ provider: 'p', payload: {} })).toBeNull()
195 expect(
196 parseValidateBody({
197 provider: 'p',
198 endpointId: 'e',
199 kind: 'sideways',
200 payload: {},
201 }),
202 ).toBeNull()
203 expect(parseValidateBody({ provider: 'p', endpointId: 'e' })).toBeNull()
204 expect(parseValidateBody('nope')).toBeNull()
205 })
206})