Open History to see the last commit
1import { readFileSync } from 'node:fs'
2import { afterEach, describe, expect, it, vi } from 'vitest'
3
4import { fetchByteplusDoc } from './byteplus-docs.ts'
5import {
6 BYTEPLUS_REASONING_URL,
7 byteplusModelReasoning,
8} from './reasoning-config.ts'
9
10afterEach(() => vi.unstubAllGlobals())
11
12/** What docs.byteplus.com served for the reasoning page on some fetches. */
13const SHELL = readFileSync(
14 new URL('./fixtures/byteplus-doc-shell.html.txt', import.meta.url),
15 'utf8',
16)
17
18const RENDERED = `<script>window._ROUTER_DATA = {"loaderData":{}}</script>
19## Adjust chain-of-thought length
20
21* \`none\`: Turn off reasoning.
22* \`high\`: Deep analysis.
23
24|Supported models |Default value |
25|---|---|
26|* \`seed-2-0-lite-260228\` |\`medium\` |
27`
28
29function serve(...bodies: Array<string>) {
30 const fetchMock = vi.fn((_url: string) => {
31 const body = bodies.length > 1 ? bodies.shift() : bodies[0]
32 return Promise.resolve(new Response(body))
33 })
34 vi.stubGlobal('fetch', fetchMock)
35 return fetchMock
36}
37
38describe('fetchByteplusDoc', () => {
39 it('refetches when the doc center serves its unrendered shell', async () => {
40 const fetchMock = serve(SHELL, RENDERED)
41 await expect(fetchByteplusDoc(BYTEPLUS_REASONING_URL)).resolves.toBe(
42 RENDERED,
43 )
44 expect(fetchMock).toHaveBeenCalledTimes(2)
45 })
46
47 it('throws once every attempt is the shell', async () => {
48 const fetchMock = serve(SHELL)
49 await expect(fetchByteplusDoc(BYTEPLUS_REASONING_URL)).rejects.toThrow(
50 `${BYTEPLUS_REASONING_URL}: served the unrendered page shell 3 times`,
51 )
52 expect(fetchMock).toHaveBeenCalledTimes(3)
53 })
54
55 it('does not retry an HTTP error', async () => {
56 const fetchMock = vi.fn(() =>
57 Promise.resolve(new Response('unavailable', { status: 503 })),
58 )
59 vi.stubGlobal('fetch', fetchMock)
60 await expect(fetchByteplusDoc(BYTEPLUS_REASONING_URL)).rejects.toThrow(
61 /503/,
62 )
63 expect(fetchMock).toHaveBeenCalledTimes(1)
64 })
65})
66
67describe('byteplusModelReasoning', () => {
68 it('reads the rendered page when the first fetch is the shell', async () => {
69 serve(SHELL, RENDERED)
70 const reasoning = await byteplusModelReasoning()
71 expect(reasoning('seed-2-0-lite-260228', true).reasoning).toEqual({
72 mode: 'effort',
73 mandatory: false,
74 efforts: ['none', 'high'],
75 })
76 })
77})