scanLine 对每个字符执行 exec(line.slice(i)),会向后扫描到下一个空白:压缩成单行的 JSON/JS 上是 O(n²),实测 618KB 1.9s、2MB 约 20s,打开差异时主线程长任务表现为「加载中」假死。改为单字符空白判定,整行只线性扫描一次(2.6MB 实测 57ms)。 超过 20K 字符的行在 DiffRows 只渲染有界纯文本预览(折叠 2K / 展开 20K),不再生成多 MB 文本节点与 title;普通长短行行为不变,完整内容仍可在内容页/源文件查看。 测试:tests/changes-ops.spec.ts(1MB minified 行线性且 token 覆盖全行、长行仍推进块注释状态)、tests/diff-fold-expand.spec.tsx(超预算行折叠/展开均有界、500 字符行仍完整渲染)。 真机复核(挂载真实宿主 + Chromium):2MB 单行 diff 最大长任务 120ms(修复前约 20s),2MB 多行 patch 无 >50ms 长任务。
201 lines
8.1 KiB
TypeScript
201 lines
8.1 KiB
TypeScript
// @vitest-environment jsdom
|
|
/**
|
|
* The git fold expansion path: DiffFiles forwards a resolveFold loader to
|
|
* DiffRows, whose first click on a rows-less git gap fold fetches the hidden
|
|
* rows (loading/failed markers in between) and later clicks toggle; folds
|
|
* that carry rows (the session-op path) expand directly, and gaps without a
|
|
* loader stay inert markers — the pre-fix behavior for op diffs.
|
|
*/
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { createElement } from 'react'
|
|
import { createRoot, type Root } from 'react-dom/client'
|
|
import { act } from 'react-dom/test-utils'
|
|
import { DiffFiles } from '../src/client/diff/DiffFiles.tsx'
|
|
import { DiffRows } from '../src/client/diff/DiffRows.tsx'
|
|
import { buildDiffSegments, diffLines, type DiffFile, type FoldSegment } from '../src/client/diff/rows.ts'
|
|
|
|
import { setupReactAct } from './test-utils.ts'
|
|
setupReactAct()
|
|
|
|
// A git-shaped two-hunk patch: the gap between the hunks (old/new lines
|
|
// 2..11) is a fold WITHOUT rows — expandable only through a resolveFold.
|
|
const gapDiff = [
|
|
'diff --git a/src/a.ts b/src/a.ts',
|
|
'--- a/src/a.ts',
|
|
'+++ b/src/a.ts',
|
|
'@@ -1 +1 @@',
|
|
'-old',
|
|
'+new',
|
|
'@@ -12 +12 @@',
|
|
'-tail',
|
|
'+tail2',
|
|
].join('\n')
|
|
|
|
// The hidden context rows a loader slices out of both sides' contents.
|
|
const hiddenRows = Array.from({ length: 10 }, (_, i) => ({
|
|
kind: 'context' as const,
|
|
oldLine: i + 2,
|
|
newLine: i + 2,
|
|
text: `ctx-${String(i + 2)}`,
|
|
}))
|
|
|
|
afterEach(() => { document.body.innerHTML = '' })
|
|
|
|
describe('DiffFiles on-demand fold expansion', () => {
|
|
it('expands a git gap fold through resolveFold, then collapses back', async () => {
|
|
const resolveFold = vi.fn((_file: DiffFile, _segment: FoldSegment) => Promise.resolve(hiddenRows))
|
|
const container = document.createElement('div')
|
|
document.body.append(container)
|
|
const root: Root = createRoot(container)
|
|
try {
|
|
act(() => { root.render(createElement(DiffFiles, { diff: gapDiff, resolveFold })) })
|
|
const fold = container.querySelector<HTMLElement>('[data-expandable="true"]')
|
|
expect(fold).not.toBeNull()
|
|
expect(container.textContent).not.toContain('ctx-5')
|
|
|
|
await act(async () => { fold!.click() })
|
|
expect(resolveFold).toHaveBeenCalledOnce()
|
|
expect(container.textContent).toContain('ctx-5')
|
|
// The loader received the gap fold itself (line ranges on board).
|
|
const segment = resolveFold.mock.calls[0]![1]
|
|
expect(segment.kind).toBe('fold')
|
|
expect(segment.oldStart).toBe(2)
|
|
|
|
// Collapse: the marker is back; the fetch never repeats.
|
|
const expanded = container.querySelector<HTMLElement>('[data-expandable="true"]')
|
|
await act(async () => { expanded!.click() })
|
|
expect(container.textContent).not.toContain('ctx-5')
|
|
expect(resolveFold).toHaveBeenCalledOnce()
|
|
|
|
// Re-expanding resolves from the per-fold cache (no second fetch).
|
|
const collapsed = container.querySelector<HTMLElement>('[data-expandable="true"]')
|
|
await act(async () => { collapsed!.click() })
|
|
expect(container.textContent).toContain('ctx-5')
|
|
expect(resolveFold).toHaveBeenCalledOnce()
|
|
} finally {
|
|
act(() => { root.unmount() })
|
|
container.remove()
|
|
}
|
|
})
|
|
|
|
it('degrades to the unavailable marker when the loader rejects', async () => {
|
|
const resolveFold = vi.fn(() => Promise.reject(new Error('boom')))
|
|
const container = document.createElement('div')
|
|
document.body.append(container)
|
|
const root: Root = createRoot(container)
|
|
try {
|
|
act(() => { root.render(createElement(DiffFiles, { diff: gapDiff, resolveFold })) })
|
|
const fold = container.querySelector<HTMLElement>('[data-expandable="true"]')
|
|
await act(async () => { fold!.click() })
|
|
// The degraded fold promises no interaction.
|
|
expect(container.querySelector('[data-expandable]')).toBeNull()
|
|
expect(container.textContent).not.toContain('ctx-5')
|
|
// jsdom's navigator.language is en-US → the en copy.
|
|
expect(container.textContent).toContain('Context unavailable')
|
|
} finally {
|
|
act(() => { root.unmount() })
|
|
container.remove()
|
|
}
|
|
})
|
|
|
|
it('keeps git gap folds inert without a resolver (session surfaces unchanged)', () => {
|
|
const container = document.createElement('div')
|
|
document.body.append(container)
|
|
const root: Root = createRoot(container)
|
|
try {
|
|
act(() => { root.render(createElement(DiffFiles, { diff: gapDiff })) })
|
|
expect(container.querySelector('[data-expandable]')).toBeNull()
|
|
// The quiet marker still shows the hidden line count.
|
|
expect(container.textContent).toContain('10 lines')
|
|
} finally {
|
|
act(() => { root.unmount() })
|
|
container.remove()
|
|
}
|
|
})
|
|
|
|
it('expands session folds (rows carried) directly, without any loader', async () => {
|
|
// buildDiffSegments output — the session-op path: folds carry their rows,
|
|
// so they toggle on click exactly as before the git expansion existed.
|
|
const rows = diffLines(
|
|
'a\nb\nc\nd\ne\nX\nf\ng\nh\ni\nj\nk\nl\n',
|
|
'a\nb\nc\nd\ne\nY\nf\ng\nh\ni\nj\nk\nl\n',
|
|
)
|
|
const segments = buildDiffSegments(rows)
|
|
const container = document.createElement('div')
|
|
document.body.append(container)
|
|
const root: Root = createRoot(container)
|
|
try {
|
|
act(() => { root.render(createElement(DiffRows, { segments })) })
|
|
const fold = container.querySelector<HTMLElement>('[data-expandable="true"]')
|
|
expect(fold).not.toBeNull()
|
|
// 'j' is hidden in the tail fold (the marker copy carries no 'j').
|
|
expect(container.textContent).not.toContain('j')
|
|
await act(async () => { fold!.click() })
|
|
expect(container.textContent).toContain('j')
|
|
const expanded = container.querySelector<HTMLElement>('[data-expandable="true"]')
|
|
await act(async () => { expanded!.click() })
|
|
expect(container.textContent).not.toContain('j')
|
|
} finally {
|
|
act(() => { root.unmount() })
|
|
container.remove()
|
|
}
|
|
})
|
|
})
|
|
|
|
/**
|
|
* The renderer's budget for pathologically long lines: >20K chars renders a
|
|
* bounded plain preview (2K folded / 20K expanded) instead of the full text.
|
|
* Normal long lines keep the old full-text rendering.
|
|
*/
|
|
describe('DiffRows long-line budget', () => {
|
|
const mount = (text: string): { container: HTMLElement; root: Root } => {
|
|
// A punctuation-heavy minified line: with lang 'js' it also exercises
|
|
// diffBlockEntries' cross-line scan on the over-budget row.
|
|
const segments = [{
|
|
kind: 'hunk' as const,
|
|
rows: [{ kind: 'add' as const, newLine: 1, text }],
|
|
}]
|
|
const container = document.createElement('div')
|
|
document.body.append(container)
|
|
const root: Root = createRoot(container)
|
|
act(() => { root.render(createElement(DiffRows, { segments, lang: 'js' })) })
|
|
return { container, root }
|
|
}
|
|
|
|
it('bounds an over-budget row while folded, then bounds it again when expanded', () => {
|
|
const long = '{' + '"k":1,'.repeat(12_000) + '}'
|
|
const { container, root } = mount(long)
|
|
try {
|
|
const row = container.querySelector<HTMLElement>('[data-folded="true"]')
|
|
expect(row).not.toBeNull()
|
|
// Folded: 2K preview + '…' (plus the line number and sign).
|
|
expect(row!.textContent!.length).toBeGreaterThan(2_000)
|
|
expect(row!.textContent!.length).toBeLessThan(3_000)
|
|
expect(row!.textContent).not.toContain(long)
|
|
|
|
act(() => { row!.click() })
|
|
expect(container.querySelector('[data-folded="true"]')).toBeNull()
|
|
// Expanded: 20K body + '…' (plus the line number and sign).
|
|
expect(container.textContent!.length).toBeGreaterThan(20_000)
|
|
expect(container.textContent!.length).toBeLessThan(21_000)
|
|
expect(container.textContent).not.toContain(long)
|
|
} finally {
|
|
act(() => { root.unmount() })
|
|
container.remove()
|
|
}
|
|
})
|
|
|
|
it('keeps a normal long row (500 chars) rendering in full', () => {
|
|
const text = 'y'.repeat(500)
|
|
const { container, root } = mount(text)
|
|
try {
|
|
expect(container.querySelector('[data-folded="true"]')).not.toBeNull()
|
|
expect(container.textContent).toContain(text)
|
|
expect(container.textContent).not.toContain('…')
|
|
} finally {
|
|
act(() => { root.unmount() })
|
|
container.remove()
|
|
}
|
|
})
|
|
})
|