Open History to see the last commit
1import { readFileSync } from 'node:fs'
2import { afterEach, describe, expect, it, vi } from 'vitest'
3import { provider } from './dashscope-cn.ts'
4
5vi.mock('./model-facts.ts', () => ({
6 cachedDocs: (_kv: unknown, _url: string, load: () => Promise<unknown>) =>
7 load(),
8}))
9afterEach(() => vi.unstubAllGlobals())
10describe('DashScope CN public native adapter', () => {
11 it('needs no international key and uses only owned Chinese sources', async () => {
12 const urls: string[] = []
13 vi.stubGlobal('fetch', (url: string) => {
14 urls.push(url)
15 expect(url).toMatch(/^https:\/\/help\.aliyun\.com\/zh\/model-studio\//)
16 const file = new URL(url).pathname.split('/').at(-1)
17 return Promise.resolve(
18 new Response(
19 readFileSync(
20 new URL(`./fixtures/dashscope-cn/${file}.txt`, import.meta.url),
21 ),
22 ),
23 )
24 })
25 const listed = await provider.listModels({})
26 expect(listed.models.length).toBeGreaterThan(400)
27 expect(
28 listed.models.find((model) => model.rawId === 'qwen-flash')?.pricing,
29 ).not.toBeNull()
30 const spec = await provider.fetchSpec({})
31 expect(spec.specs).toEqual([])
32 expect(spec.sources).toHaveLength(4)
33 expect(
34 spec.sources.every((entry) => /^[a-f0-9]{64}$/.test(entry.hash)),
35 ).toBe(true)
36 expect(spec.skipped).toContain('no sourced complete API body schema')
37 expect(urls).toHaveLength(8)
38 expect(provider.generationEndpointId).toBeUndefined()
39 })
40 it('does not replace failed native fetches with Intl metadata or empty catalogs', async () => {
41 vi.stubGlobal('fetch', () =>
42 Promise.resolve(new Response('unavailable', { status: 503 })),
43 )
44 await expect(provider.listModels({})).rejects.toThrow(/503/)
45 await expect(provider.fetchSpec({})).rejects.toThrow(/503/)
46 })
47})
48
49it('rejects a successful wrong native page rather than retaining partial facts', async () => {
50 vi.stubGlobal('fetch', (url: string) => {
51 const file = new URL(url).pathname.split('/').at(-1)
52 let text = readFileSync(
53 new URL(`./fixtures/dashscope-cn/${file}.txt`, import.meta.url),
54 'utf8',
55 )
56 if (file === 'qwen-flash.md')
57 text = text.replace('# qwen-flash', '# unrelated')
58 return Promise.resolve(new Response(text))
59 })
60 await expect(provider.listModels({})).rejects.toThrow(
61 'malformed native source',
62 )
63})