~james/core

core/web/src/lib/api/repoIndex.test.ts -rw-r--r-- 8.0 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { describe, expect, it, vi } from "vitest";
import { ClientResponseError } from "#lib/api/client.ts";
import {
	classifyRepoAvailability,
	withAuthorHandles,
	withImportAvailability,
	type RepoAvailability
} from "#lib/api/repoIndex.ts";
import type { ImportState } from "#lib/api/repo.ts";
import { sshFingerprint } from "#lib/api/settings.ts";
import type { CommitSummary } from "#lib/components/repo/types.ts";
import type { Fetch } from "#lib/fetch.ts";

const notSynced = () => new ClientResponseError({ status: 404, data: { error: "RepoNotFound" } });

describe("classifyRepoAvailability", () => {
	it("recognizes a repo the mirror has not synced yet", () => {
		const result = classifyRepoAvailability(
			[notSynced(), notSynced(), notSynced()].map((cause) => ({
				value: null,
				error: cause
			}))
		);
		expect(result).toBe("not-synced");
	});

	it("does not treat other 404s as not synced", () => {
		const result = classifyRepoAvailability(
			["tree", "log", "branches"].map(() => ({
				value: null,
				error: new ClientResponseError({ status: 404, data: { error: "XRPCNotSupported" } })
			}))
		);
		expect(result).toBe("error");
	});

	it("keeps transport failures as errors", () => {
		const result = classifyRepoAvailability(
			["tree", "log", "branches"].map(() => ({
				value: null,
				error: new TypeError("offline")
			}))
		);
		expect(result).toBe("error");
	});

	it("does not mistake a missing ref for an unsynced repo", () => {
		const result = classifyRepoAvailability([
			{ value: null, error: notSynced() },
			{ value: null, error: notSynced() },
			{ value: { branches: [] }, error: null }
		]);
		expect(result).toBe("ok");
	});
});

describe("withImportAvailability", () => {
	it.each([
		["ok", { content: "fetching" as const }, "ok"],
		["ok", { content: "partial" as const }, "ok"],
		["ok", { content: "failed" as const }, "ok"],
		["not-synced", null, "not-synced"],
		["not-synced", { content: "fetching" as const }, "importing"],
		["not-synced", { content: "partial" as const }, "importing"],
		["not-synced", { content: "failed" as const }, "import-failed"],
		["empty", null, "empty"],
		["error", null, "error"],
		["empty", { content: "failed" as const }, "import-failed"],
		["error", { content: "failed" as const }, "import-failed"],
		["empty", { content: "fetching" as const }, "importing"],
		["empty", { content: "partial" as const }, "importing"],
		["error", { content: "fetching" as const }, "importing"],
		["error", { content: "partial" as const }, "importing"]
	] satisfies [RepoAvailability, ImportState | null, RepoAvailability][])(
		"maps (%s, %j) -> %s",
		(availability, status, expected) => {
			expect(withImportAvailability(availability, status)).toBe(expected);
		}
	);
});

const commit = (c: Partial<CommitSummary>): CommitSummary => ({
	hash: "h",
	shortHash: "h",
	subject: "s",
	body: "",
	authorName: "",
	authorEmail: "",
	when: "2026-01-01T00:00:00Z",
	...c
});

// dispatches the two upstreams withAuthorHandles touches: the internal kv
// route (relative) and bobbin's resolveMiniDoc.
const fetchMock = vi.fn(async (input: string | URL, _init?: RequestInit) => {
	const url = String(input);
	if (url.startsWith("/_internal/email-did")) {
		return new Response(
			JSON.stringify({
				"alice@example.com": "did:plc:alice",
				"bob@example.com": "did:plc:bob",
				"MIXED@Example.com": "did:plc:mixed",
				"ghost@example.com": null
			})
		);
	}
	if (url.includes("blue.microcosm.identity.resolveMiniDoc")) {
		const did = new URL(url).searchParams.get("identifier") ?? "";
		return new Response(JSON.stringify({ did, handle: `${did.replace("did:plc:", "")}.test` }));
	}
	return new Response("unexpected", { status: 404 });
});

describe("withAuthorHandles", () => {
	it("resolves verified emails to a did before hydrating handles", async () => {
		const commits = [
			commit({ authorName: "Alice", authorEmail: "alice@example.com" }),
			commit({
				authorName: "Signed",
				authorEmail: "did:plc:signed",
				authorDid: "did:plc:signed"
			})
		];
		const out = await withAuthorHandles(commits, "https://bobbin.test", fetchMock as Fetch);
		expect(out[0]).toMatchObject({ authorDid: "did:plc:alice", authorHandle: "alice.test" });
		expect(out[1]).toMatchObject({ authorDid: "did:plc:signed", authorHandle: "signed.test" });
	});

	it("keeps the requested email case when looking up the kv result", async () => {
		const out = await withAuthorHandles(
			[commit({ authorEmail: "MIXED@Example.com" })],
			"https://bobbin.test",
			fetchMock as Fetch
		);
		expect(out[0]).toMatchObject({ authorDid: "did:plc:mixed", authorHandle: "mixed.test" });
	});

	it("leaves unresolved emails as plain authors", async () => {
		const out = await withAuthorHandles(
			[commit({ authorEmail: "ghost@example.com" })],
			"https://bobbin.test",
			fetchMock as Fetch
		);
		expect(out[0].authorDid).toBeUndefined();
		expect(out[0].authorHandle).toBeUndefined();
	});

	it("fails open when the kv route errors", async () => {
		const broken = vi.fn(async () => new Response("nope", { status: 500 }));
		const out = await withAuthorHandles(
			[commit({ authorEmail: "alice@example.com" })],
			"https://bobbin.test",
			broken as Fetch
		);
		expect(out[0].authorDid).toBeUndefined();
	});

	it("keeps a did from the email pass when handle resolution fails", async () => {
		const brokenBobbin = vi.fn(async (input: string | URL) => {
			if (String(input).startsWith("/_internal/email-did")) {
				return new Response(JSON.stringify({ "alice@example.com": "did:plc:alice" }));
			}
			return new Response("nope", { status: 500 });
		});
		const out = await withAuthorHandles(
			[commit({ authorEmail: "alice@example.com" })],
			"https://bobbin.test",
			brokenBobbin as Fetch
		);
		expect(out[0]).toMatchObject({ authorDid: "did:plc:alice" });
		expect(out[0].authorHandle).toBeUndefined();
	});
});

describe("withAuthorHandles signatures", () => {
	const signKey =
		"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHEisipfUi+zRyW1reo72Rt4+EUbM5mKZrI9/PkyHMpR";
	const authKey =
		"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBKTt0Ub2mIVQbZG0DL1fB6vk1PB8iI5bmNTe7IfBnMO";

	const keysFetch = vi.fn(async (input: string | URL, init?: RequestInit) => {
		if (!String(input).includes("org.tangled.publicKey.listKeys"))
			return fetchMock(input, init);
		const subject = new URL(String(input)).searchParams.get("subject");
		const items =
			subject === "did:plc:alice"
				? [
						{ value: { publicKey: signKey } },
						{ value: { publicKey: authKey, usages: ["auth"] } }
					].map((item, i) => ({
						uri: `at://${subject}/org.tangled.key.sshKey/${i}`,
						...item
					}))
				: [];
		return new Response(JSON.stringify({ items }));
	});

	const verifiedFor = async (
		committerEmail: string,
		key: string | null,
		authorEmail = "bob@example.com"
	) => {
		const [out] = await withAuthorHandles(
			[commit({ authorEmail, committerEmail, signatureFingerprint: key ?? undefined })],
			"https://bobbin.test",
			keysFetch as Fetch
		);
		return out;
	};

	it("verifies a signature made with one of the committer's signing keys", async () => {
		const out = await verifiedFor("alice@example.com", await sshFingerprint(signKey));
		expect(out).toMatchObject({
			authorDid: "did:plc:bob",
			committerDid: "did:plc:alice",
			committerHandle: "alice.test",
			signature: "verified"
		});
	});

	it("still maps the author when the signature does not verify", async () => {
		for (const key of ["SHA256:someoneelse", await sshFingerprint(authKey)]) {
			const out = await verifiedFor("alice@example.com", key);
			expect(out).toMatchObject({
				authorDid: "did:plc:bob",
				committerDid: "did:plc:alice",
				signature: "unverified"
			});
		}
		const notTheirs = await verifiedFor("bob@example.com", await sshFingerprint(signKey));
		expect(notTheirs.signature).toBe("unverified");
	});

	it("leaves unsigned commits without a signature state", async () => {
		const out = await verifiedFor("alice@example.com", null);
		expect(out).toMatchObject({
			authorDid: "did:plc:bob",
			committerDid: "did:plc:alice",
			committerHandle: "alice.test"
		});
		expect(out.signature).toBeUndefined();
	});
});