Open History to see the last commit
1import { afterEach, expect, it, vi } from 'vitest'
2
3import {
4 DEEPSEEK_CHAT_DOCS,
5 decodeDeepseekOperation,
6 discoverDeepseekChunk,
7 fetchNativeDeepseekSpec,
8 nativeDeepseekDocument,
9 ownedDeepseekAsset,
10} from './deepseek-native-spec.ts'
11
12const original = globalThis.fetch
13afterEach(() => {
14 globalThis.fetch = original
15})
16const payload = {
17 servers: [{ url: 'https://api.deepseek.com' }],
18 method: 'post',
19 path: '/chat/completions',
20 info: { title: 'Native test operation' },
21 requestBody: {
22 required: true,
23 content: {
24 'application/json': {
25 schema: {
26 type: 'object',
27 properties: {
28 messages: {
29 type: 'array',
30 items: {
31 oneOf: [
32 {
33 type: 'object',
34 title: 'System message',
35 required: ['role', 'content'],
36 },
37 ],
38 },
39 },
40 },
41 },
42 },
43 },
44 },
45 responses: {
46 '200': {
47 content: {
48 'application/json': {
49 schema: { type: 'object', required: ['choices'] },
50 },
51 },
52 },
53 },
54}
55const main =
56 'abc:[()=>Promise.all([n.e(1)]).then(n.bind(n,2)),"@site/docs/api/create-chat-completion.api.mdx",2]'
57const runtime = 'names={1:"abc"};hashes={1:"deadbeef"}'
58async function chunkOf(value: unknown) {
59 const compressed = await new Response(
60 new Blob([JSON.stringify(value)])
61 .stream()
62 .pipeThrough(new CompressionStream('deflate')),
63 ).arrayBuffer()
64 const encoded = btoa(String.fromCharCode(...new Uint8Array(compressed)))
65 return `const y={api:"${encoded}"}`
66}
67it('discovers only owned native assets without evaluating JavaScript', () => {
68 expect(discoverDeepseekChunk(main, runtime)).toBe(
69 'https://api-docs.deepseek.com/assets/js/abc.deadbeef.js',
70 )
71 expect(() =>
72 ownedDeepseekAsset('https://elsewhere.test/assets/js/main.js'),
73 ).toThrow('not owned')
74 expect(() => ownedDeepseekAsset('/assets/js/main.js?redirect=1')).toThrow(
75 'not owned',
76 )
77 expect(() => discoverDeepseekChunk('changed', runtime)).toThrow(
78 'module not found',
79 )
80 expect(() => discoverDeepseekChunk(main, 'changed')).toThrow(
81 'mapping not found',
82 )
83})
84it('preserves incomplete native schema fragments without filling missing fields', async () => {
85 const decoded = await decodeDeepseekOperation(await chunkOf(payload))
86 const document = nativeDeepseekDocument(decoded)
87 const operation = document.paths?.['/chat/completions']?.post
88 expect(operation?.requestBody).toEqual(payload.requestBody)
89 expect(operation?.responses).toEqual(payload.responses)
90 expect(operation).not.toHaveProperty('path')
91 expect(JSON.stringify(operation?.requestBody)).not.toContain('"enum"')
92})
93it('accepts duplicate identical embedded metadata but rejects conflicting native payloads', async () => {
94 const chunk = await chunkOf(payload)
95 expect(await decodeDeepseekOperation(chunk + chunk)).toEqual(payload)
96 await expect(
97 decodeDeepseekOperation(
98 chunk + (await chunkOf({ ...payload, method: 'get' })),
99 ),
100 ).rejects.toThrow('not found uniquely')
101})
102it('normalizes the native documented non-streaming status without substituting streaming schemas', () => {
103 const nonStreaming = payload.responses['200']
104 const document = nativeDeepseekDocument({
105 ...payload,
106 responses: {
107 '200 (No streaming)': nonStreaming,
108 '200 (Streaming)': {
109 content: { 'text/event-stream': { schema: { type: 'string' } } },
110 },
111 },
112 })
113 expect(document.paths?.['/chat/completions']?.post?.responses).toEqual({
114 '200': nonStreaming,
115 })
116 expect(() =>
117 nativeDeepseekDocument({
118 ...payload,
119 responses: { success: nonStreaming },
120 }),
121 ).toThrow('unreadable native response status')
122})
123it('fetches only owned docs assets and records the actual chunk hash', async () => {
124 const chunk = await chunkOf(payload)
125 const pages: Record<string, string> = {
126 [DEEPSEEK_CHAT_DOCS]:
127 '<script src="/assets/js/runtime~main.hash.js"></script><script src="/assets/js/main.hash.js"></script>',
128 'https://api-docs.deepseek.com/assets/js/runtime~main.hash.js': runtime,
129 'https://api-docs.deepseek.com/assets/js/main.hash.js': main,
130 'https://api-docs.deepseek.com/assets/js/abc.deadbeef.js': chunk,
131 }
132 const urls: Array<string> = []
133 globalThis.fetch = vi.fn(async (input) => {
134 const url = String(input)
135 urls.push(url)
136 const body = pages[url]
137 if (body === undefined) throw new Error(`unexpected ${url}`)
138 return new Response(body)
139 })
140 const result = await fetchNativeDeepseekSpec()
141 expect(urls).toHaveLength(4)
142 expect(
143 urls.every(
144 (url) => new URL(url).origin === 'https://api-docs.deepseek.com',
145 ),
146 ).toBe(true)
147 expect(result.sources[0]?.url).toBe(
148 'https://api-docs.deepseek.com/assets/js/abc.deadbeef.js',
149 )
150 expect(result.sources[0]?.hash).toMatch(/^[a-f0-9]{64}$/)
151 expect(result.specs[0]?.servers).toEqual(payload.servers)
152})
153it('fails on unavailable or malformed native evidence', async () => {
154 globalThis.fetch = vi.fn(
155 async () => new Response('unavailable', { status: 503 }),
156 )
157 await expect(fetchNativeDeepseekSpec()).rejects.toThrow('503')
158 await expect(decodeDeepseekOperation('api:"broken"')).rejects.toThrow()
159 expect(() =>
160 nativeDeepseekDocument({
161 ...payload,
162 servers: [{ url: 'https://elsewhere.test' }],
163 }),
164 ).toThrow('not owned')
165 expect(() =>
166 nativeDeepseekDocument({ ...payload, path: '/made-up' }),
167 ).toThrow('contract changed')
168})