diff --git a/src/client/changes/change-tree.ts b/src/client/changes/change-tree.ts index 15c7e678..992ed75a 100644 --- a/src/client/changes/change-tree.ts +++ b/src/client/changes/change-tree.ts @@ -150,16 +150,19 @@ export function buildChangeTree(entries: readonly GitStatusEntry[]): ChangeNode[ export interface ChangeLineTotals { additions: number deletions: number - /** Rows that actually carry counts. 0 means the group has nothing to sum - * (e.g. only untracked files), so the header must show no total at all. */ + /** Rows that actually PRINT numbers. 0 means the group has nothing to sum + * (only untracked files, binary blobs — or only 0/0 entries such as a pure + * rename), so the header must show no total at all. */ files: number } /** * Sum one group tree's line counts (#131): the group header's total is exactly - * the sum of the numbers its own file rows print. Binary rows and rows without - * counts (untracked files) contribute nothing — never a fabricated 0 — and - * folding a directory is a view state, so every member counts either way. + * the sum of the numbers its own file rows print. Binary rows, rows without + * counts (untracked files) and rows whose two numbers are both zero (a pure + * rename, a mode-only change — their rows print nothing) contribute nothing, + * never a fabricated 0 — and folding a directory is a view state, so every + * member counts either way. */ export function sumLineCounts(nodes: readonly ChangeNode[]): ChangeLineTotals { const totals: ChangeLineTotals = { additions: 0, deletions: 0, files: 0 } @@ -172,6 +175,10 @@ export function sumLineCounts(nodes: readonly ChangeNode[]): ChangeLineTotals { continue } if (node.counts === undefined || !('additions' in node.counts)) continue + // A 0/0 reading is a row with nothing to print (`+0 −0` would be an + // invented number), so it must not make the header print an empty cluster + // either — a group holding nothing but renames then stays bare. + if (node.counts.additions === 0 && node.counts.deletions === 0) continue totals.additions += node.counts.additions totals.deletions += node.counts.deletions totals.files += 1 diff --git a/src/client/changes/changes.module.css b/src/client/changes/changes.module.css index f1a45cf3..cc9cea32 100644 --- a/src/client/changes/changes.module.css +++ b/src/client/changes/changes.module.css @@ -199,13 +199,16 @@ /* A row's / band's line counts (#131): `+N −M` in the diff pane's own ink (the `.statAdd` / `.statDel` atoms of `diff.module.css`, reused rather than re-coloured here). Tabular figures keep the column from shifting between - readings, and the code face matches every other number in the tab. */ + readings, and the code face matches every other number in the tab. Size and + line height come from the smallest host font token (11px/14px) — the family + is overridden back to the code stack right after the shorthand, which is the + order the shorthand requires. */ .lineCounts { flex: none; display: inline-flex; gap: 5px; + font: var(--dsw-font-xxxs-11); font-family: var(--ds-font-family-code, monospace); - font-size: 10px; font-variant-numeric: tabular-nums; } diff --git a/tests/change-tree-counts.spec.ts b/tests/change-tree-counts.spec.ts index 8bb8a706..03877117 100644 --- a/tests/change-tree-counts.spec.ts +++ b/tests/change-tree-counts.spec.ts @@ -60,6 +60,22 @@ describe('change tree line counts', () => { .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. diff --git a/tests/changes-tab.spec.tsx b/tests/changes-tab.spec.tsx index 20c7e50f..d8cef920 100644 --- a/tests/changes-tab.spec.tsx +++ b/tests/changes-tab.spec.tsx @@ -17,7 +17,7 @@ import { createRoot, type Root } from 'react-dom/client' import { act } from 'react-dom/test-utils' import { ChangesTab, opCountOf } from '../src/client/changes/ChangesTab.tsx' import { createSidebarStore } from '../src/client/state.ts' -import { api, type GitStatusResult, type GitWorktree } from '../src/client/api.ts' +import { api, type GitStatusEntry, type GitStatusResult, type GitWorktree } from '../src/client/api.ts' import { t } from '../src/client/locales.ts' import type { Context } from '../src/context-types.ts' import type { SidebarTab } from '../src/client/state.ts' @@ -55,7 +55,7 @@ function mount(root: Root, tab: SidebarTab = { id: 'git', type: 'git', title: 'C }) } -function mockGit(entries: Array<{ path: string; xy: string }>): void { +function mockGit(entries: GitStatusEntry[]): void { vi.spyOn(api, 'gitWorktrees').mockResolvedValue([ { path: MAIN, branch: 'main', current: true, changes: entries.length }, ] as GitWorktree[]) @@ -136,6 +136,84 @@ describe('ChangesTab', () => { } }) + /** + * Issue #131, appearance half: a pure rename is `0 0` in numstat, so its row + * prints no numbers — and a group whose ONLY changes are renames used to + * render an empty `+N −M` cluster in its header, because the total counted + * the (numberless) rename row as a contributor. The contrast case is the + * other half of the gate: with one row that really has numbers, the cluster + * is back and carries exactly those numbers. + */ + it('renders no count cluster for a group whose only changes are pure renames (#131)', async () => { + mockGit([ + { path: 'renamed.ts', xy: 'R ', counts: { additions: 0, deletions: 0 } }, + { path: 'moved.md', xy: ' R', counts: { additions: 0, deletions: 0 } }, + ]) + + const container = document.createElement('div') + document.body.append(container) + const root: Root = createRoot(container) + try { + mount(root) + await flushEffects() + + const staged = container.querySelector('[data-group="staged"]') + const unstaged = container.querySelector('[data-group="unstaged"]') + // Both groups really rendered their rename row, so the absence asserted + // below is the counts cluster's, not the whole group's. + expect(staged!.querySelectorAll('button[data-path]')).toHaveLength(1) + expect(unstaged!.querySelectorAll('button[data-path]')).toHaveLength(1) + expect(staged!.querySelector('[data-lines="group"]')).toBeNull() + expect(unstaged!.querySelector('[data-lines="group"]')).toBeNull() + // The renames are still listed with their count pill (the FILE count). + expect(staged!.querySelector('[data-count]')!.textContent).toBe('1') + expect(unstaged!.querySelector('[data-count]')!.textContent).toBe('1') + } finally { + act(() => { root.unmount() }) + container.remove() + } + }) + + it('keeps the group total when a row in the group really has numbers (#131)', async () => { + mockGit([ + { path: 'mode-only.ts', xy: ' M', counts: { additions: 0, deletions: 0 } }, + { path: 'edited.ts', xy: ' M', counts: { additions: 3, deletions: 1 } }, + ]) + + const container = document.createElement('div') + document.body.append(container) + const root: Root = createRoot(container) + try { + mount(root) + await flushEffects() + + const unstaged = container.querySelector('[data-group="unstaged"]') + const cluster = unstaged!.querySelector('[data-lines="group"]') + expect(cluster).not.toBeNull() + expect(cluster!.getAttribute('data-added')).toBe('3') + expect(cluster!.getAttribute('data-deleted')).toBe('1') + expect(cluster!.textContent).toContain('+3') + // The cluster is not merely present — it carries text (an EMPTY cluster + // is exactly the artifact this pair of cases is about). + expect(cluster!.textContent).not.toBe('') + } finally { + act(() => { root.unmount() }) + container.remove() + } + }) + + it('sizes the count cluster from a font token, never from a bare px (#131)', () => { + // Skin contract (guide §12): visual values ride `--dsw-font-*` / `--ds-*` + // tokens. The row/band cluster is the one place in this sheet that used a + // literal `font-size: 10px`, two pixels off the smallest host token. + const sheet = readFileSync('src/client/changes/changes.module.css', 'utf8') + const block = sheet.match(/\n\.lineCounts \{([\s\S]*?)\n\}/)?.[1] + expect(block, 'the .lineCounts rule must exist in changes.module.css').toBeDefined() + expect(block!).toContain('font: var(--dsw-font-') + expect(block!).toContain('font-family: var(--ds-font-family-code') + expect(block!).not.toMatch(/font-size:\s*[\d.]+px/) + }) + it('folds the polled session events into session-lens rows and the badge cache', async () => { mockGit([]) const ops = vi.spyOn(api, 'changesOps')