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

lovable-video.test.ts

261 lines7.5 KB

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