108 lines · 5.5 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { branchChanges, commitDetail, compareCommits, diffRows, lineDiff, mergeBase, rememberReads, rowsInRange } from "../diff.ts"; |
| 4 | |
| 5 | function fakeRepo(sameSecond = false) { |
| 6 | const commits: Record<string, { treeHash: string; parents: string[] }> = { |
| 7 | a: { treeHash: "ta", parents: [] }, |
| 8 | b: { treeHash: "tb", parents: ["a"] }, |
| 9 | c: { treeHash: "tc", parents: ["b"] }, |
| 10 | d: { treeHash: "td", parents: ["b"] }, |
| 11 | m: { treeHash: "tm", parents: ["d", "c"] }, |
| 12 | }; |
| 13 | const trees: Record<string, ArtifactsTreeEntry[]> = { |
| 14 | ta: [{ name: "file.txt", hash: "one", type: "blob", mode: "100644" }], |
| 15 | tb: [{ name: "file.txt", hash: "two", type: "blob", mode: "100644" }], |
| 16 | tc: [ |
| 17 | { name: "file.txt", hash: "three", type: "blob", mode: "100644" }, |
| 18 | { name: "new.txt", hash: "new", type: "blob", mode: "100644" }, |
| 19 | ], |
| 20 | td: [{ name: "file.txt", hash: "four", type: "blob", mode: "100644" }], |
| 21 | tm: [ |
| 22 | { name: "file.txt", hash: "four", type: "blob", mode: "100644" }, |
| 23 | { name: "new.txt", hash: "new", type: "blob", mode: "100644" }, |
| 24 | ], |
| 25 | }; |
| 26 | const blobs: Record<string, string> = { one: "one\n", two: "one\ntwo\n", three: "one\nchanged\n", four: "branch\n", new: "new\n" }; |
| 27 | return { |
| 28 | readCommit: async (hash: string) => { |
| 29 | const commit = commits[hash]; |
| 30 | 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; |
| 31 | }, |
| 32 | readTree: async (hash: string) => trees[hash] ?? null, |
| 33 | readBlob: async (hash: string) => (hash in blobs ? new Blob([blobs[hash]]) : null), |
| 34 | } as unknown as ArtifactsRepo; |
| 35 | } |
| 36 | |
| 37 | test("diff rows number both sides, deletions before additions", () => { |
| 38 | assert.deepEqual(diffRows("same\nold\nlast\n", "same\nnew\nlast\n"), [ |
| 39 | { kind: "context", text: "same", oldLine: 1, newLine: 1 }, |
| 40 | { kind: "delete", text: "old", oldLine: 2, newLine: null }, |
| 41 | { kind: "add", text: "new", oldLine: null, newLine: 2 }, |
| 42 | { kind: "context", text: "last", oldLine: 3, newLine: 3 }, |
| 43 | ]); |
| 44 | }); |
| 45 | |
| 46 | test("counts a small change in a large file exactly", () => { |
| 47 | const before = Array.from({ length: 3000 }, (_, index) => `line ${index + 1}`); |
| 48 | const after = [...before.slice(0, 1499), "changed", ...before.slice(1500, 2600), "inserted", ...before.slice(2600)]; |
| 49 | assert.deepEqual(lineDiff(`${before.join("\n")}\n`, `${after.join("\n")}\n`), { additions: 2, deletions: 1, truncated: false }); |
| 50 | assert.deepEqual(lineDiff("", "a\nb\n"), { additions: 2, deletions: 0, truncated: false }); |
| 51 | }); |
| 52 | |
| 53 | test("a selected range covers the rows between its ends across both sides", () => { |
| 54 | const before = "a\nold\nc\nd\n"; |
| 55 | const after = "a\nnew\nc\nd\n"; |
| 56 | const texts = (range: Parameters<typeof rowsInRange>[2]) => rowsInRange(before, after, range)?.map((row) => row.text); |
| 57 | assert.deepEqual(texts({ start: 2, side: "deletions", end: 3, endSide: "additions" }), ["old", "new", "c"]); |
| 58 | assert.deepEqual(texts({ start: 3, side: "additions", end: 1, endSide: "additions" }), ["a", "old", "new", "c"]); |
| 59 | assert.deepEqual(texts({ start: 2, side: "additions", end: 2, endSide: "additions" }), ["new"]); |
| 60 | assert.equal(rowsInRange(before, after, { start: 9, side: "additions", end: 9, endSide: "additions" }), null); |
| 61 | }); |
| 62 | |
| 63 | test("finds the nearest common ancestor", async () => { |
| 64 | assert.equal(await mergeBase(fakeRepo(), "c", "d"), "b"); |
| 65 | }); |
| 66 | |
| 67 | test("compares trees and blobs with file totals", async () => { |
| 68 | const changes = await compareCommits(fakeRepo(), "b", "c"); |
| 69 | assert.equal(changes.files.length, 2); |
| 70 | assert.equal(changes.files[0].path, "file.txt"); |
| 71 | assert.equal(changes.files[0].additions, 1); |
| 72 | assert.equal(changes.files[0].deletions, 1); |
| 73 | assert.equal(changes.files[1].status, "added"); |
| 74 | assert.equal(changes.additions, 2); |
| 75 | assert.equal(changes.deletions, 1); |
| 76 | }); |
| 77 | |
| 78 | test("a branch that merged its base lists only its own commits", async () => { |
| 79 | const changes = await branchChanges(fakeRepo(), "c", "m"); |
| 80 | assert.equal(changes.base, "c"); |
| 81 | assert.deepEqual(changes.commits.map((commit) => commit.hash).sort(), ["d", "m"]); |
| 82 | assert.deepEqual(changes.files.map((file) => file.path), ["file.txt"]); |
| 83 | }); |
| 84 | |
| 85 | test("commits made in the same second still list oldest first", async () => { |
| 86 | const changes = await branchChanges(fakeRepo(true), "c", "m"); |
| 87 | assert.deepEqual(changes.commits.map((commit) => commit.hash), ["d", "m"]); |
| 88 | }); |
| 89 | |
| 90 | test("each commit on a branch lists the paths it changed against its first parent", async () => { |
| 91 | const changes = await branchChanges(fakeRepo(), "c", "m"); |
| 92 | assert.deepEqual(Object.fromEntries(changes.commits.map((commit) => [commit.hash, commit.files])), { d: ["file.txt"], m: ["new.txt"] }); |
| 93 | }); |
| 94 | |
| 95 | test("remembered reads stay inside their repository", async () => { |
| 96 | let reads = 0; |
| 97 | const counting = (message: string) => { |
| 98 | const base = fakeRepo(); |
| 99 | return { ...base, readCommit: async (hash: string) => { reads++; const commit = await base.readCommit(hash); return commit && { ...commit, message }; } } as unknown as ArtifactsRepo; |
| 100 | }; |
| 101 | const secret = rememberReads(counting("secret"), "private-repo"); |
| 102 | const other = rememberReads(counting("public"), "public-repo"); |
| 103 | assert.equal((await commitDetail(secret, "a"))?.message, "secret"); |
| 104 | const before = reads; |
| 105 | assert.equal((await commitDetail(secret, "a"))?.message, "secret"); |
| 106 | assert.equal(reads, before, "a second read of the same commit comes from memory"); |
| 107 | assert.equal((await commitDetail(other, "a"))?.message, "public"); |
| 108 | }); |