Skip to content

Commit b7406f0

Browse files
committed
fix: trim diff output context and normalize +/- indentation
1 parent e3542e6 commit b7406f0

3 files changed

Lines changed: 34 additions & 9 deletions

File tree

src/__tests__/cli-diff.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ test('diff snapshot renders human-readable unified diff text', async () => {
7777
assert.equal(result.code, null);
7878
assert.equal(result.calls.length, 1);
7979
assert.match(result.stdout, /^@e2 \[window\]/m);
80-
assert.match(result.stdout, /^- @e3 \[text\] "67"$/m);
81-
assert.match(result.stdout, /^\+ @e3 \[text\] "134"$/m);
80+
assert.match(result.stdout, /^- @e3 \[text\] "67"$/m);
81+
assert.match(result.stdout, /^\+ @e3 \[text\] "134"$/m);
8282
assert.match(result.stdout, /1 additions, 1 removals, 1 unchanged/);
8383
assert.equal(result.stderr, '');
8484
});

src/utils/__tests__/output.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@ import { formatSnapshotDiffText } from '../output.ts';
55
test('formatSnapshotDiffText renders unified diff lines with summary', () => {
66
const text = formatSnapshotDiffText({
77
baselineInitialized: false,
8-
summary: { additions: 2, removals: 2, unchanged: 2 },
8+
summary: { additions: 2, removals: 2, unchanged: 4 },
99
lines: [
10+
{ kind: 'unchanged', text: '@e0 [application]' },
1011
{ kind: 'unchanged', text: '@e2 [window]' },
1112
{ kind: 'removed', text: ' @e3 [other] "67"' },
1213
{ kind: 'removed', text: ' @e4 [text] "67"' },
1314
{ kind: 'added', text: ' @e3 [other] "134"' },
1415
{ kind: 'added', text: ' @e4 [text] "134"' },
1516
{ kind: 'unchanged', text: ' @e5 [button] "Increment"' },
17+
{ kind: 'unchanged', text: ' @e6 [text] "Footer"' },
1618
],
1719
});
1820

21+
assert.doesNotMatch(text, /^@e0 \[application\]$/m);
1922
assert.match(text, /^@e2 \[window\]/m);
20-
assert.match(text, /^- @e3 \[other\] "67"$/m);
21-
assert.match(text, /^\+ @e3 \[other\] "134"$/m);
22-
assert.match(text, /2 additions, 2 removals, 2 unchanged/);
23+
assert.match(text, /^- @e3 \[other\] "67"$/m);
24+
assert.match(text, /^\+ @e3 \[other\] "134"$/m);
25+
assert.match(text, /^ @e5 \[button\] "Increment"$/m);
26+
assert.doesNotMatch(text, /^ @e6 \[text\] "Footer"$/m);
27+
assert.match(text, /2 additions, 2 removals, 4 unchanged/);
2328
});
2429

2530
test('formatSnapshotDiffText renders baseline initialization text', () => {

src/utils/output.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ export function formatSnapshotDiffText(data: Record<string, unknown>): string {
8484
return `Baseline initialized (${unchanged} lines).\n`;
8585
}
8686
const rawLines = Array.isArray(data.lines) ? (data.lines as SnapshotDiffLine[]) : [];
87-
const lines = rawLines.map((line) => {
87+
const contextLines = applyContextWindow(rawLines, 1);
88+
const lines = contextLines.map((line) => {
8889
const text = typeof line.text === 'string' ? line.text : '';
89-
if (line.kind === 'added') return `+${text}`;
90-
if (line.kind === 'removed') return `-${text}`;
90+
if (line.kind === 'added') return `+ ${text.trimStart()}`;
91+
if (line.kind === 'removed') return `- ${text.trimStart()}`;
9192
return text;
9293
});
9394
const body = lines.length > 0 ? `${lines.join('\n')}\n` : '';
@@ -97,3 +98,22 @@ export function formatSnapshotDiffText(data: Record<string, unknown>): string {
9798
function toNumber(value: unknown): number {
9899
return typeof value === 'number' && Number.isFinite(value) ? value : 0;
99100
}
101+
102+
function applyContextWindow(lines: SnapshotDiffLine[], contextWindow: number): SnapshotDiffLine[] {
103+
if (lines.length === 0) return lines;
104+
const changedIndices = lines
105+
.map((line, index) => ({ index, kind: line.kind }))
106+
.filter((entry) => entry.kind === 'added' || entry.kind === 'removed')
107+
.map((entry) => entry.index);
108+
if (changedIndices.length === 0) return lines;
109+
110+
const keep = new Array<boolean>(lines.length).fill(false);
111+
for (const index of changedIndices) {
112+
const start = Math.max(0, index - contextWindow);
113+
const end = Math.min(lines.length - 1, index + contextWindow);
114+
for (let i = start; i <= end; i += 1) {
115+
keep[i] = true;
116+
}
117+
}
118+
return lines.filter((_, index) => keep[index]);
119+
}

0 commit comments

Comments
 (0)