Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { provider } from './bfl.ts'
4
5describe('bfl adapter metadata', () => {
6 it('exports a seeded upstream-spec adapter', () => {
7 expect(provider.id).toBe('bfl')
8 expect(provider.displayName).toBe('Black Forest Labs')
9 expect(provider.authEnvVar).toBe('BFL_API_KEY')
10 expect(provider.defaultDerivation).toBe('upstream-spec')
11 expect(provider.specSourceUrl).toBe('https://api.bfl.ai/openapi.json')
12 expect(provider.modelsEndpoint).toBe('https://api.bfl.ai/v1/models')
13 })
14})
15
16describe('bfl classify', () => {
17 const models = { tags: ['Models'] }
18 const utility = { tags: ['Utility'] }
19
20 it('maps FLUX generation POSTs and drops utility/admin', () => {
21 expect(provider.classify('/v1/flux-2-pro', models)).toBe('image')
22 expect(provider.classify('/v1/flux-2-flex', models)).toBe('image')
23 expect(provider.classify('/v1/flux-tools/vto-v1', models)).toBe('image')
24 expect(provider.classify('/v1/flux-kontext-pro', models)).toBe('image')
25 expect(provider.classify('/v1/flux-3-video', models)).toBe('video')
26 expect(provider.classify('/v1/delete_finetune', utility)).toBeNull()
27 expect(provider.classify('/v1/get_result', utility)).toBeNull()
28 expect(provider.classify('/v1/credits', {})).toBeNull()
29 expect(provider.classify('/v1/my_finetunes', utility)).toBeNull()
30 })
31})
32
33describe('bfl listModels', () => {
34 it('skips when BFL_API_KEY is absent', async () => {
35 const result = await provider.listModels({})
36 expect(result.models).toEqual([])
37 expect(result.skipped).toBe('bfl: BFL_API_KEY not set — skipped')
38 })
39
40 it('falls back to path-derived ids when /v1/models is missing', async () => {
41 const original = globalThis.fetch
42 const urls: Array<string> = []
43 globalThis.fetch = ((url: string) => {
44 urls.push(String(url))
45 return Promise.resolve(new Response('not found', { status: 404 }))
46 }) as typeof fetch
47 try {
48 const result = await provider.listModels({ BFL_API_KEY: 'test-key' })
49 expect(urls).toEqual(['https://api.bfl.ai/v1/models'])
50 expect(result.skipped).toBeUndefined()
51 expect(result.models.map((m) => m.rawId)).toEqual(
52 expect.arrayContaining(['flux-2-pro', 'flux-3-video']),
53 )
54 expect(
55 result.models.find((m) => m.rawId === 'flux-3-video')?.activity,
56 ).toBe('video')
57 } finally {
58 globalThis.fetch = original
59 }
60 })
61
62 it('uses a live listing when /v1/models returns data', async () => {
63 const original = globalThis.fetch
64 globalThis.fetch = ((url: string) => {
65 expect(String(url)).toBe('https://api.bfl.ai/v1/models')
66 return Promise.resolve(
67 new Response(
68 JSON.stringify({
69 data: [{ id: 'flux-2-pro', created: 1_700_000_000 }],
70 }),
71 { status: 200, headers: { 'content-type': 'application/json' } },
72 ),
73 )
74 }) as typeof fetch
75 try {
76 const result = await provider.listModels({ BFL_API_KEY: 'test-key' })
77 expect(result).toEqual({
78 models: [
79 {
80 rawId: 'flux-2-pro',
81 activity: 'image',
82 releasedAt: 1_700_000_000,
83 },
84 ],
85 })
86 } finally {
87 globalThis.fetch = original
88 }
89 })
90})
91
92describe('bfl fetchSpec', () => {
93 it('loads the public OpenAPI document without a key', async () => {
94 const original = globalThis.fetch
95 const spec = {
96 openapi: '3.1.0',
97 info: { title: 'BFL API', version: '0.0.1' },
98 paths: { '/v1/flux-2-pro': { post: { tags: ['Models'] } } },
99 }
100 const urls: Array<string> = []
101 globalThis.fetch = ((url: string) => {
102 urls.push(String(url))
103 return Promise.resolve(
104 new Response(JSON.stringify(spec), {
105 status: 200,
106 headers: { 'content-type': 'application/json' },
107 }),
108 )
109 }) as typeof fetch
110 try {
111 const fetched = await provider.fetchSpec({})
112 expect(urls).toEqual(['https://api.bfl.ai/openapi.json'])
113 expect(fetched.specs).toHaveLength(1)
114 expect(fetched.specs[0]?.info?.title).toBe('BFL API')
115 expect(fetched.sources[0]?.url).toBe('https://api.bfl.ai/openapi.json')
116 expect(fetched.sources[0]?.hash).toMatch(/^[0-9a-f]{64}$/)
117 expect(fetched.outputStrategy).toBe('post-200')
118 } finally {
119 globalThis.fetch = original
120 }
121 })
122})