|
|
@@ -0,0 +1,152 @@ |
| 1 |
import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 2 |
import { mkdtemp, rm } from "node:fs/promises"; |
| 3 |
import { tmpdir } from "node:os"; |
| 4 |
import { join } from "node:path"; |
| 5 |
import { createApp, type App } from "../src/app.ts"; |
| 6 |
import { serve, type Servers } from "../src/http/server.ts"; |
| 7 |
import { checksLine } from "../src/web/pages/pull.ts"; |
| 8 |
import { conflictsPhrase } from "../src/web/studio/words.ts"; |
| 9 |
import { gitClientEnv } from "./helpers/git-client.ts"; |
| 10 |
|
| 11 |
const gitEnv = gitClientEnv(); |
| 12 |
|
| 13 |
describe("a pull that conflicts with its base says so first", () => { |
| 14 |
let dataDir: string; |
| 15 |
let work: string; |
| 16 |
let app: App; |
| 17 |
let servers: Servers; |
| 18 |
let base: string; |
| 19 |
let cookie = ""; |
| 20 |
let token = ""; |
| 21 |
let dir: string; |
| 22 |
|
| 23 |
async function git(args: string[]): Promise<void> { |
| 24 |
const p = Bun.spawn(["git", ...args], { |
| 25 |
cwd: dir, |
| 26 |
env: gitEnv, |
| 27 |
stdout: "ignore", |
| 28 |
stderr: "pipe", |
| 29 |
}); |
| 30 |
const [err, code] = await Promise.all([ |
| 31 |
new Response(p.stderr).text(), |
| 32 |
p.exited, |
| 33 |
]); |
| 34 |
if (code !== 0) throw new Error(`git ${args.join(" ")}: ${err}`); |
| 35 |
} |
| 36 |
const api = async (path: string) => |
| 37 |
(await ( |
| 38 |
await fetch(`${base}/api/v1/repos/dev/app${path}`, { |
| 39 |
headers: { authorization: `Bearer ${token}` }, |
| 40 |
}) |
| 41 |
).json()) as { |
| 42 |
mergeable: boolean; |
| 43 |
conflicts: string[]; |
| 44 |
status: { mergeable: boolean }; |
| 45 |
}; |
| 46 |
|
| 47 |
beforeAll(async () => { |
| 48 |
dataDir = await mkdtemp(join(tmpdir(), "forge-pc-")); |
| 49 |
work = await mkdtemp(join(tmpdir(), "forge-pc-work-")); |
| 50 |
app = createApp({ dataDir, port: 0 }); |
| 51 |
servers = await serve(app); |
| 52 |
base = servers.url; |
| 53 |
const signup = await fetch(`${base}/signup`, { |
| 54 |
method: "POST", |
| 55 |
body: new URLSearchParams({ |
| 56 |
username: "dev", |
| 57 |
email: "[email protected]", |
| 58 |
password: "pull-request-pass", |
| 59 |
}), |
| 60 |
redirect: "manual", |
| 61 |
}); |
| 62 |
cookie = (signup.headers.get("set-cookie") ?? "").split(";")[0]!; |
| 63 |
token = await fetch(`${base}/api/v1/tokens`, { |
| 64 |
method: "POST", |
| 65 |
headers: { "content-type": "application/json" }, |
| 66 |
body: JSON.stringify({ |
| 67 |
adminConfirm: true, |
| 68 |
username: "dev", |
| 69 |
password: "pull-request-pass", |
| 70 |
name: "cli", |
| 71 |
scope: "rw", |
| 72 |
}), |
| 73 |
}) |
| 74 |
.then((r) => r.json()) |
| 75 |
.then((j) => (j as { token: string }).token); |
| 76 |
await fetch(`${base}/api/v1/repos`, { |
| 77 |
method: "POST", |
| 78 |
headers: { |
| 79 |
authorization: `Bearer ${token}`, |
| 80 |
"content-type": "application/json", |
| 81 |
}, |
| 82 |
body: JSON.stringify({ name: "app", visibility: "public" }), |
| 83 |
}); |
| 84 |
const remote = `${base.replace("http://", `http: |
| 85 |
dir = work; |
| 86 |
await git(["clone", remote, "app"]); |
| 87 |
dir = join(work, "app"); |
| 88 |
await Bun.write(join(dir, "code.txt"), "alpha\nbeta\ngamma\n"); |
| 89 |
await git(["add", "."]); |
| 90 |
await git(["commit", "-m", "base"]); |
| 91 |
await git(["push", "origin", "main"]); |
| 92 |
}); |
| 93 |
|
| 94 |
afterAll(async () => { |
| 95 |
servers.stop(); |
| 96 |
app.close(); |
| 97 |
await rm(dataDir, { recursive: true, force: true }); |
| 98 |
await rm(work, { recursive: true, force: true }); |
| 99 |
}); |
| 100 |
|
| 101 |
test("conflicted in the API and on the page; resolving clears both", async () => { |
| 102 |
await git(["checkout", "-b", "side"]); |
| 103 |
await Bun.write(join(dir, "code.txt"), "alpha\nbeta\nGAMMA-side\n"); |
| 104 |
await git(["commit", "-am", "side"]); |
| 105 |
await git(["push", "origin", "side"]); |
| 106 |
await git(["checkout", "main"]); |
| 107 |
await Bun.write(join(dir, "code.txt"), "alpha\nbeta\nGAMMA-main\n"); |
| 108 |
await git(["commit", "-am", "main"]); |
| 109 |
await git(["push", "origin", "main"]); |
| 110 |
|
| 111 |
const created = await fetch(`${base}/dev/app/pulls/new`, { |
| 112 |
method: "POST", |
| 113 |
headers: { cookie }, |
| 114 |
body: new URLSearchParams({ |
| 115 |
base: "main", |
| 116 |
head: "side", |
| 117 |
title: "Side", |
| 118 |
body: "", |
| 119 |
}), |
| 120 |
redirect: "manual", |
| 121 |
}); |
| 122 |
const loc = created.headers.get("location")!; |
| 123 |
|
| 124 |
const sick = await api("/pulls/1"); |
| 125 |
expect(sick.mergeable).toBe(false); |
| 126 |
expect(sick.conflicts).toEqual(["code.txt"]); |
| 127 |
let page = await (await fetch(`${base}${loc}/files`)).text(); |
| 128 |
expect(page).toContain("Conflicts in 1 file"); |
| 129 |
expect(page).not.toContain("Checks passed"); |
| 130 |
|
| 131 |
|
| 132 |
await git(["checkout", "side"]); |
| 133 |
await git(["merge", "main", "-X", "theirs", "-m", "resolve"]); |
| 134 |
await git(["push", "origin", "side"]); |
| 135 |
const well = await api("/pulls/1"); |
| 136 |
expect(well.mergeable).toBe(true); |
| 137 |
expect(well.conflicts).toEqual([]); |
| 138 |
page = await (await fetch(`${base}${loc}/files`)).text(); |
| 139 |
expect(page).not.toContain("Conflicts in 1 file"); |
| 140 |
}); |
| 141 |
|
| 142 |
test("a conflict outranks a passing check on every line that shows one", () => { |
| 143 |
const check = { state: "ok", run: null } as never; |
| 144 |
const line = String(checksLine("/dev/app", check, true, true, false, 2)); |
| 145 |
expect(line).toContain("Conflicts in 2 files"); |
| 146 |
expect(line).not.toContain("Checks passed"); |
| 147 |
expect(String(checksLine("/dev/app", check, true, true))).toContain( |
| 148 |
"Checks passed", |
| 149 |
); |
| 150 |
expect(conflictsPhrase(1)).toBe("Conflicts in 1 file"); |
| 151 |
}); |
| 152 |
}); |