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

dashscope-cn-docs.test.ts

215 lines6.7 KB

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 }
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 )
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('<table><tr><td rowSpan={3}>a</td></tr></table>'),
108 ).toThrow('span extends')
109 expect(() =>
110 cnTable('<table><tr><td rowSpan={oops}>a</td></tr></table>'),
111 ).toThrow('unreadable table span')
112 expect(
113 cnBeijingTabs(
114 '<Tabs><Tab title="华北2(北京)"><Tabs><Tab title="family">own</Tab></Tabs></Tab><Tab title="新加坡">foreign</Tab></Tabs>',
115 )[0]?.body,
116 ).toContain('own')
117 expect(() => cnBeijingTabs('<Tab title="华北2(北京)">')).toThrow(
118 'unclosed',
119 )
120 })
121 it('throws malformed recognized rates or units and never returns an empty success', () => {
122 expect(() =>
123 cnPriceModels({
124 ...prices,
125 text: prices.text.replace('0.15元', 'unknown元'),
126 }),
127 ).toThrow('unreadable RMB')
128 expect(() =>
129 cnPriceModels({
130 ...prices,
131 text: prices.text.replace('0.15元', '0.15USD'),
132 }),
133 ).toThrow('unreadable RMB')
134 expect(() =>
135 cnPriceModels({
136 ...prices,
137 text: prices.text.replace('0\\<Token≤128K', 'unreadable-range'),
138 }),
139 ).toThrow('token range')
140 expect(() =>
141 cnPriceModels({
142 ...prices,
143 text: prices.text.replaceAll('title="华北2(北京)"', 'title="新加坡"'),
144 }),
145 ).toThrow('no Beijing')
146 })
147 it('keeps distinct native thinking or media prices null instead of choosing a mode', () => {
148 const rows = native()
149 expect(rows.find((row) => row.rawId === 'qwen-plus')?.pricing).toBeNull()
150 expect(
151 rows.find((row) => row.rawId === 'qwen3-omni-flash')?.pricing,
152 ).toBeNull()
153 expect(
154 rows.some((row) => row.maxOutput === null && row.modalities === null),
155 ).toBe(true)
156 expect(
157 rows
158 .filter((row) => row.pricing === null)
159 .every((row) => row.absent?.pricing === 'cleared'),
160 ).toBe(true)
161 })
162})
163​
164describe('DashScope evidence boundaries', () => {
165 it('records explicit negatives without claiming a complete capability vocabulary', () => {
166 const rows = applyCnTextFacts(
167 cnPriceModels(prices),
168 doc('text-generation-model'),
169 )
170 const row = rows.find((item) => item.rawId === 'qwen-mt-plus')!
171 expect(row.exactCapabilities).toBeUndefined()
172 expect(row.unsupportedCapabilities).toEqual([
173 'reasoning',
174 'tools',
175 'structured_outputs',
176 ])
177 expect(row.factSources?.capabilities?.reasoning?.sourceUrl).toBe(
178 doc('text-generation-model').url,
179 )
180 })
181 it('rejects partial or wrong-region cards instead of fabricating empty capabilities', () => {
182 const base = cnPriceModels(prices)
183 const card = doc('qwen-flash')
184 expect(() =>
185 applyCnCardFacts(base, {
186 ...card,
187 text: card.text.replace('title="华北2(北京)"', 'title="新加坡"'),
188 }),
189 ).toThrow('no Beijing')
190 expect(() =>
191 applyCnCardFacts(base, {
192 ...card,
193 text: card.text.replace('## 上下文限制', '## unrelated'),
194 }),
195 ).toThrow('incomplete native card')
196 expect(() =>
197 applyCnCardFacts(base, {
198 ...card,
199 text: card.text.replace('## 模型能力', '## unrelated'),
200 }),
201 ).toThrow('incomplete native card')
202 })
203 it('rejects conflicting published token limits', () => {
204 const card = doc('qwen-flash')
205 expect(() =>
206 applyCnCardFacts(cnPriceModels(prices), {
207 ...card,
208 text: card.text.replace(
209 '## 模型价格',
210 '## 上下文限制\n<table><tr><th>参数</th><th>值</th></tr><tr><td>上下文长度</td><td>999999</td></tr></table>\n## 模型价格',
211 ),
212 }),
213 ).toThrow('conflicting card token limit')
214 })
215})