Files
apes-sidebar/tests/editor-load.spec.ts
T
Menghuan1918 27f71a3bd7 test: 适配流式传输 / JSONL / 大文件分页的新契约
- 新增 fs-read-page / jsonl / jsonl-language / file-stream-route 四个 spec
- 新增 archive 相关的两个 spec 之外的:写路由 spec 改指向 /sidebar/write,
  并补上「远超旧的 1 MiB JSON 上限的一次保存」这条回归
- 上传与 fs-operations 的 size cap 断言改为「远超旧上限仍成功且不留残file」
- 查看器契约 port 到 kind 联合 + largeFile;新增 editor-large-file 取代
  截断保存用例;内置 viewer 清单变 4 个
- smoke / plugin-shape 适配:删掉的四个配置键、新增的 /sidebar/write 路由、
  文件名对应的 route 错误码由 500 变 400、fs.read 的 too-large 分支
- smoke 的 GET 改走真 node:http(字节路由要真 Writable),并 closeAllConnections
  以免 undici 的 keep-alive 把用例拖过超时
- 21 份词典同步:18(+1) 个新 key、删除 truncation/zipProgress、
  pluginVideoPreviewDesc 去掉已不存在的 mediaLimit 说法

测试发现的两个真实缺陷一并修复:
- lang-jsonl 的字符串分支把 StringStream.next() 的 undefined 当成 null,
  遇到第一个未闭合字符串就无限增长直到 RangeError
- 分页读取原先锁死第一页嗅探到的编码,会让「长 ASCII 前缀 + GBK 正文」的
  文件后半段变乱码;改为只锁宽字符编码,字节序编码逐页各自嗅探(分页按行
  对齐,不会切断字符)
2026-10-11 22:36:42 +08:00

144 lines
5.8 KiB
TypeScript

/**
* Tests for the editor-load planning: the pure strategy dispatch the
* editor host runs (planFirstMatch / planFsReadOutcome / decodeHead).
*/
import { describe, expect, it } from 'vitest'
import { decodeHead, planFirstMatch, planFsReadOutcome } from '../src/client/editor-load.ts'
import type { FileViewerDescriptor } from '../src/client/service.ts'
const viewer = (over: Partial<FileViewerDescriptor>): FileViewerDescriptor => ({
id: 'v',
exts: [],
fetchStrategy: 'fsRead',
component: () => null,
...over,
})
describe('planFirstMatch', () => {
it('no viewer → binary (download UI)', () => {
expect(planFirstMatch(undefined, () => '/m')).toEqual({ kind: 'binary' })
})
it('binary-download strategy → binary', () => {
const v = viewer({ id: 'dl', fetchStrategy: 'binary-download' })
expect(planFirstMatch(v, () => '/m')).toEqual({ kind: 'binary' })
})
it('mediaUrl / none → render with the media URL', () => {
const v = viewer({ id: 'img', fetchStrategy: 'mediaUrl' })
expect(planFirstMatch(v, () => '/media')).toEqual({ kind: 'render', viewer: v, mediaUrl: '/media' })
expect(planFirstMatch(viewer({ id: 'none', fetchStrategy: 'none' }), () => '/m').kind).toBe('render')
})
it('custom → customLoad (the viewer loads its own bytes)', () => {
const v = viewer({ id: 'csv', fetchStrategy: 'custom', load: async () => [] })
expect(planFirstMatch(v, () => '/m')).toEqual({ kind: 'customLoad', viewer: v })
})
it('fsRead → fetchFsRead', () => {
const v = viewer({ id: 'code', fetchStrategy: 'fsRead' })
expect(planFirstMatch(v, () => '/m')).toEqual({ kind: 'fetchFsRead', viewer: v })
})
})
describe('planFsReadOutcome', () => {
const rematch = (head: Uint8Array): FileViewerDescriptor | undefined => {
// The builtin binary-download NUL probe shape.
if (head.includes(0)) return viewer({ id: 'binary-download', fetchStrategy: 'binary-download' })
return undefined
}
/** No sniffer claims the head bytes (the built-in registry carries no detect). */
const unclaimed = (): FileViewerDescriptor | undefined => undefined
/** The head a NUL probe (and any byte sniffer) matches. */
const NUL_HEAD = 'AAAAAA=='
const LARGE = 6 * 1024 * 1024
it('text result renders with content and size, same viewer', () => {
const v = viewer({ id: 'code' })
const action = planFsReadOutcome(v, { kind: 'text', content: 'hello', size: 5 }, rematch, () => '/m')
expect(action).toEqual({ kind: 'render', viewer: v, content: 'hello', size: 5 })
})
it('binary whose head re-matches no viewer → binary', () => {
const action = planFsReadOutcome(viewer({}), { kind: 'binary', size: 3, head: NUL_HEAD }, unclaimed, () => '/m')
expect(action).toEqual({ kind: 'binary' })
})
it('binary whose NUL head re-matches nothing renderable → binary', () => {
const action = planFsReadOutcome(
viewer({}),
{ kind: 'binary', size: 3, head: NUL_HEAD }, // 0x00 bytes
rematch,
() => '/m',
)
// binary-download claims it — still the download UI.
expect(action).toEqual({ kind: 'binary' })
})
it('binary re-matched to a custom viewer → customLoad (external sniffer)', () => {
const sniffer = viewer({ id: 'my:raw', fetchStrategy: 'custom', load: async () => ({}) })
const action = planFsReadOutcome(
viewer({}),
{ kind: 'binary', size: 3, head: NUL_HEAD },
() => sniffer,
() => '/m',
)
expect(action).toEqual({ kind: 'customLoad', viewer: sniffer })
})
it('binary re-matched to a mediaUrl viewer → render with the media URL', () => {
const media = viewer({ id: 'my:binimg', fetchStrategy: 'mediaUrl' })
const action = planFsReadOutcome(
viewer({}),
{ kind: 'binary', size: 3, head: NUL_HEAD },
() => media,
() => '/m?x=1',
)
expect(action).toEqual({ kind: 'render', viewer: media, mediaUrl: '/m?x=1' })
})
it('too-large whose head re-matches a custom viewer → customLoad (the head is honored)', () => {
const sniffer = viewer({ id: 'my:raw', fetchStrategy: 'custom', load: async () => ({}) })
const action = planFsReadOutcome(
viewer({ id: 'code' }),
{ kind: 'too-large', size: LARGE, head: NUL_HEAD },
() => sniffer,
() => '/m',
)
expect(action).toEqual({ kind: 'customLoad', viewer: sniffer })
})
it('too-large whose head re-matches a mediaUrl viewer → render the claim with the media URL', () => {
const media = viewer({ id: 'my:binimg', fetchStrategy: 'mediaUrl' })
const action = planFsReadOutcome(
viewer({ id: 'code' }),
{ kind: 'too-large', size: LARGE, head: NUL_HEAD },
() => media,
() => '/m?x=1',
)
expect(action).toEqual({ kind: 'render', viewer: media, mediaUrl: '/m?x=1' })
})
it('too-large with no claim renders the matched fsRead viewer as the paged preview', () => {
const v = viewer({ id: 'code' })
const action = planFsReadOutcome(v, { kind: 'too-large', size: LARGE, head: 'e30=' }, unclaimed, () => '/m')
expect(action).toEqual({ kind: 'render', viewer: v, size: LARGE, largeFile: { size: LARGE } })
// The document is NOT handed over — the whole point of the shape: the
// viewer gets the size (to page by) and nothing that looks savable.
expect(action).not.toHaveProperty('content')
})
it('too-large whose matched viewer is not an fsRead viewer → binary', () => {
const custom = viewer({ id: 'my:csv', fetchStrategy: 'custom', load: async () => ({}) })
const action = planFsReadOutcome(custom, { kind: 'too-large', size: LARGE, head: 'e30=' }, unclaimed, () => '/m')
expect(action).toEqual({ kind: 'binary' })
})
})
describe('decodeHead', () => {
it('decodes base64 to the exact bytes', () => {
const head = decodeHead('AP//AA==')
expect(Array.from(head)).toEqual([0x00, 0xff, 0xff, 0x00])
})
})