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

kling.test.ts

131 lines4.7 KB

1import { describe, expect, it } from 'vitest'
2​
3import { findDanglingRefs } from '#/server/ingest/bundle.ts'
4import { classifyAndBundle } from '#/server/ingest/sync.ts'
5​
6import { provider } from './kling.ts'
7import { KLING_PRICING_URL } from '../kling-pricing.ts'
8​
9describe('kling classify', () => {
10 it('maps generation create paths and drops platform/query paths', () => {
11 expect(provider.classify('/v1/videos/text2video', {})).toBe('video')
12 expect(provider.classify('/v1/videos/image2video', {})).toBe('video')
13 expect(provider.classify('/v1/images/generations', {})).toBe('image')
14 expect(provider.classify('/v1/videos/text2video/{task_id}', {})).toBeNull()
15 expect(provider.classify('/v1/images/generations/{task_id}', {})).toBeNull()
16 expect(provider.classify('/account/costs', {})).toBeNull()
17 expect(provider.classify('/v1/videos/video-extend', {})).toBeNull()
18 })
19})
20​
21describe('kling listModels', () => {
22 it('skips when KLING_API_KEY is absent', async () => {
23 const result = await provider.listModels({})
24 expect(result.skipped).toBe('kling: KLING_API_KEY not set — skipped')
25 expect(result.models).toEqual([])
26 })
27​
28 it('returns the docs-derived catalog when a bearer is present', async () => {
29 const original = globalThis.fetch
30 globalThis.fetch = ((url: string) => {
31 expect(String(url)).toBe(KLING_PRICING_URL)
32 return Promise.resolve(
33 new Response(
34 JSON.stringify({
35 en: {
36 newImageApi: {
37 main: {
38 list: [
39 {
40 table: {
41 data: [
42 {
43 model: 'Kling Image O1',
44 price: '8 Units ($0.028) / image',
45 },
46 ],
47 },
48 },
49 ],
50 },
51 },
52 },
53 }),
54 ),
55 )
56 }) as typeof fetch
57 let result: Awaited<ReturnType<typeof provider.listModels>>
58 try {
59 result = await provider.listModels({ KLING_API_KEY: 'kling-test' })
60 } finally {
61 globalThis.fetch = original
62 }
63 expect(result.skipped).toBeUndefined()
64 expect(result.models.length).toBeGreaterThan(0)
65 expect(new Set(result.models.map((m) => m.rawId)).size).toBe(
66 result.models.length,
67 )
68 const activities = new Set(result.models.map((m) => m.activity))
69 expect(activities.has('video')).toBe(true)
70 expect(activities.has('image')).toBe(true)
71 })
72})
73​
74describe('kling fetchSpec', () => {
75 it('serves the embedded spec without touching the network', async () => {
76 const original = globalThis.fetch
77 const calls: Array<string> = []
78 globalThis.fetch = ((url: string) => {
79 calls.push(String(url))
80 return Promise.reject(new Error('network should not be used'))
81 }) as typeof fetch
82 try {
83 const fetched = await provider.fetchSpec({})
84 expect(calls).toEqual([])
85 expect(fetched.outputStrategy).toBe('post-200')
86 expect(fetched.sources[0]?.url).toBe(
87 'https://app.klingai.com/global/dev/document-api',
88 )
89 expect(fetched.sources[0]?.hash).toMatch(/^[0-9a-f]{64}$/)
90​
91 const { endpoints, warnings } = classifyAndBundle(provider, fetched)
92 expect(warnings).toEqual([])
93 expect(endpoints.map((e) => [e.dbId, e.activity]).sort()).toEqual([
94 ['kling/v1/images/generations', 'image'],
95 ['kling/v1/videos/image2video', 'video'],
96 ['kling/v1/videos/text2video', 'video'],
97 ])
98 for (const endpoint of endpoints) {
99 expect(endpoint.derivation).toBe('docs-derived')
100 expect(endpoint.input, endpoint.dbId).toBeDefined()
101 expect(endpoint.output, endpoint.dbId).toBeDefined()
102 if (endpoint.input) expect(findDanglingRefs(endpoint.input)).toEqual([])
103 if (endpoint.output) {
104 expect(findDanglingRefs(endpoint.output)).toEqual([])
105 }
106 }
107​
108 const t2v = endpoints.find((e) => e.path === '/v1/videos/text2video')
109 expect(t2v?.input?.required).toEqual(['prompt'])
110 } finally {
111 globalThis.fetch = original
112 }
113 })
114​
115 it('hashes the embedded document stably', async () => {
116 const a = await provider.fetchSpec({})
117 const b = await provider.fetchSpec({})
118 expect(a.sources.map((s) => s.hash)).toEqual(b.sources.map((s) => s.hash))
119 })
120})
121​
122describe('kling seed metadata', () => {
123 it('exports the isolation-required fields', () => {
124 expect(provider.id).toBe('kling')
125 expect(provider.displayName).toBe('Kling AI')
126 expect(provider.authEnvVar).toBe('KLING_API_KEY')
127 expect(provider.defaultDerivation).toBe('docs-derived')
128 expect(provider.specSourceUrl).toMatch(/^https:\/\//)
129 expect(provider.modelsEndpoint).toMatch(/^https:\/\//)
130 })
131})