Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import type { ProviderConfig } from '#/server/providers/types.ts'
4import directorAsyncApi from './fixtures/fal-minimax-h3-max-director.asyncapi.json'
5import {
6 asyncApiMessageTypes,
7 extractAsyncApiSchemas,
8 hasAsyncApiFlag,
9 preserveAsyncApiFlag,
10 withAsyncApiFlag,
11} from './asyncapi.ts'
12import { EXTRACTOR_VERSION, findDanglingRefs } from './bundle.ts'
13import { classifyAndBundle } from './sync.ts'
14
15describe('extractAsyncApiSchemas', () => {
16 it('bundles Director client/server messages as type-discriminated oneOf', () => {
17 const extracted = extractAsyncApiSchemas(directorAsyncApi)
18 expect(extracted.warnings).toEqual([])
19 expect(extracted.input).toBeDefined()
20 expect(extracted.output).toBeDefined()
21 expect(findDanglingRefs(extracted.input ?? {})).toEqual([])
22 expect(findDanglingRefs(extracted.output ?? {})).toEqual([])
23 expect(asyncApiMessageTypes(extracted.input)).toEqual(
24 expect.arrayContaining(['configure', 'prompt']),
25 )
26 expect(asyncApiMessageTypes(extracted.output)).toEqual(
27 expect.arrayContaining(['session_info', 'chunk', 'error']),
28 )
29 expect(extracted.output?.$defs).toMatchObject({
30 MiniMaxH3MaxDirectorServerDispatchAccounting: {
31 type: 'object',
32 },
33 })
34 })
35
36 it('falls back to components.messages when operations are absent', () => {
37 const extracted = extractAsyncApiSchemas({
38 asyncapi: '3.1.0',
39 components: {
40 messages: {
41 'client.configure': {
42 payload: {
43 type: 'object',
44 properties: { type: { const: 'configure' } },
45 },
46 },
47 'server.error': {
48 payload: {
49 type: 'object',
50 properties: { type: { const: 'error' } },
51 },
52 },
53 },
54 },
55 })
56 expect(asyncApiMessageTypes(extracted.input)).toEqual(['configure'])
57 expect(asyncApiMessageTypes(extracted.output)).toEqual(['error'])
58 })
59
60 it('does not bump the OpenAPI extractor version', () => {
61 expect(EXTRACTOR_VERSION).toBe('1')
62 })
63})
64
65describe('asyncapi catalog flag helpers', () => {
66 it('adds, detects, drops, and preserves asyncapi: true', () => {
67 const listed = { tools: true }
68 const flagged = withAsyncApiFlag(listed, true)
69 expect(flagged).toEqual({ tools: true, asyncapi: true })
70 expect(hasAsyncApiFlag(flagged)).toBe(true)
71 expect(hasAsyncApiFlag(listed)).toBe(false)
72 expect(withAsyncApiFlag(flagged, false)).toEqual({ tools: true })
73 expect(preserveAsyncApiFlag(flagged, listed)).toEqual({
74 tools: true,
75 asyncapi: true,
76 })
77 expect(preserveAsyncApiFlag(listed, listed)).toEqual(listed)
78 // A row with no flags of its own: the flag is the whole map, or none.
79 expect(withAsyncApiFlag(null, true)).toEqual({ asyncapi: true })
80 expect(withAsyncApiFlag({ asyncapi: true }, false)).toBeNull()
81 expect(preserveAsyncApiFlag({ asyncapi: true }, null)).toEqual({
82 asyncapi: true,
83 })
84 })
85})
86
87describe('classifyAndBundle bundledEndpoints', () => {
88 it('appends AsyncAPI endpoints next to OpenAPI paths', () => {
89 const extracted = extractAsyncApiSchemas(directorAsyncApi)
90 const provider: ProviderConfig = {
91 id: 'fal',
92 displayName: 'FAL',
93 defaultDerivation: 'upstream-spec',
94 fetchSpec: () =>
95 Promise.resolve({
96 specs: [],
97 sources: [],
98 outputStrategy: 'sibling-get',
99 }),
100 listModels: () => Promise.resolve({ models: [] }),
101 classify: (path) => (path === '/fal-ai/flux/dev' ? 'image' : null),
102 }
103 const { endpoints } = classifyAndBundle(provider, {
104 specs: [
105 {
106 paths: {
107 '/fal-ai/flux/dev': {
108 post: {
109 summary: 'Flux',
110 requestBody: {
111 content: {
112 'application/json': {
113 schema: { type: 'object' },
114 },
115 },
116 },
117 },
118 },
119 },
120 },
121 ],
122 sources: [{ url: 'https://example.com/openapi.json', hash: 'abc' }],
123 outputStrategy: 'post-200',
124 bundledEndpoints: [
125 {
126 path: '/minimax/h3-max/director',
127 activity: 'video',
128 description: 'Director',
129 source: {
130 url: 'https://fal.ai/api/apps/fal-ai/minimax-h3-max-director/asyncapi.json',
131 hash: 'def',
132 },
133 derivation: 'upstream-spec',
134 input: extracted.input,
135 output: extracted.output,
136 asyncapi: true,
137 },
138 ],
139 })
140 expect(endpoints.map((e) => e.dbId)).toEqual([
141 'fal/fal-ai/flux/dev',
142 'fal/minimax/h3-max/director',
143 ])
144 expect(endpoints[1]).toMatchObject({
145 dbId: 'fal/minimax/h3-max/director',
146 path: '/minimax/h3-max/director',
147 activity: 'video',
148 asyncapi: true,
149 derivation: 'upstream-spec',
150 })
151 })
152})