Files
apes-sidebar/tests/changes-ops.spec.ts
T
Menghuan1918 95270afa2f fix(diff): 超长单行 diff 不再卡死(线性高亮 + 有界预览)
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 长任务。
2026-11-12 01:00:16 +08:00

419 lines
19 KiB
TypeScript

/**
* Changes tab data layer: event-log folding into file operations, the
* directory tree the Git lens renders its groups through, and a sanity pass
* over the shared diff/highlight engines (their full behavior suites live in
* the standalone dsh-file-trace plugin).
*/
import { describe, expect, it } from 'vitest'
import { createToolResultMessage, ToolCallId } from '@deepseek-ai/dsh-llm'
import { extractFileOps, knownContentBefore, parseReadContent, parseReadLines } from '../src/client/changes/ops.ts'
import { buildChangeTree, type ChangeNode } from '../src/client/changes/change-tree.ts'
import { diffLines, buildDiffSegments, coalesceInline, diffInline } from '../src/client/diff/rows.ts'
import { langOfPath, scanLine } from '../src/client/diff/highlight.ts'
import type { GitStatusEntry } from '../src/client/api.ts'
import type { SidebarSessionEvent } from '../src/context-types.ts'
/** One synthetic session event. */
function ev(type: string, seq: number, time: number, data: Record<string, unknown>): SidebarSessionEvent {
return { type, seq, time, data }
}
/** A tool/call event. */
function call(seq: number, name: string, callId: string, args: unknown, time = seq): SidebarSessionEvent {
return ev('tool/call', seq, time, { name, callId, arguments: JSON.stringify(args) })
}
/** A tool/result event carrying one first-class tool-role message (the 0.1.7
* shape: result blocks and `isError` at the message's own top level). */
function result(seq: number, callId: string, text: string, isError = false, time = seq): SidebarSessionEvent {
return ev('tool/result', seq, time, {
message: {
role: 'tool',
source: { kind: 'tool', callId },
toolCallId: callId,
isError,
content: [{ type: 'text', text }],
},
})
}
/** The retired 0.1.6 shape: a role-'user' message wrapping the result in one
* `tool-result` content block. Historical logs still carry it. */
function legacyResult(seq: number, callId: string, text: string, isError = false, time = seq): SidebarSessionEvent {
return ev('tool/result', seq, time, {
message: {
role: 'user',
source: { kind: 'tool', callId },
content: [{ type: 'tool-result', isError, content: [{ type: 'text', text }] }],
},
})
}
describe('parseReadContent', () => {
it('strips the envelope and line-number prefixes but keeps blank lines', () => {
const raw = [
'<path>notes.md</path>',
'<type>file</type>',
'(Showing lines 1 to 4)',
'<content>',
'1: # Title',
'2: ',
'3: para one',
'4: ',
'</content>',
].join('\n')
expect(parseReadContent(raw)).toBe('# Title\n\npara one')
})
it('returns the raw text unchanged when no <content> section is present', () => {
expect(parseReadContent('plain text')).toBe('plain text')
})
})
describe('extractFileOps', () => {
it('resolves paths before finding earlier content', () => {
const ops = extractFileOps([
call(1, 'write', 'first', { file_path: 'src/package.json', content: 'first' }),
call(2, 'edit', 'second', { path: '/repo/src/package.json', old_string: 'first', new_string: 'second' }),
call(3, 'read', 'third', { filePath: 'tests/package.json' }),
], '/repo')
// The same file reached by a relative and an absolute spelling resolves to
// ONE path — the row text the lens prints, and the key the earlier-content
// lookup uses.
expect(ops.map(op => op.path).sort()).toEqual([
'/repo/src/package.json', '/repo/src/package.json', '/repo/tests/package.json',
])
expect(knownContentBefore(ops, '/repo/src/package.json', ops.find(op => op.callId === 'second')!)).toBe('first')
})
it('resolves Windows relative paths and preserves absolute drive and UNC paths', () => {
const ops = extractFileOps([
call(1, 'read', 'relative', { file_path: 'src\\package.json' }),
call(2, 'read', 'drive', { file_path: 'D:\\other\\package.json' }),
call(3, 'read', 'network', { file_path: '\\\\server\\share\\package.json' }),
], 'C:\\repo')
expect(ops.find(op => op.callId === 'relative')?.path).toBe('C:\\repo\\src\\package.json')
expect(ops.find(op => op.callId === 'drive')?.path).toBe('D:\\other\\package.json')
expect(ops.find(op => op.callId === 'network')?.path).toBe('\\\\server\\share\\package.json')
})
it('seeds running ops from tool/call and settles them from tool/result', () => {
const ops = extractFileOps([
call(1, 'write', 'w1', { file_path: 'a.ts', content: 'body' }),
result(2, 'w1', '<path>a.ts</path>\n<type>file</type>\n<content>\nbody\n</content>'),
])
expect(ops).toHaveLength(1)
expect(ops[0]!.running).toBe(false)
expect(ops[0]!.isError).toBe(false)
expect(ops[0]!.content).toBe('body')
})
it('captures the error text of an errored result for every kind', () => {
const ops = extractFileOps([
call(1, 'write', 'w', { file_path: 'a', content: 'x' }),
result(2, 'w', '写入失败:目标只读', true),
call(3, 'read', 'r', { file_path: 'b' }),
result(4, 'r', 'cannot read "b": not found', true),
])
expect(ops.every(op => op.isError && op.errorText !== undefined)).toBe(true)
expect(ops.find(op => op.kind === 'read')?.errorText).toBe('cannot read "b": not found')
})
it('keeps a call without result as running, and reads capture their content', () => {
const ops = extractFileOps([
call(1, 'read', 'r1', { file_path: 'a.ts' }),
result(2, 'r1', '<path>a.ts</path>\n<type>file</type>\n<content>\n1: hello\n\n(End of file - total 1 lines)\n</content>'),
call(3, 'edit', 'e1', { file_path: 'c.ts', old_string: 'x', new_string: 'y' }),
])
const read = ops.find(op => op.callId === 'r1')
expect(read?.running).toBe(false)
expect(read?.read).toContain('1: hello')
const edit = ops.find(op => op.callId === 'e1')
expect(edit?.running).toBe(true)
expect(edit?.edit).toEqual({ oldString: 'x', newString: 'y' })
})
it('ignores non-file tools and unrelated results, newest first', () => {
const ops = extractFileOps([
call(1, 'pwsh', 'p', { command: 'ls' }),
result(2, 'unknown-call', 'orphan'),
call(3, 'read', 'r', { file_path: 'z.ts' }),
])
expect(ops.map(op => op.callId)).toEqual(['r'])
})
it('reads the tool/result message dsh-llm actually produces (producer round-trip)', () => {
const message = createToolResultMessage({
callId: ToolCallId('r'),
content: [{ type: 'text', text: 'produced file body' }],
isError: false,
})
const ops = extractFileOps([
call(1, 'read', 'r', { file_path: 'a.ts' }),
ev('tool/result', 2, 2, { message: message as unknown as Record<string, unknown> }),
])
expect(ops[0]!.running).toBe(false)
expect(ops[0]!.read).toBe('produced file body')
})
it('reads a LEGACY 0.1.6-era tool-result wrapper (historical logs keep that shape)', () => {
const ops = extractFileOps([
// No content in the model's arguments, so the settled result supplies it.
call(1, 'write', 'w', { file_path: 'a' }),
legacyResult(2, 'w', 'legacy write detail'),
call(3, 'read', 'r', { file_path: 'b' }),
legacyResult(4, 'r', 'cannot read "b": not found', true),
])
expect(ops.find(op => op.kind === 'write')?.content).toBe('legacy write detail')
expect(ops.find(op => op.kind === 'read')?.errorText).toBe('cannot read "b": not found')
})
it('records PTC file calls by subCallId and settles them from dispatch results', () => {
const rootCallId = 'root'
const write = {
rootCallId, parentCallId: rootCallId, subCallId: 'root:ptc:1', name: 'write',
arguments: { file_path: 'notes.txt', content: 'PTC content' },
}
const read = {
rootCallId, parentCallId: rootCallId, subCallId: 'root:ptc:2', name: 'read',
arguments: { file_path: 'notes.txt' },
}
const ops = extractFileOps([
call(1, 'run_code', rootCallId, { code: 'write(); read()' }),
ev('tool/ptc-dispatch-start', 2, 20, write),
ev('tool/ptc-dispatch-start', 3, 30, read),
ev('tool/ptc-dispatch', 4, 40, { ...read, isError: false, content: [{ type: 'text', text: '<content>\n1: PTC content\n</content>' }] }),
ev('tool/ptc-dispatch', 5, 50, { ...write, isError: false, content: [{ type: 'text', text: 'written' }] }),
result(6, rootCallId, 'run_code completed'),
])
expect(ops.map(op => op.callId)).toEqual(['root:ptc:2', 'root:ptc:1'])
expect(ops[0]).toMatchObject({ kind: 'read', path: 'notes.txt', time: 30, running: false, isError: false, read: '<content>\n1: PTC content\n</content>' })
expect(ops[1]).toMatchObject({ kind: 'write', path: 'notes.txt', time: 20, running: false, isError: false, content: 'PTC content' })
})
it('keeps an unfinished PTC call running and recovers a complete dispatch without its start', () => {
const rootCallId = 'root'
const edit = {
rootCallId, parentCallId: rootCallId, subCallId: 'root:ptc:1', name: 'edit',
arguments: { file_path: 'draft.txt', old_string: 'before', new_string: 'after' },
}
const failedWrite = {
rootCallId, parentCallId: rootCallId, subCallId: 'root:ptc:2', name: 'write',
arguments: { file_path: 'readonly.txt', content: 'new' },
isError: true, content: [{ type: 'text', text: 'permission denied' }],
}
const ops = extractFileOps([
ev('tool/ptc-dispatch-start', 1, 10, edit),
ev('tool/ptc-dispatch', 2, 20, failedWrite),
])
expect(ops.find(op => op.callId === edit.subCallId)).toMatchObject({ kind: 'edit', running: true, edit: { oldString: 'before', newString: 'after' } })
expect(ops.find(op => op.callId === failedWrite.subCallId)).toMatchObject({ kind: 'write', time: 20, running: false, isError: true, errorText: 'permission denied' })
})
it('settles a PTC edit with its replacement payload', () => {
const dispatch = {
rootCallId: 'root', parentCallId: 'root', subCallId: 'root:ptc:1', name: 'edit',
arguments: { file_path: 'draft.txt', old_string: 'before', new_string: 'after' },
isError: false, content: [{ type: 'text', text: 'edited' }],
}
expect(extractFileOps([ev('tool/ptc-dispatch', 1, 10, dispatch)])).toMatchObject([
{ callId: 'root:ptc:1', kind: 'edit', path: 'draft.txt', running: false, isError: false, edit: { oldString: 'before', newString: 'after' } },
])
})
it('keeps operations newest-first and recovers prior write content', () => {
const ops = extractFileOps([
call(1, 'write', 'w1', { file_path: 'a.ts', content: 'first' }),
call(2, 'write', 'w2', { file_path: 'a.ts', content: 'second' }),
call(3, 'write', 'w3', { file_path: 'b.ts', content: 'x' }),
])
expect(ops.map(op => op.callId)).toEqual(['w3', 'w2', 'w1'])
expect(knownContentBefore(ops, 'a.ts', ops.find(op => op.callId === 'w2')!)).toBe('first')
})
})
describe('parseReadLines', () => {
it('recovers real line numbers from the read envelope', () => {
const lines = parseReadLines('<path>a.ts</path>\n<type>file</type>\n<content>\n10: x\n11: y\n\n(Showing lines 10-11 of 20. Use offset=12 to continue.)\n</content>')
expect(lines).toEqual([{ line: 10, text: 'x' }, { line: 11, text: 'y' }])
})
})
describe('change tree (the Git lens reads its groups through it)', () => {
/** One `git status` row from a path and its porcelain code. */
const entry = (path: string, xy = ' M'): GitStatusEntry => ({ path, xy })
/** The tree of one band. These cases exercise the SHAPE only, so the side is
* fixed; the per-side readings have their own suite. */
const band = (entries: readonly GitStatusEntry[]): ChangeNode[] => buildChangeTree(entries, 'unstaged')
/** A compact, assertion-friendly projection of one tree. */
const shape = (nodes: readonly ChangeNode[]): unknown[] => nodes.map(node => node.kind === 'dir'
? { dir: node.name, path: node.path, changes: node.changes, children: shape(node.children) }
: { file: node.name, path: node.path, letter: node.status.letter, tone: node.status.tone })
it('keeps a single root-level file as one leaf', () => {
expect(shape(band([entry('readme.md')]))).toEqual([
{ file: 'readme.md', path: 'readme.md', letter: 'M', tone: 'modified' },
])
})
it('nests a deep path and compresses its single-child chain into one row', () => {
const tree = band([entry('src/client/changes/a.ts')])
expect(shape(tree)).toEqual([
{
dir: 'src/client/changes',
path: 'src/client/changes',
changes: 1,
children: [{ file: 'a.ts', path: 'src/client/changes/a.ts', letter: 'M', tone: 'modified' }],
},
])
})
it('stops compressing where the path branches or a directory holds its own file', () => {
// Two child directories: 'src' keeps its own row.
expect(shape(band([entry('src/a/x.ts'), entry('src/b/y.ts')]))).toEqual([
{
dir: 'src',
path: 'src',
changes: 2,
children: [
{ dir: 'a', path: 'src/a', changes: 1, children: [{ file: 'x.ts', path: 'src/a/x.ts', letter: 'M', tone: 'modified' }] },
{ dir: 'b', path: 'src/b', changes: 1, children: [{ file: 'y.ts', path: 'src/b/y.ts', letter: 'M', tone: 'modified' }] },
],
},
])
// A file AT this level: 'a' keeps its own row (and its file leads the child).
expect(shape(band([entry('a/x.ts'), entry('a/b/y.ts')]))).toEqual([
{
dir: 'a',
path: 'a',
changes: 2,
children: [
{ dir: 'b', path: 'a/b', changes: 1, children: [{ file: 'y.ts', path: 'a/b/y.ts', letter: 'M', tone: 'modified' }] },
{ file: 'x.ts', path: 'a/x.ts', letter: 'M', tone: 'modified' },
],
},
])
})
it('orders directories before files, each by name case-insensitively', () => {
const tree = band([
entry('Zed.ts'), entry('B.ts'), entry('a.ts'), entry('zdir/f.ts'), entry('Adir/f.ts'),
])
expect(shape(tree).map(node => (node as { dir?: string; file?: string }).dir ?? (node as { file: string }).file))
.toEqual(['Adir', 'zdir', 'a.ts', 'B.ts', 'Zed.ts'])
})
it('mixes root-level files with nested paths and keeps same-named files apart', () => {
expect(shape(band([
entry('b/index.ts'), entry('index.ts'), entry('a/index.ts'),
]))).toEqual([
{ dir: 'a', path: 'a', changes: 1, children: [{ file: 'index.ts', path: 'a/index.ts', letter: 'M', tone: 'modified' }] },
{ dir: 'b', path: 'b', changes: 1, children: [{ file: 'index.ts', path: 'b/index.ts', letter: 'M', tone: 'modified' }] },
{ file: 'index.ts', path: 'index.ts', letter: 'M', tone: 'modified' },
])
})
it('carries every file porcelain status and counts the files under each row', () => {
const tree = band([
entry('src/a.ts', ' M'), entry('src/new.ts', '??'), entry('src/gone.ts', ' D'), entry('src/kept.ts', ' '),
])
expect(shape(tree)).toEqual([
{
dir: 'src',
path: 'src',
changes: 3,
children: [
{ file: 'a.ts', path: 'src/a.ts', letter: 'M', tone: 'modified' },
{ file: 'gone.ts', path: 'src/gone.ts', letter: 'D', tone: 'deleted' },
{ file: 'new.ts', path: 'src/new.ts', letter: 'U', tone: 'untracked' },
],
},
])
})
it('pins the collation, so the order cannot move with the runtime locale', () => {
// Mixed case + accents + non-letters: the comparator runs an explicit
// 'en' base collation and breaks ties by code point. A bare
// `localeCompare(other)` would follow the machine's ICU default and could
// reorder the same change list on another machine.
const names = ['b.ts', 'A.ts', 'ä.ts', 'Z.ts', 'a.ts', 'Ä.ts', '_x.ts', '1.ts']
const order = (list: readonly string[]): string[] =>
band(list.map(path => entry(path))).map(node => node.name)
expect(order(names)).toEqual(['_x.ts', '1.ts', 'A.ts', 'a.ts', 'Ä.ts', 'ä.ts', 'b.ts', 'Z.ts'])
// …and it is a TOTAL order: reversing the input cannot move a row.
expect(order([...names].reverse())).toEqual(order(names))
const dirs = ['Zdir/f.ts', 'adir/f.ts', 'Ädir/f.ts', '_dir/f.ts']
const dirOrder = (list: readonly string[]): string[] =>
band(list.map(path => entry(path))).map(node => node.name)
expect(dirOrder(dirs)).toEqual(['_dir', 'adir', 'Ädir', 'Zdir'])
expect(dirOrder([...dirs].reverse())).toEqual(dirOrder(dirs))
// Why the pin is load-bearing: under Swedish collation 'ä' sorts AFTER
// 'z', so an unpinned comparator would order this very list differently on
// a machine whose runtime locale happens to be Swedish.
expect(['z.ts', 'ä.ts'].sort((left, right) => left.localeCompare(right, 'sv', { sensitivity: 'base' })))
.toEqual(['z.ts', 'ä.ts'])
})
it('returns no nodes for an empty or all-clean list', () => {
expect(band([])).toEqual([])
expect(band([entry('a.ts', ' '), entry('b.ts', '!!')])).toEqual([])
})
it('counts a repeated path once', () => {
expect(shape(band([entry('a.ts'), entry('a.ts', '??')]))).toEqual([
{ file: 'a.ts', path: 'a.ts', letter: 'M', tone: 'modified' },
])
})
})
describe('ported diff/highlight engines (sanity)', () => {
it('diffLines pairs a rewrite as mod and folds context into hunks', () => {
const rows = diffLines('hello world', 'hello dsh')
expect(rows.map(r => r.kind)).toEqual(['mod', 'mod'])
const before = Array.from({ length: 6 }, (_, i) => ({ kind: 'context' as const, oldLine: i + 1, newLine: i + 1, text: 'c' + String(i) }))
const change = [{ kind: 'mod' as const, oldLine: 7, newLine: 7, text: 'X' }]
const after = Array.from({ length: 8 }, (_, i) => ({ kind: 'context' as const, oldLine: i + 8, newLine: i + 8, text: 'c' + String(i + 8) }))
const segments = buildDiffSegments([...before, ...change, ...after], 3)
expect(segments.map(s => s.kind)).toEqual(['fold', 'hunk', 'fold'])
})
it('coalesces the ported prefix/suffix inline diff into runs', () => {
const { old: oldSide } = diffInline('hello world', 'hello dsh')
expect(coalesceInline(oldSide)).toEqual([
{ text: 'hello ', changed: false },
{ text: 'world', changed: true },
])
})
it('highlights keywords and threads block-comment state', () => {
expect(langOfPath('a.cpp')).toBe('cpp')
const open = scanLine('/* header', 'cpp')
expect(open.inBlock).toBe(true)
const mid = scanLine(' * interior note', 'cpp', true)
expect(mid.tokens).toEqual([{ text: ' * interior note', type: 'comment' }])
const code = scanLine('int main() { return 42; } // done', 'cpp')
expect(code.tokens).toContainEqual({ text: 'int', type: 'keyword' })
expect(code.tokens).toContainEqual({ text: '42', type: 'number' })
expect(code.tokens).toContainEqual({ text: '// done', type: 'comment' })
})
it('scans a minified megabyte line linearly and keeps block state', () => {
// The whitespace probe used to scan ahead from every character, making a
// minified line quadratic (618KB measured 1.9s; 1MB would be ~5s). Linear
// scanning must stay well under the ceiling.
const minified = '{' + '"k":1,'.repeat(150_000) + '}'
const startedAt = performance.now()
const scanned = scanLine(minified, 'json')
expect(performance.now() - startedAt).toBeLessThan(2_000)
// Every character is still covered: no early bail-out for long lines.
expect(scanned.tokens.map(token => token.text).join('')).toBe(minified)
// A long line that opens a block comment still threads the state onward.
const open = scanLine('/*' + 'x'.repeat(100_000), 'cpp')
expect(open.inBlock).toBe(true)
})
})