~james/core

core/web/src/lib/api/fileHistory.test.ts -rw-r--r-- 2.2 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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"]);
	});
});