- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/test/diff.test.ts?version=26&format=json
100 linesFirst import 100%Last change First import in version 0, 1 version changed it
You're reading version 26. The newest is 318, and no version since then changed this file.
1// The client's line diff: hunks as git prints them, and edit scripts that are
2// correct and as short as possible.
4import assert from "node:assert/strict";
5import { describe, it } from "node:test";
6import { lineCounts, unifiedDiff } from "../src/diff.ts";
8const text = (lines: readonly string[]) => (lines.length === 0 ? "" : `${lines.join("\n")}\n`);
10/** Length of the longest common subsequence, by the textbook table. */
11function lcs(a: readonly string[], b: readonly string[]): number {
12 const row = new Array<number>(b.length + 1).fill(0);
13 for (const x of a) {
14 let diag = 0;
15 for (let j = 1; j <= b.length; j++) {
16 const up = row[j]!;
17 row[j] = x === b[j - 1] ? diag + 1 : Math.max(row[j]!, row[j - 1]!);
18 diag = up;
19 }
20 }
21 return row[b.length]!;
22}
24describe("unifiedDiff", () => {
25 it("prints hunks with three lines of context, as git does", () => {
26 const before = text(["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]);
27 const after = text(["1", "two", "3", "4", "5", "6", "7", "8", "9", "10", "eleven", "12", "13"]);
28 assert.equal(
29 unifiedDiff({ path: "f", before, after }),
30 [
31 "--- a/f",
32 "+++ b/f",
33 "@@ -1,5 +1,5 @@",
34 " 1",
35 "-2",
36 "+two",
37 " 3",
38 " 4",
39 " 5",
40 "@@ -8,5 +8,6 @@",
41 " 8",
42 " 9",
43 " 10",
44 "-11",
45 "+eleven",
46 " 12",
47 "+13",
48 ].join("\n"),
49 );
50 });
52 it("joins changes up to six lines apart into one hunk", () => {
53 const before = text(["a", "b", "c", "d", "e", "f", "g", "h"]);
54 const after = text(["A", "b", "c", "d", "e", "f", "g", "H"]);
55 assert.equal(
56 unifiedDiff({ path: "f", before, after })
57 .split("\n")
58 .filter((l) => l.startsWith("@@")).length,
59 1,
60 );
61 });
63 it("prints new and deleted files against /dev/null, and nothing for no change", () => {
64 assert.equal(unifiedDiff({ path: "n", before: null, after: "x\n" }), "--- /dev/null\n+++ b/n\n@@ -0,0 +1 @@\n+x");
65 assert.equal(
66 unifiedDiff({ path: "d", before: "x\ny\n", after: null }),
67 "--- a/d\n+++ /dev/null\n@@ -1,2 +0,0 @@\n-x\n-y",
68 );
69 assert.equal(unifiedDiff({ path: "s", before: "same\n", after: "same\n" }), "--- a/s\n+++ b/s");
70 });
72 it("rebuilds both sides and changes as few lines as possible, on random texts", () => {
73 let seed = 7;
74 const rand = (n: number) => ((seed = (seed * 1103515245 + 12345) % 2147483648), seed % n);
75 for (let round = 0; round < 400; round++) {
76 const a = Array.from({ length: rand(14) }, () => "abcd"[rand(4)]!);
77 const b = Array.from({ length: rand(14) }, () => "abcd"[rand(4)]!);
78 const body = unifiedDiff({ path: "f", before: text(a), after: text(b) }, 1000)
79 .split("\n")
80 .slice(2)
81 .filter((l) => !l.startsWith("@@"));
82 if (body.length === 0) {
83 assert.deepEqual(a, b, `no hunks only when nothing changed, round ${round}`);
84 continue;
85 }
86 assert.deepEqual(
87 body.filter((l) => l[0] !== "+").map((l) => l.slice(1)),
88 a,
89 `before, round ${round}`,
90 );
91 assert.deepEqual(
92 body.filter((l) => l[0] !== "-").map((l) => l.slice(1)),
93 b,
94 `after, round ${round}`,
95 );
96 const { added, removed } = lineCounts({ path: "f", before: text(a), after: text(b) });
97 assert.equal(added + removed, a.length + b.length - 2 * lcs(a, b), `shortest, round ${round}`);
98 }
99 });
100});