Files
apes-sidebar/tests/jsonl.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

111 lines
4.7 KiB
TypeScript

/**
* JSONL record handling for the preview: one line, one independent document.
*
* The preview never parses the file as a single JSON value — that is the whole
* point of treating JSONL as its own format. These are the pure helpers behind
* that: the extension test, the per-line parse, and the collapsed one-line
* summary a record row shows before it is expanded into the host's JsonTree.
*/
import { describe, expect, it } from 'vitest'
import { isJsonlPath, isTreeRecord, parseRecord, summarizeRecord } from '../src/client/jsonl.ts'
describe('isJsonlPath', () => {
it('claims .jsonl and .ndjson, case-insensitively', () => {
expect(isJsonlPath('/ws/logs/session.jsonl')).toBe(true)
expect(isJsonlPath('/ws/data/events.ndjson')).toBe(true)
expect(isJsonlPath('/ws/DATA/EVENTS.JSONL')).toBe(true)
})
it('leaves plain .json, .jsonc and unrelated extensions alone', () => {
// `.json` is a single document: the `code` viewer's stock JSON grammar is
// right for it, and the record list would be wrong.
expect(isJsonlPath('/ws/package.json')).toBe(false)
expect(isJsonlPath('/ws/tsconfig.jsonc')).toBe(false)
expect(isJsonlPath('/ws/notes.txt')).toBe(false)
expect(isJsonlPath('/ws/jsonl')).toBe(false)
})
})
describe('parseRecord', () => {
it('parses each line as its own document', () => {
expect(parseRecord('{"a":1}')).toEqual({ ok: true, value: { a: 1 } })
expect(parseRecord('[1,2,3]')).toEqual({ ok: true, value: [1, 2, 3] })
expect(parseRecord('42')).toEqual({ ok: true, value: 42 })
expect(parseRecord('"a string"')).toEqual({ ok: true, value: 'a string' })
expect(parseRecord('null')).toEqual({ ok: true, value: null })
expect(parseRecord('true')).toEqual({ ok: true, value: true })
})
it('tolerates surrounding whitespace (the host trims at a line break, not a token)', () => {
expect(parseRecord(' {"a": 1} ')).toEqual({ ok: true, value: { a: 1 } })
expect(parseRecord('\t{"a": 1}')).toEqual({ ok: true, value: { a: 1 } })
})
it('reports a blank line as an empty failure, so the caller can SKIP it', () => {
// JSONL files normally end with a newline, and the host's pages are
// line-aligned — so a trailing blank lands as its own row. Rendering it as
// "this line is not valid JSON" would put a fake error at the end of every
// well-formed file in the world.
expect(parseRecord('')).toEqual({ ok: false, error: '' })
expect(parseRecord(' ')).toEqual({ ok: false, error: '' })
})
it('reports a broken line with its reason', () => {
const broken = parseRecord('{"a": }')
expect(broken.ok).toBe(false)
if (!broken.ok) expect(broken.error.length).toBeGreaterThan(0)
})
it('is not fooled by a JSON value that spans lines', () => {
// A record CANNOT span lines in JSONL: the first line is incomplete and
// the second is a stray continuation. Both must fail rather than being
// silently glued together.
expect(parseRecord('{"a":').ok).toBe(false)
expect(parseRecord('1}').ok).toBe(false)
})
})
describe('isTreeRecord', () => {
it('accepts objects and arrays, rejects primitives and null', () => {
expect(isTreeRecord({})).toBe(true)
expect(isTreeRecord([])).toBe(true)
expect(isTreeRecord('s')).toBe(false)
expect(isTreeRecord(1)).toBe(false)
expect(isTreeRecord(true)).toBe(false)
// `null` is typeof 'object' — the classic trap this guard exists for.
expect(isTreeRecord(null)).toBe(false)
})
})
describe('summarizeRecord', () => {
it('inlines first-level primitives and elides nesting', () => {
expect(summarizeRecord({ type: 'assistant', n: 3, ok: true, none: null }))
.toBe('{type: "assistant", n: 3, ok: true, none: null}')
expect(summarizeRecord({ a: { deep: 1 }, b: [1, 2, 3] })).toBe('{a: {…}, b: […3]}')
})
it('names arrays by length and empty objects literally', () => {
expect(summarizeRecord([])).toBe('[0 items]')
expect(summarizeRecord([1, 2])).toBe('[2 items]')
expect(summarizeRecord({})).toBe('{}')
})
it('renders scalars as their JSON literal', () => {
expect(summarizeRecord('hi')).toBe('"hi"')
expect(summarizeRecord(12)).toBe('12')
expect(summarizeRecord(null)).toBe('null')
})
it('caps the summary at one scannable line', () => {
const wide = Object.fromEntries(Array.from({ length: 200 }, (_, i) => [`key${i}`, 'v'.repeat(20)]))
const summary = summarizeRecord(wide)
expect(summary.length).toBeLessThanOrEqual(200)
expect(summary.endsWith('…}')).toBe(true)
})
it('marks an elided object with an ellipsis tail and no trailing comma', () => {
const summary = summarizeRecord({ a: 1, b: { deep: true } })
expect(summary).toBe('{a: 1, b: {…}}')
})
})