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

kimi-doc-tables.test.ts

185 lines5.6 KB

1import { readFileSync } from 'node:fs'
2​
3import { describe, expect, it } from 'vitest'
4​
5import { parseKimiDocTables } from './kimi-doc-tables.ts'
6​
7const page = (name: string) =>
8 readFileSync(new URL(`./fixtures/${name}`, import.meta.url), 'utf8')
9​
10// The compiled MDX bodies of both pricing pages (2026-10-23).
11const CN = page('platform.kimi.com-pricing-chat.html.txt')
12const INTL = page('platform.kimi.ai-pricing-chat.html.txt')
13​
14/** platform.kimi.com/docs/pricing/chat.md as served on 2026-10-20. */
15const EMPTY_MD_EXPORT = `## 模型定价
16​
17### K3 系列模型
18​
19<table>
20 <thead>
21 <tr />
22 </thead>
23​
24 <tbody />
25</table>
27​
28describe('parseKimiDocTables', () => {
29 it('reads each table under its headings', () => {
30 const tables = parseKimiDocTables(CN)
31 expect(tables.map((table) => [table.section, table.subsection])).toEqual([
32 ['模型定价', 'K3 系列模型'],
33 ['模型定价', 'K2 系列模型'],
34 ])
35 // Tooltip titles and linked ids read as their text (issue #484).
36 expect(tables[0]?.titles).toEqual([
37 '模型',
38 '输入',
39 '输出',
40 '缓存命中',
41 '缓存写入(5min)',
42 '缓存写入(1h)',
43 ])
44 expect(tables[0]?.rows.at(-1)).toEqual([
45 'kimi-k3',
46 '¥20.00 / MTok',
47 '¥100.00 / MTok',
48 '¥2.00 / MTok',
49 '¥20.00 / MTok',
50 '¥40.00 / MTok',
51 ])
52 expect(tables[1]?.rows.map((row) => row[0])).toEqual([
53 'kimi-k2.7-code',
54 'kimi-k2.7-code-highspeed',
55 'kimi-k2.6',
56 ])
57 })
58​
59 it('reads the international page', () => {
60 const [k3] = parseKimiDocTables(INTL)
61 expect(k3?.section).toBe('Model Pricing')
62 expect(k3?.titles).toEqual([
63 'Model',
64 'Input',
65 'Output',
66 'Cached Input',
67 '5m writes',
68 '1h writes',
69 ])
70 expect(k3?.rows.at(-1)).toEqual([
71 'kimi-k3',
72 '$3.00 / MTok',
73 '$15.00 / MTok',
74 '$0.30 / MTok',
75 '$3.00 / MTok',
76 '$6.00 / MTok',
77 ])
78 })
79​
80 it('joins a fragment of text into one cell', () => {
81 const tables = parseKimiDocTables(
82 CN.replace(
83 '`¥1.10 / MTok`',
84 '_jsxs(_Fragment,{children:[`¥`,`1.10 / MTok`]})',
85 ),
86 )
87 expect(tables[1]?.rows[2]?.[3]).toBe('¥1.10 / MTok')
88 })
89​
90 it('reads only the first copy of the body', () => {
91 const twice = CN.replace('</script>', `${CN}</script>`)
92 expect(parseKimiDocTables(twice)).toEqual(parseKimiDocTables(CN))
93 })
94​
95 it('gives a cell or heading that is not plain text as null', () => {
96 const tables = parseKimiDocTables(
97 CN.replace(
98 '`¥1.10 / MTok`',
99 '_jsx(_components.del,{children:`¥1.10 / MTok`})',
100 )
101 .replace(
102 'children:`K2 系列模型`',
103 'children:_jsx(_components.code,{children:`K2`})',
104 )
105 .replace('{title:`输出`,width:`22%`}', '{width:`22%`}')
106 .replace(
107 'children:`kimi-k2.6`',
108 'children:_jsx(_components.code,{children:`kimi-k2.6`})',
109 ),
110 )
111 expect(tables[1]?.subsection).toBeNull()
112 expect(tables[1]?.titles[2]).toBeNull()
113 expect(tables[1]?.rows[2]?.[0]).toBeNull()
114 expect(tables[1]?.rows[2]?.[3]).toBeNull()
115 expect(tables[1]?.rows[2]?.[1]).toBe('¥6.50 / MTok')
116 })
117​
118 it('reads a link only as the id and a tooltip only as a title', () => {
119 const tables = parseKimiDocTables(
120 CN.replace(
121 '`¥1.10 / MTok`',
122 '_jsx(Tooltip,{tip:`限时`,children:`¥1.10 / MTok`})',
123 ).replace(
124 '`¥27.00 / MTok`,_jsx(Tooltip',
125 '_jsx(_components.a,{href:`/x`,children:`¥27.00 / MTok`}),_jsx(Tooltip',
126 ),
127 )
128 expect(tables[1]?.rows[2]?.[0]).toBe('kimi-k2.6')
129 expect(tables[1]?.rows[2]?.[2]).toBeNull()
130 expect(tables[1]?.rows[2]?.[3]).toBeNull()
131 expect(tables[1]?.titles[3]).toBe('缓存命中')
132 })
133​
134 it('reads a single-child fragment and heading props in any order', () => {
135 const tables = parseKimiDocTables(
136 CN.replace(
137 '`¥1.10 / MTok`',
138 '_jsx(_Fragment,{children:`¥1.10 / MTok`})',
139 ).replace(
140 '_jsx(Heading,{level:`3`,id:`k2-系列模型`,children:`K2 系列模型`})',
141 '_jsx(Heading,{id:`k2-系列模型`,children:`K2 系列模型`,level:`3`})',
142 ),
143 )
144 expect(tables[1]?.subsection).toBe('K2 系列模型')
145 expect(tables[1]?.rows[2]?.[3]).toBe('¥1.10 / MTok')
146 })
147​
148 it('reads a markdown heading the page compiles to _components.h2', () => {
149 const tables = parseKimiDocTables(
150 CN.replace(
151 '_jsx(Heading,{level:`2`,id:`模型定价`,children:`模型定价`})',
152 '_jsx(_components.h2,{id:`模型定价`,children:`模型定价`})',
153 ),
154 )
155 expect(tables[0]?.section).toBe('模型定价')
156 })
157​
158 it('throws on the empty markdown export and on unknown table props', () => {
159 expect(() => parseKimiDocTables(EMPTY_MD_EXPORT)).toThrow(
160 'page has no compiled MDX body',
161 )
162 expect(() =>
163 parseKimiDocTables(CN.replaceAll('_jsx(DocTable,', '_jsx(Callout,')),
164 ).toThrow('page has no DocTable')
165 expect(() =>
166 parseKimiDocTables(CN.replace('}],rows:[[', '}],data:[[')),
167 ).toThrow('DocTable props are not columns, rows')
168 expect(() =>
169 parseKimiDocTables(
170 CN.replace(
171 '_jsx(DocTable,{className:',
172 '_jsx(DocTable,{caption:`x`,className:',
173 ),
174 ),
175 ).toThrow('DocTable props are not columns, rows')
176 expect(() =>
177 parseKimiDocTables(CN.replace('rows:[[', 'rows:[...more,[')),
178 ).toThrow('DocTable row is not an array')
179 expect(() =>
180 parseKimiDocTables(
181 CN.replace('_jsx(Heading,{level:`3`,', '_jsx(Heading,{level:three,'),
182 ),
183 ).toThrow('heading level is unreadable')
184 })
185})