上一个提交(bdf3c33)过了一轮「代码精简」审查(复用 / 简化 / 效率 / 修在哪一层
四个角度,四个只读子代理并发),这里只落地行为完全不变的那批。
- 同一个「两侧不能相加」的理由原先在 5 处源码注释与 4 处测试注释里各写一遍,
措辞已经开始分叉。完整理由只留在权威位置 `GitStatusEntry` 的字段文档;
`status()`、`api.ts` 的镜像类型、`ChangeFile.counts`、GitLens 的调用处与三处
测试注释压成一行并指回去。
- 用例夹具:`change-tree-counts.spec.ts` 的 `entry()` 从「传一个 sides 对象」改成
「传一个读数 + 一个侧」,收掉 13 处 `{ unstaged: … }` 外壳;那个 `side` 用生产
侧导出的 `ChangeSide`——它此前导出了却没有任何消费者。
- 同文件「两侧不相加」的用例原先两侧读数对称(都是 1/1),两条断言的期望值逐字
相同:把两侧读反也照样绿。夹具改成不对称(staged 1/1、unstaged 5/0),「相加」
与「读错边」两种错误现在都会失败。
- `git.spec.ts` 里第 4 份「建仓引导」提成局部 `mkRepo(files, config)`,与紧邻用例
共用(少一次重复的 4 个 git 进程)。
- `git-view-worktree.spec.tsx` 的 `mountTree` 形参原先手写镜像了 `GitStatusEntry`
的四个成员,改回 `GitStatusEntry[]`(同文件上方本就 import 了它)。
按「超出本次 diff / 会改变行为」跳过的四条,留档:GitLens 里 band↔侧的六处穿线
(要重写两个本 diff 没动过、近乎逐字重复的渲染块)、「这行会不会印数字」的两处
判断(合一就必须让 0/0 行不再渲染那个空 span,那是改输出)、快照身份的两份实现
(造键 vs 判等,本就是两件事)、服务端 `git.suggest-message` 又推一遍「属于哪
一侧」(本 diff 之外,且与本次 bug 同类)。另:`GitStatusEntry` 在 `git.ts` 与
`api.ts` 的两份声明是刻意的镜像(浏览器侧声明图不能碰 `node:*`),不合并。
验证:`tsc --noEmit` 通过;eslint 对 7 个改动文件无输出;受影响的 6 个测试文件
173 条全绿(含真起 git 进程的用例);完整单测 189 文件 / 2234 用例,失败仍是与
改动前同一批 8 条环境性失败(3 条 Windows 建符号链接 EPERM、1 条 `tar` 在 Windows
输出 CRLF、4 条中文语言标签),无新增。
117 lines
6.1 KiB
TypeScript
117 lines
6.1 KiB
TypeScript
/**
|
||
* Line counts in the change tree (#131).
|
||
*
|
||
* The status answer carries each path's added/deleted lines PER SIDE; the tree
|
||
* is built once per band and keeps that band's reading on its leaves, so a row
|
||
* prints its own numbers and a band's header total is the sum of exactly those
|
||
* leaves. Binary rows and rows without counts (untracked files — git diffs
|
||
* nothing for them) contribute NOTHING: the alternative, counting them as 0,
|
||
* would make the header disagree with the numbers printed under it.
|
||
*/
|
||
import { describe, expect, it } from 'vitest'
|
||
import { buildChangeTree, sumLineCounts, type ChangeSide } from '../src/client/changes/change-tree.ts'
|
||
import type { GitLineCounts, GitStatusEntry } from '../src/client/api.ts'
|
||
|
||
/** One status entry carrying a reading on ONE side (the worktree by default).
|
||
* `xy` decides which bands the entry lands in; each band's tree reads its own
|
||
* side off the entry. */
|
||
function entry(path: string, xy: string, counts?: GitLineCounts, side: ChangeSide = 'unstaged'): GitStatusEntry {
|
||
return counts === undefined ? { path, xy } : { path, xy, [side]: counts }
|
||
}
|
||
|
||
describe('change tree line counts', () => {
|
||
it('keeps each entry counts on its own leaf', () => {
|
||
const tree = buildChangeTree([
|
||
entry('src/a.ts', ' M', { additions: 4, deletions: 2 }),
|
||
entry('src/deep/b.ts', ' M', { binary: true }),
|
||
entry('new.txt', '??'),
|
||
], 'unstaged')
|
||
const src = tree.find(node => node.kind === 'dir' && node.path === 'src')!
|
||
expect(src.kind).toBe('dir')
|
||
const leaves = src.kind === 'dir' ? src.children : []
|
||
expect(leaves.find(node => node.kind === 'file' && node.path === 'src/a.ts'))
|
||
.toMatchObject({ counts: { additions: 4, deletions: 2 } })
|
||
// The binary leaf keeps the binary marker, never a count of 0.
|
||
const nested = leaves.find(node => node.kind === 'dir')!
|
||
const nestedLeaves = nested.kind === 'dir' ? nested.children : []
|
||
expect(nestedLeaves[0]).toMatchObject({ path: 'src/deep/b.ts', counts: { binary: true } })
|
||
// The untracked leaf has NO counts property at all.
|
||
const untracked = tree.find(node => node.kind === 'file' && node.path === 'new.txt')!
|
||
expect(untracked.kind === 'file' && 'counts' in untracked).toBe(false)
|
||
})
|
||
|
||
it('reads ONLY the band\'s own side off a path that changed on both sides', () => {
|
||
// The double-count regression (#131): the two diffs have different bases,
|
||
// so `MM` carries two readings. Each band's tree must print its own, not
|
||
// the sum — summing reported the same edited line as `+2 −2` in BOTH bands.
|
||
// The two readings differ, so a band reading the WRONG side fails too.
|
||
const both: GitStatusEntry = {
|
||
path: 'a.txt',
|
||
xy: 'MM',
|
||
staged: { additions: 1, deletions: 1 },
|
||
unstaged: { additions: 5, deletions: 0 },
|
||
}
|
||
expect(sumLineCounts(buildChangeTree([both], 'staged')))
|
||
.toEqual({ additions: 1, deletions: 1, files: 1 })
|
||
expect(sumLineCounts(buildChangeTree([both], 'unstaged')))
|
||
.toEqual({ additions: 5, deletions: 0, files: 1 })
|
||
// A side with no reading stays count-less in that band only.
|
||
const stagedOnly = entry('b.txt', 'M ', { additions: 3, deletions: 0 }, 'staged')
|
||
expect(buildChangeTree([stagedOnly], 'unstaged')[0]).not.toHaveProperty('counts')
|
||
expect(buildChangeTree([stagedOnly], 'staged')[0]).toMatchObject({ counts: { additions: 3, deletions: 0 } })
|
||
})
|
||
|
||
it('sums a group to exactly the numbers its members carry', () => {
|
||
const tree = buildChangeTree([
|
||
entry('src/a.ts', ' M', { additions: 4, deletions: 2 }),
|
||
entry('src/b.ts', ' M', { additions: 1, deletions: 0 }),
|
||
entry('docs/c.md', ' M', { additions: 3, deletions: 3 }),
|
||
], 'unstaged')
|
||
expect(sumLineCounts(tree)).toEqual({ additions: 8, deletions: 5, files: 3 })
|
||
})
|
||
|
||
it('counts only the rows that HAVE numbers (binary and untracked add nothing)', () => {
|
||
const tree = buildChangeTree([
|
||
entry('src/a.ts', ' M', { additions: 4, deletions: 2 }),
|
||
entry('blob.bin', ' M', { binary: true }),
|
||
entry('new.txt', '??'),
|
||
], 'unstaged')
|
||
// 4/2 — not 4/2 plus a fabricated zero from either count-less row.
|
||
expect(sumLineCounts(tree)).toEqual({ additions: 4, deletions: 2, files: 1 })
|
||
// A group with nothing to sum reports zero files, so its header stays bare.
|
||
expect(sumLineCounts(buildChangeTree([
|
||
entry('blob.bin', ' M', { binary: true }), entry('n.txt', '??'),
|
||
], 'unstaged'))).toEqual({ additions: 0, deletions: 0, files: 0 })
|
||
})
|
||
|
||
it('counts only the rows that PRINT numbers: a 0/0 rename has none to sum (#131)', () => {
|
||
// A rename with no content change is `0 0` in numstat (a mode-only change
|
||
// reads the same way): its row prints nothing at all, so it must not make
|
||
// a group's total claim it contributed numbers — otherwise a group holding
|
||
// nothing but renames renders an empty `+N −M` cluster in its header.
|
||
expect(sumLineCounts(buildChangeTree([
|
||
entry('renamed.ts', 'R ', { additions: 0, deletions: 0 }, 'staged'),
|
||
], 'staged'))).toEqual({ additions: 0, deletions: 0, files: 0 })
|
||
expect(sumLineCounts(buildChangeTree([
|
||
entry('mode-only.ts', ' M', { additions: 0, deletions: 0 }),
|
||
], 'unstaged'))).toEqual({ additions: 0, deletions: 0, files: 0 })
|
||
// A rename that DID move lines keeps contributing them...
|
||
expect(sumLineCounts(buildChangeTree([
|
||
entry('edited.ts', 'R ', { additions: 2, deletions: 1 }, 'staged'),
|
||
entry('renamed.ts', 'R ', { additions: 0, deletions: 0 }, 'staged'),
|
||
], 'staged'))).toEqual({ additions: 2, deletions: 1, files: 1 })
|
||
})
|
||
|
||
it('counts every member regardless of how deep the directory chain folds', () => {
|
||
// One compressed chain row stands for `src/client/changes`; the total must
|
||
// still be the sum of the leaves under it.
|
||
const tree = buildChangeTree([
|
||
entry('src/client/changes/a.ts', ' M', { additions: 2, deletions: 1 }),
|
||
entry('src/client/changes/b.ts', ' M', { additions: 5, deletions: 0 }),
|
||
], 'unstaged')
|
||
expect(tree).toHaveLength(1)
|
||
expect(tree[0]).toMatchObject({ kind: 'dir', name: 'src/client/changes' })
|
||
expect(sumLineCounts(tree)).toEqual({ additions: 7, deletions: 1, files: 2 })
|
||
})
|
||
})
|