Open History to see the last commit
1import { createHash } from 'node:crypto'
2import { readFileSync } from 'node:fs'
3import { cardCurrency, price, rateCardSchema } from '@modelschemas/rate-card'
4import { describe, expect, it } from 'vitest'
5import {
6 applyCnCardFacts,
7 applyCnCatalog,
8 applyCnTextFacts,
9 cnBeijingTabs,
10 cnPriceModels,
11 cnTable,
12} from './dashscope-cn-docs.ts'
13import type { CnDoc } from './dashscope-cn-docs.ts'
14
15const doc = (file: string): CnDoc => {
16 const text = readFileSync(
17 new URL(`./fixtures/dashscope-cn/${file}.md.txt`, import.meta.url),
18 'utf8',
19 )
20 return {
21 text,
22 url: `https://help.aliyun.com/zh/model-studio/${file}.md`,
23 hash: createHash('sha256').update(text).digest('hex'),
24 }
25}
26
27const prices = doc('model-pricing')
28function native() {
29 return applyCnCardFacts(
30 applyCnTextFacts(
31 applyCnCatalog(cnPriceModels(prices), doc('models')),
32 doc('text-generation-model'),
33 ),
34 doc('qwen-flash'),
35 )
36}
37describe('DashScope China native source parsing', () => {
38 it('reads explicit Beijing ids with exact own CNY tiers, never international quotes', () => {
39 const rows = native()
40 expect(rows.length).toBeGreaterThan(400)
41 const flash = rows.find((row) => row.rawId === 'qwen-flash')!
42 expect(flash.contextWindow).toBe(1000000)
43 expect(flash.maxOutput).toBe(32768)
44 expect(flash.modalities).toEqual({ input: ['text'], output: ['text'] })
45 expect(flash.capabilities).toEqual([
46 'reasoning',
47 'tools',
48 'structured_outputs',
49 ])
50 expect(flash.schemaEndpointId).toBeNull()
51 expect(flash.reasoning).toBeNull()
52 expect(flash.requestMap).toBeNull()
53 expect(cardCurrency(rateCardSchema.parse(flash.pricing))).toBe('CNY')
54 expect(
55 price(
56 rateCardSchema.parse(flash.pricing),
57 {},
58 { input_tokens: 128000, output_tokens: 1000000 },
59 ),
60 ).toBeCloseTo(1.5192)
61 expect(
62 price(
63 rateCardSchema.parse(flash.pricing),
64 {},
65 { input_tokens: 128001, output_tokens: 1000000 },
66 ),
67 ).toBeCloseTo(6.0768006)
68 expect(
69 price(
70 rateCardSchema.parse(flash.pricing),
71 {},
72 { input_tokens: 256001, output_tokens: 1000000 },
73 ),
74 ).toBeCloseTo(12.3072012)
75 const math = rows.find((row) => row.rawId === 'qwen-math-plus')!
76 expect(
77 price(
78 rateCardSchema.parse(math.pricing),
79 {},
80 { input_tokens: 1000000, output_tokens: 1000000 },
81 ),
82 ).toBe(16)
83 expect(
84 rows.find((row) => row.rawId === 'qwen-deep-research')?.pricing,
85 ).not.toBeNull()
86 expect(flash.factSources?.modalities?.sourceUrl).toContain(
87 '/zh/model-studio/qwen-flash.md',
88 )
89 expect(
90 rows.every(
91 (row) =>
92 row.pricing === null ||
93 cardCurrency(rateCardSchema.parse(row.pricing)) === 'CNY',
94 ),
95 ).toBe(true)
96 })
97 it('expands sourced spans and balances nested model-family tabs', () => {
98 expect(
99 cnTable(
100 '<table><tr><td rowSpan={2}>a</td><td colSpan={2}>b</td></tr><tr><td>c</td><td /></tr></table>',
101 ),
102 ).toEqual([
103 ['a', 'b', 'b'],
104 ['a', 'c', ''],
105 ])
106 expect(
107 cnTable(
108 '<table><tr><td>x</td><td rowSpan={5}>a</td></tr><tr><td>y</td></tr></table>',
109 ),
110 ).toEqual([
111 ['x', 'a'],
112 ['y', 'a'],
113 ])
114 expect(
115 cnTable(
116 '<table><tr><td>x</td><td rowSpan={4}>a</td><td>p</td></tr><tr><td>y</td><td>q</td></tr></table>',
117 ),
118 ).toEqual([
119 ['x', 'a', 'p'],
120 ['y', 'a', 'q'],
121 ])
122 expect(() =>
123 cnTable('<table><tr><td rowSpan={oops}>a</td></tr></table>'),
124 ).toThrow('unreadable table span')
125 expect(
126 cnBeijingTabs(
127 '<Tabs><Tab title="华北2(北京)"><Tabs><Tab title="family">own</Tab></Tabs></Tab><Tab title="新加坡">foreign</Tab></Tabs>',
128 )[0]?.body,
129 ).toContain('own')
130 expect(() => cnBeijingTabs('<Tab title="华北2(北京)">')).toThrow(
131 'unclosed',
132 )
133 })
134 it('clips a native rowSpan that runs past the last table row', () => {
135 const rows = cnPriceModels(doc('model-pricing-span-overflow'))
136 expect(rows.map((row) => row.rawId)).toEqual(['MiniMax-M2.5'])
137 expect(rows[0]!.absent).toBeUndefined()
138 const card = rateCardSchema.parse(rows[0]!.pricing)
139 expect(cardCurrency(card)).toBe('CNY')
140 expect(
141 price(card, {}, { input_tokens: 1000000, output_tokens: 1000000 }),
142 ).toBeCloseTo(10.5)
143 })
144 it('throws malformed recognized rates or units and never returns an empty success', () => {
145 expect(() =>
146 cnPriceModels({
147 ...prices,
148 text: prices.text.replace('0.15元', 'unknown元'),
149 }),
150 ).toThrow('unreadable RMB')
151 expect(() =>
152 cnPriceModels({
153 ...prices,
154 text: prices.text.replace('0.15元', '0.15USD'),
155 }),
156 ).toThrow('unreadable RMB')
157 expect(() =>
158 cnPriceModels({
159 ...prices,
160 text: prices.text.replace('0\\<Token≤128K', 'unreadable-range'),
161 }),
162 ).toThrow('token range')
163 expect(() =>
164 cnPriceModels({
165 ...prices,
166 text: prices.text.replaceAll('title="华北2(北京)"', 'title="新加坡"'),
167 }),
168 ).toThrow('no Beijing')
169 })
170 it('reads a price cell that tiers itself by prompt size', () => {
171 const tiered = (input: string, output: string): CnDoc => ({
172 ...prices,
173 text: `## 文本生成\n<Tabs><Tab title="华北2(北京)"><table><tr><th>模型 ID(Model ID)</th><th>模式</th><th>输入单价(每百万Token)</th><th>输出单价(每百万Token)</th></tr><tr><td>MiniMax/MiniMax-M3</td><td>非思考和思考模式</td><td>${input}</td><td>${output}</td></tr></table></Tab></Tabs>`,
174 })
175 const [m3] = cnPriceModels(
176 tiered(
177 '输入不超过 512k Token:2.1元\n\n输入超过 512k Token:4.2元',
178 '输入不超过 512k Token:8.4元\n\n输入超过 512k Token:16.8元',
179 ),
180 )
181 const card = rateCardSchema.parse(m3!.pricing)
182 expect(cardCurrency(card)).toBe('CNY')
183 expect(
184 price(card, {}, { input_tokens: 512000, output_tokens: 1000000 }),
185 ).toBeCloseTo(1.0752 + 8.4)
186 expect(
187 price(card, {}, { input_tokens: 512001, output_tokens: 1000000 }),
188 ).toBeCloseTo(512001 * 4.2e-6 + 16.8)
189 expect(() =>
190 cnPriceModels(
191 tiered(
192 '输入不超过 512k Token:2.1元 输入超过 512k Token:4.2元',
193 '输入不超过 256k Token:8.4元 输入超过 256k Token:16.8元',
194 ),
195 ),
196 ).toThrow('mismatched input and output price tiers')
197 expect(() =>
198 cnPriceModels(tiered('输入不超过 512k Token:2.1元', '8.4元')),
199 ).toThrow('unreadable tiered RMB token quote')
200 })
201 it('keeps distinct native thinking or media prices null instead of choosing a mode', () => {
202 const rows = native()
203 expect(rows.find((row) => row.rawId === 'qwen-plus')?.pricing).toBeNull()
204 expect(
205 rows.find((row) => row.rawId === 'qwen3-omni-flash')?.pricing,
206 ).toBeNull()
207 expect(
208 rows.some((row) => row.maxOutput === null && row.modalities === null),
209 ).toBe(true)
210 expect(
211 rows
212 .filter((row) => row.pricing === null)
213 .every((row) => row.absent?.pricing === 'cleared'),
214 ).toBe(true)
215 const priced = rows.filter((row) => row.pricing !== null)
216 expect(priced.length).toBeGreaterThan(0)
217 expect(priced.every((row) => row.absent?.pricing === undefined)).toBe(true)
218 })
219})
220
221describe('DashScope evidence boundaries', () => {
222 it('records explicit negatives without claiming a complete capability vocabulary', () => {
223 const rows = applyCnTextFacts(
224 cnPriceModels(prices),
225 doc('text-generation-model'),
226 )
227 const row = rows.find((item) => item.rawId === 'qwen-mt-plus')!
228 expect(row.exactCapabilities).toBeUndefined()
229 expect(row.unsupportedCapabilities).toEqual([
230 'reasoning',
231 'tools',
232 'structured_outputs',
233 ])
234 expect(row.factSources?.capabilities?.reasoning?.sourceUrl).toBe(
235 doc('text-generation-model').url,
236 )
237 })
238 it('rejects partial or wrong-region cards instead of fabricating empty capabilities', () => {
239 const base = cnPriceModels(prices)
240 const card = doc('qwen-flash')
241 expect(() =>
242 applyCnCardFacts(base, {
243 ...card,
244 text: card.text.replace('title="华北2(北京)"', 'title="新加坡"'),
245 }),
246 ).toThrow('no Beijing')
247 expect(() =>
248 applyCnCardFacts(base, {
249 ...card,
250 text: card.text.replace('## 上下文限制', '## unrelated'),
251 }),
252 ).toThrow('incomplete native card')
253 expect(() =>
254 applyCnCardFacts(base, {
255 ...card,
256 text: card.text.replace('## 模型能力', '## unrelated'),
257 }),
258 ).toThrow('incomplete native card')
259 })
260 it('rejects conflicting published token limits', () => {
261 const card = doc('qwen-flash')
262 expect(() =>
263 applyCnCardFacts(cnPriceModels(prices), {
264 ...card,
265 text: card.text.replace(
266 '## 模型价格',
267 '## 上下文限制\n<table><tr><th>参数</th><th>值</th></tr><tr><td>上下文长度</td><td>999999</td></tr></table>\n## 模型价格',
268 ),
269 }),
270 ).toThrow('conflicting card token limit')
271 })
272})