Open History to see the last commit
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 { serviceIndex } from '../routes/v1/index.ts'
8import { listChanges } from './changes-api.ts'
9import {
10 getModelDetail,
11 listModelsCatalog,
12 listProvidersCatalog,
13} from './catalog.ts'
14import type { HalLink } from './hal.ts'
15import {
16 getActivitySchemaMap,
17 getEndpointSchema,
18 getProviderSchemaIndex,
19 getSystemSchemaIndex,
20} from './schemas-api.ts'
21import { getServiceStatus } from './status.ts'
22import { assembleProviderOpenApi } from './provider-openapi.ts'
23import { estimateCost } from './estimate.ts'
24import type { EstimateRequestBody } from './estimate.ts'
25import { validatePayload } from './validate.ts'
26import type { ValidateRequestBody } from './validate.ts'
27import { GPT_4O } from '../../packages/rate-card/src/fixtures/gpt-4o.ts'
28
29let db: Db
30
31const SCHEMA = JSON.stringify({
32 type: 'object',
33 required: ['model', 'max_tokens'],
34 properties: {
35 model: { type: 'string' },
36 max_tokens: { type: 'integer' },
37 },
38})
39
40beforeAll(async () => {
41 db = getDb(env)
42 // Seed the data the serviceIndex examples reference, so walking the
43 // examples proves they are live URLs.
44 await db.insert(providers).values({
45 id: 'anthropic',
46 displayName: 'Anthropic',
47 specSourceUrl: 'https://example.com/spec.json',
48 })
49 const NOW = 1_781_150_000
50 await db.insert(models).values({
51 id: 'anthropic-claude-sonnet-4-5',
52 providerId: 'anthropic',
53 rawId: 'claude-sonnet-4-5',
54 activity: 'chat',
55 firstSeenAt: NOW,
56 lastSeenAt: NOW,
57 })
58 await db.insert(endpoints).values({
59 id: 'anthropic/v1/messages',
60 providerId: 'anthropic',
61 path: '/v1/messages',
62 method: 'POST',
63 activity: 'chat',
64 })
65 await db.insert(schemaVersions).values({
66 id: 'sv-hal-input',
67 endpointId: 'anthropic/v1/messages',
68 kind: 'input',
69 contentHash: 'hash-hal-input',
70 schema: SCHEMA,
71 createdAt: NOW,
72 })
73 await db.insert(providers).values({
74 id: 'openai',
75 displayName: 'OpenAI',
76 specSourceUrl: 'https://example.com/openai.json',
77 })
78 await db.insert(models).values({
79 id: 'openai-gpt-4o',
80 providerId: 'openai',
81 rawId: 'gpt-4o',
82 activity: 'chat',
83 pricing: GPT_4O,
84 firstSeenAt: NOW,
85 lastSeenAt: NOW,
86 })
87})
88
89/** Resolves a link the way the HTTP router would; throws on unknown paths
90 * so a new link without a dispatcher entry fails the walk. */
91async function resolve(target: string | Record<string, unknown>) {
92 if (typeof target !== 'string') {
93 if ('endpointId' in target) {
94 const outcome = await validatePayload(
95 db,
96 target as unknown as ValidateRequestBody,
97 )
98 expect(outcome.ok).toBe(true)
99 return
100 }
101 const outcome = await estimateCost(
102 db,
103 target as unknown as EstimateRequestBody,
104 )
105 expect(outcome.ok).toBe(true)
106 return
107 }
108 if (!target.startsWith('/v1')) return // .well-known etc. — out of scope
109 const url = new URL(target, 'https://walk.test')
110 const segments = url.pathname.split('/').filter(Boolean).slice(1)
111 const [head, ...rest] = segments
112 switch (head) {
113 case undefined:
114 return // /v1 — the document under test
115 case 'status':
116 expect((await getServiceStatus(db)).providers.length).toBeGreaterThan(0)
117 return
118 case 'providers':
119 expect((await listProvidersCatalog(db)).providers.length).toBeGreaterThan(
120 0,
121 )
122 return
123 case 'models': {
124 if (rest.length === 0) {
125 const activity = url.searchParams.get('activity')
126 const result = await listModelsCatalog(db, {
127 activity: activity === 'chat' ? 'chat' : undefined,
128 q: url.searchParams.get('q') ?? undefined,
129 })
130 expect(result.count).toBeGreaterThan(0)
131 return
132 }
133 const [provider, modelId] = rest
134 expect(
135 await getModelDetail(db, provider ?? '', modelId ?? ''),
136 ).not.toBeNull()
137 return
138 }
139 case 'schemas': {
140 const [provider, activity, ...endpointParts] = rest
141 if (!provider) {
142 // /v1/schemas — the system-wide index.
143 expect((await getSystemSchemaIndex(db)).count).toBeGreaterThan(0)
144 return
145 }
146 if (!activity) {
147 expect(
148 (await getProviderSchemaIndex(db, provider)).count,
149 ).toBeGreaterThan(0)
150 return
151 }
152 if (endpointParts.length === 0) {
153 expect(activity).toBe('chat')
154 expect(
155 (await getActivitySchemaMap(db, provider, 'chat')).count,
156 ).toBeGreaterThan(0)
157 return
158 }
159 const endpointId = decodeURIComponent(endpointParts.join('/'))
160 const kind =
161 url.searchParams.get('kind') === 'output' ? 'output' : 'input'
162 expect(activity).toBe('chat')
163 expect(
164 await getEndpointSchema(db, provider, 'chat', endpointId, kind),
165 ).not.toBeNull()
166 return
167 }
168 case 'changes': {
169 const outcome = await listChanges(db, {})
170 expect(outcome.ok).toBe(true)
171 return
172 }
173 case 'openapi': {
174 const [provider] = rest
175 const assembled = await assembleProviderOpenApi(db, provider ?? '', {
176 model: url.searchParams.get('model') ?? undefined,
177 activity: url.searchParams.get('activity') ?? undefined,
178 })
179 expect(assembled.ok).toBe(true)
180 return
181 }
182 default:
183 throw new Error(`no dispatcher for link target ${target}`)
184 }
185}
186
187function assertHalShape(name: string, link: HalLink) {
188 expect(link.href, name).toBeTruthy()
189 expect(['GET', 'POST', 'DELETE'], name).toContain(link.method)
190 expect(link.contentType, name).toBeTruthy()
191 if (link.href.includes('{')) {
192 expect(link.templated, `${name} must mark templated`).toBe(true)
193 expect(link.example, `${name} template needs an example`).toBeDefined()
194 }
195}
196
197describe('HAL self-description (task 11.1)', () => {
198 it('walks every GET /v1 link to a live resource', async () => {
199 for (const [name, link] of Object.entries(serviceIndex._links)) {
200 assertHalShape(name, link)
201 await resolve(link.example ?? link.href)
202 }
203 })
204
205 it('walks every GET /v1/models link', async () => {
206 const result = await listModelsCatalog(db, {})
207 for (const [name, link] of Object.entries(result._links)) {
208 assertHalShape(name, link)
209 await resolve(link.example ?? link.href)
210 }
211 // Model rows carry HAL links too.
212 const model = result.models[0]
213 for (const [name, link] of Object.entries(model?._links ?? {})) {
214 assertHalShape(name, link)
215 await resolve(link.href)
216 }
217 })
218})