testFoldDiffSendsWhatItShows
A diff sends what it shows: changes with two lines of context, each line once, the rest counted.
import { foldDiff } from "#codebase/tcmts/share"; import type { ReviewItem } from "#codebase/tcmts/share"; import assert from "node:assert/strict"; export const testFoldDiffSendsWhatItShows = () => { const lines: ReviewItem["lines"] = Array.from({ length: 20 }, (_, i) => [" ", `l${i}`]); lines.splice(10, 1, ["-", "old"], ["+", "new"]); const rows = foldDiff( lines, lines.map(([, t]) => `<i>${t}</i>`), ); assert.deepEqual(rows[0], { gap: 8 }); assert.deepEqual(rows.slice(1, 3), [ { op: " ", a: 9, b: 9, html: "<i>l8</i>" }, { op: " ", a: 10, b: 10, html: "<i>l9</i>" }, ]); assert.deepEqual(rows.slice(3, 5), [ { op: "-", a: 11, b: undefined, html: "<i>old</i>" }, { op: "+", a: undefined, b: 11, html: "<i>new</i>" }, ]); assert.deepEqual(rows.at(-1), { gap: 7 }); // (a long addition: its first rows, then the rest counted) const long = foldDiff( Array.from({ length: 50 }, (_, i) => ["+", `x${i}`] as ["+", string]), undefined, 10, ); assert.equal(long.length, 11); assert.deepEqual(long.at(-1), { gap: 40, cut: true }); assert.deepEqual(long[0], { op: "+", a: undefined, b: 1, html: "x0" }); }