Open History to see the last commit
1import { createHash } from 'node:crypto'
2import { readFileSync } from 'node:fs'
3import { cardCurrency, price } from '@modelschemas/rate-card'
4import { rateCardSchema } from '@modelschemas/rate-card/schema'
5import { describe, expect, it } from 'vitest'
6import {
7 applyCnCardFacts,
8 applyCnCatalog,
9 applyCnTextFacts,
10 cnBeijingTabs,
11 cnPriceModels,
12 cnTable,
13} from './dashscope-cn-docs.ts'
14import type { CnDoc } from './dashscope-cn-docs.ts'
15
16const doc = (file: string): CnDoc => {
17 const text = readFileSync(
18 new URL(`./fixtures/dashscope-cn/${file}.md.txt`, import.meta.url),
19 'utf8',
20 )
21 return {
22 text,
23 url: `https://help.aliyun.com/zh/model-studio/${file}.md`,
24 hash: createHash('sha256').update(text).digest('hex'),
25 }
26}
27
28const prices = doc('model-pricing')
29function native() {
30 return applyCnCardFacts(
31 applyCnTextFacts(
32 applyCnCatalog(cnPriceModels(prices), doc('models')),
33 doc('text-generation-model'),
34 ),
35 doc('qwen-flash'),
36 )
37}
38describe('DashScope China native source parsing', () => {
39 it('reads explicit Beijing ids with exact own CNY tiers, never international quotes', () => {
40 const rows = native()
41 expect(rows.length).toBeGreaterThan(400)
42 const flash = rows.find((row) => row.rawId === 'qwen-flash')!
43 expect(flash.contextWindow).toBe(1000000)
44 expect(flash.maxOutput).toBe(32768)
45 expect(flash.modalities).toEqual({ input: ['text'], output: ['text'] })
46 expect(flash.capabilities).toEqual([
47 'reasoning',
48 'tools',
49 'structured_outputs',
50 ])
51 expect(flash.schemaEndpointId).toBeNull()
52 expect(flash.reasoning).toBeNull()
53 expect(flash.requestMap).toBeNull()
54 expect(cardCurrency(rateCardSchema.parse(flash.pricing))).toBe('CNY')
55 expect(
56 price(
57 rateCardSchema.parse(flash.pricing),
58 {},
59 { input_tokens: 128000, output_tokens: 1000000 },
60 ),
61 ).toBeCloseTo(1.5192)
62 expect(
63 price(
64 rateCardSchema.parse(flash.pricing),
65 {},
66 { input_tokens: 128001, output_tokens: 1000000 },
67 ),
68 ).toBeCloseTo(6.0768006)
69 expect(
70 price(
71 rateCardSchema.parse(flash.pricing),
72 {},
73 { input_tokens: 256001, output_tokens: 1000000 },
74 ),
75 ).toBeCloseTo(12.3072012)
76 const math = rows.find((row) => row.rawId === 'qwen-math-plus')!
77 expect(
78 price(
79 rateCardSchema.parse(math.pricing),
80 {},
81 { input_tokens: 1000000, output_tokens: 1000000 },
82 ),
83 ).toBe(16)
84 expect(
85 rows.find((row) => row.rawId === 'qwen-deep-research')?.pricing,
86 ).not.toBeNull()
87 expect(flash.factSources?.modalities?.sourceUrl).toContain(
88 '/zh/model-studio/qwen-flash.md',
89 )
90 expect(
91 rows.every(
92 (row) =>
93 row.pricing === null ||
94 cardCurrency(rateCardSchema.parse(row.pricing)) === 'CNY',
95 ),
96 ).toBe(true)
97 })
98 it('reads Beijing console ids from selection-page cards and anchors', () => {
99 const rows = applyCnCatalog([], doc('models'))
100 const glm = rows.find((row) => row.rawId === 'ZHIPU/GLM-5.3')!
101 expect(glm.displayName).toBe('ZHIPU/GLM-5.3')
102 expect(glm.activity).toBe('chat')
103 expect(rows).toHaveLength(33)
104 expect(rows.find((row) => row.rawId === 'qwen3.8-max')?.activity).toBe(
105 'chat',
106 )
107 const anchored = applyCnCatalog([], {
108 ...doc('models'),
109 text: '## 文本生成\n<a href="https://bailian.console.aliyun.com/cn-beijing/model/market/detail/kimi%2Fkimi-k3">Kimi K3</a>',
110 })
111 expect(anchored.map((row) => [row.rawId, row.displayName])).toEqual([
112 ['kimi/kimi-k3', 'Kimi K3'],
113 ])
114 expect(() =>
115 applyCnCatalog([], {
116 ...doc('models'),
117 text: '<Card href="https://bailian.console.aliyun.com/cn-beijing/model/market/detail/qwen3.8-max">x</Card>',
118 }),
119 ).toThrow('card has no title')
120 })
121 it('expands sourced spans and balances nested model-family tabs', () => {
122 expect(
123 cnTable(
124 '<table><tr><td rowSpan={2}>a</td><td colSpan={2}>b</td></tr><tr><td>c</td><td /></tr></table>',
125 ),
126 ).toEqual([
127 ['a', 'b', 'b'],
128 ['a', 'c', ''],
129 ])
130 expect(
131 cnTable(
132 '<table><tr><td>x</td><td rowSpan={5}>a</td></tr><tr><td>y</td></tr></table>',
133 ),
134 ).toEqual([
135 ['x', 'a'],
136 ['y', 'a'],
137 ])
138 expect(
139 cnTable(
140 '<table><tr><td>x</td><td rowSpan={4}>a</td><td>p</td></tr><tr><td>y</td><td>q</td></tr></table>',
141 ),
142 ).toEqual([
143 ['x', 'a', 'p'],
144 ['y', 'a', 'q'],
145 ])
146 expect(() =>
147 cnTable('<table><tr><td rowSpan={oops}>a</td></tr></table>'),
148 ).toThrow('unreadable table span')
149 expect(
150 cnBeijingTabs(
151 '<Tabs><Tab title="华北2(北京)"><Tabs><Tab title="family">own</Tab></Tabs></Tab><Tab title="新加坡">foreign</Tab></Tabs>',
152 )[0]?.body,
153 ).toContain('own')
154 expect(() => cnBeijingTabs('<Tab title="华北2(北京)">')).toThrow(
155 'unclosed',
156 )
157 })
158 it('clips a native rowSpan that runs past the last table row', () => {
159 const rows = cnPriceModels(doc('model-pricing-span-overflow'))
160 expect(rows.map((row) => row.rawId)).toEqual(['MiniMax-M2.5'])
161 expect(rows[0]!.absent).toBeUndefined()
162 const card = rateCardSchema.parse(rows[0]!.pricing)
163 expect(cardCurrency(card)).toBe('CNY')
164 expect(
165 price(card, {}, { input_tokens: 1000000, output_tokens: 1000000 }),
166 ).toBeCloseTo(10.5)
167 })
168 it('throws malformed recognized rates or units and never returns an empty success', () => {
169 expect(() =>
170 cnPriceModels({
171 ...prices,
172 text: prices.text.replace('0.15元', 'unknown元'),
173 }),
174 ).toThrow('unreadable RMB')
175 expect(() =>
176 cnPriceModels({
177 ...prices,
178 text: prices.text.replace('0.15元', '0.15USD'),
179 }),
180 ).toThrow('unreadable RMB')
181 expect(() =>
182 cnPriceModels({
183 ...prices,
184 text: prices.text.replace('0\\<Token≤128K', 'unreadable-range'),
185 }),
186 ).toThrow('token range')
187 expect(() =>
188 cnPriceModels({
189 ...prices,
190 text: prices.text.replaceAll('title="华北2(北京)"', 'title="新加坡"'),
191 }),
192 ).toThrow('no Beijing')
193 })
194 it('reads a cache-hit input price stated in the same cell', () => {
195 const cell = '13元 缓存命中输入:1.3元'
196 const quoted = (input: string): CnDoc => ({
197 ...prices,
198 text: `## 文本生成\n<Tabs><Tab title="华北2(北京)"><table><tr><th>模型 ID(Model ID)</th><th>模式</th><th>输入单价(每百万Token)</th><th>输出单价(每百万Token)</th></tr><tr><td>kimi/kimi-k3.1-pro</td><td>思考与非思考(默认开启思考)</td><td>${input}</td><td>68元</td></tr></table></Tab></Tabs>`,
199 })
200 const [row] = cnPriceModels(quoted(cell))
201 const card = rateCardSchema.parse(row!.pricing)
202 expect(cardCurrency(card)).toBe('CNY')
203 expect(
204 price(card, {}, { input_tokens: 1_000_000, output_tokens: 0 }),
205 ).toBeCloseTo(13)
206 expect(
207 price(
208 card,
209 {},
210 { input_tokens: 0, cache_read_tokens: 1_000_000, output_tokens: 0 },
211 ),
212 ).toBeCloseTo(1.3)
213 expect(() => cnPriceModels(quoted('13元 未知后缀:1.3元'))).toThrow(
214 'unreadable RMB token quote: 13元 未知后缀:1.3元',
215 )
216 })
217 it('bills 原价N元 (限时X折) at the fold of list price, with no invented end', () => {
218 const cell = '原价2元 (限时2.5折)'
219 const quoted = (
220 input: string,
221 output = '原价6元 (限时2.5折)',
222 ): CnDoc => ({
223 ...prices,
224 text: `## 文本生成-第三方模型\n<Tabs><Tab title="华北2(北京)"><table><tr><th>模型 ID(Model ID)</th><th>输入单价(每百万Token)</th><th>输出单价(每百万Token)</th></tr><tr><td>antdigital/Ling-3.1-flash</td><td>${input}</td><td>${output}</td></tr></table></Tab></Tabs>`,
225 })
226 const [row] = cnPriceModels(quoted(cell))
227 const card = rateCardSchema.parse(row!.pricing)
228 expect(row!.rawId).toBe('antdigital/Ling-3.1-flash')
229 expect(cardCurrency(card)).toBe('CNY')
230 expect(card.source.expiresAt).toBeUndefined()
231 expect(
232 price(card, {}, { input_tokens: 1_000_000, output_tokens: 0 }),
233 ).toBeCloseTo(0.5)
234 expect(
235 price(card, {}, { input_tokens: 0, output_tokens: 1_000_000 }),
236 ).toBeCloseTo(1.5)
237 const eight = rateCardSchema.parse(
238 cnPriceModels(quoted('原价2元 (限时8折)', '8元'))[0]!.pricing,
239 )
240 expect(
241 price(eight, {}, { input_tokens: 1_000_000, output_tokens: 0 }),
242 ).toBeCloseTo(1.6)
243 expect(() => cnPriceModels(quoted('原价2元 (限时优惠)'))).toThrow(
244 'unreadable RMB token quote: 原价2元 (限时优惠)',
245 )
246 expect(() =>
247 cnPriceModels(quoted('原价2元 (限时2.5折至2026-12-31)')),
248 ).toThrow('unreadable RMB token quote: 原价2元 (限时2.5折至2026-12-31)')
249 })
250 it('reads a price cell that tiers itself by prompt size', () => {
251 const tiered = (input: string, output: string): CnDoc => ({
252 ...prices,
253 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>`,
254 })
255 const [m3] = cnPriceModels(
256 tiered(
257 '输入不超过 512k Token:2.1元\n\n输入超过 512k Token:4.2元',
258 '输入不超过 512k Token:8.4元\n\n输入超过 512k Token:16.8元',
259 ),
260 )
261 const card = rateCardSchema.parse(m3!.pricing)
262 expect(cardCurrency(card)).toBe('CNY')
263 expect(
264 price(card, {}, { input_tokens: 512000, output_tokens: 1000000 }),
265 ).toBeCloseTo(1.0752 + 8.4)
266 expect(
267 price(card, {}, { input_tokens: 512001, output_tokens: 1000000 }),
268 ).toBeCloseTo(512001 * 4.2e-6 + 16.8)
269 expect(() =>
270 cnPriceModels(
271 tiered(
272 '输入不超过 512k Token:2.1元 输入超过 512k Token:4.2元',
273 '输入不超过 256k Token:8.4元 输入超过 256k Token:16.8元',
274 ),
275 ),
276 ).toThrow('mismatched input and output price tiers')
277 expect(() =>
278 cnPriceModels(tiered('输入不超过 512k Token:2.1元', '8.4元')),
279 ).toThrow('unreadable tiered RMB token quote')
280 })
281 it('keeps distinct native thinking or media prices null instead of choosing a mode', () => {
282 const rows = native()
283 expect(rows.find((row) => row.rawId === 'qwen-plus')?.pricing).toBeNull()
284 expect(
285 rows.find((row) => row.rawId === 'qwen3-omni-flash')?.pricing,
286 ).toBeNull()
287 expect(
288 rows.some((row) => row.maxOutput === null && row.modalities === null),
289 ).toBe(true)
290 expect(
291 rows
292 .filter((row) => row.pricing === null)
293 .every((row) => row.absent?.pricing === 'cleared'),
294 ).toBe(true)
295 const priced = rows.filter((row) => row.pricing !== null)
296 expect(priced.length).toBeGreaterThan(0)
297 expect(priced.every((row) => row.absent?.pricing === undefined)).toBe(true)
298 })
299})
300
301describe('DashScope evidence boundaries', () => {
302 const structuredCell = (cell: string) =>
303 applyCnTextFacts([{ rawId: 'kimi-k3.1-pro', displayName: null }], {
304 text: `<table><tr><th>模型ID</th><th>上下文</th><th>结构化输出</th></tr><tr><td><code>kimi-k3.1-pro</code></td><td>1M</td><td>${cell}</td></tr></table>`,
305 url: 'https://help.aliyun.com/zh/model-studio/text-generation-model.md',
306 hash: 'a'.repeat(64),
307 })
308 it('reads JSON Mode as response_format, not structured outputs', () => {
309 const rows = structuredCell('支持(JSON Mode)')
310 expect(rows[0]?.capabilities).toEqual(['response_format'])
311 expect(rows[0]?.unsupportedCapabilities).toEqual([])
312 })
313 it('refuses any other qualified support note', () => {
314 expect(() => structuredCell('支持(仅思考模式)')).toThrow(
315 'unknown capability support value: 支持(仅思考模式)',
316 )
317 })
318 it('records explicit negatives without claiming a complete capability vocabulary', () => {
319 const rows = applyCnTextFacts(
320 cnPriceModels(prices),
321 doc('text-generation-model'),
322 )
323 const row = rows.find((item) => item.rawId === 'qwen-mt-plus')!
324 expect(row.exactCapabilities).toBeUndefined()
325 expect(row.unsupportedCapabilities).toEqual([
326 'reasoning',
327 'tools',
328 'structured_outputs',
329 ])
330 expect(row.factSources?.capabilities?.reasoning?.sourceUrl).toBe(
331 doc('text-generation-model').url,
332 )
333 })
334 it('rejects partial or wrong-region cards instead of fabricating empty capabilities', () => {
335 const base = cnPriceModels(prices)
336 const card = doc('qwen-flash')
337 expect(() =>
338 applyCnCardFacts(base, {
339 ...card,
340 text: card.text.replace('title="华北2(北京)"', 'title="新加坡"'),
341 }),
342 ).toThrow('no Beijing')
343 expect(() =>
344 applyCnCardFacts(base, {
345 ...card,
346 text: card.text.replace('## 上下文限制', '## unrelated'),
347 }),
348 ).toThrow('incomplete native card')
349 expect(() =>
350 applyCnCardFacts(base, {
351 ...card,
352 text: card.text.replace('## 模型能力', '## unrelated'),
353 }),
354 ).toThrow('incomplete native card')
355 })
356 it('rejects conflicting published token limits', () => {
357 const card = doc('qwen-flash')
358 expect(() =>
359 applyCnCardFacts(cnPriceModels(prices), {
360 ...card,
361 text: card.text.replace(
362 '## 模型价格',
363 '## 上下文限制\n<table><tr><th>参数</th><th>值</th></tr><tr><td>上下文长度</td><td>999999</td></tr></table>\n## 模型价格',
364 ),
365 }),
366 ).toThrow('conflicting card token limit')
367 })
368})