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

261 lines
8.9 KiB
TypeScript

/**
* The append-only pager behind the over-limit preview.
*
* What is under test is the reader-visible behaviour: scroll to the bottom and
* the next window is APPENDED (nothing already read disappears), `loadAll`
* walks to EOF one request at a time, and the caller can stop that walk early
* with its own ceiling.
*
* `api.fsReadPage` is stubbed — this is the pager's contract with the host, not
* the host's paging (that lives in `tests/fs-read-page.spec.ts`).
*/
// @vitest-environment jsdom
import './browser-globals.ts'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { createElement } from 'react'
import { act } from 'react-dom/test-utils'
import { useTextPager, type TextPager } from '../src/client/text-pages.ts'
import { renderRoot, setupReactAct } from './test-utils.ts'
setupReactAct()
const SCOPE = { sessionId: 's', cwd: '/ws' }
const PATH = '/ws/log.jsonl'
/** One page the stubbed host hands back. */
interface FakePage {
text: string
nextOffset: number
eof: boolean
}
/** A file of `count` identical pages. */
function pagesOf(count: number, text = 'line\n'): FakePage[] {
return Array.from({ length: count }, (_, index) => ({
text,
nextOffset: (index + 1) * text.length,
eof: index === count - 1,
}))
}
/** The offsets each page starts at, i.e. the byte-offset request sequence. */
function boundariesOf(pages: FakePage[]): number[] {
let running = 0
return pages.map((page) => {
const start = running
running = page.nextOffset
return start
})
}
/** Install a stubbed `fsReadPage` that serves `pages` BY OFFSET, like the host. */
function stubPages(pages: FakePage[], failAtRequest?: number): number[] {
const requests: number[] = []
const boundaries = boundariesOf(pages)
const total = pages[pages.length - 1]?.nextOffset ?? 0
vi.spyOn(api, 'fsReadPage').mockImplementation(async (_scope, _path, offset) => {
requests.push(offset)
if (failAtRequest !== undefined && requests.length === failAtRequest) {
throw new Error('page fetch failed')
}
const index = boundaries.indexOf(offset)
if (index === -1) throw new Error(`test setup: unexpected offset ${offset}`)
const page = pages[index]!
return {
kind: 'text' as const,
text: page.text,
offset,
nextOffset: page.nextOffset,
eof: page.eof,
size: total,
encoding: 'utf8',
}
})
return requests
}
/** The pager face the tests drive, captured from inside the rendered tree. */
interface Harness {
pager: () => TextPager
loadMore: () => void
loadAll: () => void
reload: () => void
rerender: (enabled: boolean, revision?: number) => void
}
/** Render the hook and expose its commands. */
function mountPager(maxLines?: number): Harness {
const seen: { value: TextPager | null } = { value: null }
function Probe(props: { enabled: boolean; revision: number }): ReturnType<typeof createElement> {
seen.value = useTextPager(SCOPE, PATH, props.enabled, props.revision, maxLines)
// A ref write proves the probe really rendered (a stale `seen` from an
// earlier mount would otherwise read as a passing assertion).
rendered.current = true
return createElement('div', { 'data-pages': seen.value.pages.length })
}
const rendered = { current: false }
const root = renderRoot(createElement(Probe, { enabled: true, revision: 0 }))
const pager = (): TextPager => {
if (!rendered.current || seen.value === null) throw new Error('the probe never rendered')
return seen.value
}
return {
pager,
loadMore: () => { void act(() => { pager().loadMore() }) },
loadAll: () => { void act(() => { pager().loadAll() }) },
reload: () => { void act(() => { pager().reload() }) },
rerender: (enabled: boolean, revision = 0) => {
root.rerender(createElement(Probe, { enabled, revision }))
},
}
}
/** Let the pending fetch promises (and their re-renders) settle. */
async function settle(): Promise<void> {
await act(async () => {
for (let i = 0; i < 8; i += 1) await Promise.resolve()
})
}
// `api` is imported after the mock registry is set up by the spies above; the
// dynamic import keeps the spy attached to the module the hook actually uses.
const { api } = await import('../src/client/api.ts')
afterEach(() => {
vi.restoreAllMocks()
})
describe('useTextPager', () => {
it('loads the first page on mount, then APPENDS the next without dropping it', async () => {
const requests = stubPages(pagesOf(3))
const harness = mountPager()
await settle()
expect(requests).toEqual([0])
expect(harness.pager().pages).toEqual(['line\n'])
expect(harness.pager().atEnd).toBe(false)
harness.loadMore()
await settle()
expect(requests).toEqual([0, 5])
// Append, not replace: page one is still on screen.
expect(harness.pager().pages).toEqual(['line\n', 'line\n'])
expect(harness.pager().lineStarts).toEqual([1, 2])
})
it('walks to EOF and then stops asking', async () => {
const requests = stubPages(pagesOf(3))
const harness = mountPager()
await settle()
harness.loadMore()
await settle()
harness.loadMore()
await settle()
expect(harness.pager().atEnd).toBe(true)
expect(harness.pager().pages).toHaveLength(3)
expect(harness.pager().loadedBytes).toBe(15)
// Past EOF the pager must not touch the network again.
harness.loadMore()
await settle()
expect(requests).toEqual([0, 5, 10])
})
it('loadAll appends every remaining page, in offset order', async () => {
const requests = stubPages(pagesOf(6))
const harness = mountPager()
await settle()
harness.loadAll()
await settle()
expect(harness.pager().pages).toHaveLength(6)
expect(harness.pager().atEnd).toBe(true)
// Each request uses the PREVIOUS response's nextOffset — the reason the walk
// cannot be parallelised.
expect(requests).toEqual([0, 5, 10, 15, 20, 25])
expect(harness.pager().lineStarts).toEqual([1, 2, 3, 4, 5, 6])
expect(harness.pager().totalLines).toBe(6)
})
it('stops loadAll at the line ceiling and reports atCap (#the row budget)', async () => {
const requests = stubPages(pagesOf(10))
// One line per page, ceiling of three: exactly three pages may land.
const harness = mountPager(3)
await settle()
harness.loadAll()
await settle()
expect(harness.pager().pages).toHaveLength(3)
expect(harness.pager().totalLines).toBe(3)
// Not EOF — the ceiling is what stopped it, and the caller can say so.
expect(harness.pager().atEnd).toBe(false)
expect(harness.pager().atCap).toBe(true)
expect(requests).toEqual([0, 5, 10])
// And loadMore must respect it too, not just loadAll.
harness.loadMore()
await settle()
expect(requests).toEqual([0, 5, 10])
})
it('never reports atCap for a file that simply ended inside the ceiling', async () => {
stubPages(pagesOf(2))
const harness = mountPager(3)
await settle()
harness.loadAll()
await settle()
expect(harness.pager().totalLines).toBe(2)
expect(harness.pager().atEnd).toBe(true)
expect(harness.pager().atCap).toBe(false)
})
it('stops loadAll on a failed page and surfaces the error', async () => {
const requests = stubPages(pagesOf(6), 3)
const harness = mountPager()
await settle()
harness.loadAll()
await settle()
// Two pages landed, the third failed, and the walk did not continue.
expect(harness.pager().pages).toHaveLength(2)
expect(harness.pager().error).toBe('page fetch failed')
expect(requests).toEqual([0, 5, 10])
expect(harness.pager().loadingAll).toBe(false)
expect(harness.pager().loading).toBe(false)
})
it('reload drops the accumulated pages and re-reads from the start', async () => {
const requests = stubPages(pagesOf(4))
const harness = mountPager()
await settle()
harness.loadMore()
await settle()
expect(harness.pager().pages).toHaveLength(2)
harness.reload()
await settle()
expect(harness.pager().pages).toHaveLength(1)
expect(harness.pager().atEnd).toBe(false)
expect(requests).toEqual([0, 5, 0])
})
it('issues nothing while parked, and resumes when enabled', async () => {
const requests = stubPages(pagesOf(3))
const harness = mountPager()
harness.rerender(false)
await settle()
// The mount itself already fetched page one; parking must add nothing.
const before = requests.length
harness.rerender(false)
await settle()
expect(requests.length).toBe(before)
})
it('counts lines for the row cap without letting a trailing newline inflate it', async () => {
// The ceiling is spent on LINES, so a phantom line per page would hit it
// early. `a\n\nb\n` is three rows (the blank one included — it renders as
// an empty row in the log view) and NOT four: the trailing newline only
// terminates the last line.
stubPages([{ text: 'a\n\nb\n', nextOffset: 5, eof: true }])
const harness = mountPager()
await settle()
expect(harness.pager().totalLines).toBe(3)
})
})