Public: anyone can read this; the crew can change it.

A test pins finishing a conflict from the forge: a stale head, a pull with no conflict, or the wrong path is refused #7100

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/7088-a-test-pins-finishing-a-conflict-from-th → main 1 commit fork

  • Merged. scout/7088-a-test-pins-finishing-a-conflict-from-th was merged into main as 6e131ab.

Review

Sign in to review this change.
1 file changed +157 −0
test/finish-conflicts-refusals.test.ts added +157
@@ -0,0 +1,157 @@
1 /**
2 * Finishing a conflict from the forge (#7088): one conflicted file and its
3 * resolution makes the pull mergeable; a stale page, a pull with no conflict
4 * and a path that is not the one conflicting file are each refused, and the
5 * head branch is untouched after every refusal.
6 */
7 import { afterAll, beforeAll, expect, test } from "bun:test";
8 import { mkdtemp, rm } from "node:fs/promises";
9 import { tmpdir } from "node:os";
10 import { join } from "node:path";
11 import { createApp, type App } from "../src/app.ts";
12 import { finishConflicts } from "../src/actions/finish-conflicts.ts";
13 import { openPull } from "../src/actions/pulls.ts";
14 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
15 import { pullStatus, type Pull } from "../src/core/pulls.ts";
16 import type { Repo } from "../src/core/repos.ts";
17 import { createUser, type User } from "../src/core/users.ts";
18 import { repoDir } from "../src/git/repos.ts";
19
20 let dataDir: string;
21 let app: App;
22 let alice: User;
23 let n = 0;
24
25 const ours = { kind: "ours" } as const;
26
27 async function commit(repo: Repo, branch: string, files: Record<string, string>, base?: string) {
28 const r = await commitToBranch(app, alice, repo, {
29 branch,
30 ...(base ? { base } : {}),
31 message: `Write ${Object.keys(files).join(", ")}`,
32 files: Object.entries(files).map(([path, content]) => ({ path, content })),
33 });
34 if (!r.ok) throw new Error(JSON.stringify(r.error));
35 }
36
37 /** A repo with a pull whose head and main each rewrote every file in `files`. */
38 async function conflicted(files: string[]): Promise<{ repo: Repo; pull: Pull; dir: string }> {
39 const made = await createRepo(app, alice, { name: `r${++n}`, visibility: "private" });
40 if (!made.ok) throw new Error(String(made.error));
41 const repo = made.value;
42 const dir = repoDir(app.reposRoot, repo.ownerName, repo.name);
43 await commit(repo, "main", Object.fromEntries(files.map((f) => [f, "line\n"])));
44 await commit(repo, "feature", Object.fromEntries(files.map((f) => [f, "feature\n"])), "main");
45 await commit(repo, "main", Object.fromEntries(files.map((f) => [f, "main\n"])));
46 const opened = await openPull(app, alice, repo, {
47 title: "Conflicts",
48 body: "",
49 baseBranch: "main",
50 headBranch: "feature",
51 });
52 if (!opened.ok) throw new Error(String(opened.error));
53 return { repo, pull: opened.value, dir };
54 }
55
56 /** What the page read: the live tips. */
57 async function seen(dir: string, pull: Pull) {
58 const s = await pullStatus(dir, pull);
59 return { headOid: s.headOid, baseOid: s.baseOid, status: s };
60 }
61
62 beforeAll(async () => {
63 dataDir = await mkdtemp(join(tmpdir(), "forge-finish-refusals-"));
64 app = createApp({ dataDir, port: 0 });
65 const a = await createUser(app.db, {
66 origin: "seed",
67 trusted: true,
68 username: "alice",
69 email: "[email protected]",
70 password: "a-good-passphrase",
71 adminIfFirst: true,
72 });
73 if (!a.ok) throw new Error(a.error);
74 alice = a.value;
75 }, 30_000);
76
77 afterAll(async () => {
78 app.close();
79 await rm(dataDir, { recursive: true, force: true });
80 });
81
82 test("a pull that conflicts on one file is finished with that file's resolution and merges", async () => {
83 const { repo, pull, dir } = await conflicted(["a.txt"]);
84 const { headOid, baseOid, status } = await seen(dir, pull);
85 expect(status.conflicts).toEqual(["a.txt"]);
86 expect(status.mergeable).toBe(false);
87 const done = await finishConflicts(app, alice, repo, pull, {
88 headOid,
89 baseOid,
90 path: "a.txt",
91 choices: [ours],
92 });
93 expect(done.ok).toBe(true);
94 const after = await pullStatus(dir, pull);
95 expect(after.mergeable).toBe(true);
96 expect(after.conflicts).toEqual([]);
97 expect(after.headOid).not.toBe(headOid);
98 });
99
100 test("a head that moved since the page was read is stale", async () => {
101 const { repo, pull, dir } = await conflicted(["a.txt"]);
102 const { headOid, baseOid } = await seen(dir, pull);
103 await commit(repo, "feature", { "a.txt": "feature again\n" });
104 const moved = (await pullStatus(dir, pull)).headOid;
105 const r = await finishConflicts(app, alice, repo, pull, { headOid, baseOid, path: "a.txt", choices: [ours] });
106 expect(r).toEqual({ ok: false, error: "stale" });
107 expect((await pullStatus(dir, pull)).headOid).toBe(moved);
108 });
109
110 test("a base that moved since the page was read is stale", async () => {
111 const { repo, pull, dir } = await conflicted(["a.txt"]);
112 const { headOid, baseOid } = await seen(dir, pull);
113 await commit(repo, "main", { "b.txt": "elsewhere\n" });
114 const r = await finishConflicts(app, alice, repo, pull, { headOid, baseOid, path: "a.txt", choices: [ours] });
115 expect(r).toEqual({ ok: false, error: "stale" });
116 expect((await pullStatus(dir, pull)).headOid).toBe(headOid);
117 });
118
119 test("a pull with no conflict is refused as no_conflicts", async () => {
120 const made = await createRepo(app, alice, { name: `r${++n}`, visibility: "private" });
121 if (!made.ok) throw new Error(String(made.error));
122 const repo = made.value;
123 const dir = repoDir(app.reposRoot, repo.ownerName, repo.name);
124 await commit(repo, "main", { "a.txt": "line\n" });
125 await commit(repo, "feature", { "b.txt": "new\n" }, "main");
126 const opened = await openPull(app, alice, repo, {
127 title: "Clean",
128 body: "",
129 baseBranch: "main",
130 headBranch: "feature",
131 });
132 if (!opened.ok) throw new Error(String(opened.error));
133 const { headOid, baseOid, status } = await seen(dir, opened.value);
134 expect(status.conflicts).toEqual([]);
135 const r = await finishConflicts(app, alice, repo, opened.value, { headOid, baseOid, path: "a.txt", choices: [ours] });
136 expect(r).toEqual({ ok: false, error: "no_conflicts" });
137 expect((await pullStatus(dir, opened.value)).headOid).toBe(headOid);
138 });
139
140 test("a path that is not the conflicting file is not_resolvable", async () => {
141 const { repo, pull, dir } = await conflicted(["a.txt"]);
142 const { headOid, baseOid } = await seen(dir, pull);
143 const r = await finishConflicts(app, alice, repo, pull, { headOid, baseOid, path: "other.txt", choices: [ours] });
144 expect(r).toEqual({ ok: false, error: "not_resolvable" });
145 expect((await pullStatus(dir, pull)).headOid).toBe(headOid);
146 });
147
148 test("a pull that conflicts on two files is not_resolvable, whichever path is sent", async () => {
149 const { repo, pull, dir } = await conflicted(["a.txt", "b.txt"]);
150 const { headOid, baseOid, status } = await seen(dir, pull);
151 expect(status.conflicts.length).toBe(2);
152 for (const path of ["a.txt", "b.txt"]) {
153 const r = await finishConflicts(app, alice, repo, pull, { headOid, baseOid, path, choices: [ours] });
154 expect(r).toEqual({ ok: false, error: "not_resolvable" });
155 expect((await pullStatus(dir, pull)).headOid).toBe(headOid);
156 }
157 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs