Open History to see the last commit
1import { describe, expect, it, vi } from 'vitest'
2import { fal } from '@fal-ai/client'
3import {
4 configureFalClient,
5 deriveAudioContentType,
6 extractUrlExtension,
7 generateId,
8} from '../src/utils'
9import { mapSizeToFalFormat } from '../src/image/image-provider-options'
10
11vi.mock('@fal-ai/client', () => ({
12 fal: {
13 config: vi.fn(),
14 },
15}))
16
17describe('generateId', () => {
18 it('returns a suffix with at least 8 characters of entropy', () => {
19 // Regression: previously used `.substring(7)` on a ~11-char base36 string,
20 // leaving only 4 random chars. Now uses `.substring(2)` to drop the "0."
21 // prefix and keep the full random portion.
22 for (let i = 0; i < 50; i += 1) {
23 const id = generateId('fal')
24 const parts = id.split('-')
25 const suffix = parts[parts.length - 1]
26 expect(suffix).toBeDefined()
27 expect(suffix!.length).toBeGreaterThanOrEqual(8)
28 }
29 })
30
31 it('preserves the provided prefix', () => {
32 const id = generateId('my-prefix')
33 expect(id.startsWith('my-prefix-')).toBe(true)
34 })
35})
36
37describe('configureFalClient', () => {
38 it('passes both credentials and proxyUrl when both are set', () => {
39 const configSpy = vi.mocked(fal.config)
40 configSpy.mockClear()
41
42 configureFalClient({ apiKey: 'test-key', proxyUrl: '/api/fal/proxy' })
43
44 expect(configSpy).toHaveBeenCalledWith({
45 credentials: 'test-key',
46 fetch: expect.any(Function),
47 proxyUrl: '/api/fal/proxy',
48 })
49 })
50
51 it('passes only credentials when proxyUrl is absent', () => {
52 const configSpy = vi.mocked(fal.config)
53 configSpy.mockClear()
54
55 configureFalClient({ apiKey: 'test-key' })
56
57 expect(configSpy).toHaveBeenCalledWith({
58 credentials: 'test-key',
59 fetch: expect.any(Function),
60 })
61 })
62})
63
64describe('extractUrlExtension', () => {
65 it('extracts simple extensions', () => {
66 expect(extractUrlExtension('https://x.com/a.mp3')).toBe('mp3')
67 expect(extractUrlExtension('https://x.com/a.wav')).toBe('wav')
68 })
69
70 it('strips query strings', () => {
71 expect(extractUrlExtension('https://x.com/a.mp3?sig=1')).toBe('mp3')
72 })
73
74 it('returns undefined for URLs without an extension', () => {
75 expect(
76 extractUrlExtension('https://x.com/path/no-extension'),
77 ).toBeUndefined()
78 expect(extractUrlExtension('https://x.com/')).toBeUndefined()
79 })
80
81 it('returns undefined when the extension is too long or has weird chars', () => {
82 expect(
83 extractUrlExtension('https://x.com/a.this-is-not-a-real-ext'),
84 ).toBeUndefined()
85 })
86})
87
88describe('deriveAudioContentType', () => {
89 it('prefers the explicit content type (stripped)', () => {
90 expect(deriveAudioContentType('audio/wav; codecs=pcm', '')).toBe(
91 'audio/wav',
92 )
93 })
94
95 it('falls back to extension-based mapping', () => {
96 expect(deriveAudioContentType(undefined, 'https://x.com/a.mp3')).toBe(
97 'audio/mpeg',
98 )
99 expect(deriveAudioContentType(undefined, 'https://x.com/a.wav')).toBe(
100 'audio/wav',
101 )
102 expect(deriveAudioContentType(undefined, 'https://x.com/a.ogg')).toBe(
103 'audio/ogg',
104 )
105 })
106
107 it('falls back to audio/mpeg when extension is missing', () => {
108 expect(deriveAudioContentType(undefined, 'https://x.com/no-ext')).toBe(
109 'audio/mpeg',
110 )
111 })
112})
113
114describe('mapSizeToFalFormat', () => {
115 it('returns undefined when size is undefined', () => {
116 expect(mapSizeToFalFormat(undefined)).toBeUndefined()
117 })
118
119 it('emits only aspect_ratio + resolution for aspect-ratio+resolution strings', () => {
120 expect(mapSizeToFalFormat('16:9_4K')).toEqual({
121 aspect_ratio: '16:9',
122 resolution: '4K',
123 })
124 })
125
126 it('emits only aspect_ratio for bare aspect-ratio strings', () => {
127 const result = mapSizeToFalFormat('16:9') as Record<string, unknown>
128 expect(result).toEqual({ aspect_ratio: '16:9' })
129 // Must not leak undefined properties.
130 expect(Object.prototype.hasOwnProperty.call(result, 'resolution')).toBe(
131 false,
132 )
133 expect(Object.prototype.hasOwnProperty.call(result, 'image_size')).toBe(
134 false,
135 )
136 })
137
138 it('emits only image_size for preset strings', () => {
139 const result = mapSizeToFalFormat('landscape_4_3') as Record<
140 string,
141 unknown
142 >
143 expect(result).toEqual({ image_size: 'landscape_4_3' })
144 expect(Object.prototype.hasOwnProperty.call(result, 'aspect_ratio')).toBe(
145 false,
146 )
147 })
148
149 it('never emits aspect_ratio: undefined', () => {
150 const result = mapSizeToFalFormat('portrait_16_9') as Record<
151 string,
152 unknown
153 >
154 expect(result['aspect_ratio']).toBeUndefined()
155 expect(Object.prototype.hasOwnProperty.call(result, 'aspect_ratio')).toBe(
156 false,
157 )
158 })
159})