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

dashscope-compat.test.ts

136 lines5.5 KB

1import { readFileSync } from 'node:fs'
2import { describe, expect, it } from 'vitest'
3​
4import {
5 dashscopeCompatCovers,
6 parseDashscopeCompat,
7 parseDashscopeCompatScope,
8} from './dashscope-compat.ts'
9​
10/**
11 * Request-parameter rows from
12 * compatibility-of-openai-with-dashscope.md, checked 2026-10-08.
13 * top_p says "keeps only the smallest set" and is not model-restricted.
14 * The Supported models line is the page's own compat scope.
15 */
16const COMPAT_FIXTURE = `
17Supported models: Qwen large language models (commercial and open-source editions), Qwen-VL, Qwen-Coder, Qwen-Omni, Qwen-Math, DeepSeek, Kimi, GLM, MiniMax.
18Qwen-Audio does not support the OpenAI compatible protocol.
19​
20## Request parameters
21​
22The request parameters are aligned with the OpenAI interface.
23​
24<table><tbody>
25<tr><td><p>model</p></td><td><p>string</p></td><td><p>-</p></td><td><p>The model to use.</p></td></tr>
26<tr><td><p>messages</p></td><td><p>array</p></td><td><p>-</p></td><td><p>Valid roles: system, user, assistant. Only messages[0] supports the system role.</p></td></tr>
27<tr><td><p>top_p</p></td><td><p>float</p></td><td><p>-</p></td><td><p>For example, a value of 0.8 keeps only the smallest set of tokens whose cumulative probability is at least 0.8.</p></td></tr>
28<tr><td><p>temperature</p></td><td><p>float</p></td><td><p>-</p></td><td><p>Controls the randomness and diversity of model responses.</p></td></tr>
29<tr><td><p>presence_penalty</p></td><td><p>float</p></td><td><p>-</p></td><td><p>Supported only on Qwen commercial models and open-source models qwen1.5 and later.</p></td></tr>
30<tr><td><p>n</p></td><td><p>integer</p></td><td><p>-</p></td><td><p>Currently supported only on qwen-plus.</p></td></tr>
31<tr><td><p>max_tokens</p></td><td><p>integer</p></td><td><p>-</p></td><td><p>The maximum number of tokens the model can generate.</p></td></tr>
32<tr><td><p>seed</p></td><td><p>integer</p></td><td><p>-</p></td><td><p>The random seed for generation.</p></td></tr>
33<tr><td><p>stop</p></td><td><p>string or array</p></td><td><p>-</p></td><td><p>Controls precise stopping of content generation.</p></td></tr>
34<tr><td><p>tools</p></td><td><p>array</p></td><td><p>-</p></td><td><p>Currently supported models: qwen-turbo, qwen-plus, and qwen-max.</p></td></tr>
35</tbody></table>
36​
37## Response parameters
39​
40describe('parseDashscopeCompat', () => {
41 it('maps unrestricted request parameters and leaves developer off', async () => {
42 const facts = await parseDashscopeCompat(COMPAT_FIXTURE)
43 expect(facts.flags).toEqual([
44 'top_p',
45 'temperature',
46 'max_tokens',
47 'seed',
48 'stop',
49 ])
50 expect(facts.requestMap).toEqual({
51 thinking: null,
52 maxTokensField: 'max_tokens',
53 developerRole: false,
54 replayReasoningContent: null,
55 store: null,
56 strictTools: null,
57 sessionAffinity: null,
58 cacheControl: null,
59 toolStream: null,
60 reasoningEffort: null,
61 })
62 expect(facts.sourceHash).toMatch(/^[0-9a-f]{64}$/)
63 expect(facts.scope).toEqual({
64 qwenLlm: true,
65 qwenKinds: ['vl', 'coder', 'omni', 'math'],
66 families: ['deepseek', 'kimi', 'glm', 'minimax'],
67 deny: ['audio'],
68 })
69 const scope = parseDashscopeCompatScope(COMPAT_FIXTURE)
70 for (const id of [
71 'qwen-plus',
72 'qwen3-omni-flash',
73 'deepseek-v4.1-flash',
74 'glm-5',
75 'kimi-k2',
76 'minimax-m2',
77 ]) {
78 expect(dashscopeCompatCovers(id, scope)).toBe(true)
79 }
80 expect(dashscopeCompatCovers('decision-model-preview', scope)).toBe(false)
81 expect(dashscopeCompatCovers('qwen-audio-turbo', scope)).toBe(false)
82 })
83​
84 it('reads the live page, whose cells carry MDX style attributes', async () => {
85 // compatibility-of-openai-with-dashscope.md, fetched 2026-10-13.
86 const live = readFileSync(
87 new URL(
88 './fixtures/dashscope/compatibility-of-openai-with-dashscope.md.txt',
89 import.meta.url,
90 ),
91 'utf8',
92 )
93 expect(live).toContain('<td style={{ verticalAlign: "top" }}>')
94 const facts = await parseDashscopeCompat(live)
95 expect(facts.flags).toEqual([
96 'top_p',
97 'temperature',
98 'max_tokens',
99 'seed',
100 'stop',
101 ])
102 expect(facts.requestMap.maxTokensField).toBe('max_tokens')
103 expect(facts.requestMap.developerRole).toBe(false)
104 })
105​
106 it('throws when the request table is missing', async () => {
107 await expect(parseDashscopeCompat('# no table')).rejects.toThrow(
108 /parsed 0 request parameters|no request-parameter section/,
109 )
110 })
111​
112 it('reads the same facts from the rendered page', async () => {
113 const page = `<!DOCTYPE html><html><head><title>Call Qwen models via OpenAI API</title></head><body>
114<h1 class="page-title">Alibaba Cloud Model Studio:Call Qwen models via OpenAI API</h1>
115<article class="product-description markdown-body">${COMPAT_FIXTURE.replace(
116 /^## (.+)$/gm,
117 '<h3 id="x">$1 <span id="y"></span></h3>',
118 )
119 .split('\n\n')
120 .map((block) => (block.startsWith('<') ? block : `<p>${block}</p>`))
121 .join('')}</article></body></html>`
122 const fromPage = await parseDashscopeCompat(page)
123 const fromMarkdown = await parseDashscopeCompat(COMPAT_FIXTURE)
124 expect(fromPage.flags).toEqual(fromMarkdown.flags)
125 expect(fromPage.requestMap).toEqual(fromMarkdown.requestMap)
126 expect(fromPage.scope).toEqual(fromMarkdown.scope)
127 })
128​
129 it('names an HTML page as HTML, with its title', async () => {
130 await expect(
131 parseDashscopeCompat(
132 '<!DOCTYPE html><html><head><title>Just a moment</title></head></html>',
133 ),
134 ).rejects.toThrow(/response is HTML, not markdown \(title: Just a moment\)/)
135 })
136})