Open History to see the last commit
1import { readFileSync } from 'node:fs'
2import { afterEach, describe, expect, it, vi } from 'vitest'
3import { provider as cnProvider } from './adapters/kimi-code-plan-cn.ts'
4import { provider as globalProvider } from './adapters/kimi-code-plan-global.ts'
5import { KIMI_CODE_MODELS_URL } from './kimi-code-docs.ts'
6
7vi.mock('./model-facts.ts', () => ({
8 cachedDocs: (_kv: unknown, _url: string, load: () => Promise<unknown>) =>
9 load(),
10}))
11const html = readFileSync(
12 new URL('./fixtures/kimi-code-models.html.txt', import.meta.url),
13 'utf8',
14)
15afterEach(() => vi.unstubAllGlobals())
16
17describe('Kimi Coding regional adapters', () => {
18 it('loads shared native docs for both documented regions and never borrows a spec', async () => {
19 const urls: Array<string> = []
20 vi.stubGlobal('fetch', (url: string) => {
21 urls.push(url)
22 if (url !== KIMI_CODE_MODELS_URL)
23 throw new Error(`unexpected source ${url}`)
24 return Promise.resolve(new Response(html))
25 })
26 const cn = cnProvider
27 const global = globalProvider
28 expect((await cn.listModels({})).models).toEqual(
29 (await global.listModels({})).models,
30 )
31 expect((await cn.fetchSpec({})).skipped).toContain(
32 'no sourced request/response schema',
33 )
34 expect((await global.fetchSpec({})).skipped).toContain(
35 'no sourced request/response schema',
36 )
37 expect(urls).toEqual(Array(4).fill(KIMI_CODE_MODELS_URL))
38 const models = (await cn.listModels({})).models
39 expect(
40 models.every(
41 (model) =>
42 model.pricing === null && model.absent?.pricing === 'cleared',
43 ),
44 ).toBe(true)
45 const spec = await cn.fetchSpec({})
46 expect(spec.sources).toHaveLength(1)
47 expect(spec.sources[0]?.url).toBe(KIMI_CODE_MODELS_URL)
48 expect(spec.sources[0]?.hash).toMatch(/^[a-f0-9]{64}$/)
49 })
50 it('rejects malformed native docs during both listing and spec refresh', async () => {
51 vi.stubGlobal('fetch', () =>
52 Promise.resolve(new Response('<html>missing model table</html>')),
53 )
54 const provider = cnProvider
55 await expect(provider.listModels({})).rejects.toThrow()
56 await expect(provider.fetchSpec({})).rejects.toThrow()
57 })
58 it('fails native source fetches instead of returning an empty successful catalog', async () => {
59 vi.stubGlobal('fetch', () =>
60 Promise.resolve(new Response('upstream unavailable', { status: 503 })),
61 )
62 const provider = cnProvider
63 await expect(provider.listModels({})).rejects.toThrow(/503/)
64 await expect(provider.fetchSpec({})).rejects.toThrow(/503/)
65 })
66})