import assert from "node:assert/strict"; import { test } from "node:test"; import { lastChanges, loadCommits } from "../browse.ts"; import { COMMITS_PER_PAGE } from "../contract/limits.ts"; function fakeRepo(history: { hash: string; parents: string[]; files: Record; message?: string }[]) { return { 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 })), readTree: async (treeHash: string) => { const commit = history.find((candidate) => `tree-${candidate.hash}` === treeHash); return commit ? Object.entries(commit.files).map(([name, hash]) => ({ name, hash, type: "blob", mode: "100644" })) : null; }, } as unknown as ArtifactsRepo; } test("attributes each entry to the newest commit that changed it", async () => { const repo = fakeRepo([ { hash: "c3", parents: ["c2"], files: { a: "a2", b: "b1", c: "c1" } }, { hash: "c2", parents: ["c1"], files: { a: "a1", b: "b1", c: "c1" } }, { hash: "c1", parents: [], files: { a: "a1", b: "b1" } }, ]); const changes = await lastChanges(repo, "main", []); assert.equal(changes.a.hash, "c3"); assert.equal(changes.c.hash, "c2"); assert.equal(changes.b.hash, "c1"); }); test("leaves entries unattributed when history beyond the log is unavailable", async () => { const repo = fakeRepo([ { hash: "c9", parents: ["c8"], files: { a: "a2", old: "o1" } }, { hash: "c8", parents: ["c7"], files: { a: "a1", old: "o1" } }, ]); const changes = await lastChanges(repo, "main", [], 1); assert.equal(changes.a.hash, "c9"); assert.equal(changes.old, undefined); }); test("pages through history until every entry is attributed", async () => { const history = Array.from({ length: 7 }, (_, i) => { const n = 7 - i; return { hash: `c${n}`, parents: n > 1 ? [`c${n - 1}`] : [], files: { churn: `v${n}`, stable: "s1" } }; }); const changes = await lastChanges(fakeRepo(history), "main", [], 2); assert.equal(changes.churn.hash, "c7"); assert.equal(changes.stable.hash, "c1"); }); test("commit history retains the full message for reasoning", async () => { 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```"; const { commits, hasOlder } = await loadCommits(fakeRepo([{ hash: "c1", parents: [], files: {}, message }]), "main", 1); assert.equal(commits[0].title, "Choose storage"); assert.equal(commits[0].message, message); assert.equal(hasOlder, false); }); test("reasoning history keeps page boundaries and newest-first order", async () => { const history = Array.from({ length: COMMITS_PER_PAGE + 1 }, (_, index) => ({ hash: `c${index}`, parents: [], files: {}, message: `Commit ${index}\n\nReason ${index}` })); const repo = fakeRepo(history); const first = await loadCommits(repo, "main", 1); assert.equal(first.commits.length, COMMITS_PER_PAGE); assert.equal(first.commits[0].message, history[0].message); assert.equal(first.commits.at(-1)?.hash, history[COMMITS_PER_PAGE - 1].hash); assert.equal(first.hasOlder, true); const second = await loadCommits(repo, "main", 2); assert.deepEqual(second.commits.map((commit) => commit.message), [history[COMMITS_PER_PAGE].message]); assert.equal(second.hasOlder, false); });