Files
apes-sidebar/tests/diff-rows.spec.ts
T
Menghuan1918 4a03e131e5 fix(diff): 会话镜头 diff 的超大输入退化为全删全增,不再建稠密 LCS 表(#734)
rows.ts 的 lcsTable 是无护栏的稠密普通数组矩阵(每单元约 8 字节),成本
完全由输入行数乘积决定。实测:1000×1000 → 8MB、3000×3000 → 71MB、
5000×5000 → 193MB、10000×10000 → heapDelta 765MB(峰值 RSS 865MB)。
会话镜头把上一次 write 的完整文件内容当 prior 传进 diffOf(ops.ts 对
content 无字节/行数截断),选中一个 op 就在 useMemo 里同步跑完,因此一份
万行文件的改动预览会一次性吃掉数百 MB 主线程内存。

护栏落在 diffLines 内部、lcsTable 调用之前(它是会话镜头唯一的 LCS 入口,
一处覆盖 diffOf 的三个分支):新增导出常量 MAX_LCS_CELLS = 4_000_000
(≈32MB,仍精确覆盖 2000×2000 的重写配对),判定
oldLines.length * newLines.length;超限不建表,直接产出全删(oldLines
顺序,oldLine 1..n)+ 全增(newLines 顺序,newLine 1..m)。

退化路径同时跳过 pairMods:在没有找到任何对齐的情况下,del 段与 add 段
会被成对标成 mod,那不是「已判定为改写」。所以退化语义是无 context、无
mod,只有 del/add。10000×10000(中间改一行制造错位)实测 0.44–0.96ms,
原实现为 592ms/765MB;渲染侧既有 MAX_ROWS = 600 截断,无需新增提示文案。

测试新增规模用例(20000 行、kind 全为 del/add、两侧行号严格递增
1..10000、单次耗时 < 2000ms,阈值按慢 CI 放宽)与边界用例(恰好
MAX_LCS_CELLS 时仍走表路径)。

Refs #734
2026-10-05 01:29:00 +08:00

370 lines
15 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* The unified-diff adapter: git's own hunks (from parseUnifiedDiff) convert
* into the shared DiffRow segments — rewrite pairing applies inside git
* hunks exactly like session-op diffs, the unemitted context gaps between
* hunks become folds carrying their line ranges (expandable on demand via
* foldRowsFromContents), and the header stats (+n −m) count one rewritten
* line as one insertion plus one deletion (git's own accounting).
*/
import { describe, expect, it } from 'vitest'
import { parseUnifiedDiff, unifiedSegments, diffLines, pairMods, diffStats, untrackedFile, foldRowsFromContents, decodeGitPath, MAX_LCS_CELLS, type FoldSegment } from '../src/client/diff/rows.ts'
const twoHunks = [
'diff --git a/a.ts b/a.ts',
'--- a/a.ts',
'+++ b/a.ts',
'@@ -1,4 +1,4 @@',
' context',
'-old line',
'+new line',
' tail',
'@@ -20,3 +20,4 @@',
' far below',
'-gone',
'+kept',
'+added',
' tail2',
].join('\n')
describe('unifiedSegments', () => {
it('converts hunk lines to rows and pairs the rewrite as mod', () => {
const file = parseUnifiedDiff(twoHunks).files[0]!
const segments = unifiedSegments(file)
// hunk, gap fold (lines 4..19 hidden), hunk
expect(segments.map(s => s.kind)).toEqual(['hunk', 'fold', 'hunk'])
const first = segments[0]!
expect(first.kind === 'hunk' && first.rows.map(r => r.kind)).toEqual(['context', 'mod', 'mod', 'context'])
// The paired rows keep their own side's line number.
const modOld = first.kind === 'hunk' ? first.rows[1]! : undefined
const modNew = first.kind === 'hunk' ? first.rows[2]! : undefined
expect(modOld?.oldLine).toBe(2)
expect(modOld?.newLine).toBeUndefined()
expect(modNew?.oldLine).toBeUndefined()
expect(modNew?.newLine).toBe(2)
})
it('emits a non-expandable fold for the unemitted gap between hunks', () => {
const file = parseUnifiedDiff(twoHunks).files[0]!
const gap = unifiedSegments(file)[1]!
expect(gap.kind).toBe('fold')
if (gap.kind === 'fold') {
expect(gap.rows).toBeUndefined()
// First hunk ends at line 3 on both sides, second starts at 20 → gap 16.
expect(gap.count).toBe(16)
expect(gap.oldStart).toBe(4)
expect(gap.oldEnd).toBe(19)
expect(gap.newStart).toBe(4)
expect(gap.newEnd).toBe(19)
}
})
it('emits a leading fold when the first hunk starts below line 1', () => {
const diff = [
'diff --git a/a.ts b/a.ts',
'--- a/a.ts',
'+++ b/a.ts',
'@@ -10 +10 @@',
'-old',
'+new',
].join('\n')
const file = parseUnifiedDiff(diff).files[0]!
const segments = unifiedSegments(file)
expect(segments.map(s => s.kind)).toEqual(['fold', 'hunk'])
const lead = segments[0]!
if (lead.kind === 'fold') {
expect(lead.count).toBe(9)
expect(lead.rows).toBeUndefined()
}
})
it('carries the no-newline marker as a meta row', () => {
const diff = [
'diff --git a/a.ts b/a.ts',
'--- a/a.ts',
'+++ b/a.ts',
'@@ -1 +1 @@',
'-old',
'\\ No newline at end of file',
'+new',
].join('\n')
const file = parseUnifiedDiff(diff).files[0]!
const rows = unifiedSegments(file).flatMap(s => (s.kind === 'hunk' ? s.rows : []))
expect(rows.map(r => r.kind)).toEqual(['del', 'meta', 'add'])
expect(rows[1]).toMatchObject({ kind: 'meta', text: ' No newline at end of file' })
})
it('counts a rewritten line as one insertion plus one deletion', () => {
const file = parseUnifiedDiff(twoHunks).files[0]!
// Hunk 1 rewrites 1 line; hunk 2 rewrites 1 line and adds 1 outright.
// Each rewrite is 1 insertion + 1 deletion (git's accounting), so
// 3 added, 2 deleted — not the 5/4 that counting both mod rows gave.
expect(diffStats(unifiedSegments(file))).toEqual({ added: 3, deleted: 2 })
// A full-file addition (untracked fallback) counts every row once.
const untracked = untrackedFile('new.ts', 'a\nb\n')
expect(diffStats(unifiedSegments(untracked))).toEqual({ added: 2, deleted: 0 })
})
it('matches git numstat on a single-line rewrite', () => {
// `git diff --numstat` for this pair of contents prints `1 1 t.txt`;
// the chips used to print +2 −2 for it.
const file = parseUnifiedDiff([
'diff --git a/t.txt b/t.txt',
'--- a/t.txt',
'+++ b/t.txt',
'@@ -1,3 +1,3 @@',
' a',
'-b',
'+B',
' c',
].join('\n')).files[0]!
expect(diffStats(unifiedSegments(file))).toEqual({ added: 1, deleted: 1 })
})
it('keeps the session-op lane on the same accounting', () => {
// The changes tab's op summary takes the same function over diffLines
// rows, so a rewrite there is 1/1 as well (it used to read +2 −2).
expect(diffStats([{ kind: 'hunk', rows: diffLines('a\nb\nc\n', 'a\nB\nc\n') }])).toEqual({ added: 1, deleted: 1 })
expect(diffStats([{ kind: 'hunk', rows: diffLines('a\n', 'a\nb\n') }])).toEqual({ added: 1, deleted: 0 })
})
})
describe('pairMods', () => {
it('pairs only the overlapping del/add rows within one run', () => {
const rows = pairMods([
{ kind: 'context' as const, oldLine: 1, newLine: 1, text: 'x' },
{ kind: 'del' as const, oldLine: 2, text: 'a' },
{ kind: 'del' as const, oldLine: 3, text: 'b' },
{ kind: 'add' as const, newLine: 2, text: 'B' },
{ kind: 'add' as const, newLine: 3, text: 'c' },
{ kind: 'add' as const, newLine: 4, text: 'd' },
])
expect(rows.map(r => r.kind)).toEqual(['context', 'mod', 'mod', 'mod', 'mod', 'add'])
})
it('matches diffLines output (the LCS walk pairs rewrites the same way)', () => {
const rows = diffLines('hello world', 'hello dsh')
expect(rows.map(r => r.kind)).toEqual(['mod', 'mod'])
})
})
/**
* The dense LCS table is one JS number per cell at ~8 bytes each, so its cost
* is the input's line-count product: 5000×5000 cells measured 193MB and
* 10000×10000 cells 765MB. A session file op can hand `diffLines` a prior
* content that big (the op carries the whole previous file), so past
* MAX_LCS_CELLS the table is never built and the output degrades to
* all-deletions + all-additions.
*/
describe('diffLines scale guard', () => {
it('degrades a 10000×10000 pair to all-del + all-add, in order and fast', () => {
const oldLines = Array.from({ length: 10_000 }, (_, i) => `line ${String(i + 1)}`)
const newLines = oldLines.slice()
// One edit in the middle: the LCS walk would misalign every line after it.
newLines[4_999] = 'line 5000 changed'
const startedAt = performance.now()
const rows = diffLines(oldLines.join('\n'), newLines.join('\n'))
const elapsed = performance.now() - startedAt
// The guard is O(n+m); building the table here would be 100M cells.
expect(elapsed).toBeLessThan(2_000)
expect(rows).toHaveLength(20_000)
expect(rows.every(r => r.kind === 'del' || r.kind === 'add')).toBe(true)
const dels = rows.slice(0, 10_000)
const adds = rows.slice(10_000)
expect(dels.every(r => r.kind === 'del')).toBe(true)
expect(adds.every(r => r.kind === 'add')).toBe(true)
// Strictly increasing 1..10000 on each side, contiguous.
expect(dels.map(r => r.oldLine)).toEqual(Array.from({ length: 10_000 }, (_, i) => i + 1))
expect(adds.map(r => r.newLine)).toEqual(Array.from({ length: 10_000 }, (_, i) => i + 1))
expect(dels[4_999]).toMatchObject({ kind: 'del', oldLine: 5_000, text: 'line 5000' })
expect(adds[4_999]).toMatchObject({ kind: 'add', newLine: 5_000, text: 'line 5000 changed' })
})
it('still builds the table at exactly the cell limit (the bound is inclusive)', () => {
const side = Math.floor(Math.sqrt(MAX_LCS_CELLS))
expect(side * side).toBe(MAX_LCS_CELLS)
const text = Array.from({ length: side }, (_, i) => `line ${String(i + 1)}`).join('\n')
const rows = diffLines(text, text)
expect(rows).toHaveLength(side)
expect(rows.every(r => r.kind === 'context')).toBe(true)
expect(rows[0]).toMatchObject({ oldLine: 1, newLine: 1 })
expect(rows[side - 1]).toMatchObject({ oldLine: side, newLine: side })
})
})
describe('foldRowsFromContents', () => {
it('slices an aligned gap into context rows carrying both line numbers', () => {
const fold: FoldSegment = { kind: 'fold', count: 4, oldStart: 4, oldEnd: 7, newStart: 6, newEnd: 9 }
const rows = foldRowsFromContents(fold, 'a\nb\nc\nd\ne\nf\ng\nh\n', 'A\nB\nC\nD\nE\nF\nG\nH\nI\n')
expect(rows).toEqual([
{ kind: 'context', oldLine: 4, newLine: 6, text: 'd' },
{ kind: 'context', oldLine: 5, newLine: 7, text: 'e' },
{ kind: 'context', oldLine: 6, newLine: 8, text: 'f' },
{ kind: 'context', oldLine: 7, newLine: 9, text: 'g' },
])
})
it('keeps the old-side number alone when the new range cannot map (pure deletion gap)', () => {
const fold: FoldSegment = { kind: 'fold', count: 2, oldStart: 3, oldEnd: 4, newStart: 3, newEnd: 2 }
const rows = foldRowsFromContents(fold, 'a\nb\nc\nd\n', 'a\nb\n')
expect(rows).toEqual([
{ kind: 'context', oldLine: 3, text: 'c' },
{ kind: 'context', oldLine: 4, text: 'd' },
])
})
it('emits pure additions for new lines the old range never reached (pure addition gap)', () => {
const fold: FoldSegment = { kind: 'fold', count: 3, oldStart: 3, oldEnd: 2, newStart: 3, newEnd: 5 }
const rows = foldRowsFromContents(fold, 'a\nb\n', 'a\nb\nc\nd\ne\n')
expect(rows).toEqual([
{ kind: 'add', newLine: 3, text: 'c' },
{ kind: 'add', newLine: 4, text: 'd' },
{ kind: 'add', newLine: 5, text: 'e' },
])
})
it('keeps \\r endings verbatim like git context lines', () => {
const fold: FoldSegment = { kind: 'fold', count: 2, oldStart: 1, oldEnd: 2, newStart: 1, newEnd: 2 }
const rows = foldRowsFromContents(fold, 'a\r\nb\r\n', 'a\r\nb\r\n')
expect(rows.map(r => r.text)).toEqual(['a\r', 'b\r'])
})
it('returns no rows for empty contents', () => {
const fold: FoldSegment = { kind: 'fold', count: 5, oldStart: 1, oldEnd: 5, newStart: 1, newEnd: 5 }
expect(foldRowsFromContents(fold, '', '')).toEqual([])
})
it('clips line numbers to the actual content (no-newline overrun)', () => {
// A file without a trailing newline: the fold's ranges can overrun the
// actual line count by one; the slice stops at the real last line.
const fold: FoldSegment = { kind: 'fold', count: 3, oldStart: 2, oldEnd: 4, newStart: 2, newEnd: 4 }
const rows = foldRowsFromContents(fold, 'a\nb\nc', 'a\nb\nc')
expect(rows).toEqual([
{ kind: 'context', oldLine: 2, newLine: 2, text: 'b' },
{ kind: 'context', oldLine: 3, newLine: 3, text: 'c' },
])
})
})
/**
* Path recovery for the sections git emits WITHOUT `---`/`+++`: a binary
* file, a pure rename and a mode-only change all reach the renderer as a
* header whose path has to come from somewhere else — otherwise the row is a
* badge with no file name, and the neighbouring rows' names make it look like
* the badge is theirs.
*/
describe('parseUnifiedDiff paths', () => {
const first = (lines: readonly string[]): { oldPath: string; newPath: string; binary: boolean; hunks: number } => {
const file = parseUnifiedDiff(lines.join('\n')).files[0]!
return { oldPath: file.oldPath, newPath: file.newPath, binary: file.binary, hunks: file.hunks.length }
}
it('names a new binary file from its own Binary files line', () => {
expect(first([
'diff --git a/docs/20_D2_Tree_current.jpg b/docs/20_D2_Tree_current.jpg',
'new file mode 100644',
'index 0000000..1234567',
'Binary files /dev/null and b/docs/20_D2_Tree_current.jpg differ',
])).toEqual({
oldPath: '/dev/null',
newPath: 'b/docs/20_D2_Tree_current.jpg',
binary: true,
hunks: 0,
})
})
it('names a modified binary file on both sides', () => {
expect(first([
'diff --git a/docs/20.jpg b/docs/20.jpg',
'index 1234567..89abcde 100644',
'Binary files a/docs/20.jpg and b/docs/20.jpg differ',
])).toEqual({ oldPath: 'a/docs/20.jpg', newPath: 'b/docs/20.jpg', binary: true, hunks: 0 })
})
it('keeps a spaced binary path whole (the ` and `/` b/` split stays honest)', () => {
expect(first([
'diff --git a/docs/img one.png b/docs/img one.png',
'index 1234567..89abcde 100644',
'Binary files a/docs/img one.png and b/docs/img one.png differ',
])).toEqual({ oldPath: 'a/docs/img one.png', newPath: 'b/docs/img one.png', binary: true, hunks: 0 })
})
it('decodes a C-quoted binary header from a default-quotePath git', () => {
expect(first([
'diff --git "a/docs/\\344\\270\\255.jpg" "b/docs/\\344\\270\\255.jpg"',
'index 1234567..89abcde 100644',
'Binary files "a/docs/\\344\\270\\255.jpg" and "b/docs/\\344\\270\\255.jpg" differ',
])).toEqual({ oldPath: 'a/docs/中.jpg', newPath: 'b/docs/中.jpg', binary: true, hunks: 0 })
})
it('names a pure rename from its rename lines', () => {
expect(first([
'diff --git a/old/name.md b/new/name.md',
'similarity index 100%',
'rename from old/name.md',
'rename to new/name.md',
])).toEqual({ oldPath: 'old/name.md', newPath: 'new/name.md', binary: false, hunks: 0 })
})
it('names a mode-only change (no ---/+++ either)', () => {
expect(first([
'diff --git a/tools/run.sh b/tools/run.sh',
'old mode 100644',
'new mode 100755',
])).toEqual({ oldPath: 'a/tools/run.sh', newPath: 'b/tools/run.sh', binary: false, hunks: 0 })
})
it('names a mode-only change whose path contains a space', () => {
expect(first([
'diff --git a/tools/run me.sh b/tools/run me.sh',
'old mode 100644',
'new mode 100755',
])).toEqual({ oldPath: 'a/tools/run me.sh', newPath: 'b/tools/run me.sh', binary: false, hunks: 0 })
})
it('strips the TAB git appends to a spaced ---/+++ path', () => {
expect(first([
'diff --git a/docs/sp ace.md b/docs/sp ace.md',
'--- a/docs/sp ace.md\t',
'+++ b/docs/sp ace.md\t',
'@@ -1 +1 @@',
'-a',
'+b',
])).toEqual({ oldPath: 'a/docs/sp ace.md', newPath: 'b/docs/sp ace.md', binary: false, hunks: 1 })
})
it('lets ---/+++ win over the fallbacks (rename plus content change)', () => {
expect(first([
'diff --git a/old.md b/new.md',
'similarity index 90%',
'rename from old.md',
'rename to new.md',
'--- a/old.md',
'+++ b/new.md',
'@@ -1 +1 @@',
'-a',
'+b',
])).toEqual({ oldPath: 'a/old.md', newPath: 'b/new.md', binary: false, hunks: 1 })
})
})
describe('decodeGitPath', () => {
it('decodes git octal escapes as UTF-8 bytes', () => {
expect(decodeGitPath('"b/docs/\\346\\226\\207.md"')).toBe('b/docs/文.md')
})
it('decodes the single-character escapes', () => {
expect(decodeGitPath('"a/tab\\there.md"')).toBe('a/tab\there.md')
expect(decodeGitPath('"a/quote\\"name.md"')).toBe('a/quote"name.md')
expect(decodeGitPath('"a/back\\\\slash.md"')).toBe('a/back\\slash.md')
})
it('leaves an unquoted path alone', () => {
expect(decodeGitPath('a/中文 空格.md')).toBe('a/中文 空格.md')
expect(decodeGitPath('/dev/null')).toBe('/dev/null')
})
it('leaves a mismatched quote alone', () => {
expect(decodeGitPath('"mismatched')).toBe('"mismatched')
})
})