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

bundle.test.ts

246 lines7.3 KB

1import { describe, expect, it } from 'vitest'
2​
3import type { OpenApiDocument } from '#/server/providers/types.ts'
4import {
5 bundleSchema,
6 endpointIdFromPath,
7 extractEndpointSchemas,
8 findDanglingRefs,
9} from './bundle.ts'
10import type { JsonValue } from './bundle.ts'
11​
12const ref = (name: string) => ({ $ref: `#/components/schemas/${name}` })
13​
14function fixtureSpec(): OpenApiDocument {
15 return {
16 openapi: '3.1.0',
17 paths: {
18 '/v1/messages': {
19 post: {
20 requestBody: {
21 content: { 'application/json': { schema: ref('CreateMessage') } },
22 },
23 responses: {
24 '200': {
25 content: { 'application/json': { schema: ref('Message') } },
26 },
27 },
28 },
29 },
30 '/v1/audio/transcriptions': {
31 post: {
32 requestBody: {
33 content: {
34 'multipart/form-data': { schema: ref('TranscriptionRequest') },
35 },
36 },
37 responses: {
38 '201': {
39 content: { 'application/json': { schema: ref('Transcription') } },
40 },
41 },
42 },
43 },
44 '/v1/audio/speech': {
45 post: {
46 requestBody: {
47 content: { 'application/json': { schema: ref('SpeechRequest') } },
48 },
49 responses: {
50 '200': { content: { 'audio/mpeg': {} } },
51 },
52 },
53 },
54 '/fal-ai/veo': {
55 post: {
56 requestBody: {
57 content: { 'application/json': { schema: ref('VeoInput') } },
58 },
59 responses: {
60 '200': {
61 content: { 'application/json': { schema: ref('QueueStatus') } },
62 },
63 },
64 },
65 },
66 '/fal-ai/veo/requests/{request_id}': {
67 get: {
68 responses: {
69 '200': {
70 content: { 'application/json': { schema: ref('VeoOutput') } },
71 },
72 },
73 },
74 },
75 },
76 components: {
77 schemas: {
78 CreateMessage: {
79 type: 'object',
80 properties: {
81 content: { type: 'array', items: ref('ContentBlock') },
82 tool: ref('Tool'),
83 },
84 },
85 ContentBlock: {
86 type: 'object',
87 properties: { nested: ref('Tool') },
88 },
89 Tool: { type: 'object', properties: { name: { type: 'string' } } },
90 Message: {
91 type: 'object',
92 properties: { blocks: { type: 'array', items: ref('ContentBlock') } },
93 },
94 TranscriptionRequest: {
95 type: 'object',
96 properties: { file: { type: 'string', format: 'binary' } },
97 },
98 Transcription: { type: 'object' },
99 SpeechRequest: { type: 'object' },
100 VeoInput: { type: 'object' },
101 QueueStatus: { type: 'object' },
102 VeoOutput: {
103 type: 'object',
104 properties: { video: ref('VideoFile') },
105 },
106 VideoFile: { type: 'object' },
107 },
108 },
109 }
111​
112describe('extractEndpointSchemas', () => {
113 it('bundles json input + post-200 output with the full $ref closure', () => {
114 const result = extractEndpointSchemas(fixtureSpec(), '/v1/messages')
115 expect(result.warnings).toEqual([])
116 expect(result.input?.type).toBe('object')
117 const defs = result.input?.$defs as Record<string, unknown>
118 expect(Object.keys(defs).sort()).toEqual(['ContentBlock', 'Tool'])
119 expect(findDanglingRefs(result.input ?? {})).toEqual([])
120 expect(findDanglingRefs(result.output ?? {})).toEqual([])
121 })
122​
123 it('handles multipart/form-data bodies and 201 success responses', () => {
124 const result = extractEndpointSchemas(
125 fixtureSpec(),
126 '/v1/audio/transcriptions',
127 )
128 expect(result.input).toBeDefined()
129 expect(
130 (result.input?.properties as Record<string, Record<string, unknown>>).file
131 ?.format,
132 ).toBe('binary')
133 expect(result.output).toBeDefined()
134 })
135​
136 it('omits output for binary-media endpoints', () => {
137 const result = extractEndpointSchemas(fixtureSpec(), '/v1/audio/speech')
138 expect(result.input).toBeDefined()
139 expect(result.output).toBeUndefined()
140 })
141​
142 it('uses the sibling GET for the sibling-get strategy', () => {
143 const result = extractEndpointSchemas(
144 fixtureSpec(),
145 '/fal-ai/veo',
146 'sibling-get',
147 )
148 const defs = result.output?.$defs as Record<string, unknown>
149 expect(Object.keys(defs)).toEqual(['VideoFile'])
150 expect(findDanglingRefs(result.output ?? {})).toEqual([])
151 })
152​
153 it('returns nothing for unknown paths', () => {
154 expect(extractEndpointSchemas(fixtureSpec(), '/nope')).toEqual({
155 warnings: [],
156 })
157 })
158})
159​
160describe('bundleSchema', () => {
161 const components: Record<string, JsonValue> = {
162 Root: {
163 type: 'object',
164 properties: {
165 child: { $ref: '#/components/schemas/Child' },
166 self: { $ref: '#/components/schemas/Root' },
167 },
168 },
169 Child: {
170 type: 'object',
171 properties: { parent: { $ref: '#/components/schemas/Root' } },
172 },
173 }
174​
175 it('rewrites self-references to # instead of leaving dangling defs', () => {
176 const { schema, warnings } = bundleSchema(
177 components.Root ?? null,
178 components,
179 'Root',
180 'test',
181 )
182 expect(warnings).toEqual([])
183 const props = schema.properties as Record<string, Record<string, unknown>>
184 expect(props.self?.$ref).toBe('#')
185 expect(props.child?.$ref).toBe('#/$defs/Child')
186 const defs = schema.$defs as Record<string, Record<string, unknown>>
187 expect(Object.keys(defs)).toEqual(['Child'])
188 expect(
189 (defs.Child?.properties as Record<string, Record<string, unknown>>).parent
190 ?.$ref,
191 ).toBe('#')
192 expect(findDanglingRefs(schema)).toEqual([])
193 })
194​
195 it('resolves dedup-renamed refs through the underscore alias (PR #622 lesson)', () => {
196 const merged: Record<string, JsonValue> = {
197 Input: {
198 type: 'object',
199 properties: { shared: { $ref: '#/components/schemas/Shared-2' } },
200 },
201 // The merge step sanitised the components key to an underscore...
202 Shared_2: { type: 'object', properties: { ok: { type: 'boolean' } } },
203 }
204 const { schema, warnings } = bundleSchema(
205 merged.Input ?? null,
206 merged,
207 'Input',
208 'test',
209 )
210 expect(warnings).toEqual([])
211 // ...so the bundled $defs carries the ref's spelling, resolved via alias.
212 const defs = schema.$defs as Record<string, unknown>
213 expect(defs['Shared-2']).toBeDefined()
214 expect(findDanglingRefs(schema)).toEqual([])
215 })
216​
217 it('records warnings for dangling refs instead of throwing', () => {
218 const { schema, warnings } = bundleSchema(
219 {
220 type: 'object',
221 properties: { x: { $ref: '#/components/schemas/Missing' } },
222 },
223 {},
224 null,
225 'ctx',
226 )
227 expect(warnings).toHaveLength(1)
228 expect(warnings[0]).toContain("'Missing' not found")
229 expect(findDanglingRefs(schema)).toEqual(['#/$defs/Missing'])
230 })
231})
232​
233describe('endpointIdFromPath / findDanglingRefs', () => {
234 it('derives endpoint ids by stripping the leading slash', () => {
235 expect(endpointIdFromPath('/v1/messages')).toBe('v1/messages')
236 expect(endpointIdFromPath('/fal-ai/veo')).toBe('fal-ai/veo')
237 })
238​
239 it('flags refs that escape the bundle', () => {
240 expect(
241 findDanglingRefs({
242 $ref: '#/components/schemas/Escaped',
243 }),
244 ).toEqual(['#/components/schemas/Escaped'])
245 })
246})