Files
apes-sidebar/tests/chunk-artifact.spec.ts
T
Menghuan1918 51c1650928 feat(preview): 超大文件改追加式滚动 + 加载全部按钮
按用户要求对齐 VS Code 的 output/log 面板:

- 滚到底**追加**下一页而不是替换当前页:读者读过的内容不会被抽走,
  行号跨页连续;footer 改成「已加载 第 a–b 行/条记录 · 已读/总大小」
- 超限提示条最右侧新增「加载全部」按钮,加载中显示字节进度,到 EOF 置灰
- 游标模型整体删除(不再有 prev/next 与页码),只剩一个只向前的累加器,
  比原来的 LRU 缓存 + 页表简单得多

唯一 VS Code 不需要而这里必须有的:行数上限 MAX_PREVIEW_ROWS = 50000。
它的 TextModel 与视图都虚拟化,模型塞进一个 GB、DOM 仍很小;这里渲染的是
真实元素,没有上限就是「加载全部」一点即卡死。到顶后停止追加并提示下载。

上限放在 hook 里(useTextPager 的 maxLines 参数)而不是视图按**已渲染**
行数判断——这是测试抓出来的:loadAll 在 await 循环中推进时 React 还没重
渲染,读渲染态的数字会滞后好几页把上限冲过去(spec 里 worker 十秒 OOM)。

另一个测试抓出的真 bug:appendOnce 原来从 setState 渲染态读 nextOffset,
在同一批 await 里拿到的是**上一页**的偏移,于是 loadAll 会反复请求同一页
(同样是 OOM)。游标改由 nextOffsetRef/atEndRef 在响应到达时同步写入。

测试:新增 tests/text-pages.spec.ts(9 例:追加不丢页、EOF 终态、loadAll
串行推进、上限截断与 atCap、失败中止、reload 清空、行数计账),
editor-large-file 补 6 例视图行为(滚动追加、中途不追加、自动填充视口、
加载全部分页、上限提示、默认上限不误报);chunk-artifact 的 externals
桩表补 react.memo(editor chunk 现在在模块求值期 memo 一行组件)。
2026-10-12 00:11:07 +08:00

98 lines
4.7 KiB
TypeScript

/**
* Built chunk artifact contract: each lib/client-<name>.js must, when
* executed as a classic script, assign its factory to the plugin-owned
* global registry (globalThis.__dshChunks__[<name>]), and the factory must
* be callable with a require that resolves the platform externals — the
* exact shape the loader (src/client/chunk-loader.ts) depends on. Reads the
* built lib/ output, so run `pnpm build` first (like manifest-consistency).
* A missing lib/ (fresh clone before the first build) skips the whole suite
* instead of crashing on ENOENT.
*/
import { existsSync, readFileSync } from 'node:fs'
import { describe, expect, it } from 'vitest'
// Browser globals first: chunk bodies probe `self`/`document` at evaluation
// (CodeMirror's UA probe).
import './browser-globals.ts'
import { CHUNK_EXTERNALS } from '../src/client/chunk-loader.ts'
import { CHUNK_NAMES } from '../src/bundle-route.ts'
const g = globalThis as Record<string, unknown>
/**
* The chunk set, DERIVED from the host route's allowlist — the same registry
* `/sidebar/bundle` serves from (src/bundle-route.ts). Never hand-mirrored:
* the hand-written list that used to live here drifted (`locale` was
* missing), so the locale artifact went unguarded.
*/
const CHUNKS = CHUNK_NAMES
/** All chunk artifacts present (tsdown emits the whole lib/ in one run). */
const chunksBuilt = CHUNKS.every(name => existsSync(`lib/client-${name}.js`))
if (!chunksBuilt) {
console.warn('[chunk-artifact] lib/ chunk artifacts missing — run `pnpm build` first; skipping this suite')
}
describe.skipIf(!chunksBuilt)('built chunk artifacts', () => {
it('each chunk assigns its global registry slot when executed as a script', () => {
g.window = g // classic-script globals
// mermaid's core hooks window.addEventListener('load') at module scope
// (its startOnLoad wiring); the Node global lacks the API, so stub it
// exactly like browser-globals.ts does for its window stub.
if (typeof g.addEventListener !== 'function') g.addEventListener = () => {}
if (typeof g.removeEventListener !== 'function') g.removeEventListener = () => {}
for (const name of CHUNKS) {
const code = readFileSync(`lib/client-${name}.js`, 'utf8')
expect(() => new Function(code)(), name).not.toThrow()
const registry = g.__dshChunks__ as Record<string, unknown>
expect(typeof registry[name], name).toBe('function')
}
})
it('each chunk factory materializes through a require over the platform externals', () => {
const registry = g.__dshChunks__ as Record<string, unknown>
/**
* Externals whose members are CALLED while a chunk module is evaluated, so
* a bare marker object is not enough. The editor chunk memoizes its JSONL
* record row at module scope, which means `react.memo` has to be callable
* the moment the factory runs. Everything else stays a marker: the point of
* this case is that the REQUIRES resolve, not that a stub behaves like
* React.
*/
const callable: Record<string, unknown> = {
react: { memo: (component: unknown) => component },
}
const table = new Map<string, unknown>(CHUNK_EXTERNALS.map(spec => [spec, callable[spec] ?? { spec }]))
for (const name of CHUNKS) {
const factory = registry[name] as (require: (spec: string) => unknown) => Record<string, unknown>
expect(() => factory((spec) => {
if (!table.has(spec)) throw new Error(`require("${spec}") missed the module table`)
return table.get(spec)
}), name).not.toThrow()
}
})
/**
* The emulator's own DOM contract, present in xterm's runtime source AND in
* its stylesheet — either half landing in the core bundle means the lazy
* split broke. `@xterm/xterm` alone would be too weak a probe: the package
* NAME also shows up in doc comments and locale copy.
*/
const XTERM_MARKER = 'xterm-char-measure-element'
it('the terminal chunk carries xterm and the core bundles carry none of it', () => {
// #774's whole build requirement: several hundred KB of emulator must not
// reach startup. The core bundle is what the plugin ships to every page
// load, so a static import of src/client/TerminalView.tsx (or of the
// xterm stylesheet) from a core module would show up here.
const chunk = readFileSync('lib/client-terminal.js', 'utf8')
expect(chunk).toContain(XTERM_MARKER)
expect(chunk).toContain('@xterm/xterm/lib/xterm.js')
for (const file of ['lib/client.js', 'lib/client-registry.js']) {
const source = readFileSync(file, 'utf8')
expect(source, `${file} must not bundle xterm`).not.toContain(XTERM_MARKER)
expect(source, `${file} must not resolve @xterm/xterm`).not.toContain('@xterm/xterm')
}
})
})