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

image-inputs.test.ts

262 lines9.0 KB

1import { createHash } from 'node:crypto'
2import { readFileSync } from 'node:fs'
3import { createRequire } from 'node:module'
4import { describe, expect, it } from 'vitest'
5import {
6 mapImageInputsToFalFields,
7 mapImageInputsToFalVideoFields,
8} from '../src/image/image-inputs'
9import { FAL_ENDPOINTS_DTS_SHA256 } from '../src/image/generated/image-field-overrides'
10import type { ImagePart, MediaInputMetadata } from '@tanstack/ai'
11​
12/** A model id unknown to the SDK — exercises the default field mapping. */
13const UNKNOWN_MODEL = 'custom-org/not-in-sdk'
14​
15function urlPart(
16 value: string,
17 metadata?: MediaInputMetadata,
18): ImagePart<MediaInputMetadata> {
19 return {
20 type: 'image',
21 source: { type: 'url', value },
22 ...(metadata && { metadata }),
23 }
25​
26describe('mapImageInputsToFalFields', () => {
27 it('returns an empty object when imageInputs is missing or empty', () => {
28 expect(mapImageInputsToFalFields(UNKNOWN_MODEL, undefined)).toEqual({})
29 expect(mapImageInputsToFalFields(UNKNOWN_MODEL, [])).toEqual({})
30 })
31​
32 it('routes a single source to image_url', () => {
33 expect(
34 mapImageInputsToFalFields(UNKNOWN_MODEL, [
35 urlPart('https://example.com/a.png'),
36 ]),
37 ).toEqual({ image_url: 'https://example.com/a.png' })
38 })
39​
40 it('routes multiple sources to image_urls', () => {
41 expect(
42 mapImageInputsToFalFields(UNKNOWN_MODEL, [
43 urlPart('https://example.com/a.png'),
44 urlPart('https://example.com/b.png'),
45 ]),
46 ).toEqual({
47 image_urls: ['https://example.com/a.png', 'https://example.com/b.png'],
48 })
49 })
50​
51 it('routes role=mask to mask_url alongside the source image_url', () => {
52 expect(
53 mapImageInputsToFalFields(UNKNOWN_MODEL, [
54 urlPart('https://example.com/img.png'),
55 urlPart('https://example.com/mask.png', { role: 'mask' }),
56 ]),
57 ).toEqual({
58 image_url: 'https://example.com/img.png',
59 mask_url: 'https://example.com/mask.png',
60 })
61 })
62​
63 it('routes role=reference to reference_image_urls', () => {
64 expect(
65 mapImageInputsToFalFields(UNKNOWN_MODEL, [
66 urlPart('https://example.com/product.png'),
67 urlPart('https://example.com/style.png', { role: 'reference' }),
68 urlPart('https://example.com/character.png', { role: 'character' }),
69 ]),
70 ).toEqual({
71 image_url: 'https://example.com/product.png',
72 reference_image_urls: [
73 'https://example.com/style.png',
74 'https://example.com/character.png',
75 ],
76 })
77 })
78​
79 it('routes role=control to control_image_url', () => {
80 expect(
81 mapImageInputsToFalFields(UNKNOWN_MODEL, [
82 urlPart('https://example.com/img.png'),
83 urlPart('https://example.com/depth.png', { role: 'control' }),
84 ]),
85 ).toEqual({
86 image_url: 'https://example.com/img.png',
87 control_image_url: 'https://example.com/depth.png',
88 })
89 })
90​
91 it('encodes data sources as data URIs', () => {
92 expect(
93 mapImageInputsToFalFields(UNKNOWN_MODEL, [
94 {
95 type: 'image',
96 source: { type: 'data', value: 'aGVsbG8=', mimeType: 'image/png' },
97 },
98 ]),
99 ).toEqual({ image_url: 'data:image/png;base64,aGVsbG8=' })
100 })
101​
102 it('throws when more than one mask is provided', () => {
103 expect(() =>
104 mapImageInputsToFalFields(UNKNOWN_MODEL, [
105 urlPart('https://example.com/m1.png', { role: 'mask' }),
106 urlPart('https://example.com/m2.png', { role: 'mask' }),
107 ]),
108 ).toThrow(/only one input with metadata.role === 'mask'/)
109 })
110​
111 describe('generated endpoint overrides', () => {
112 it('routes a single source to image_urls on endpoints without a scalar field', () => {
113 // nano-banana edit has image_urls but no image_url
114 expect(
115 mapImageInputsToFalFields('fal-ai/nano-banana/edit', [
116 urlPart('https://example.com/a.png'),
117 ]),
118 ).toEqual({ image_urls: ['https://example.com/a.png'] })
119 })
120​
121 it('merges sources and references when both resolve to the same list field', () => {
122 expect(
123 mapImageInputsToFalFields('fal-ai/nano-banana/edit', [
124 urlPart('https://example.com/product.png'),
125 urlPart('https://example.com/style.png', { role: 'reference' }),
126 ]),
127 ).toEqual({
128 image_urls: [
129 'https://example.com/product.png',
130 'https://example.com/style.png',
131 ],
132 })
133 })
134​
135 it('routes role=mask to endpoint-specific mask field names', () => {
136 // gpt-image-1.5 edit uses mask_image_url instead of mask_url
137 expect(
138 mapImageInputsToFalFields('fal-ai/gpt-image-1.5/edit', [
139 urlPart('https://example.com/img.png'),
140 urlPart('https://example.com/mask.png', { role: 'mask' }),
141 ]),
142 ).toEqual({
143 image_urls: ['https://example.com/img.png'],
144 mask_image_url: 'https://example.com/mask.png',
145 })
146 })
147 })
148})
149​
150describe('mapImageInputsToFalVideoFields', () => {
151 it('returns empty for missing/empty inputs', () => {
152 expect(mapImageInputsToFalVideoFields(UNKNOWN_MODEL, undefined)).toEqual({})
153 expect(mapImageInputsToFalVideoFields(UNKNOWN_MODEL, [])).toEqual({})
154 })
155​
156 it('routes a single positional source to image_url (start frame)', () => {
157 expect(
158 mapImageInputsToFalVideoFields(UNKNOWN_MODEL, [
159 urlPart('https://example.com/start.png'),
160 ]),
161 ).toEqual({ image_url: 'https://example.com/start.png' })
162 })
163​
164 it('routes role=start_frame to start_image_url and role=end_frame to end_image_url', () => {
165 expect(
166 mapImageInputsToFalVideoFields(UNKNOWN_MODEL, [
167 urlPart('https://example.com/a.png', { role: 'start_frame' }),
168 urlPart('https://example.com/z.png', { role: 'end_frame' }),
169 ]),
170 ).toEqual({
171 start_image_url: 'https://example.com/a.png',
172 end_image_url: 'https://example.com/z.png',
173 })
174 })
175​
176 it('routes role=reference to reference_image_urls', () => {
177 expect(
178 mapImageInputsToFalVideoFields(UNKNOWN_MODEL, [
179 urlPart('https://example.com/start.png'),
180 urlPart('https://example.com/character.png', { role: 'reference' }),
181 ]),
182 ).toEqual({
183 image_url: 'https://example.com/start.png',
184 reference_image_urls: ['https://example.com/character.png'],
185 })
186 })
187​
188 it('throws on mask/control roles instead of repurposing them as sources', () => {
189 expect(() =>
190 mapImageInputsToFalVideoFields(UNKNOWN_MODEL, [
191 urlPart('https://example.com/start.png'),
192 urlPart('https://example.com/mask.png', { role: 'mask' }),
193 ]),
194 ).toThrow(/'mask' is not supported for video generation/)
195 expect(() =>
196 mapImageInputsToFalVideoFields(UNKNOWN_MODEL, [
197 urlPart('https://example.com/depth.png', { role: 'control' }),
198 ]),
199 ).toThrow(/'control' is not supported for video generation/)
200 })
201​
202 describe('generated endpoint overrides', () => {
203 it('routes role=start_frame to the source field on image-to-video endpoints', () => {
204 // Kling i2v takes the start frame as plain image_url, the end frame
205 // as tail_image_url
206 expect(
207 mapImageInputsToFalVideoFields(
208 'fal-ai/kling-video/v2.5-turbo/pro/image-to-video',
209 [
210 urlPart('https://example.com/start.png', { role: 'start_frame' }),
211 urlPart('https://example.com/end.png', { role: 'end_frame' }),
212 ],
213 ),
214 ).toEqual({
215 image_url: 'https://example.com/start.png',
216 tail_image_url: 'https://example.com/end.png',
217 })
218 })
219​
220 it('routes frame roles to first/last frame fields on frame-to-video endpoints', () => {
221 expect(
222 mapImageInputsToFalVideoFields(
223 'fal-ai/veo3.1/first-last-frame-to-video',
224 [
225 urlPart('https://example.com/first.png', { role: 'start_frame' }),
226 urlPart('https://example.com/last.png', { role: 'end_frame' }),
227 ],
228 ),
229 ).toEqual({
230 first_frame_url: 'https://example.com/first.png',
231 last_frame_url: 'https://example.com/last.png',
232 })
233 })
234​
235 it('throws when a source and start_frame both resolve to the same scalar field', () => {
236 expect(() =>
237 mapImageInputsToFalVideoFields(
238 'fal-ai/kling-video/v2.5-turbo/pro/image-to-video',
239 [
240 urlPart('https://example.com/source.png'),
241 urlPart('https://example.com/start.png', { role: 'start_frame' }),
242 ],
243 ),
244 ).toThrow(/multiple inputs map to 'image_url'/)
245 })
246 })
247})
248​
249describe('generated image-field-overrides artifact', () => {
250 it('matches the installed @fal-ai/client endpoint types', () => {
251 const require = createRequire(import.meta.url)
252 const endpointsJs = require.resolve('@fal-ai/client/endpoints')
253 const endpointsDts = endpointsJs.replace(/\.js$/, '.d.ts')
254 const hash = createHash('sha256')
255 .update(readFileSync(endpointsDts))
256 .digest('hex')
257 expect(
258 hash,
259 'image-field-overrides.ts is stale for the installed @fal-ai/client. Run: pnpm generate:fal-image-fields',
260 ).toBe(FAL_ENDPOINTS_DTS_SHA256)
261 })
262})