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

kimi-doc-tables.test.ts

151 lines4.5 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-20).
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 expect(tables[0]?.titles).toEqual([
36 '模型',
37 '计费单位',
38 '缓存写入(TTL 5min)',
39 '缓存写入(TTL 1h)',
40 '输入价格(缓存命中)',
41 '输入价格(缓存未命中)',
42 '输出价格',
43 '上下文窗口',
44 ])
45 expect(tables[0]?.rows).toEqual([
46 [
47 'kimi-k3',
48 '1M tokens',
49 '¥20.00',
50 '¥40.00',
51 '¥2.00',
52 '¥20.00',
53 '¥100.00',
54 '1,048,576 tokens',
55 ],
56 ])
57 expect(tables[1]?.rows.map((row) => row[0])).toEqual([
58 'kimi-k2.7-code',
59 'kimi-k2.7-code-highspeed',
60 'kimi-k2.6',
61 ])
62 })
63​
64 it('joins a fragment of text into one cell', () => {
65 const [k3] = parseKimiDocTables(INTL)
66 expect(k3?.section).toBe('Model Pricing')
67 expect(k3?.rows[0]).toEqual([
68 'kimi-k3',
69 '1M tokens',
70 '$3.00',
71 '$6.00',
72 '$0.30',
73 '$3.00',
74 '$15.00',
75 '1,048,576 tokens',
76 ])
77 })
78​
79 it('reads only the first copy of the body', () => {
80 const twice = CN.replace('</script>', `${CN}</script>`)
81 expect(parseKimiDocTables(twice)).toEqual(parseKimiDocTables(CN))
82 })
83​
84 it('gives a cell or heading that is not plain text as null', () => {
85 const tables = parseKimiDocTables(
86 CN.replace('`¥1.10`', '_jsx(_components.del,{children:`¥1.10`})')
87 .replace(
88 'children:`K2 系列模型`',
89 'children:_jsx(_components.code,{children:`K2`})',
90 )
91 .replace('{title:`输出价格`,width:`14%`}', '{width:`14%`}'),
92 )
93 expect(tables[1]?.subsection).toBeNull()
94 expect(tables[1]?.titles[4]).toBeNull()
95 expect(tables[1]?.rows[2]?.[2]).toBeNull()
96 expect(tables[1]?.rows[2]?.[3]).toBe('¥6.50')
97 })
98​
99 it('reads a single-child fragment and heading props in any order', () => {
100 const tables = parseKimiDocTables(
101 CN.replace('`¥1.10`', '_jsx(_Fragment,{children:`¥1.10`})').replace(
102 '_jsx(Heading,{level:`3`,id:`k2-系列模型`,children:`K2 系列模型`})',
103 '_jsx(Heading,{id:`k2-系列模型`,children:`K2 系列模型`,level:`3`})',
104 ),
105 )
106 expect(tables[1]?.subsection).toBe('K2 系列模型')
107 expect(tables[1]?.rows[2]?.[2]).toBe('¥1.10')
108 })
109​
110 it('reads a markdown heading the page compiles to _components.h2', () => {
111 const tables = parseKimiDocTables(
112 CN.replace(
113 '_jsx(Heading,{level:`2`,id:`模型定价`,children:`模型定价`})',
114 '_jsx(_components.h2,{id:`模型定价`,children:`模型定价`})',
115 ),
116 )
117 expect(tables[0]?.section).toBe('模型定价')
118 })
119​
120 it('throws on the empty markdown export and on unknown table props', () => {
121 expect(() => parseKimiDocTables(EMPTY_MD_EXPORT)).toThrow(
122 'page has no compiled MDX body',
123 )
124 expect(() =>
125 parseKimiDocTables(CN.replaceAll('_jsx(DocTable,', '_jsx(Callout,')),
126 ).toThrow('page has no DocTable')
127 expect(() =>
128 parseKimiDocTables(
129 CN.replace('}],rows:[[`kimi-k3`', '}],data:[[`kimi-k3`'),
130 ),
131 ).toThrow('DocTable props are not columns, rows')
132 expect(() =>
133 parseKimiDocTables(
134 CN.replace(
135 '_jsx(DocTable,{columns:',
136 '_jsx(DocTable,{caption:`x`,columns:',
137 ),
138 ),
139 ).toThrow('DocTable props are not columns, rows')
140 expect(() =>
141 parseKimiDocTables(
142 CN.replace('rows:[[`kimi-k3`', 'rows:[...more,[`kimi-k3`'),
143 ),
144 ).toThrow('DocTable row is not an array')
145 expect(() =>
146 parseKimiDocTables(
147 CN.replace('_jsx(Heading,{level:`3`,', '_jsx(Heading,{level:three,'),
148 ),
149 ).toThrow('heading level is unreadable')
150 })
151})