Open History to see the last commit
1import { Validator } from '@cfworker/json-schema'
2import { describe, expect, it } from 'vitest'
3
4import { classifyAndBundle } from '#/server/ingest/sync.ts'
5import { provider } from './adapters/lovable.ts'
6import { applyLovableVideoSchema } from './lovable-video.ts'
7import type { OpenApiDocument } from './types.ts'
8
9const LISTED = [
10 'google/veo-3.1',
11 'google/veo-3.1-fast',
12 'google/veo-3.1-lite',
13 'google/gemini-omni-1.1-flash',
14]
15
16function soraSpec(): OpenApiDocument {
17 return {
18 openapi: '3.1.0',
19 paths: {
20 '/videos': {
21 post: {
22 summary: 'Create a video',
23 // The live OpenAI operation carries its own examples (#680).
24 'x-oaiMeta': {
25 name: 'Create video',
26 examples: {
27 request: {
28 curl: 'curl https://api.openai.com/v1/videos \\\n -H "Authorization: Bearer $OPENAI_API_KEY" \\\n -F "model=sora-2" \\\n -F "prompt=A calico cat playing a piano on stage"\n',
29 },
30 response:
31 '{\n "id": "video_123",\n "object": "video",\n "model": "sora-2",\n "size": "1024x1792",\n "seconds": "8"\n}\n',
32 },
33 },
34 requestBody: {
35 content: {
36 'multipart/form-data': {
37 schema: {
38 $ref: '#/components/schemas/CreateVideoMultipartBody',
39 },
40 },
41 },
42 },
43 responses: {
44 '200': {
45 description: 'Success',
46 content: {
47 'application/json': {
48 schema: { $ref: '#/components/schemas/VideoResource' },
49 },
50 },
51 },
52 },
53 },
54 },
55 },
56 components: {
57 schemas: {
58 VideoModel: {
59 anyOf: [
60 { type: 'string' },
61 { type: 'string', enum: ['sora-2', 'sora-2-pro'] },
62 ],
63 },
64 VideoSeconds: { type: 'string', enum: ['4', '8', '12'] },
65 VideoSize: {
66 type: 'string',
67 enum: ['720x1280', '1280x720', '1024x1792', '1792x1024'],
68 },
69 CreateVideoMultipartBody: {
70 type: 'object',
71 description: 'Create a Sora video.',
72 properties: {
73 model: {
74 $ref: '#/components/schemas/VideoModel',
75 description:
76 'The video generation model to use (allowed values: sora-2, sora-2-pro). Defaults to `sora-2`.',
77 },
78 prompt: {
79 type: 'string',
80 description: 'Text prompt that describes the video to generate.',
81 },
82 input_reference: {
83 type: 'string',
84 format: 'binary',
85 description: 'Optional reference asset.',
86 },
87 seconds: {
88 $ref: '#/components/schemas/VideoSeconds',
89 description:
90 'Clip duration in seconds (allowed values: 4, 8, 12). Defaults to 4 seconds.',
91 },
92 size: {
93 $ref: '#/components/schemas/VideoSize',
94 description:
95 'Output resolution formatted as width x height (allowed values: 720x1280, 1280x720, 1024x1792, 1792x1024). Defaults to 720x1280.',
96 },
97 },
98 required: ['prompt'],
99 },
100 CreateVideoJsonBody: {
101 type: 'object',
102 properties: {
103 model: {
104 description: 'allowed values: sora-2, sora-2-pro',
105 },
106 prompt: { type: 'string' },
107 seconds: {
108 description: 'allowed values: 4, 8, 12',
109 },
110 },
111 required: ['prompt'],
112 },
113 VideoResource: {
114 type: 'object',
115 properties: {
116 model: { $ref: '#/components/schemas/VideoModel' },
117 seconds: { $ref: '#/components/schemas/VideoSeconds' },
118 size: { $ref: '#/components/schemas/VideoSize' },
119 },
120 },
121 },
122 },
123 }
124}
125
126function bundledInput(ids: ReadonlyArray<string> = LISTED) {
127 const spec = soraSpec()
128 applyLovableVideoSchema(spec, ids)
129 const { endpoints } = classifyAndBundle(provider, {
130 specs: [spec],
131 sources: [{ url: 'https://example.test/openapi.yml', hash: 'fixture' }],
132 outputStrategy: 'post-200',
133 })
134 const video = endpoints.find((endpoint) => endpoint.path === '/videos')
135 if (!video?.input) throw new Error('missing bundled video input')
136 return { spec, video, input: video.input }
137}
138
139function isRecord(value: unknown): value is Record<string, unknown> {
140 return typeof value === 'object' && value !== null && !Array.isArray(value)
141}
142
143function branch(
144 input: Record<string, unknown>,
145 model: string,
146): Record<string, unknown> {
147 const defs = input.$defs
148 if (!isRecord(defs)) throw new Error('no $defs')
149 const found = Object.values(defs).find((schema) => {
150 if (!isRecord(schema) || !isRecord(schema.properties)) return false
151 const modelField = schema.properties.model
152 if (!isRecord(modelField)) return false
153 return JSON.stringify(modelField.enum) === JSON.stringify([model])
154 })
155 if (!isRecord(found)) throw new Error(`no branch for ${model}`)
156 return found
157}
158
159function accepts(schema: Record<string, unknown>, payload: unknown): boolean {
160 return new Validator(schema, '2020-12', false).validate(payload).valid
161}
162
163describe('lovable video schema', () => {
164 it('allows 6 seconds on Veo and rejects 12 and Sora sizes', () => {
165 const { video, input } = bundledInput()
166 expect(video.derivation).toBe('docs-derived')
167 expect(video.verifiedAt).toBe('2026-11-16')
168 expect(JSON.stringify(input)).not.toMatch(/sora/i)
169 expect(JSON.stringify(video.output)).not.toMatch(/sora/i)
170
171 const veo = branch(input, 'google/veo-3.1')
172 const lite = branch(input, 'google/veo-3.1-lite')
173 const omni = branch(input, 'google/gemini-omni-1.1-flash')
174
175 expect(
176 accepts(veo, { model: 'google/veo-3.1', prompt: 'a cat', seconds: '6' }),
177 ).toBe(true)
178 expect(
179 accepts(veo, { model: 'google/veo-3.1', prompt: 'a cat', seconds: '12' }),
180 ).toBe(false)
181 expect(
182 accepts(veo, {
183 model: 'google/veo-3.1',
184 prompt: 'a cat',
185 seconds: '8',
186 size: '3840x2160',
187 }),
188 ).toBe(true)
189 expect(
190 accepts(veo, {
191 model: 'google/veo-3.1',
192 prompt: 'a cat',
193 seconds: '4',
194 size: '1920x1080',
195 }),
196 ).toBe(false)
197 expect(
198 accepts(lite, {
199 model: 'google/veo-3.1-lite',
200 prompt: 'a cat',
201 seconds: '6',
202 size: '3840x2160',
203 }),
204 ).toBe(false)
205 expect(
206 accepts(lite, {
207 model: 'google/veo-3.1-lite',
208 prompt: 'a cat',
209 seconds: '6',
210 size: '1280x720',
211 }),
212 ).toBe(true)
213 expect(
214 accepts(omni, {
215 model: 'google/gemini-omni-1.1-flash',
216 prompt: 'a cat',
217 seconds: '3',
218 size: '640x360',
219 }),
220 ).toBe(true)
221 expect(
222 accepts(omni, {
223 model: 'google/gemini-omni-1.1-flash',
224 prompt: 'a cat',
225 seconds: '10',
226 }),
227 ).toBe(true)
228 expect(
229 accepts(omni, {
230 model: 'google/gemini-omni-1.1-flash',
231 prompt: 'a cat',
232 seconds: '12',
233 }),
234 ).toBe(false)
235 expect(
236 accepts(omni, {
237 model: 'google/gemini-omni-1.1-flash',
238 prompt: 'a cat',
239 size: '1024x1792',
240 }),
241 ).toBe(false)
242 })
243
244 it('rejects the whole request when the model and the branch disagree', () => {
245 const { input } = bundledInput()
246 expect(
247 accepts(input, {
248 model: 'google/veo-3.1-lite',
249 prompt: 'a cat',
250 seconds: '6',
251 size: '3840x2160',
252 }),
253 ).toBe(false)
254 expect(
255 accepts(input, {
256 model: 'google/veo-3.1',
257 prompt: 'a cat',
258 seconds: '6',
259 size: '1280x720',
260 }),
261 ).toBe(true)
262 })
263
264 it('drops the OpenAI examples and names the path of any Sora text left', () => {
265 const { spec } = bundledInput()
266 expect(JSON.stringify(spec.paths?.['/videos']?.post)).not.toMatch(/sora/i)
267
268 const leftover = soraSpec()
269 const post = leftover.paths?.['/videos']?.post
270 if (!post) throw new Error('fixture has no POST /videos')
271 post.description = 'Create a video with Sora.'
272 expect(() => applyLovableVideoSchema(leftover, LISTED)).toThrow(
273 'lovable: video schema still mentions Sora at post.description',
274 )
275 })
276
277 it('throws when a listed video model has no limits', () => {
278 expect(() => applyLovableVideoSchema(soraSpec(), ['google/veo-4'])).toThrow(
279 /no video limits for google\/veo-4/,
280 )
281 expect(() => applyLovableVideoSchema(soraSpec(), [])).toThrow(
282 /listed no video ids/,
283 )
284 })
285})