67 lines · 3.5 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { lastChanges, loadCommits } from "../browse.ts"; |
| 4 | import { COMMITS_PER_PAGE } from "../contract/limits.ts"; |
| 5 | |
| 6 | function fakeRepo(history: { hash: string; parents: string[]; files: Record<string, string>; message?: string }[]) { |
| 7 | return { |
| 8 | log: async ({ limit, offset = 0 }: { limit: number; offset?: number }) => history.slice(offset, offset + limit).map((commit) => ({ hash: commit.hash, treeHash: `tree-${commit.hash}`, parents: commit.parents, message: commit.message ?? `msg ${commit.hash}`, author: { name: "A" }, committedAt: 1 })), |
| 9 | readTree: async (treeHash: string) => { |
| 10 | const commit = history.find((candidate) => `tree-${candidate.hash}` === treeHash); |
| 11 | return commit ? Object.entries(commit.files).map(([name, hash]) => ({ name, hash, type: "blob", mode: "100644" })) : null; |
| 12 | }, |
| 13 | } as unknown as ArtifactsRepo; |
| 14 | } |
| 15 | |
| 16 | test("attributes each entry to the newest commit that changed it", async () => { |
| 17 | const repo = fakeRepo([ |
| 18 | { hash: "c3", parents: ["c2"], files: { a: "a2", b: "b1", c: "c1" } }, |
| 19 | { hash: "c2", parents: ["c1"], files: { a: "a1", b: "b1", c: "c1" } }, |
| 20 | { hash: "c1", parents: [], files: { a: "a1", b: "b1" } }, |
| 21 | ]); |
| 22 | const changes = await lastChanges(repo, "main", []); |
| 23 | assert.equal(changes.a.hash, "c3"); |
| 24 | assert.equal(changes.c.hash, "c2"); |
| 25 | assert.equal(changes.b.hash, "c1"); |
| 26 | }); |
| 27 | |
| 28 | test("leaves entries unattributed when history beyond the log is unavailable", async () => { |
| 29 | const repo = fakeRepo([ |
| 30 | { hash: "c9", parents: ["c8"], files: { a: "a2", old: "o1" } }, |
| 31 | { hash: "c8", parents: ["c7"], files: { a: "a1", old: "o1" } }, |
| 32 | ]); |
| 33 | const changes = await lastChanges(repo, "main", [], 1); |
| 34 | assert.equal(changes.a.hash, "c9"); |
| 35 | assert.equal(changes.old, undefined); |
| 36 | }); |
| 37 | |
| 38 | test("pages through history until every entry is attributed", async () => { |
| 39 | const history = Array.from({ length: 7 }, (_, i) => { |
| 40 | const n = 7 - i; |
| 41 | return { hash: `c${n}`, parents: n > 1 ? [`c${n - 1}`] : [], files: { churn: `v${n}`, stable: "s1" } }; |
| 42 | }); |
| 43 | const changes = await lastChanges(fakeRepo(history), "main", [], 2); |
| 44 | assert.equal(changes.churn.hash, "c7"); |
| 45 | assert.equal(changes.stable.hash, "c1"); |
| 46 | }); |
| 47 | |
| 48 | test("commit history retains the full message for reasoning", async () => { |
| 49 | const message = "Choose storage\n\nKeep writes serialized.\n\n```decision\ndecision_id: DEC-01JB7Q3M8Z4K2N6V9XWRT5HYCE\ntask: 1\ndecision: Use durable storage.\nreason:\n - Serialize writes\n```"; |
| 50 | const { commits, hasOlder } = await loadCommits(fakeRepo([{ hash: "c1", parents: [], files: {}, message }]), "main", 1); |
| 51 | assert.equal(commits[0].title, "Choose storage"); |
| 52 | assert.equal(commits[0].message, message); |
| 53 | assert.equal(hasOlder, false); |
| 54 | }); |
| 55 | |
| 56 | test("reasoning history keeps page boundaries and newest-first order", async () => { |
| 57 | const history = Array.from({ length: COMMITS_PER_PAGE + 1 }, (_, index) => ({ hash: `c${index}`, parents: [], files: {}, message: `Commit ${index}\n\nReason ${index}` })); |
| 58 | const repo = fakeRepo(history); |
| 59 | const first = await loadCommits(repo, "main", 1); |
| 60 | assert.equal(first.commits.length, COMMITS_PER_PAGE); |
| 61 | assert.equal(first.commits[0].message, history[0].message); |
| 62 | assert.equal(first.commits.at(-1)?.hash, history[COMMITS_PER_PAGE - 1].hash); |
| 63 | assert.equal(first.hasOlder, true); |
| 64 | const second = await loadCommits(repo, "main", 2); |
| 65 | assert.deepEqual(second.commits.map((commit) => commit.message), [history[COMMITS_PER_PAGE].message]); |
| 66 | assert.equal(second.hasOlder, false); |
| 67 | }); |