Files
apes-sidebar/tests/change-tree-counts.spec.ts
T
Menghuan1918 504d428b79 fix(git-lens): 纯重命名不再计入分组合计,计数簇改用字体令牌(#131)
(a) numstat 为 0/0 的条目(纯重命名、仅改权限)行上什么都不打印,但
sumLineCounts 仍把它计进 files,于是「只含纯重命名的分组」在分组头渲染
出一个空的 +N −M 计数簇。合计函数改为跳过两侧都为 0 的条目:合计=「行上
真的打印出来的数字之和」,与既有「二进制 / 未跟踪不贡献任何数」同一条
口径。只改这一个源头,两个分组头共用的 files > 0 门控不用动。

(b) .lineCounts 的裸 font-size: 10px 换成 font: var(--dsw-font-xxxs-11)
(11px/14px,weight 400),并按简写要求在其后用 font-family 覆盖回
--ds-font-family-code 代码字体。前后值:10px(行高继承 18px)→ 11px/14px;
行与分组头都是 28px 固定高度、文字 18px 行高,故布局不变,只有数字本身
大 1px。

测试:
- change-tree-counts:新增 0/0 条目不参与合计(纯重命名 / 仅改权限),
  同时钉住「改了内容的 rename 仍贡献数字」。
- changes-tab:新增「只含纯重命名的两个分组都查不到 [data-lines="group"]
  空计数簇」用例(并断言两个重命名行确实渲染了,避免空过),配一条对照
  用例「组内真有数字时计数簇照常出现且数字正确」;另加一条样式守护,钉住
  .lineCounts 走 --dsw-font-* 令牌、不含裸 font-size px。
- 还原源码/CSS 改动后这三条全红(空 span 被断言到、font 令牌断言失败)。
2026-10-05 22:39:22 +08:00

91 lines
4.6 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.
/**
* Line counts in the change tree (#131).
*
* The status answer carries each path's added/deleted lines; the tree keeps
* them on its leaves so a row prints its own numbers, and a group'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 } from '../src/client/changes/change-tree.ts'
import type { GitStatusEntry } from '../src/client/api.ts'
/** One status entry with counts. */
function entry(path: string, xy: string, counts?: GitStatusEntry['counts']): GitStatusEntry {
return counts === undefined ? { path, xy } : { path, xy, 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', '??'),
])
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('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 }),
])
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', '??'),
])
// 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', '??')])))
.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 })])))
.toEqual({ additions: 0, deletions: 0, files: 0 })
expect(sumLineCounts(buildChangeTree([entry('mode-only.ts', ' M', { additions: 0, deletions: 0 })])))
.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 }),
entry('renamed.ts', 'R ', { additions: 0, deletions: 0 }),
]))).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 }),
])
expect(tree).toHaveLength(1)
expect(tree[0]).toMatchObject({ kind: 'dir', name: 'src/client/changes' })
expect(sumLineCounts(tree)).toEqual({ additions: 7, deletions: 1, files: 2 })
})
})