import { describe, expect, it } from "vitest";
import { authorsOf, mockFileHistory } from "#lib/api/fileHistory.ts";
import type { CommitSummary } from "#lib/api/repo.ts";
const repo = "at://did:plc:o/sh.tangled.repo/abc";
const now = new Date("2026-08-31T12:00:00Z");
describe("mockFileHistory", () => {
it("invents the same history for one path and a different one per path", () => {
const once = mockFileHistory(repo, "src/app.css", [], now);
const again = mockFileHistory(repo, "src/app.css", [], now);
const other = mockFileHistory(repo, "README.md", [], now);
expect(again).toEqual(once);
expect(other).not.toEqual(once);
});
it("reads newest first and stays inside the sha and date shapes the view needs", () => {
const history = mockFileHistory(repo, "src/app.css", [], now);
expect(history.length).toBeGreaterThan(0);
for (const commit of history) {
expect(commit.hash).toMatch(/^[0-9a-f]{40}$/);
expect(commit.shortHash).toBe(commit.hash.slice(0, 8));
expect(commit.subject).not.toBe("");
expect(new Date(commit.when).getTime()).not.toBeNaN();
}
const times = history.map((commit) => new Date(commit.when).getTime());
expect(times).toEqual([...times].sort((a, b) => b - a));
expect(times[0]).toBeLessThanOrEqual(now.getTime());
});
it("credits the authors it was given", () => {
const history = mockFileHistory(
repo,
"src/app.css",
[{ authorName: "alice", authorEmail: "did:plc:e", authorDid: "did:plc:e" }],
now
);
expect(new Set(history.map((commit) => commit.authorName))).toEqual(new Set(["alice"]));
});
});
describe("authorsOf", () => {
it("keeps one entry per identity, did first", () => {
const commit = (authorName: string, extra: Partial<CommitSummary>): CommitSummary => ({
hash: "0".repeat(40),
shortHash: "00000000",
subject: "s",
body: "",
authorName,
authorEmail: "",
when: now.toISOString(),
...extra
});
const authors = authorsOf([
commit("alice", { authorDid: "did:plc:e" }),
commit("alice again", { authorDid: "did:plc:e" }),
commit("bob", { authorEmail: "bob@tangled.org" }),
commit("nobody", { authorEmail: "" })
]);
expect(authors.map((author) => author.authorName)).toEqual(["alice", "bob"]);
});
});