Open History to see the last commit
1import { expect, it } from 'vitest'
2import {
3 catalogGatewayEndpoint,
4 parseGatewayRunContract,
5} from './cloudflare-gateway-schema.ts'
6
7const source = {
8 url: 'https://developers.cloudflare.com/native-fixture',
9 hash: 'a'.repeat(64),
10}
11const docs =
12 '| `POST /ai/run` | Envelope with `model`, `input` |\nModel-specific parameters go inside `input`.\ncurl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run"'
13const contract = parseGatewayRunContract(docs, source)
14const info = { rawId: 'author/model', activity: 'chat' as const }
15it('uses the sourced universal path with a logical model identity and unchanged native inputs', () => {
16 const input = {
17 type: 'object',
18 properties: { max_tokens: { type: 'integer', minimum: 1 } },
19 required: ['max_tokens'],
20 }
21 const endpoint = catalogGatewayEndpoint(
22 { schema: { input, output: { type: 'string' } } },
23 info,
24 source,
25 contract,
26 )
27 expect(endpoint).toMatchObject({
28 publicId: 'author/model',
29 path: '/accounts/{account_id}/ai/run',
30 activity: 'chat',
31 source,
32 derivation: 'generated',
33 })
34 expect(endpoint?.input).toEqual({
35 type: 'object',
36 properties: { model: { type: 'string', const: 'author/model' }, input },
37 'x-modelschemas-envelope-source': source,
38 })
39 expect(endpoint?.output).toBeUndefined()
40 expect(endpoint?.input).not.toHaveProperty('required')
41})
42it('does not guess unknown activities or manufacture missing schema bodies', () => {
43 expect(
44 catalogGatewayEndpoint(
45 { schema: { input: { type: 'object' } } },
46 { ...info, activity: null },
47 source,
48 contract,
49 ),
50 ).toBeNull()
51 expect(
52 catalogGatewayEndpoint({ schema: { input: {} } }, info, source, contract),
53 ).toBeNull()
54 expect(catalogGatewayEndpoint({}, info, source, contract)).toBeNull()
55})
56it('rebases internal references into the model input without changing their target', () => {
57 const endpoint = catalogGatewayEndpoint(
58 {
59 schema: {
60 input: {
61 $defs: { Text: { type: 'string' } },
62 properties: { prompt: { $ref: '#/$defs/Text' } },
63 },
64 },
65 },
66 info,
67 source,
68 contract,
69 )
70 expect(endpoint?.input).toMatchObject({
71 properties: {
72 input: {
73 properties: { prompt: { $ref: '#/properties/input/$defs/Text' } },
74 },
75 },
76 })
77})
78it('rejects ambiguous schema scopes and external refs', () => {
79 expect(() =>
80 catalogGatewayEndpoint(
81 { schema: { input: { $id: 'https://native/schema', type: 'object' } } },
82 info,
83 source,
84 contract,
85 ),
86 ).toThrow('scoped input $id')
87 expect(() =>
88 catalogGatewayEndpoint(
89 { schema: { input: { $ref: 'https://native/schema' } } },
90 info,
91 source,
92 contract,
93 ),
94 ).toThrow('external reference')
95})
96it('rejects changed or missing native envelope and POST evidence', () => {
97 expect(() =>
98 parseGatewayRunContract(docs.replace('Envelope with', 'Other'), source),
99 ).toThrow('no model/input envelope')
100 expect(() =>
101 parseGatewayRunContract(
102 docs.replace('curl -X POST', 'curl -X GET'),
103 source,
104 ),
105 ).toThrow('no universal POST path')
106})