~james/core

core/web/src/lib/api/pullCompose.test.ts -rw-r--r-- 7.1 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
import { describe, expect, it, vi } from "vitest";
import {
	composeQuery,
	defaultSourceBranch,
	defaultTargetBranch,
	loadCompose,
	parseSource,
	sortBranchesByRecency,
	sourceBranchChoices
} from "#lib/api/pullCompose.ts";
import type { BranchSummary } from "#lib/api/repo.ts";
import type { Fetch } from "#lib/fetch.ts";

const branch = (name: string, when?: string, isDefault = false): BranchSummary => ({
	name,
	hash: `hash-${name}`,
	when,
	isDefault
});

// main is the default; feat is the newest of the rest
const branches = [
	branch("main", "2024-01-01T00:00:00Z", true),
	branch("old", "2024-01-02T00:00:00Z"),
	branch("feat", "2024-03-01T00:00:00Z")
];

const names = (list: BranchSummary[]) => list.map((entry) => entry.name);

describe("sortBranchesByRecency", () => {
	it("puts the newest commit first", () => {
		expect(names(sortBranchesByRecency(branches))).toEqual(["feat", "old", "main"]);
	});

	it("sinks branches with no commit without reordering them", () => {
		const list = [
			branch("empty-a"),
			branch("dated", "2024-01-01T00:00:00Z"),
			branch("empty-b")
		];
		expect(names(sortBranchesByRecency(list))).toEqual(["dated", "empty-a", "empty-b"]);
	});
});

describe("sourceBranchChoices", () => {
	it("drops the default branch and sorts the rest", () => {
		expect(names(sourceBranchChoices(branches))).toEqual(["feat", "old"]);
	});
});

describe("defaultTargetBranch", () => {
	it("keeps a value that names a real branch", () => {
		expect(defaultTargetBranch(branches, "old")).toBe("old");
	});

	it("falls back to the default branch when the value is unknown or absent", () => {
		expect(defaultTargetBranch(branches, "gone")).toBe("main");
		expect(defaultTargetBranch(branches, "")).toBe("main");
	});

	it("gives up when no branch is marked default", () => {
		expect(defaultTargetBranch([branch("a", "2024-01-01T00:00:00Z")], "")).toBe("");
	});
});

describe("defaultSourceBranch", () => {
	const choices = sourceBranchChoices(branches);
	const forkBranches = [
		branch("fork-old", "2024-01-01T00:00:00Z"),
		branch("fork-new", "2024-05-01T00:00:00Z")
	];

	it("picks the most recent non-default branch", () => {
		expect(defaultSourceBranch("branch", "", choices, [])).toBe("feat");
	});

	it("replaces a value that is not a candidate", () => {
		// main is the default branch, so it never appears in the source list
		expect(defaultSourceBranch("branch", "main", choices, [])).toBe("feat");
	});

	it("keeps a value that is a candidate", () => {
		expect(defaultSourceBranch("branch", "old", choices, [])).toBe("old");
	});

	it("reads the fork's branches in fork mode", () => {
		expect(defaultSourceBranch("fork", "", choices, forkBranches)).toBe("fork-old");
		expect(defaultSourceBranch("fork", "fork-new", choices, forkBranches)).toBe("fork-new");
		// a branch of the target repo is not a candidate against a fork
		expect(defaultSourceBranch("fork", "feat", choices, forkBranches)).toBe("fork-old");
	});

	it("leaves the value alone in patch mode", () => {
		expect(defaultSourceBranch("patch", "whatever", choices, forkBranches)).toBe("whatever");
	});

	it("returns nothing when there are no candidates", () => {
		expect(defaultSourceBranch("branch", "feat", [], [])).toBe("");
	});
});

describe("parseSource", () => {
	it("accepts the three modes case-insensitively", () => {
		expect(parseSource("branch")).toBe("branch");
		expect(parseSource("FORK")).toBe("fork");
		expect(parseSource("patch")).toBe("patch");
	});

	it("rejects anything else", () => {
		expect(parseSource("")).toBeNull();
		expect(parseSource(null)).toBeNull();
		expect(parseSource("stack")).toBeNull();
	});
});

describe("composeQuery", () => {
	it("omits the default source and a fork picked outside fork mode", () => {
		expect(
			composeQuery({
				source: "branch",
				sourceBranch: "feat",
				targetBranch: "main",
				fork: "did:x"
			})
		).toBe("sourceBranch=feat&targetBranch=main");
	});

	it("carries the fork only in fork mode", () => {
		expect(
			composeQuery({
				source: "fork",
				sourceBranch: "feat",
				targetBranch: "main",
				fork: "did:x"
			})
		).toBe("source=fork&sourceBranch=feat&targetBranch=main&fork=did%3Ax");
	});

	it("is empty when nothing is selected", () => {
		expect(
			composeQuery({ source: "branch", sourceBranch: "", targetBranch: "", fork: "" })
		).toBe("");
	});
});

describe("loadCompose", () => {
	const UPSTREAM = "did:plc:upstream";
	const FORK = "did:plc:fork";
	const upstreamMain = "a".repeat(40);
	const forkMain = "f".repeat(40);
	const mergeBase = "b".repeat(40);
	const signature = { name: "alice", email: "alice@example.com", when: "2026-01-01T00:00:00Z" };

	it("compares a fork branch against the same-named upstream branch by commit id", async () => {
		const fetchMock = vi.fn<Fetch>(async (input) => {
			const url = new URL(String(input));
			const param = (name: string) => url.searchParams.get(name);
			switch (url.pathname.replace(/^\/xrpc\//, "")) {
				case "org.tangled.temp.git.listBranches":
					return Response.json({
						defaultBranch: "main",
						branches: [
							{
								name: "main",
								oid: param("repo") === FORK ? forkMain : upstreamMain,
								committer: signature,
								message: "tip"
							}
						]
					});
				case "org.tangled.repo.listRepos":
					return Response.json({
						items: [
							{
								uri: `at://${FORK}/sh.tangled.repo/self`,
								value: { source: { did: UPSTREAM }, declaration: { name: "core" } }
							}
						]
					});
				case "org.tangled.temp.git.listCommits":
					return Response.json({
						commits: [
							{
								oid: forkMain,
								parents: [mergeBase],
								tree: "tree",
								author: signature,
								committer: signature,
								message: "fork only",
								extraHeaders: []
							}
						]
					});
				case "org.tangled.temp.git.getMergeBase":
					return Response.json({ commit: mergeBase });
				default:
					return Response.json({ error: "NotFound" }, { status: 404 });
			}
		});

		const data = await loadCompose({
			config: { bobbinUrl: "https://bobbin.test" },
			repo: {
				uri: `at://${UPSTREAM}/sh.tangled.repo/self`,
				repoDid: UPSTREAM,
				ownerHandle: "upstream.test",
				name: "core",
				knot: "knot.test"
			} as never,
			viewer: { did: "did:plc:alice", handle: "alice.test" },
			params: new URLSearchParams({
				source: "fork",
				fork: FORK,
				sourceBranch: "main",
				targetBranch: "main"
			}),
			fetch: fetchMock
		});

		const call = (nsid: string) =>
			fetchMock.mock.calls
				.map(([input]) => new URL(String(input)))
				.find((url) => url.pathname === `/xrpc/${nsid}`)!;
		const commits = call("org.tangled.temp.git.listCommits");
		expect(commits.searchParams.getAll("repos")).toEqual([FORK, UPSTREAM]);
		expect(commits.searchParams.getAll("ranges")).toEqual([`${upstreamMain}..${forkMain}`]);
		const base = call("org.tangled.temp.git.getMergeBase");
		expect(base.searchParams.getAll("repos")).toEqual([FORK, UPSTREAM]);
		expect(base.searchParams.get("base")).toBe(upstreamMain);
		expect(base.searchParams.get("head")).toBe(forkMain);

		expect(data.commitsError).toBe("");
		expect(data.commits.map((commit) => commit.hash)).toEqual([forkMain]);
		expect(data.mergeBase).toBe(mergeBase);
		expect(data.sourceHead).toBe(forkMain);
	});
});