Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { env } from 'cloudflare:test'
3import { eq } from 'drizzle-orm'
4
5import { getDb } from '../../db/index.ts'
6import { endpoints, models, providers } from '../../db/schema.ts'
7import { getModelDetail, listModelsCatalog } from '../catalog.ts'
8import { assembleProviderOpenApi } from '../provider-openapi.ts'
9import { FAL_ACTIVITY_MARKER, falProvider } from '../providers/fal.ts'
10import type { ProviderConfig, SpecFetchResult } from '../providers/types.ts'
11import { getEndpointSchema } from '../schemas-api.ts'
12import { asyncApiMessageTypes, extractAsyncApiSchemas } from './asyncapi.ts'
13import directorAsyncApi from './fixtures/fal-minimax-h3-max-director.asyncapi.json'
14import { syncProvider } from './sync.ts'
15import type { SyncDeps } from './sync.ts'
16
17const NOW = 1_781_150_000
18const DIRECTOR = 'minimax/h3-max/director'
19const OPENAPI_MODEL = 'fal-ai/wma-open'
20const GHOST = 'ghost/no-spec'
21
22function wmaProvider(fetched: SpecFetchResult, id = 'fal'): ProviderConfig {
23 return {
24 ...falProvider,
25 id,
26 fetchSpec: () => Promise.resolve(fetched),
27 listModels: () => Promise.resolve({ models: [] }),
28 }
29}
30
31async function seedFal(db: SyncDeps['db']): Promise<SyncDeps> {
32 await db
33 .insert(providers)
34 .values({
35 id: 'fal',
36 displayName: 'FAL',
37 specSourceUrl: 'https://api.fal.ai/v1/models?expand=openapi-3.0',
38 })
39 .onConflictDoNothing()
40 await db
41 .insert(models)
42 .values([
43 {
44 id: 'fal-minimax-h3-max-director',
45 providerId: 'fal',
46 rawId: DIRECTOR,
47 activity: 'video',
48 displayName: 'H3 Max Director',
49 providerMetadata: { category: 'text-to-video' },
50 firstSeenAt: NOW,
51 lastSeenAt: NOW,
52 },
53 {
54 id: 'fal-fal-ai-wma-open',
55 providerId: 'fal',
56 rawId: OPENAPI_MODEL,
57 activity: 'image',
58 providerMetadata: { category: 'text-to-image' },
59 firstSeenAt: NOW,
60 lastSeenAt: NOW,
61 },
62 {
63 id: 'fal-ghost-no-spec',
64 providerId: 'fal',
65 rawId: GHOST,
66 activity: 'image',
67 providerMetadata: { category: 'text-to-image' },
68 firstSeenAt: NOW,
69 lastSeenAt: NOW,
70 },
71 ])
72 .onConflictDoNothing()
73 let tick = NOW
74 return {
75 db,
76 kv: env.SCHEMA_CACHE,
77 secrets: {},
78 now: () => tick++,
79 }
80}
81
82function directorFetched(): SpecFetchResult {
83 const extracted = extractAsyncApiSchemas(directorAsyncApi)
84 return {
85 specs: [
86 {
87 paths: {
88 [`/${OPENAPI_MODEL}`]: {
89 post: {
90 [FAL_ACTIVITY_MARKER]: 'image',
91 summary: 'Flux',
92 requestBody: {
93 content: {
94 'application/json': {
95 schema: {
96 type: 'object',
97 properties: { prompt: { type: 'string' } },
98 },
99 },
100 },
101 },
102 responses: {
103 '200': {
104 content: {
105 'application/json': {
106 schema: {
107 type: 'object',
108 properties: { id: { type: 'string' } },
109 },
110 },
111 },
112 },
113 },
114 },
115 },
116 },
117 },
118 ],
119 sources: [
120 {
121 url: `https://api.fal.ai/v1/models?expand=openapi-3.0#${OPENAPI_MODEL}`,
122 hash: 'openapi-source',
123 },
124 ],
125 outputStrategy: 'post-200',
126 bundledEndpoints: [
127 {
128 path: `/${DIRECTOR}`,
129 activity: 'video',
130 description: 'MiniMaxH3MaxDirector realtime client API',
131 source: {
132 url: 'https://fal.ai/api/apps/fal-ai/minimax-h3-max-director/asyncapi.json',
133 hash: 'director-source',
134 },
135 derivation: 'upstream-spec',
136 input: extracted.input,
137 output: extracted.output,
138 asyncapi: true,
139 },
140 ],
141 asyncApiRawIds: [DIRECTOR],
142 }
143}
144
145describe('FAL AsyncAPI ingest', () => {
146 it('stores message schemas, flags the catalog, and leaves OpenAPI models alone', async () => {
147 const db = getDb(env)
148 const deps = await seedFal(db)
149 const outcome = await syncProvider(deps, wmaProvider(directorFetched()))
150 expect(outcome.error).toBeUndefined()
151 expect(outcome.endpointsSeen).toBe(2)
152
153 const input = await getEndpointSchema(db, 'fal', 'video', DIRECTOR, 'input')
154 expect(input).not.toBeNull()
155 expect(asyncApiMessageTypes(input?.schema)).toEqual(
156 expect.arrayContaining(['configure', 'prompt']),
157 )
158 expect(input?.provenance).toMatchObject({
159 derivation: 'upstream-spec',
160 sourceUrl:
161 'https://fal.ai/api/apps/fal-ai/minimax-h3-max-director/asyncapi.json',
162 })
163
164 const output = await getEndpointSchema(
165 db,
166 'fal',
167 'video',
168 DIRECTOR,
169 'output',
170 )
171 expect(asyncApiMessageTypes(output?.schema)).toEqual(
172 expect.arrayContaining(['session_info', 'chunk', 'error']),
173 )
174
175 const director = await getModelDetail(db, 'fal', DIRECTOR)
176 expect(director?.capabilities).toEqual({ asyncapi: true })
177 expect(director?.providerMetadata).toEqual({ category: 'text-to-video' })
178 expect(director?.schemaEndpointId).toBe(DIRECTOR)
179
180 const flagged = await listModelsCatalog(db, {
181 provider: 'fal',
182 capability: 'asyncapi',
183 })
184 expect(flagged.models.map((m) => m.rawId)).toContain(DIRECTOR)
185 expect(flagged.models.map((m) => m.rawId)).not.toContain(OPENAPI_MODEL)
186 expect(flagged.models.map((m) => m.rawId)).not.toContain(GHOST)
187
188 const openapiModel = await getModelDetail(db, 'fal', OPENAPI_MODEL)
189 expect(openapiModel?.capabilities).toBeNull()
190 const openapiInput = await getEndpointSchema(
191 db,
192 'fal',
193 'image',
194 OPENAPI_MODEL,
195 'input',
196 )
197 expect(openapiInput?.schema).toMatchObject({
198 properties: { prompt: { type: 'string' } },
199 })
200
201 expect(
202 await getEndpointSchema(db, 'fal', 'image', GHOST, 'input'),
203 ).toBeNull()
204 const ghost = await getModelDetail(db, 'fal', GHOST)
205 expect(ghost?.capabilities).toBeNull()
206
207 const assembled = await assembleProviderOpenApi(db, 'fal', {
208 model: DIRECTOR,
209 })
210 expect(assembled).toMatchObject({
211 ok: false,
212 status: 404,
213 code: 'unknown_model',
214 })
215 if (!assembled.ok) {
216 expect(assembled.message).toContain('AsyncAPI')
217 }
218
219 const directorEndpoints = await db
220 .select()
221 .from(endpoints)
222 .where(eq(endpoints.id, `fal/${DIRECTOR}`))
223 expect(directorEndpoints[0]?.method).toBe('POST')
224 expect(directorEndpoints[0]?.activity).toBe('video')
225 })
226
227 it('clears the catalog flag when a later sync finds no AsyncAPI models', async () => {
228 const db = getDb(env)
229 const id = 'fal-unflag'
230 await db.insert(providers).values({
231 id,
232 displayName: 'FAL',
233 specSourceUrl: 'https://example.com/fal.json',
234 })
235 await db.insert(models).values({
236 id: 'fal-unflag-cleared-wma',
237 providerId: id,
238 rawId: 'cleared/wma',
239 activity: 'video',
240 capabilities: { asyncapi: true, tools: true },
241 firstSeenAt: NOW,
242 lastSeenAt: NOW,
243 })
244 const deps: SyncDeps = {
245 db,
246 kv: env.SCHEMA_CACHE,
247 secrets: {},
248 now: () => NOW + 50,
249 }
250 await syncProvider(
251 deps,
252 wmaProvider(
253 {
254 specs: [],
255 sources: [],
256 outputStrategy: 'sibling-get',
257 bundledEndpoints: [],
258 asyncApiRawIds: [],
259 },
260 id,
261 ),
262 )
263 const row = await db.query.models.findFirst({
264 where: eq(models.id, 'fal-unflag-cleared-wma'),
265 })
266 expect(row?.capabilities).toEqual({ tools: true })
267 })
268})