~james/core

core/web/src/lib/server/repo.test.ts -rw-r--r-- 1.5 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 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
import { describe, expect, it, vi } from "vitest";
import { resolveRepoViewFromParams } from "#lib/server/repo.ts";
import type { Fetch } from "#lib/fetch.ts";

const OWNER_DID = "did:plc:kelp";

const json = (body: unknown): Response =>
	new Response(JSON.stringify(body), { headers: { "content-type": "application/json" } });

const event = (handle: string) => {
	const fetchMock = vi.fn<Fetch>().mockImplementation(async (input) => {
		const target = String(input);
		if (target.includes("resolveMiniDoc")) {
			return json({ did: OWNER_DID, handle: "handle.invalid" });
		}
		if (target.includes("getRepoByName")) {
			return json({
				uri: "at://did:plc:reef/org.tangled.repo.manifest/self",
				value: {
					$type: "org.tangled.repo.manifest",
					declaration: { owner: OWNER_DID, name: "reef" },
					createdAt: "2026-10-01T00:00:00Z"
				}
			});
		}
		return json({});
	});
	return {
		url: new URL(`http://web.test/${handle}/reef`),
		params: { handle, repo: "reef" },
		fetch: fetchMock
	};
};

describe("resolveRepoViewFromParams", () => {
	it("accepts DID as handle param", async () => {
		const { doc, view } = await resolveRepoViewFromParams(event(OWNER_DID) as never);

		expect(doc.did).toBe(OWNER_DID);
		expect(view.uri).toBe("at://did:plc:reef/org.tangled.repo.manifest/self");
	});

	it("404 on BS instead of tryna resolve as actors", async () => {
		const bare = event("bareword");

		await expect(resolveRepoViewFromParams(bare as never)).rejects.toMatchObject({
			status: 404
		});
		expect(bare.fetch).not.toHaveBeenCalled();
	});
});