~james/core

ref: 452f17b75181d62d1d92016edecd38e1d70fecab core/web/src/lib/api/gitclient.test.ts -rw-r--r-- 4.3 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 17 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
import { describe, expect, it, vi } from "vitest";
import {
	blobRawUrl,
	branches,
	gitTarget,
	knotMirrorTarget,
	log,
	requireGit,
	tag,
	tags,
	tree
} from "#lib/api/gitclient.ts";
import type { Fetch } from "#lib/fetch.ts";

const config = { bobbinUrl: "https://bobbin.example" };
const repo = {
	uri: "at://did:plc:owner/sh.tangled.repo/core",
	repoDid: "did:plc:reporepo",
	git: { defaultBranch: "main", isEmpty: false }
};

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

describe("gitTarget", () => {
	it("routes git ops to the mirror surface on bobbin keyed by repo DID", () => {
		const target = gitTarget(config, repo, globalThis.fetch);
		expect(target.ctx.serviceUrl).toBe("https://bobbin.example");
		expect(target.repo).toBe("did:plc:reporepo");
	});

	it("404s when the repo DID is unknown", () => {
		expect(() => gitTarget(config, { uri: repo.uri }, globalThis.fetch)).toThrow(
			expect.objectContaining({ status: 404 })
		);
	});

	it("keys the mirror target by repo DID", () => {
		expect(knotMirrorTarget(config, repo, globalThis.fetch).repo).toBe("did:plc:reporepo");
	});
});

describe("requireGit", () => {
	it("passes a repo that supports git through", () => {
		expect(requireGit(repo)).toBe(repo);
	});

	it("404s a repo without git", () => {
		expect(() => requireGit({ git: undefined })).toThrow(
			expect.objectContaining({ status: 404 })
		);
	});
});

describe("endpoint routing", () => {
	const mirrorTarget = (fetchMock: Fetch) => gitTarget(config, repo, fetchMock);
	const calledUrl = (fetchMock: ReturnType<typeof vi.fn<Fetch>>, call = 0) =>
		new URL(String(fetchMock.mock.calls[call][0]));

	it("sends log to the mirror's listCommitsByRef keyed by DID, skip intact", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(jsonResponse({ commits: [] }));

		await log(mirrorTarget(fetchMock), { ref: "main", limit: 10, skip: 20 });

		const url = calledUrl(fetchMock);
		expect(url.origin).toBe("https://bobbin.example");
		expect(url.pathname).toBe("/xrpc/org.tangled.temp.git.listCommitsByRef");
		expect(url.searchParams.get("repo")).toBe(repo.repoDid);
		expect(url.searchParams.get("ranges")).toBe("main");
		expect(url.searchParams.get("skip")).toBe("20");
	});

	it("sends tree to bobbin's getTree with the path param", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(jsonResponse({ entries: [] }));

		await tree(mirrorTarget(fetchMock), { ref: "main", path: "src/lib" });

		const url = calledUrl(fetchMock);
		expect(url.origin).toBe("https://bobbin.example");
		expect(url.pathname).toBe("/xrpc/org.tangled.temp.git.getTree");
		expect(url.searchParams.get("repo")).toBe(repo.repoDid);
		expect(url.searchParams.get("path")).toBe("src/lib");
	});

	it("sends branches and tags to the list endpoints on the mirror", async () => {
		const fetchMock = vi
			.fn<Fetch>()
			.mockImplementation(() => Promise.resolve(jsonResponse({ branches: [], tags: [] })));
		const target = mirrorTarget(fetchMock);

		await branches(target, 5);
		await tags(target, 5);

		expect(calledUrl(fetchMock).pathname).toBe("/xrpc/org.tangled.temp.git.listBranches");
		expect(calledUrl(fetchMock, 1).pathname).toBe("/xrpc/org.tangled.temp.git.listTags");
	});

	it("routes single-tag reads to getRef on the mirror", async () => {
		const fetchMock = vi
			.fn<Fetch>()
			.mockImplementation(() => Promise.resolve(jsonResponse({})));

		await tag(mirrorTarget(fetchMock), "v1.0.0");
		expect(calledUrl(fetchMock).pathname).toBe("/xrpc/org.tangled.temp.git.getRef");
	});

	it("builds the raw url against the mirror without a raw flag", () => {
		const url = new URL(
			blobRawUrl(mirrorTarget(globalThis.fetch), { ref: "main", path: "README.md" })
		);
		expect(url.origin).toBe("https://bobbin.example");
		expect(url.pathname).toBe("/xrpc/org.tangled.temp.git.getBlob");
		expect(url.searchParams.get("repo")).toBe(repo.repoDid);
		expect(url.searchParams.has("raw")).toBe(false);
		expect(url.searchParams.has("download")).toBe(false);
	});

	it("asks the mirror to name the file when downloading", () => {
		const url = new URL(
			blobRawUrl(mirrorTarget(globalThis.fetch), {
				ref: "main",
				path: "a/b.txt",
				download: true
			})
		);
		expect(url.searchParams.get("download")).toBe("true");
	});
});