Files
apes-sidebar/tests/changes-ops.spec.ts
T
Menghuan1918 b011be1e98 feat(sidebar): 打开方式双源并存 + 多选压缩下载 + 文件变动层级树
用户追加三条需求,全部落地:

1) 打开方式 = 宿主探测应用 + 插件自研 open-with(并存)
- 逐字恢复 src/client/open-with.ts / open-with-settings.tsx / plugin-settings.ts /
  src/open-external.ts / open.external 路由 / api.openExternal / editor descriptor
  的 settings.render,以及 20 个 openWith* 词条(19 份词典从 main 取回原文)
- 恢复 menu-flip.ts + layout.css 子菜单翻转规则(子菜单回来了)
- FileTree 菜单段(无标题行):宿主行 → 分隔线 → 插件 pinned + 「在应用中打开 >」
  子菜单(图钉 / SSH 后缀原样)→ 分隔线 → 宿主 reveal;宿主可用时过滤插件
  explorer 目标,宿主不可用时插件行照旧(SSH 远端场景保持可用)
- 死键清理:菜单去掉标题行后 openInApp 词条无引用,已从 21 份词典删除

2) 文件页多选右键「压缩并打包下载」
- 新增 src/zip.ts(自研 ZIP:方法 0/8 按收益二选一、UTF-8 bit 11、目录条目、
  自实现 CRC32、无第三方依赖)+ src/archive-route.ts(名净化 + 递归收集)+
  GET /sidebar/archive(同一道 workspace fence、目录递归跳过符号链接、
  10000 条目 / 256 MiB 上限)+ api.archiveUrl
- FileTree:多选 ≥2 项或单选目录时出现;fetch → blob → objectURL 下载,
  路由错误(超限 / fence / 非 GET)以 zipFailed 显示在错误条
- 修掉真实缺陷:central directory 只写 MS-DOS 位而 version-made-by 声称 Unix,
  Info-ZIP 会把每个文件解成 mode 000;改为 Unix 语义(0o100644 * 0x10000,
  << 16 会溢出 int32)。系统 unzip 复验:mode 644/755、unzip -t 无错

3) 文件变动页层级树 + 视觉打磨
- 新增 src/client/changes/change-tree.ts:单子目录链压缩成一行、目录先于文件、
  大小写不敏感排序、文件带 GitFileStatus、目录带下级变更数
- GitLens 两组各自渲染树:折叠状态按「组+路径」独立、默认展开;分组头吸顶 +
  计数胶囊;Stage all/Unstage all 改为带 aria-label 的 IconButton;选中 =
  interactive-bg-active + 2px 内嵌强调条;12px/级缩进 + l1 参考线;
  历史行 hash 徽标 / ref 胶囊 / 作者·时间右对齐

验证:typecheck / lint / test(134 文件 1422 通过)/ build 全绿;
DSH_CMD='npx -y --package @deepseek-ai/dsh@0.1.7-rc.1 dsh' pnpm test:mount 7/7 通过;
真实宿主截图确认三条需求(多选+双源菜单+zip 项、变动页树形层级)
2026-09-28 18:08:21 +08:00

296 lines
13 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, groupByFile, 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('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('groups by file 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([...groupByFile(ops).keys()]).toEqual(['b.ts', 'a.ts'])
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 })
/** 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(buildChangeTree([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 = buildChangeTree([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(buildChangeTree([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(buildChangeTree([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 = buildChangeTree([
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(buildChangeTree([
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 = buildChangeTree([
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('returns no nodes for an empty or all-clean list', () => {
expect(buildChangeTree([])).toEqual([])
expect(buildChangeTree([entry('a.ts', ' '), entry('b.ts', '!!')])).toEqual([])
})
it('counts a repeated path once', () => {
expect(shape(buildChangeTree([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' })
})
})