import assert from "node:assert/strict"; import { test } from "node:test"; import { branchChanges, commitDetail, compareCommits, diffRows, lineDiff, mergeBase, rememberReads, rowsInRange } from "../diff.ts"; function fakeRepo(sameSecond = false) { const commits: Record = { a: { treeHash: "ta", parents: [] }, b: { treeHash: "tb", parents: ["a"] }, c: { treeHash: "tc", parents: ["b"] }, d: { treeHash: "td", parents: ["b"] }, m: { treeHash: "tm", parents: ["d", "c"] }, }; const trees: Record = { ta: [{ name: "file.txt", hash: "one", type: "blob", mode: "100644" }], tb: [{ name: "file.txt", hash: "two", type: "blob", mode: "100644" }], tc: [ { name: "file.txt", hash: "three", type: "blob", mode: "100644" }, { name: "new.txt", hash: "new", type: "blob", mode: "100644" }, ], td: [{ name: "file.txt", hash: "four", type: "blob", mode: "100644" }], tm: [ { name: "file.txt", hash: "four", type: "blob", mode: "100644" }, { name: "new.txt", hash: "new", type: "blob", mode: "100644" }, ], }; const blobs: Record = { one: "one\n", two: "one\ntwo\n", three: "one\nchanged\n", four: "branch\n", new: "new\n" }; return { readCommit: async (hash: string) => { const commit = commits[hash]; return commit ? { hash, ...commit, message: `commit ${hash}`, author: { name: "A", email: "a@example.com" }, committer: { name: "A", email: "a@example.com" }, authoredAt: 1, committedAt: sameSecond ? 1 : "abcdm".indexOf(hash) + 1 } : null; }, readTree: async (hash: string) => trees[hash] ?? null, readBlob: async (hash: string) => (hash in blobs ? new Blob([blobs[hash]]) : null), } as unknown as ArtifactsRepo; } test("diff rows number both sides, deletions before additions", () => { assert.deepEqual(diffRows("same\nold\nlast\n", "same\nnew\nlast\n"), [ { kind: "context", text: "same", oldLine: 1, newLine: 1 }, { kind: "delete", text: "old", oldLine: 2, newLine: null }, { kind: "add", text: "new", oldLine: null, newLine: 2 }, { kind: "context", text: "last", oldLine: 3, newLine: 3 }, ]); }); test("counts a small change in a large file exactly", () => { const before = Array.from({ length: 3000 }, (_, index) => `line ${index + 1}`); const after = [...before.slice(0, 1499), "changed", ...before.slice(1500, 2600), "inserted", ...before.slice(2600)]; assert.deepEqual(lineDiff(`${before.join("\n")}\n`, `${after.join("\n")}\n`), { additions: 2, deletions: 1, truncated: false }); assert.deepEqual(lineDiff("", "a\nb\n"), { additions: 2, deletions: 0, truncated: false }); }); test("a selected range covers the rows between its ends across both sides", () => { const before = "a\nold\nc\nd\n"; const after = "a\nnew\nc\nd\n"; const texts = (range: Parameters[2]) => rowsInRange(before, after, range)?.map((row) => row.text); assert.deepEqual(texts({ start: 2, side: "deletions", end: 3, endSide: "additions" }), ["old", "new", "c"]); assert.deepEqual(texts({ start: 3, side: "additions", end: 1, endSide: "additions" }), ["a", "old", "new", "c"]); assert.deepEqual(texts({ start: 2, side: "additions", end: 2, endSide: "additions" }), ["new"]); assert.equal(rowsInRange(before, after, { start: 9, side: "additions", end: 9, endSide: "additions" }), null); }); test("finds the nearest common ancestor", async () => { assert.equal(await mergeBase(fakeRepo(), "c", "d"), "b"); }); test("compares trees and blobs with file totals", async () => { const changes = await compareCommits(fakeRepo(), "b", "c"); assert.equal(changes.files.length, 2); assert.equal(changes.files[0].path, "file.txt"); assert.equal(changes.files[0].additions, 1); assert.equal(changes.files[0].deletions, 1); assert.equal(changes.files[1].status, "added"); assert.equal(changes.additions, 2); assert.equal(changes.deletions, 1); }); test("a branch that merged its base lists only its own commits", async () => { const changes = await branchChanges(fakeRepo(), "c", "m"); assert.equal(changes.base, "c"); assert.deepEqual(changes.commits.map((commit) => commit.hash).sort(), ["d", "m"]); assert.deepEqual(changes.files.map((file) => file.path), ["file.txt"]); }); test("commits made in the same second still list oldest first", async () => { const changes = await branchChanges(fakeRepo(true), "c", "m"); assert.deepEqual(changes.commits.map((commit) => commit.hash), ["d", "m"]); }); test("each commit on a branch lists the paths it changed against its first parent", async () => { const changes = await branchChanges(fakeRepo(), "c", "m"); assert.deepEqual(Object.fromEntries(changes.commits.map((commit) => [commit.hash, commit.files])), { d: ["file.txt"], m: ["new.txt"] }); }); test("remembered reads stay inside their repository", async () => { let reads = 0; const counting = (message: string) => { const base = fakeRepo(); return { ...base, readCommit: async (hash: string) => { reads++; const commit = await base.readCommit(hash); return commit && { ...commit, message }; } } as unknown as ArtifactsRepo; }; const secret = rememberReads(counting("secret"), "private-repo"); const other = rememberReads(counting("public"), "public-repo"); assert.equal((await commitDetail(secret, "a"))?.message, "secret"); const before = reads; assert.equal((await commitDetail(secret, "a"))?.message, "secret"); assert.equal(reads, before, "a second read of the same commit comes from memory"); assert.equal((await commitDetail(other, "a"))?.message, "public"); });