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

kimi-code.test.ts

90 lines3.6 KB

1import { readFileSync } from 'node:fs'
2import { afterEach, describe, expect, it, vi } from 'vitest'
3import type * as ModelFacts from './model-facts.ts'
4import { provider as cnProvider } from './adapters/kimi-code-plan-cn.ts'
5import { provider as globalProvider } from './adapters/kimi-code-plan-global.ts'
6import { KIMI_CODE_MODELS_URL } from './kimi-code-docs.ts'
7​
8vi.mock('./model-facts.ts', async (importOriginal) => ({
9 ...(await importOriginal<typeof ModelFacts>()),
10 cachedDocs: (_kv: unknown, _url: string, load: () => Promise<unknown>) =>
11 load(),
12}))
13// Captured 2026-11-21: the Base URLs table names Chat Completions.
14const html = readFileSync(
15 new URL('./fixtures/kimi-code-models-2026-11-21.html.txt', import.meta.url),
16 'utf8',
18afterEach(() => vi.unstubAllGlobals())
19​
20describe('Kimi Coding regional adapters', () => {
21 it('loads shared native docs for both documented regions and stores only the stated route', async () => {
22 const urls: Array<string> = []
23 vi.stubGlobal('fetch', (url: string) => {
24 urls.push(url)
25 if (url !== KIMI_CODE_MODELS_URL)
26 throw new Error(`unexpected source ${url}`)
27 return Promise.resolve(new Response(html))
28 })
29 const cn = cnProvider
30 const global = globalProvider
31 const cnModels = (await cn.listModels({})).models
32 const globalModels = (await global.listModels({})).models
33 expect(cnModels.map((model) => model.rawId)).toEqual(
34 globalModels.map((model) => model.rawId),
35 )
36 for (const [models, region] of [
37 [cnModels, 'China'],
38 [globalModels, 'Overseas'],
39 ] as const) {
40 expect(models).toHaveLength(6)
41 for (const model of models) {
42 expect(model.schemaEndpointId).toBe('coding/v1/chat/completions')
43 expect(model.factSources?.schemaEndpointId).toMatchObject({
44 derivation: 'docs-derived',
45 sourceUrl: KIMI_CODE_MODELS_URL,
46 path: `Base URLs; Chat Completions; ${region} base URL + the protocol's /chat/completions`,
47 })
48 }
49 }
50 for (const provider of [cn, global]) {
51 const spec = await provider.fetchSpec({})
52 expect(spec.skipped).toBeUndefined()
53 // Route-only: the docs state the path, not a request schema.
54 const paths = spec.specs[0]?.paths ?? {}
55 expect(Object.keys(paths)).toEqual(['/coding/v1/chat/completions'])
56 expect(paths['/coding/v1/chat/completions']?.post).toMatchObject({
57 'x-modelschemas-route-only': true,
58 responses: {},
59 })
60 }
61 expect(urls).toEqual(Array(4).fill(KIMI_CODE_MODELS_URL))
62 const models = (await cn.listModels({})).models
63 expect(
64 models.every(
65 (model) =>
66 model.pricing === null && model.absent?.pricing === 'cleared',
67 ),
68 ).toBe(true)
69 const spec = await cn.fetchSpec({})
70 expect(spec.sources).toHaveLength(1)
71 expect(spec.sources[0]?.url).toBe(KIMI_CODE_MODELS_URL)
72 expect(spec.sources[0]?.hash).toMatch(/^[a-f0-9]{64}$/)
73 })
74 it('rejects malformed native docs during both listing and spec refresh', async () => {
75 vi.stubGlobal('fetch', () =>
76 Promise.resolve(new Response('<html>missing model table</html>')),
77 )
78 const provider = cnProvider
79 await expect(provider.listModels({})).rejects.toThrow()
80 await expect(provider.fetchSpec({})).rejects.toThrow()
81 })
82 it('fails native source fetches instead of returning an empty successful catalog', async () => {
83 vi.stubGlobal('fetch', () =>
84 Promise.resolve(new Response('upstream unavailable', { status: 503 })),
85 )
86 const provider = cnProvider
87 await expect(provider.listModels({})).rejects.toThrow(/503/)
88 await expect(provider.fetchSpec({})).rejects.toThrow(/503/)
89 })
90})