|
|
@@ -1,512 +1,54 @@ |
| 1 | 1 |
|
| 2 | 2 |
|
| 3 | 3 |
|
| 4 | |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 5 | 8 |
|
| 6 | 9 |
|
| 7 | |
import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 8 | |
import { existsSync, mkdirSync, writeFileSync } from "node:fs"; |
| 10 |
import { describe, expect, test } from "bun:test"; |
| 9 | 11 |
import { mkdtemp, rm } from "node:fs/promises"; |
| 10 | 12 |
import { tmpdir } from "node:os"; |
| 11 | 13 |
import { join } from "node:path"; |
| 12 | |
import { createApp, type App } from "../src/app.ts"; |
| 13 | |
import { mergePull, openPull, submitReview } from "../src/actions/pulls.ts"; |
| 14 | |
import { setIssueState } from "../src/actions/issues.ts"; |
| 14 |
import { createApp } from "../src/app.ts"; |
| 15 |
import { openPull, submitReview } from "../src/actions/pulls.ts"; |
| 15 | 16 |
import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts"; |
| 16 | |
import { parseBunTestFailure } from "../src/checks/bun-test.ts"; |
| 17 | |
import { summariseCheckFailure } from "../src/checks/summary.ts"; |
| 18 | |
import { SLOW_SECONDS, summaryLines } from "../scripts/gate-summary.ts"; |
| 19 | |
import { tally } from "../scripts/shards.ts"; |
| 20 | |
import { |
| 21 | |
holdsLastSlot, |
| 22 | |
pickNext, |
| 23 | |
requeueAbandonedChecks, |
| 24 | |
startChecks, |
| 25 | |
sweepSoon, |
| 26 | |
STARVATION_MS, |
| 27 | |
type ChecksHandle, |
| 28 | |
} from "../src/checks/service.ts"; |
| 29 | |
import { |
| 30 | |
cancelRun, |
| 31 | |
cancelRunsForPull, |
| 32 | |
CHECK_RESTART_LIMIT, |
| 33 | |
checkSummary, |
| 34 | |
checksForHead, |
| 35 | |
claimRun, |
| 36 | |
createRun, |
| 37 | |
getRun, |
| 38 | |
listRuns, |
| 39 | |
REQUEUED_SUMMARY, |
| 40 | |
type Run, |
| 41 | |
} from "../src/core/runs.ts"; |
| 42 | |
import type { Pull } from "../src/core/pulls.ts"; |
| 17 |
import { startChecks } from "../src/checks/service.ts"; |
| 18 |
import { checkSummary, checksForHead, listRuns, type Run } from "../src/core/runs.ts"; |
| 43 | 19 |
import { checksText } from "../src/tui/screens/pull.ts"; |
| 44 | 20 |
import { checksLine } from "../src/web/pages/pull.ts"; |
| 45 | |
import { repoDir } from "../src/git/repos.ts"; |
| 46 | |
import { STALE_AFTER_MS } from "../src/runs/lease.ts"; |
| 47 | |
import { pullWord, servingHistory } from "../src/web/studio/words.ts"; |
| 48 | |
import { createUser, type User } from "../src/core/users.ts"; |
| 49 | |
import { getRepo, type Repo } from "../src/core/repos.ts"; |
| 50 | |
import { serve, type Servers } from "../src/http/server.ts"; |
| 21 |
import { createUser } from "../src/core/users.ts"; |
| 22 |
import { getRepo } from "../src/core/repos.ts"; |
| 23 |
import { serve } from "../src/http/server.ts"; |
| 51 | 24 |
import { listEvents } from "../src/core/events.ts"; |
| 52 | 25 |
import { readRunLog } from "../src/runs/log.ts"; |
| 53 | |
import { promote } from "./helpers/members.ts"; |
| 54 | |
|
| 55 | |
const checkJson = (run: string, extra = ""): string => |
| 56 | |
`{"check": {"run": ${JSON.stringify(run)}, "install": ""${extra}}}\n`; |
| 57 | |
|
| 58 | |
describe("cancelRunsForPull: a landed or closed pull leaves nothing in the lane", () => { |
| 59 | |
test("queued and running checks and queued deploys of that pull are cancelled, building deploys and other pulls kept", async () => { |
| 60 | |
|
| 61 | |
const dir = await mkdtemp(join(tmpdir(), "forge-cancel-")); |
| 62 | |
const db = createApp({ dataDir: dir, port: 0 }); |
| 63 | |
try { |
| 64 | |
const u = await createUser(db.db, { |
| 65 | |
origin: "seed", |
| 66 | |
trusted: true, |
| 67 | |
username: "alice", |
| 68 | |
email: "[email protected]", |
| 69 | |
password: "a-good-passphrase", |
| 70 | |
adminIfFirst: true, |
| 71 | |
}); |
| 72 | |
if (!u.ok) throw new Error(u.error); |
| 73 | |
const made = await createRepo(db, u.value, { name: "lib" }); |
| 74 | |
if (!made.ok) throw new Error(String(made.error)); |
| 75 | |
const repoId = made.value.id; |
| 76 | |
const first = await commitToBranch(db, u.value, made.value, { |
| 77 | |
message: "Start", |
| 78 | |
files: [{ path: "README.md", content: "x\n" }], |
| 79 | |
}); |
| 80 | |
if (!first.ok) throw new Error(JSON.stringify(first.error)); |
| 81 | |
const pullOn = async (branch: string): Promise<string> => { |
| 82 | |
const c = await commitToBranch(db, u.value, made.value, { |
| 83 | |
branch, |
| 84 | |
message: branch, |
| 85 | |
files: [{ path: `${branch}.md`, content: "x\n" }], |
| 86 | |
}); |
| 87 | |
if (!c.ok) throw new Error(JSON.stringify(c.error)); |
| 88 | |
const o = await openPull(db, u.value, made.value, { |
| 89 | |
title: branch, |
| 90 | |
baseBranch: "main", |
| 91 | |
headBranch: branch, |
| 92 | |
}); |
| 93 | |
if (!o.ok) throw new Error(String(o.error)); |
| 94 | |
return o.value.id; |
| 95 | |
}; |
| 96 | |
const p1 = await pullOn("one"); |
| 97 | |
const p2 = await pullOn("two"); |
| 98 | |
const mk = (kind: "check" | "deploy", issueId: string) => |
| 99 | |
createRun(db.db, { repoId, kind, env: "pull-1", issueId }); |
| 100 | |
const queued = mk("check", p1); |
| 101 | |
const running = mk("check", p1); |
| 102 | |
claimRun(db.db, running.id); |
| 103 | |
const deploy = mk("deploy", p1); |
| 104 | |
const building = mk("deploy", p1); |
| 105 | |
claimRun(db.db, building.id); |
| 106 | |
const other = mk("check", p2); |
| 107 | |
const stopped = cancelRunsForPull(db.db, p1, "pull landed"); |
| 108 | |
expect(stopped.map((r) => r.id).sort()).toEqual( |
| 109 | |
[queued.id, running.id, deploy.id].sort(), |
| 110 | |
); |
| 111 | |
|
| 112 | |
expect(getRun(db.db, building.id)?.state).toBe("running"); |
| 113 | |
for (const r of [queued, running, deploy]) { |
| 114 | |
expect(getRun(db.db, r.id)?.state).toBe("cancelled"); |
| 115 | |
expect(getRun(db.db, r.id)?.summary).toBe("pull landed"); |
| 116 | |
} |
| 117 | |
expect(getRun(db.db, other.id)?.state).toBe("queued"); |
| 118 | |
} finally { |
| 119 | |
db.close(); |
| 120 | |
await rm(dir, { recursive: true, force: true }); |
| 121 | |
} |
| 122 | |
}); |
| 123 | |
}); |
| 124 | |
|
| 125 | |
describe("parseBunTestFailure: a convenience over one known runner, not a general protocol", () => { |
| 126 | |
test("bun test's own tail: the counts, and the failing names bounded to a few", () => { |
| 127 | |
const lines = [ |
| 128 | |
"bun test v1.3.14 (0d9b296a)", |
| 129 | |
"", |
| 130 | |
"check.test.ts:", |
| 131 | |
"(fail) first [0.16ms]", |
| 132 | |
"(fail) second [0.07ms]", |
| 133 | |
"(fail) third [0.05ms]", |
| 134 | |
"(fail) fourth [0.04ms]", |
| 135 | |
"", |
| 136 | |
" 3 pass", |
| 137 | |
" 4 fail", |
| 138 | |
" 7 expect() calls", |
| 139 | |
"Ran 7 tests across 1 file. [51.00ms]", |
| 140 | |
]; |
| 141 | |
expect(parseBunTestFailure(lines)).toEqual({ |
| 142 | |
passed: 3, |
| 143 | |
failed: 4, |
| 144 | |
names: ["first", "second", "third"], |
| 145 | |
}); |
| 146 | |
}); |
| 147 | |
|
| 148 | |
test("a name arbitrary text from a subprocess, kept verbatim — escaping is the page's job, not the parser's", () => { |
| 149 | |
const lines = [ |
| 150 | |
"(fail) <script>alert(1)</script> [1.00ms]", |
| 151 | |
" 0 pass", |
| 152 | |
" 1 fail", |
| 153 | |
"Ran 1 test across 1 file. [1.00ms]", |
| 154 | |
]; |
| 155 | |
expect(parseBunTestFailure(lines)?.names).toEqual([ |
| 156 | |
"<script>alert(1)</script>", |
| 157 | |
]); |
| 158 | |
}); |
| 159 | |
|
| 160 | |
test("a failing test bun printed without a duration — as it does under CI for one that took no time — is still named", () => { |
| 161 | |
const lines = [ |
| 162 | |
"(pass) a passing test", |
| 163 | |
"(fail) first failing test [0.10ms]", |
| 164 | |
"(fail) second failing test", |
| 165 | |
" 1 pass", |
| 166 | |
" 2 fail", |
| 167 | |
"Ran 3 tests across 1 file. [9.00ms]", |
| 168 | |
]; |
| 169 | |
expect(parseBunTestFailure(lines)).toEqual({ |
| 170 | |
passed: 1, |
| 171 | |
failed: 2, |
| 172 | |
names: ["first failing test", "second failing test"], |
| 173 | |
}); |
| 174 | |
}); |
| 175 | |
|
| 176 | |
test("a shard's named failure leads; the (unnamed) fallout behind it is not named", () => { |
| 177 | |
const lines = [ |
| 178 | |
"── shard 1/1 (exit 1, 3s): test/a.test.ts", |
| 179 | |
"test/a.test.ts:", |
| 180 | |
"(fail) a > writes the row [2ms]", |
| 181 | |
"error: database is closed", |
| 182 | |
"(fail) (unnamed) [1ms]", |
| 183 | |
" 4 pass", |
| 184 | |
" 2 fail", |
| 185 | |
]; |
| 186 | |
expect(parseBunTestFailure(lines)?.names).toEqual(["a > writes the row"]); |
| 187 | |
|
| 188 | |
const only = ["test/b.test.ts:", "error: boom", "(fail) (unnamed)", " 0 pass", " 1 fail"]; |
| 189 | |
expect(parseBunTestFailure(only)?.names).toEqual(["test/b.test.ts > (unnamed): error: boom"]); |
| 190 | |
}); |
| 191 | |
|
| 192 | |
test("shards that finished out of order are named in shard order, each shard's first failure first", () => { |
| 193 | |
const lines = [ |
| 194 | |
"── shard 3/3 (exit 1, 2s): test/c.test.ts", |
| 195 | |
"test/c.test.ts:", |
| 196 | |
"(fail) c one [1ms]", |
| 197 | |
"(fail) c two [1ms]", |
| 198 | |
" 1 pass", |
| 199 | |
" 2 fail", |
| 200 | |
"── shard 1/3 (exit 1, 9s): test/a.test.ts", |
| 201 | |
"test/a.test.ts:", |
| 202 | |
"error: boom", |
| 203 | |
"(fail) (unnamed) [1ms]", |
| 204 | |
" 3 pass", |
| 205 | |
" 1 fail", |
| 206 | |
"── shard 2/3 (exit 0, 5s): test/b.test.ts", |
| 207 | |
" 7 pass", |
| 208 | |
" 0 fail", |
| 209 | |
"2 of 3 shards failed:", |
| 210 | |
"(fail) test/a.test.ts > (unnamed): error: boom [1ms] [shard 1]", |
| 211 | |
"(fail) c one [1ms] [shard 3]", |
| 212 | |
"(fail) c two [1ms] [shard 3]", |
| 213 | |
]; |
| 214 | |
expect(parseBunTestFailure(lines)).toEqual({ |
| 215 | |
passed: 11, |
| 216 | |
failed: 3, |
| 217 | |
names: ["test/a.test.ts > (unnamed): error: boom", "c one", "c two"], |
| 218 | |
}); |
| 219 | |
}); |
| 220 | |
|
| 221 | |
test("output that is not bun test's own shape is not guessed at", () => { |
| 222 | |
expect( |
| 223 | |
parseBunTestFailure([ |
| 224 | |
"$ test -f NOPE.md", |
| 225 | |
"test: NOPE.md: No such file or directory", |
| 226 | |
]), |
| 227 | |
).toBeUndefined(); |
| 228 | |
|
| 229 | |
expect( |
| 230 | |
parseBunTestFailure([" 5 pass", "Ran 5 tests across 1 file."]), |
| 231 | |
).toBeUndefined(); |
| 232 | |
|
| 233 | |
expect( |
| 234 | |
parseBunTestFailure([" 5 pass", " 0 fail", "Ran 5 tests across 1 file."]), |
| 235 | |
).toBeUndefined(); |
| 236 | |
}); |
| 237 | |
}); |
| 238 | |
|
| 239 | |
describe("summariseCheckFailure: what a failed check's summary says first", () => { |
| 240 | |
test("real bun test output with one failure yields the name and the counts", () => { |
| 241 | |
const lines = [ |
| 242 | |
"bun test v1.3.14 (0d9b296a)", |
| 243 | |
"", |
| 244 | |
"check.test.ts:", |
| 245 | |
"1 tests failed:", |
| 246 | |
"(fail) the forge's own restart never reads as this deploy's failure (issue #255) > an install still in flight when the runtime stops finishes cancelled, not failed [30000.49ms]", |
| 247 | |
" ^ this test timed out after 30000ms.", |
| 248 | |
"", |
| 249 | |
" 409 pass", |
| 250 | |
" 1 fail", |
| 251 | |
"Ran 410 tests across 1 file. [30.05s]", |
| 252 | |
]; |
| 253 | |
expect(summariseCheckFailure(lines, "check exited with code 1")).toEqual({ |
| 254 | |
summary: |
| 255 | |
"1 of 410 tests — \"the forge's own restart never reads as this deploy's failure (issue #255) > an install still in flight when the runtime stops finishes cancelled, not failed\"", |
| 256 | |
detail: { |
| 257 | |
passed: 409, |
| 258 | |
failed: 1, |
| 259 | |
names: [ |
| 260 | |
"the forge's own restart never reads as this deploy's failure (issue #255) > an install still in flight when the runtime stops finishes cancelled, not failed", |
| 261 | |
], |
| 262 | |
}, |
| 263 | |
}); |
| 264 | |
}); |
| 265 | |
|
| 266 | |
test("output with no bun test shape yields the last non-empty line, not the bare exit code", () => { |
| 267 | |
const lines = ["src/foo.ts(10,5): error TS2345: not assignable", ""]; |
| 268 | |
expect(summariseCheckFailure(lines, "check exited with code 2")).toEqual({ |
| 269 | |
summary: "src/foo.ts(10,5): error TS2345: not assignable", |
| 270 | |
detail: undefined, |
| 271 | |
}); |
| 272 | |
}); |
| 273 | |
|
| 274 | |
test("no output at all falls back to the bare exit code — nothing was there to name", () => { |
| 275 | |
expect(summariseCheckFailure([], "check exited with code 1")).toEqual({ |
| 276 | |
summary: "check exited with code 1", |
| 277 | |
detail: undefined, |
| 278 | |
}); |
| 279 | |
}); |
| 280 | |
|
| 281 | |
const failing = (n: number) => [ |
| 282 | |
...Array.from({ length: n }, (_, i) => `(fail) t${i + 1} [1.00ms]`), |
| 283 | |
" 5 pass", |
| 284 | |
` ${n} fail`, |
| 285 | |
]; |
| 286 | |
|
| 287 | |
test("three failures name all three; five name three and how many more", () => { |
| 288 | |
expect(summariseCheckFailure(failing(3), "x").summary).toBe( |
| 289 | |
'3 of 8 tests — "t1", "t2" and "t3"', |
| 290 | |
); |
| 291 | |
expect(summariseCheckFailure(failing(5), "x").summary).toBe( |
| 292 | |
'5 of 10 tests — "t1", "t2", "t3" and 2 more', |
| 293 | |
); |
| 294 | |
}); |
| 295 | |
|
| 296 | |
test("a name list longer than the limit is still bounded to one line", () => { |
| 297 | |
const long = "y".repeat(100); |
| 298 | |
const lines = ["a", "b", "c", "d"].map((c) => `(fail) ${c}${long} [1.00ms]`); |
| 299 | |
const { summary } = summariseCheckFailure([...lines, " 0 pass", " 4 fail"], "x"); |
| 300 | |
expect(summary.length).toBe(200); |
| 301 | |
expect(summary.endsWith("…")).toBe(true); |
| 302 | |
}); |
| 303 | |
|
| 304 | |
test("a very long name is bounded to one line", () => { |
| 305 | |
const long = "x".repeat(500); |
| 306 | |
const lines = [`(fail) ${long} [1.00ms]`, " 0 pass", " 1 fail"]; |
| 307 | |
const { summary } = summariseCheckFailure( |
| 308 | |
lines, |
| 309 | |
"check exited with code 1", |
| 310 | |
); |
| 311 | |
expect(summary.length).toBeLessThanOrEqual(200); |
| 312 | |
expect(summary.endsWith("…")).toBe(true); |
| 313 | |
}); |
| 314 | |
}); |
| 315 | |
|
| 316 | |
describe("summariseCheckFailure: a red gate is summarised by its red test, never a warning line (#2271)", () => { |
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
const red = |
| 321 | |
"bun test v1.3.14\n\ntest/standards.test.ts:\n(pass) the README names every script [1.00ms]\n" + |
| 322 | |
"(fail) releases.json is the shipped releases > every entry is a release [3.00ms]\n\n" + |
| 323 | |
" 11 pass\n 1 fail\n 40 expect() calls\nRan 12 tests across 1 file. [80.00ms]\n"; |
| 324 | |
const slow = |
| 325 | |
"bun test v1.3.14\n\ntest/crew.test.ts:\n(pass) a crew is listed [2.00ms]\n\n" + |
| 326 | |
" 30 pass\n 0 fail\n 90 expect() calls\nRan 30 tests across 1 file. [52.40s]\n"; |
| 327 | |
const results = [ |
| 328 | |
{ files: ["test/standards.test.ts"], code: 1, output: red, seconds: 1.2 }, |
| 329 | |
{ files: ["test/crew.test.ts"], code: 0, output: slow, seconds: 52.5 }, |
| 330 | |
]; |
| 331 | |
const block = (times: { file: string; seconds: number }[]): string[] => |
| 332 | |
summaryLines({ |
| 333 | |
shards: 2, |
| 334 | |
cpus: 16, |
| 335 | |
shardsMax: 16, |
| 336 | |
files: 2, |
| 337 | |
override: undefined, |
| 338 | |
memoryBytes: 8 * 1024 ** 3, |
| 339 | |
workerBytes: 1024 ** 3, |
| 340 | |
memory: { limit: undefined, peak: undefined }, |
| 341 | |
wallSeconds: 52.6, |
| 342 | |
times, |
| 343 | |
}); |
| 344 | |
const log = (shards: typeof results): string[] => [ |
| 345 | |
...shards.flatMap((r, i) => [`── shard ${i + 1}/2 (exit ${r.code}, ${r.seconds}s): ${r.files[0]}`, ...r.output.split("\n")]), |
| 346 | |
...tally(shards, 52.6).lines, |
| 347 | |
"", |
| 348 | |
...block(shards.map((r) => ({ file: r.files[0]!, seconds: r.seconds }))), |
| 349 | |
]; |
| 350 | |
|
| 351 | |
test("one red file and one slow file: the summary is the red file's test", () => { |
| 352 | |
const lines = log(results); |
| 353 | |
expect(lines.at(-1)).toBe(`test-shards: over ${SLOW_SECONDS} s: test/crew.test.ts (52.5s)`); |
| 354 | |
expect(summariseCheckFailure(lines, "check exited with code 1")).toEqual({ |
| 355 | |
summary: '1 of 42 tests — "releases.json is the shipped releases > every entry is a release"', |
| 356 | |
detail: { |
| 357 | |
passed: 41, |
| 358 | |
failed: 1, |
| 359 | |
names: ["releases.json is the shipped releases > every entry is a release"], |
| 360 | |
}, |
| 361 | |
}); |
| 362 | |
}); |
| 363 | |
|
| 364 | |
test("a red shard that named no test is named by its files, not by the gate summary block", () => { |
| 365 | |
const crashed = [ |
| 366 | |
{ files: ["test/standards.test.ts"], code: 137, output: "bun test v1.3.14\n", seconds: 1.2 }, |
| 367 | |
results[1]!, |
| 368 | |
]; |
| 369 | |
expect(summariseCheckFailure(log(crashed), "check exited with code 1").summary).toBe( |
| 370 | |
"shard 1 exited 137 without naming a test: test/standards.test.ts", |
| 371 | |
); |
| 372 | |
}); |
| 373 | |
|
| 374 | |
test("a killed shard's line is the summary, and the old line is still recognised", () => { |
| 375 | |
expect( |
| 376 | |
summariseCheckFailure(["shard 258 killed after 412 s, running test/x.test.ts"], "check exited with code 1").summary, |
| 377 | |
).toBe("shard 258 killed after 412 s, running test/x.test.ts"); |
| 378 | |
expect( |
| 379 | |
summariseCheckFailure(["shard 258 exited 143 without naming a test: a b"], "check exited with code 1").summary, |
| 380 | |
).toBe("shard 258 exited 143 without naming a test: a b"); |
| 381 | |
}); |
| 382 | |
|
| 383 | |
test("without names or a tally, the last line before the gate summary block, never a line of it", () => { |
| 384 | |
const lines = ["test-shards: something went wrong", "", ...block([{ file: "test/crew.test.ts", seconds: 52.5 }])]; |
| 385 | |
expect(summariseCheckFailure(lines, "check exited with code 1").summary).toBe( |
| 386 | |
"test-shards: something went wrong", |
| 387 | |
); |
| 388 | |
}); |
| 389 | |
}); |
| 26 |
import { checkJson, checksForge } from "./helpers/checks-forge.ts"; |
| 390 | 27 |
|
| 391 | 28 |
describe("checks", () => { |
| 392 | |
let dataDir: string; |
| 393 | |
let app: App; |
| 394 | |
let servers: Servers; |
| 395 | |
let checks: ChecksHandle; |
| 396 | |
let alice: User; |
| 397 | |
let bob: User; |
| 398 | |
let repo: Repo; |
| 399 | |
|
| 400 | |
async function settled( |
| 401 | |
where: (run: Run) => boolean, |
| 402 | |
timeoutMs = 4_000, |
| 403 | |
): Promise<Run> { |
| 404 | |
const deadline = Date.now() + timeoutMs; |
| 405 | |
while (Date.now() < deadline) { |
| 406 | |
const run = listRuns(app.db, repo.id, { kind: "check", limit: 50 }).find( |
| 407 | |
where, |
| 408 | |
); |
| 409 | |
if (run && (run.state === "ok" || run.state === "failed")) return run; |
| 410 | |
await Bun.sleep(10); |
| 411 | |
} |
| 412 | |
throw new Error("check did not settle"); |
| 413 | |
} |
| 414 | |
|
| 415 | |
|
| 416 | |
async function until(done: () => boolean, what: string): Promise<void> { |
| 417 | |
const deadline = Date.now() + 4_000; |
| 418 | |
while (!done()) { |
| 419 | |
if (Date.now() > deadline) throw new Error(`never came: ${what}`); |
| 420 | |
await Bun.sleep(10); |
| 421 | |
} |
| 422 | |
} |
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
const held = (name: string, then = "test -f OK.md"): string => |
| 430 | |
`while [ ! -f '${dataDir}/go-${name}' ]; do sleep 0.01; done; ${then}`; |
| 431 | |
const release = (name: string): void => |
| 432 | |
writeFileSync(join(dataDir, `go-${name}`), ""); |
| 433 | |
|
| 434 | |
|
| 435 | |
const headAnnounced = (pull: Pull, headOid: string): Promise<void> => |
| 436 | |
until( |
| 437 | |
() => |
| 438 | |
listEvents(app.db, { repoId: repo.id, kind: "pull.pushed" }).some( |
| 439 | |
(e) => e.payload["number"] === pull.number && e.payload["headOid"] === headOid, |
| 440 | |
), |
| 441 | |
"the pull's new head announced", |
| 442 | |
); |
| 443 | |
|
| 444 | |
const checksOf = (commitOid: string): Run[] => |
| 445 | |
listRuns(app.db, repo.id, { kind: "check", limit: 50 }).filter( |
| 446 | |
(r) => r.commitOid === commitOid, |
| 447 | |
); |
| 448 | |
|
| 449 | |
beforeAll(async () => { |
| 450 | |
dataDir = await mkdtemp(join(tmpdir(), "forge-checks-")); |
| 451 | |
app = createApp({ dataDir, port: 0 }); |
| 452 | |
servers = await serve(app); |
| 453 | |
checks = startChecks(app, { forgeUrl: servers.url, concurrency: 1 }); |
| 454 | |
const a = await createUser(app.db, { |
| 455 | |
origin: "seed", |
| 456 | |
trusted: true, |
| 457 | |
username: "alice", |
| 458 | |
email: "[email protected]", |
| 459 | |
password: "a-good-passphrase", |
| 460 | |
adminIfFirst: true, |
| 461 | |
}); |
| 462 | |
if (!a.ok) throw new Error(a.error); |
| 463 | |
alice = a.value; |
| 464 | |
const b = await createUser(app.db, { |
| 465 | |
origin: "seed", |
| 466 | |
trusted: true, |
| 467 | |
username: "bob", |
| 468 | |
email: "[email protected]", |
| 469 | |
password: "a-good-passphrase", |
| 470 | |
}); |
| 471 | |
if (!b.ok) throw new Error(b.error); |
| 472 | |
bob = b.value; |
| 473 | |
promote(app.db, bob); |
| 474 | |
const made = await createRepo(app, alice, { |
| 475 | |
name: "lib", |
| 476 | |
visibility: "public", |
| 477 | |
}); |
| 478 | |
if (!made.ok) throw new Error(String(made.error)); |
| 479 | |
repo = made.value; |
| 480 | |
}); |
| 481 | |
|
| 482 | |
afterAll(async () => { |
| 483 | |
await checks.stop(); |
| 484 | |
servers.stop(); |
| 485 | |
app.close(); |
| 486 | |
await rm(dataDir, { recursive: true, force: true }); |
| 487 | |
}); |
| 29 |
const f = checksForge("forge-checks-"); |
| 488 | 30 |
|
| 489 | 31 |
test("a commit that declares no check is not checked", async () => { |
| 490 | 32 |
|
| 491 | |
const seeded = await commitToBranch(app, alice, repo, { |
| 33 |
const seeded = await commitToBranch(f.app, f.alice, f.repo, { |
| 492 | 34 |
message: "Start", |
| 493 | 35 |
files: [{ path: "README.md", content: "# lib\n" }], |
| 494 | 36 |
}); |
| 495 | 37 |
expect(seeded.ok).toBe(true); |
| 496 | 38 |
|
| 497 | 39 |
|
| 498 | |
const declared = await commitToBranch(app, alice, repo, { |
| 40 |
const declared = await commitToBranch(f.app, f.alice, f.repo, { |
| 499 | 41 |
message: "Declare a passing check", |
| 500 | 42 |
files: [{ path: "forge.json", content: checkJson("true # first") }], |
| 501 | 43 |
}); |
| 502 | 44 |
if (!declared.ok) throw new Error(JSON.stringify(declared.error)); |
| 503 | |
const run = await settled((r) => r.commitOid === declared.value.commitOid); |
| 504 | |
expect(listRuns(app.db, repo.id, { kind: "check" }).map((r) => r.id)).toEqual([run.id]); |
| 45 |
const run = await f.settled((r) => r.commitOid === declared.value.commitOid); |
| 46 |
expect(listRuns(f.app.db, f.repo.id, { kind: "check" }).map((r) => r.id)).toEqual([run.id]); |
| 505 | 47 |
}); |
| 506 | 48 |
|
| 507 | 49 |
test("the default branch is checked on push, with the log kept", async () => { |
| 508 | 50 |
|
| 509 | |
const declared = await commitToBranch(app, alice, repo, { |
| 51 |
const declared = await commitToBranch(f.app, f.alice, f.repo, { |
| 510 | 52 |
message: "Declare a check", |
| 511 | 53 |
files: [ |
| 512 | 54 |
{ path: "forge.json", content: checkJson("test -f OK.md") }, |
|
|
@@ -514,12 +56,12 @@ describe("checks", () => { |
| 514 | 56 |
], |
| 515 | 57 |
}); |
| 516 | 58 |
if (!declared.ok) throw new Error(JSON.stringify(declared.error)); |
| 517 | |
const run = await settled((r) => r.commitOid === declared.value.commitOid); |
| 59 |
const run = await f.settled((r) => r.commitOid === declared.value.commitOid); |
| 518 | 60 |
expect(run.state).toBe("ok"); |
| 519 | 61 |
expect(run.env).toBe("main"); |
| 520 | 62 |
expect(run.reason).toBe("push"); |
| 521 | 63 |
expect(run.summary).toMatch(/^check passed in \d+s$/); |
| 522 | |
const log = readRunLog(dataDir, run.id).lines.join("\n"); |
| 64 |
const log = readRunLog(f.dataDir, run.id).lines.join("\n"); |
| 523 | 65 |
expect(log).toContain("$ test -f OK.md"); |
| 524 | 66 |
}); |
| 525 | 67 |
|
|
|
@@ -527,7 +69,7 @@ describe("checks", () => { |
| 527 | 69 |
|
| 528 | 70 |
|
| 529 | 71 |
|
| 530 | |
const declared = await commitToBranch(app, alice, repo, { |
| 72 |
const declared = await commitToBranch(f.app, f.alice, f.repo, { |
| 531 | 73 |
message: "A check that reads its cell", |
| 532 | 74 |
files: [ |
| 533 | 75 |
{ |
|
|
@@ -539,9 +81,9 @@ describe("checks", () => { |
| 539 | 81 |
], |
| 540 | 82 |
}); |
| 541 | 83 |
if (!declared.ok) throw new Error(JSON.stringify(declared.error)); |
| 542 | |
const run = await settled((r) => r.commitOid === declared.value.commitOid); |
| 84 |
const run = await f.settled((r) => r.commitOid === declared.value.commitOid); |
| 543 | 85 |
expect(run.state).toBe("ok"); |
| 544 | |
expect(readRunLog(dataDir, run.id).lines.join("\n")).toContain( |
| 86 |
expect(readRunLog(f.dataDir, run.id).lines.join("\n")).toContain( |
| 545 | 87 |
"cells: off", |
| 546 | 88 |
); |
| 547 | 89 |
}); |
|
|
@@ -549,25 +91,25 @@ describe("checks", () => { |
| 549 | 91 |
test("a pull is checked when it opens and each time its branch moves; a failure names the exit", async () => { |
| 550 | 92 |
|
| 551 | 93 |
|
| 552 | |
const gate = await commitToBranch(app, alice, repo, { |
| 94 |
const gate = await commitToBranch(f.app, f.alice, f.repo, { |
| 553 | 95 |
message: "The base's check", |
| 554 | 96 |
files: [{ path: "forge.json", content: checkJson("test -f NOPE.md") }], |
| 555 | 97 |
}); |
| 556 | 98 |
if (!gate.ok) throw new Error(JSON.stringify(gate.error)); |
| 557 | |
const branched = await commitToBranch(app, alice, repo, { |
| 99 |
const branched = await commitToBranch(f.app, f.alice, f.repo, { |
| 558 | 100 |
branch: "feature", |
| 559 | 101 |
message: "Leave the check failing", |
| 560 | 102 |
files: [{ path: "a.txt", content: "a\n" }], |
| 561 | 103 |
}); |
| 562 | 104 |
if (!branched.ok) throw new Error(JSON.stringify(branched.error)); |
| 563 | |
const opened = await openPull(app, alice, repo, { |
| 105 |
const opened = await openPull(f.app, f.alice, f.repo, { |
| 564 | 106 |
title: "Feature", |
| 565 | 107 |
baseBranch: "main", |
| 566 | 108 |
headBranch: "feature", |
| 567 | 109 |
}); |
| 568 | 110 |
if (!opened.ok) throw new Error(String(opened.error)); |
| 569 | 111 |
const pull = opened.value; |
| 570 | |
const failed = await settled( |
| 112 |
const failed = await f.settled( |
| 571 | 113 |
(r) => r.issueId === pull.id && r.commitOid === branched.value.commitOid, |
| 572 | 114 |
); |
| 573 | 115 |
expect(failed.state).toBe("failed"); |
|
|
@@ -575,7 +117,7 @@ describe("checks", () => { |
| 575 | 117 |
expect(failed.reason).toBe("pull"); |
| 576 | 118 |
|
| 577 | 119 |
|
| 578 | |
expect(checksOf(branched.value.commitOid).map((r) => r.id)).toEqual([failed.id]); |
| 120 |
expect(f.checksOf(branched.value.commitOid).map((r) => r.id)).toEqual([failed.id]); |
| 579 | 121 |
expect(failed.summary).toBe("check failed: check exited with code 1"); |
| 580 | 122 |
|
| 581 | 123 |
|
|
|
@@ -585,9 +127,9 @@ describe("checks", () => { |
| 585 | 127 |
|
| 586 | 128 |
|
| 587 | 129 |
expect(failed.baseOid).toBe(gate.value.commitOid); |
| 588 | |
const onMain = await settled((r) => r.commitOid === gate.value.commitOid && r.issueId === null); |
| 130 |
const onMain = await f.settled((r) => r.commitOid === gate.value.commitOid && r.issueId === null); |
| 589 | 131 |
expect(onMain.baseOid).toBe(""); |
| 590 | |
const seen = checkSummary(app.db, pull.id, branched.value.commitOid); |
| 132 |
const seen = checkSummary(f.app.db, pull.id, branched.value.commitOid); |
| 591 | 133 |
const line = checksLine( |
| 592 | 134 |
"/alice/lib", |
| 593 | 135 |
seen, |
|
|
@@ -612,16 +154,16 @@ describe("checks", () => { |
| 612 | 154 |
expect(checksText(green, false, true, "main", false)).toContain("Check passed"); |
| 613 | 155 |
expect(checksText(green, false, true, "main", false)).not.toContain("older"); |
| 614 | 156 |
expect( |
| 615 | |
checksForHead(app.db, pull.id, branched.value.commitOid)[0]?.id, |
| 157 |
checksForHead(f.app.db, pull.id, branched.value.commitOid)[0]?.id, |
| 616 | 158 |
).toBe(failed.id); |
| 617 | 159 |
|
| 618 | |
const fixed = await commitToBranch(app, alice, repo, { |
| 160 |
const fixed = await commitToBranch(f.app, f.alice, f.repo, { |
| 619 | 161 |
branch: "feature", |
| 620 | 162 |
message: "Fix the check", |
| 621 | 163 |
files: [{ path: "NOPE.md", content: "there\n" }], |
| 622 | 164 |
}); |
| 623 | 165 |
if (!fixed.ok) throw new Error(JSON.stringify(fixed.error)); |
| 624 | |
const passed = await settled( |
| 166 |
const passed = await f.settled( |
| 625 | 167 |
(r) => r.issueId === pull.id && r.commitOid === fixed.value.commitOid, |
| 626 | 168 |
); |
| 627 | 169 |
expect(passed.state).toBe("ok"); |
|
|
@@ -630,14 +172,14 @@ describe("checks", () => { |
| 630 | 172 |
|
| 631 | 173 |
test("a bun test failure in the check step is summarised: how many passed, how many failed, and their names", async () => { |
| 632 | 174 |
|
| 633 | |
const gate = await commitToBranch(app, alice, repo, { |
| 175 |
const gate = await commitToBranch(f.app, f.alice, f.repo, { |
| 634 | 176 |
message: "The base's check runs bun test", |
| 635 | 177 |
files: [ |
| 636 | 178 |
{ path: "forge.json", content: checkJson("bun test check.test.ts") }, |
| 637 | 179 |
], |
| 638 | 180 |
}); |
| 639 | 181 |
if (!gate.ok) throw new Error(JSON.stringify(gate.error)); |
| 640 | |
const branched = await commitToBranch(app, alice, repo, { |
| 182 |
const branched = await commitToBranch(f.app, f.alice, f.repo, { |
| 641 | 183 |
branch: "red-tests", |
| 642 | 184 |
message: "Two failing tests", |
| 643 | 185 |
files: [ |
|
|
@@ -654,14 +196,14 @@ describe("checks", () => { |
| 654 | 196 |
], |
| 655 | 197 |
}); |
| 656 | 198 |
if (!branched.ok) throw new Error(JSON.stringify(branched.error)); |
| 657 | |
const opened = await openPull(app, alice, repo, { |
| 199 |
const opened = await openPull(f.app, f.alice, f.repo, { |
| 658 | 200 |
title: "Red tests", |
| 659 | 201 |
baseBranch: "main", |
| 660 | 202 |
headBranch: "red-tests", |
| 661 | 203 |
}); |
| 662 | 204 |
if (!opened.ok) throw new Error(String(opened.error)); |
| 663 | 205 |
const pull = opened.value; |
| 664 | |
const failed = await settled( |
| 206 |
const failed = await f.settled( |
| 665 | 207 |
(r) => r.issueId === pull.id && r.commitOid === branched.value.commitOid, |
| 666 | 208 |
); |
| 667 | 209 |
expect(failed.state).toBe("failed"); |
|
|
@@ -677,21 +219,21 @@ describe("checks", () => { |
| 677 | 219 |
|
| 678 | 220 |
test("a reader's fork pull is checked at the base's snapshot once a writer approves each head", async () => { |
| 679 | 221 |
|
| 680 | |
const gate = await commitToBranch(app, alice, repo, { |
| 222 |
const gate = await commitToBranch(f.app, f.alice, f.repo, { |
| 681 | 223 |
message: "The base's check passes", |
| 682 | 224 |
files: [{ path: "forge.json", content: checkJson("true") }], |
| 683 | 225 |
}); |
| 684 | 226 |
if (!gate.ok) throw new Error(JSON.stringify(gate.error)); |
| 685 | |
const forked = await forkRepo(app, bob, repo); |
| 227 |
const forked = await forkRepo(f.app, f.bob, f.repo); |
| 686 | 228 |
if (!forked.ok) throw new Error(String(forked.error)); |
| 687 | 229 |
const fork = forked.value; |
| 688 | |
const committed = await commitToBranch(app, bob, fork, { |
| 230 |
const committed = await commitToBranch(f.app, f.bob, fork, { |
| 689 | 231 |
branch: "from-fork", |
| 690 | 232 |
message: "From the fork", |
| 691 | 233 |
files: [{ path: "FORK.md", content: "hi\n" }], |
| 692 | 234 |
}); |
| 693 | 235 |
if (!committed.ok) throw new Error(JSON.stringify(committed.error)); |
| 694 | |
const opened = await openPull(app, bob, repo, { |
| 236 |
const opened = await openPull(f.app, f.bob, f.repo, { |
| 695 | 237 |
title: "From the fork", |
| 696 | 238 |
baseBranch: "main", |
| 697 | 239 |
headBranch: "from-fork", |
|
|
@@ -701,16 +243,16 @@ describe("checks", () => { |
| 701 | 243 |
const pull = opened.value; |
| 702 | 244 |
expect(pull.crossRepo).toBe(true); |
| 703 | 245 |
|
| 704 | |
const approved = await submitReview(app, alice, repo, pull, { |
| 246 |
const approved = await submitReview(f.app, f.alice, f.repo, pull, { |
| 705 | 247 |
state: "approved", |
| 706 | 248 |
}); |
| 707 | 249 |
if (!approved.ok) throw new Error(String(approved.error)); |
| 708 | |
const first = await settled( |
| 250 |
const first = await f.settled( |
| 709 | 251 |
(r) => r.issueId === pull.id && r.commitOid === committed.value.commitOid, |
| 710 | 252 |
); |
| 711 | 253 |
expect(first.state).toBe("ok"); |
| 712 | 254 |
|
| 713 | |
const again = await commitToBranch(app, bob, fork, { |
| 255 |
const again = await commitToBranch(f.app, f.bob, fork, { |
| 714 | 256 |
branch: "from-fork", |
| 715 | 257 |
message: "Again", |
| 716 | 258 |
files: [{ path: "FORK.md", content: "hi again\n" }], |
|
|
@@ -719,40 +261,40 @@ describe("checks", () => { |
| 719 | 261 |
|
| 720 | 262 |
|
| 721 | 263 |
|
| 722 | |
await headAnnounced(pull, again.value.commitOid); |
| 723 | |
const reapproved = await submitReview(app, alice, repo, pull, { |
| 264 |
await f.headAnnounced(pull, again.value.commitOid); |
| 265 |
const reapproved = await submitReview(f.app, f.alice, f.repo, pull, { |
| 724 | 266 |
state: "approved", |
| 725 | 267 |
}); |
| 726 | 268 |
if (!reapproved.ok) throw new Error(String(reapproved.error)); |
| 727 | |
const second = await settled( |
| 269 |
const second = await f.settled( |
| 728 | 270 |
(r) => r.issueId === pull.id && r.commitOid === again.value.commitOid, |
| 729 | 271 |
); |
| 730 | 272 |
expect(second.state).toBe("ok"); |
| 731 | 273 |
|
| 732 | 274 |
|
| 733 | 275 |
expect(second.reason).toBe("approved"); |
| 734 | |
expect(checksOf(again.value.commitOid).map((r) => r.id)).toEqual([second.id]); |
| 276 |
expect(f.checksOf(again.value.commitOid).map((r) => r.id)).toEqual([second.id]); |
| 735 | 277 |
}); |
| 736 | 278 |
|
| 737 | 279 |
test("approving a head whose check is already green queues no second check (#4391)", async () => { |
| 738 | 280 |
|
| 739 | |
const based = await commitToBranch(app, alice, repo, { |
| 281 |
const based = await commitToBranch(f.app, f.alice, f.repo, { |
| 740 | 282 |
message: "The base's check passes", |
| 741 | 283 |
files: [{ path: "forge.json", content: checkJson("true") }], |
| 742 | 284 |
}); |
| 743 | 285 |
if (!based.ok) throw new Error(JSON.stringify(based.error)); |
| 744 | 286 |
|
| 745 | 287 |
const fork = |
| 746 | |
getRepo(app.db, "bob", repo.name) ?? |
| 747 | |
(await forkRepo(app, bob, repo).then((f) => (f.ok ? f.value : undefined))); |
| 288 |
getRepo(f.app.db, "bob", f.repo.name) ?? |
| 289 |
(await forkRepo(f.app, f.bob, f.repo).then((made) => (made.ok ? made.value : undefined))); |
| 748 | 290 |
if (fork === undefined) throw new Error("no fork"); |
| 749 | |
const committed = await commitToBranch(app, bob, fork, { |
| 291 |
const committed = await commitToBranch(f.app, f.bob, fork, { |
| 750 | 292 |
branch: "green-then-approved", |
| 751 | 293 |
message: "Green before the second look", |
| 752 | 294 |
files: [{ path: "GREEN.md", content: "x\n" }], |
| 753 | 295 |
}); |
| 754 | 296 |
if (!committed.ok) throw new Error(JSON.stringify(committed.error)); |
| 755 | |
const opened = await openPull(app, bob, repo, { |
| 297 |
const opened = await openPull(f.app, f.bob, f.repo, { |
| 756 | 298 |
title: "Green before the second look", |
| 757 | 299 |
baseBranch: "main", |
| 758 | 300 |
headBranch: "green-then-approved", |
|
|
@@ -761,16 +303,16 @@ describe("checks", () => { |
| 761 | 303 |
if (!opened.ok) throw new Error(String(opened.error)); |
| 762 | 304 |
const pull = opened.value; |
| 763 | 305 |
const mine = (): Run[] => |
| 764 | |
listRuns(app.db, repo.id, { kind: "check" }).filter( |
| 306 |
listRuns(f.app.db, f.repo.id, { kind: "check" }).filter( |
| 765 | 307 |
(r) => r.issueId === pull.id && r.commitOid === committed.value.commitOid, |
| 766 | 308 |
); |
| 767 | 309 |
|
| 768 | |
const approved = await submitReview(app, alice, repo, pull, { state: "approved" }); |
| 310 |
const approved = await submitReview(f.app, f.alice, f.repo, pull, { state: "approved" }); |
| 769 | 311 |
if (!approved.ok) throw new Error(String(approved.error)); |
| 770 | |
const first = await settled((r) => r.issueId === pull.id); |
| 312 |
const first = await f.settled((r) => r.issueId === pull.id); |
| 771 | 313 |
expect(first.state).toBe("ok"); |
| 772 | 314 |
|
| 773 | |
const again = await submitReview(app, alice, repo, pull, { |
| 315 |
const again = await submitReview(f.app, f.alice, f.repo, pull, { |
| 774 | 316 |
state: "approved", |
| 775 | 317 |
body: "Still good.", |
| 776 | 318 |
}); |
|
|
@@ -778,22 +320,22 @@ describe("checks", () => { |
| 778 | 320 |
|
| 779 | 321 |
|
| 780 | 322 |
|
| 781 | |
const next = await commitToBranch(app, bob, fork, { |
| 323 |
const next = await commitToBranch(f.app, f.bob, fork, { |
| 782 | 324 |
branch: "green-then-approved", |
| 783 | 325 |
message: "After the second look", |
| 784 | 326 |
files: [{ path: "GREEN.md", content: "y\n" }], |
| 785 | 327 |
}); |
| 786 | 328 |
if (!next.ok) throw new Error(JSON.stringify(next.error)); |
| 787 | |
await headAnnounced(pull, next.value.commitOid); |
| 788 | |
const third = await submitReview(app, alice, repo, pull, { state: "approved" }); |
| 329 |
await f.headAnnounced(pull, next.value.commitOid); |
| 330 |
const third = await submitReview(f.app, f.alice, f.repo, pull, { state: "approved" }); |
| 789 | 331 |
if (!third.ok) throw new Error(String(third.error)); |
| 790 | |
await settled((r) => r.issueId === pull.id && r.commitOid === next.value.commitOid); |
| 332 |
await f.settled((r) => r.issueId === pull.id && r.commitOid === next.value.commitOid); |
| 791 | 333 |
expect(mine().map((r) => r.id)).toEqual([first.id]); |
| 792 | 334 |
}); |
| 793 | 335 |
|
| 794 | 336 |
test("a commit already proven green is not run again; the re-request answers with the earlier verdict (#1795)", async () => { |
| 795 | 337 |
|
| 796 | |
const made = await commitToBranch(app, alice, repo, { |
| 338 |
const made = await commitToBranch(f.app, f.alice, f.repo, { |
| 797 | 339 |
message: "Proven once", |
| 798 | 340 |
files: [ |
| 799 | 341 |
{ path: "forge.json", content: checkJson("echo ran-once; test -f OK.md # proven") }, |
|
|
@@ -801,626 +343,28 @@ describe("checks", () => { |
| 801 | 343 |
], |
| 802 | 344 |
}); |
| 803 | 345 |
if (!made.ok) throw new Error(JSON.stringify(made.error)); |
| 804 | |
const first = await settled((r) => r.commitOid === made.value.commitOid); |
| 346 |
const first = await f.settled((r) => r.commitOid === made.value.commitOid); |
| 805 | 347 |
expect(first.state).toBe("ok"); |
| 806 | |
expect(readRunLog(dataDir, first.id).lines.join("\n")).toContain("ran-once"); |
| 807 | |
const asked = await checks.request(repo, "main", made.value.commitOid, "again"); |
| 808 | |
const second = await settled((r) => r.id === asked!.id); |
| 348 |
expect(readRunLog(f.dataDir, first.id).lines.join("\n")).toContain("ran-once"); |
| 349 |
const asked = await f.checks.request(f.repo, "main", made.value.commitOid, "again"); |
| 350 |
const second = await f.settled((r) => r.id === asked!.id); |
| 809 | 351 |
expect(second.state).toBe("ok"); |
| 810 | 352 |
expect(second.summary).toBe(`already proven green by ${first.id}`); |
| 811 | |
expect(readRunLog(dataDir, second.id).lines.join("\n")).not.toContain("ran-once"); |
| 353 |
expect(readRunLog(f.dataDir, second.id).lines.join("\n")).not.toContain("ran-once"); |
| 812 | 354 |
}); |
| 813 | |
|
| 814 | |
test("a newer commit supersedes a check still waiting for the same environment", async () => { |
| 815 | |
|
| 816 | |
const slow = await commitToBranch(app, alice, repo, { |
| 817 | |
message: "A slow check", |
| 818 | |
files: [{ path: "forge.json", content: checkJson(held("supersede")) }], |
| 819 | |
}); |
| 820 | |
if (!slow.ok) throw new Error(JSON.stringify(slow.error)); |
| 821 | |
|
| 822 | |
|
| 823 | |
const one = await checks.request( |
| 824 | |
repo, |
| 825 | |
"main", |
| 826 | |
slow.value.commitOid, |
| 827 | |
"push", |
| 828 | |
); |
| 829 | |
expect(one).toBeDefined(); |
| 830 | |
const two = await commitToBranch(app, alice, repo, { |
| 831 | |
message: "Two", |
| 832 | |
files: [{ path: "TWO.md", content: "2\n" }], |
| 833 | |
}); |
| 834 | |
if (!two.ok) throw new Error(JSON.stringify(two.error)); |
| 835 | |
await until(() => checksOf(two.value.commitOid).length > 0, "two queued"); |
| 836 | |
const three = await commitToBranch(app, alice, repo, { |
| 837 | |
message: "Three", |
| 838 | |
files: [{ path: "THREE.md", content: "3\n" }], |
| 839 | |
}); |
| 840 | |
if (!three.ok) throw new Error(JSON.stringify(three.error)); |
| 841 | |
await until(() => checksOf(three.value.commitOid).length > 0, "three queued"); |
| 842 | |
release("supersede"); |
| 843 | |
const last = await settled((r) => r.commitOid === three.value.commitOid); |
| 844 | |
expect(last.state).toBe("ok"); |
| 845 | |
const middle = checksOf(two.value.commitOid)[0]; |
| 846 | |
expect(middle?.state).toBe("cancelled"); |
| 847 | |
expect(middle?.summary).toContain("superseded by"); |
| 848 | |
}); |
| 849 | |
|
| 850 | |
test("a new head on a pull stops its running check for the old head, superseded not failed", async () => { |
| 851 | |
|
| 852 | |
const slow = await commitToBranch(app, alice, repo, { |
| 853 | |
branch: "supersede", |
| 854 | |
message: "A check that never ends on its own", |
| 855 | |
files: [ |
| 856 | |
{ path: "forge.json", content: checkJson(held("never")) }, |
| 857 | |
{ path: "S.md", content: "s\n" }, |
| 858 | |
], |
| 859 | |
}); |
| 860 | |
if (!slow.ok) throw new Error(JSON.stringify(slow.error)); |
| 861 | |
const opened = await openPull(app, alice, repo, { |
| 862 | |
title: "supersede", |
| 863 | |
baseBranch: "main", |
| 864 | |
headBranch: "supersede", |
| 865 | |
}); |
| 866 | |
if (!opened.ok) throw new Error(JSON.stringify(opened.error)); |
| 867 | |
const env = `pull-${opened.value.number}`; |
| 868 | |
const old = await checks.request( |
| 869 | |
repo, |
| 870 | |
env, |
| 871 | |
slow.value.commitOid, |
| 872 | |
"push", |
| 873 | |
opened.value.id, |
| 874 | |
); |
| 875 | |
if (!old) throw new Error("no run"); |
| 876 | |
await until(() => getRun(app.db, old.id)?.state === "running", "old head running"); |
| 877 | |
const next = await commitToBranch(app, alice, repo, { |
| 878 | |
branch: "supersede", |
| 879 | |
message: "Fast", |
| 880 | |
files: [ |
| 881 | |
{ path: "forge.json", content: checkJson("test -f S.md") }, |
| 882 | |
], |
| 883 | |
}); |
| 884 | |
if (!next.ok) throw new Error(JSON.stringify(next.error)); |
| 885 | |
const fresh = await settled((r) => r.commitOid === next.value.commitOid); |
| 886 | |
expect(fresh.state).toBe("ok"); |
| 887 | |
const stopped = getRun(app.db, old.id); |
| 888 | |
expect(stopped?.state).toBe("cancelled"); |
| 889 | |
expect(stopped?.summary).toContain("superseded by"); |
| 890 | |
|
| 891 | |
|
| 892 | |
await until( |
| 893 | |
() => readRunLog(dataDir, old.id).lines.join("\n").includes("superseded"), |
| 894 | |
"the old head's check cut off", |
| 895 | |
); |
| 896 | |
}); |
| 897 | |
|
| 898 | |
for (const ending of ["merge", "close"] as const) |
| 899 | |
test(`${ending === "merge" ? "merging" : "closing"} a pull leaves no queued or running run for it`, async () => { |
| 900 | |
|
| 901 | |
const blocker = await commitToBranch(app, alice, repo, { |
| 902 | |
message: "Hold the lane", |
| 903 | |
files: [{ path: "forge.json", content: checkJson(held(`lane-${ending}`)) }], |
| 904 | |
}); |
| 905 | |
if (!blocker.ok) throw new Error(JSON.stringify(blocker.error)); |
| 906 | |
await checks.request(repo, "lane", blocker.value.commitOid, "push"); |
| 907 | |
const branched = await commitToBranch(app, alice, repo, { |
| 908 | |
branch: `lands-${ending}`, |
| 909 | |
message: "Lands before its check runs", |
| 910 | |
files: [{ path: "LANDS.md", content: "x\n" }], |
| 911 | |
}); |
| 912 | |
if (!branched.ok) throw new Error(JSON.stringify(branched.error)); |
| 913 | |
const opened = await openPull(app, alice, repo, { |
| 914 | |
title: "Lands soon", |
| 915 | |
baseBranch: "main", |
| 916 | |
headBranch: `lands-${ending}`, |
| 917 | |
}); |
| 918 | |
if (!opened.ok) throw new Error(String(opened.error)); |
| 919 | |
const pull = opened.value; |
| 920 | |
const mine = (): Run[] => |
| 921 | |
listRuns(app.db, repo.id, { kind: "check" }).filter( |
| 922 | |
(r) => r.issueId === pull.id, |
| 923 | |
); |
| 924 | |
await until(() => mine().length > 0, "the pull's check queued"); |
| 925 | |
expect(mine().some((r) => r.state === "queued")).toBe(true); |
| 926 | |
if (ending === "merge") { |
| 927 | |
const landed = await mergePull(app, alice, repo, pull, "merge"); |
| 928 | |
if (!landed.ok) throw new Error(String(landed.error)); |
| 929 | |
} else { |
| 930 | |
const closed = setIssueState(app, alice, repo, pull, "closed"); |
| 931 | |
if (!closed.ok) throw new Error(String(closed.error)); |
| 932 | |
} |
| 933 | |
await until(() => mine().every((r) => r.state !== "queued"), "the pull's check gone"); |
| 934 | |
expect(mine().filter((r) => r.state === "queued")).toEqual([]); |
| 935 | |
expect( |
| 936 | |
mine().some( |
| 937 | |
(r) => |
| 938 | |
r.summary === (ending === "merge" ? "pull landed" : "pull closed"), |
| 939 | |
), |
| 940 | |
).toBe(true); |
| 941 | |
|
| 942 | |
release(`lane-${ending}`); |
| 943 | |
await until( |
| 944 | |
() => checksOf(blocker.value.commitOid).every((r) => r.state !== "queued" && r.state !== "running"), |
| 945 | |
"the lane's holder done", |
| 946 | |
); |
| 947 | |
}); |
| 948 | |
|
| 949 | |
test("a queued gate keeps the last free slot from pull checks", () => { |
| 950 | |
|
| 951 | |
expect(holdsLastSlot(1, true)).toBe(true); |
| 952 | |
|
| 953 | |
expect(holdsLastSlot(1, false)).toBe(false); |
| 954 | |
|
| 955 | |
expect(holdsLastSlot(2, true)).toBe(false); |
| 956 | |
}); |
| 957 | |
|
| 958 | |
test("pickNext takes the newest queued check, unless one has starved", () => { |
| 959 | |
const a = createRun(app.db, { repoId: repo.id, kind: "check", env: "a" }); |
| 960 | |
const b = createRun(app.db, { repoId: repo.id, kind: "check", env: "b" }); |
| 961 | |
const c = createRun(app.db, { repoId: repo.id, kind: "check", env: "c" }); |
| 962 | |
|
| 963 | |
expect(pickNext([a, b, c])?.id).toBe(c.id); |
| 964 | |
expect(pickNext([c, a, b])?.id).toBe(c.id); |
| 965 | |
expect(pickNext([a])?.id).toBe(a.id); |
| 966 | |
expect(pickNext([])).toBeUndefined(); |
| 967 | |
|
| 968 | |
|
| 969 | |
app.db |
| 970 | |
.query("UPDATE run SET created_at = ?2 WHERE id = ?1") |
| 971 | |
.run(a.id, new Date(Date.now() - STARVATION_MS - 1_000).toISOString()); |
| 972 | |
const starved = getRun(app.db, a.id)!; |
| 973 | |
expect(pickNext([starved, b, c])?.id).toBe(starved.id); |
| 974 | |
|
| 975 | |
app.db |
| 976 | |
.query("UPDATE run SET created_at = ?2 WHERE id = ?1") |
| 977 | |
.run(a.id, new Date(Date.now() - STARVATION_MS + 1_000).toISOString()); |
| 978 | |
expect(pickNext([getRun(app.db, a.id)!, b, c])?.id).toBe(c.id); |
| 979 | |
|
| 980 | |
|
| 981 | |
|
| 982 | |
for (const run of [a, b, c]) cancelRun(app.db, run.id); |
| 983 | |
}); |
| 984 | |
|
| 985 | |
test("pickNext puts an approved pull's check first, newest among them, and the starved before all", () => { |
| 986 | |
const a = createRun(app.db, { repoId: repo.id, kind: "check", env: "a" }); |
| 987 | |
const b = createRun(app.db, { repoId: repo.id, kind: "check", env: "b" }); |
| 988 | |
const c = createRun(app.db, { repoId: repo.id, kind: "check", env: "c" }); |
| 989 | |
const now = Date.parse(c.createdAt); |
| 990 | |
const only = (...ids: string[]) => (run: { id: string }) => ids.includes(run.id); |
| 991 | |
|
| 992 | |
expect(pickNext([a, b, c], now, only(a.id))?.id).toBe(a.id); |
| 993 | |
|
| 994 | |
expect(pickNext([a, b, c], now, only(a.id, b.id))?.id).toBe(b.id); |
| 995 | |
|
| 996 | |
expect(pickNext([a, b, c], now, () => false)?.id).toBe(c.id); |
| 997 | |
|
| 998 | |
const later = now + STARVATION_MS + 1_000; |
| 999 | |
const old = { ...a, createdAt: new Date(now - 1_000).toISOString() }; |
| 1000 | |
const fresh = { ...b, createdAt: new Date(later).toISOString() }; |
| 1001 | |
const approvedFresh = { ...c, createdAt: new Date(later + 1).toISOString() }; |
| 1002 | |
expect(pickNext([fresh, approvedFresh, old], later, only(approvedFresh.id))?.id).toBe(old.id); |
| 1003 | |
for (const run of [a, b, c]) cancelRun(app.db, run.id); |
| 1004 | |
}); |
| 1005 | |
|
| 1006 | |
test("when a slot frees, the newest of several queued checks runs first", async () => { |
| 1007 | |
|
| 1008 | |
|
| 1009 | |
|
| 1010 | |
|
| 1011 | |
|
| 1012 | |
const oldest = await commitToBranch(app, alice, repo, { |
| 1013 | |
branch: "order-a", |
| 1014 | |
message: "A", |
| 1015 | |
files: [{ path: "forge.json", content: checkJson("test -f OK.md") }], |
| 1016 | |
}); |
| 1017 | |
if (!oldest.ok) throw new Error(JSON.stringify(oldest.error)); |
| 1018 | |
const middle = await commitToBranch(app, alice, repo, { |
| 1019 | |
branch: "order-b", |
| 1020 | |
message: "B", |
| 1021 | |
files: [{ path: "forge.json", content: checkJson("test -f OK.md") }], |
| 1022 | |
}); |
| 1023 | |
if (!middle.ok) throw new Error(JSON.stringify(middle.error)); |
| 1024 | |
const newest = await commitToBranch(app, alice, repo, { |
| 1025 | |
branch: "order-c", |
| 1026 | |
message: "C", |
| 1027 | |
files: [{ path: "forge.json", content: checkJson("test -f OK.md") }], |
| 1028 | |
}); |
| 1029 | |
if (!newest.ok) throw new Error(JSON.stringify(newest.error)); |
| 1030 | |
|
| 1031 | |
|
| 1032 | |
|
| 1033 | |
const holding = await commitToBranch(app, alice, repo, { |
| 1034 | |
branch: "order-hold", |
| 1035 | |
message: "Hold the slot", |
| 1036 | |
files: [ |
| 1037 | |
{ path: "forge.json", content: checkJson(held("order")) }, |
| 1038 | |
], |
| 1039 | |
}); |
| 1040 | |
if (!holding.ok) throw new Error(JSON.stringify(holding.error)); |
| 1041 | |
const holder = await checks.request( |
| 1042 | |
repo, |
| 1043 | |
"order-hold", |
| 1044 | |
holding.value.commitOid, |
| 1045 | |
"push", |
| 1046 | |
); |
| 1047 | |
expect(holder).toBeDefined(); |
| 1048 | |
|
| 1049 | |
const oldestRun = await checks.request( |
| 1050 | |
repo, |
| 1051 | |
"order-a", |
| 1052 | |
oldest.value.commitOid, |
| 1053 | |
"push", |
| 1054 | |
); |
| 1055 | |
const middleRun = await checks.request( |
| 1056 | |
repo, |
| 1057 | |
"order-b", |
| 1058 | |
middle.value.commitOid, |
| 1059 | |
"push", |
| 1060 | |
); |
| 1061 | |
const newestRun = await checks.request( |
| 1062 | |
repo, |
| 1063 | |
"order-c", |
| 1064 | |
newest.value.commitOid, |
| 1065 | |
"push", |
| 1066 | |
); |
| 1067 | |
expect(oldestRun && middleRun && newestRun).toBeTruthy(); |
| 1068 | |
expect(oldestRun?.state).toBe("queued"); |
| 1069 | |
expect(middleRun?.state).toBe("queued"); |
| 1070 | |
expect(newestRun?.state).toBe("queued"); |
| 1071 | |
|
| 1072 | |
release("order"); |
| 1073 | |
await settled((r) => r.id === newestRun!.id); |
| 1074 | |
await settled((r) => r.id === middleRun!.id); |
| 1075 | |
await settled((r) => r.id === oldestRun!.id); |
| 1076 | |
|
| 1077 | |
const runs = listRuns(app.db, repo.id, { kind: "check", limit: 50 }); |
| 1078 | |
const startedAt = (id: string): number => |
| 1079 | |
Date.parse(runs.find((r) => r.id === id)!.startedAt!); |
| 1080 | |
expect(startedAt(newestRun!.id)).toBeLessThan(startedAt(middleRun!.id)); |
| 1081 | |
expect(startedAt(middleRun!.id)).toBeLessThan(startedAt(oldestRun!.id)); |
| 1082 | |
}); |
| 1083 | |
|
| 1084 | |
test("a check in flight when the forge stops is queued again, and the next forge runs it", async () => { |
| 1085 | |
|
| 1086 | |
|
| 1087 | |
const id = await slowCheck( |
| 1088 | |
`if [ -f '${dataDir}/ran-once' ]; then test -f OK.md; else touch '${dataDir}/ran-once'; ${held("never")}; fi`, |
| 1089 | |
"A slower check", |
| 1090 | |
); |
| 1091 | |
|
| 1092 | |
await until(() => existsSync(join(dataDir, "ran-once")), "the command begun"); |
| 1093 | |
const oid = getRun(app.db, id)!.commitOid; |
| 1094 | |
const cut = getRun(app.db, id); |
| 1095 | |
|
| 1096 | |
|
| 1097 | |
await checks.stop(); |
| 1098 | |
const runs = listRuns(app.db, repo.id, { kind: "check", limit: 50 }); |
| 1099 | |
const interrupted = runs.find((r) => r.id === cut!.id); |
| 1100 | |
expect(interrupted?.state).toBe("cancelled"); |
| 1101 | |
expect(interrupted?.summary).toContain("queued again"); |
| 1102 | |
const again = runs.find((r) => r.commitOid === oid && r.state === "queued"); |
| 1103 | |
expect(again?.attempt).toBe(1); |
| 1104 | |
|
| 1105 | |
|
| 1106 | |
checks = startChecks(app, { forgeUrl: servers.url, concurrency: 1 }); |
| 1107 | |
const done = await settled((r) => r.id === again!.id); |
| 1108 | |
expect(done.state).toBe("ok"); |
| 1109 | |
}); |
| 1110 | |
|
| 1111 | |
async function slowCheck(command: string, message: string): Promise<string> { |
| 1112 | |
const made = await commitToBranch(app, alice, repo, { |
| 1113 | |
message, |
| 1114 | |
files: [{ path: "forge.json", content: checkJson(command) }], |
| 1115 | |
}); |
| 1116 | |
if (!made.ok) throw new Error(JSON.stringify(made.error)); |
| 1117 | |
const oid = made.value.commitOid; |
| 1118 | |
await until(() => checksOf(oid)[0]?.state === "running", `${message} running`); |
| 1119 | |
return checksOf(oid)[0]!.id; |
| 1120 | |
} |
| 1121 | |
|
| 1122 | |
test("a roll waits for a running check to finish, within its budget, instead of throwing it away", async () => { |
| 1123 | |
|
| 1124 | |
await checks.stop(); |
| 1125 | |
checks = startChecks(app, { |
| 1126 | |
forgeUrl: servers.url, |
| 1127 | |
concurrency: 1, |
| 1128 | |
drainMs: 30_000, |
| 1129 | |
}); |
| 1130 | |
const id = await slowCheck(held("drained"), "A drained check"); |
| 1131 | |
|
| 1132 | |
|
| 1133 | |
const stopping = checks.stop(); |
| 1134 | |
release("drained"); |
| 1135 | |
await stopping; |
| 1136 | |
const run = getRun(app.db, id); |
| 1137 | |
expect(run?.state).toBe("ok"); |
| 1138 | |
expect( |
| 1139 | |
listRuns(app.db, repo.id, { kind: "check", limit: 50 }).some( |
| 1140 | |
(r) => r.state === "queued", |
| 1141 | |
), |
| 1142 | |
).toBe(false); |
| 1143 | |
checks = startChecks(app, { forgeUrl: servers.url, concurrency: 1 }); |
| 1144 | |
}); |
| 1145 | |
|
| 1146 | |
test("a person's stop cuts a running check off at once, and queues it again", async () => { |
| 1147 | |
|
| 1148 | |
await checks.stop(); |
| 1149 | |
checks = startChecks(app, { |
| 1150 | |
forgeUrl: servers.url, |
| 1151 | |
concurrency: 1, |
| 1152 | |
drainMs: 30_000, |
| 1153 | |
}); |
| 1154 | |
const id = await slowCheck(held("cut"), "A cut-off check"); |
| 1155 | |
await checks.stop({ drain: false }); |
| 1156 | |
const run = getRun(app.db, id); |
| 1157 | |
expect(run?.state).toBe("cancelled"); |
| 1158 | |
expect(run?.summary).toContain("queued again"); |
| 1159 | |
const again = listRuns(app.db, repo.id, { kind: "check", limit: 50 }).find( |
| 1160 | |
(r) => r.commitOid === run?.commitOid && r.state === "queued", |
| 1161 | |
); |
| 1162 | |
expect(again).toBeDefined(); |
| 1163 | |
release("cut"); |
| 1164 | |
checks = startChecks(app, { forgeUrl: servers.url, concurrency: 1 }); |
| 1165 | |
const done = await settled((r) => r.id === again?.id); |
| 1166 | |
expect(done.state).toBe("ok"); |
| 1167 | |
}); |
| 1168 | |
|
| 1169 | |
test("a check whose pull lands while it runs ends as superseded, not failed", async () => { |
| 1170 | |
|
| 1171 | |
const id = await slowCheck(held("overtaken", "false"), "A check overtaken"); |
| 1172 | |
cancelRun(app.db, id, "pull landed"); |
| 1173 | |
release("overtaken"); |
| 1174 | |
const log = (): string => readRunLog(dataDir, id).lines.join("\n"); |
| 1175 | |
await until(() => log().includes("superseded"), "superseded in the log"); |
| 1176 | |
const lines = log(); |
| 1177 | |
expect(lines).toContain("superseded: pull landed"); |
| 1178 | |
expect(lines).not.toContain("failed:"); |
| 1179 | |
expect(getRun(app.db, id)?.state).toBe("cancelled"); |
| 1180 | |
}); |
| 1181 | |
|
| 1182 | 355 |
test("a check that is declared wrong fails with the reason", async () => { |
| 1183 | 356 |
|
| 1184 | |
const wrong = await commitToBranch(app, alice, repo, { |
| 357 |
const wrong = await commitToBranch(f.app, f.alice, f.repo, { |
| 1185 | 358 |
message: "A wrong check", |
| 1186 | 359 |
files: [{ path: "forge.json", content: '{"check": "bun test"}\n' }], |
| 1187 | 360 |
}); |
| 1188 | 361 |
if (!wrong.ok) throw new Error(JSON.stringify(wrong.error)); |
| 1189 | |
const run = await settled((r) => r.commitOid === wrong.value.commitOid); |
| 362 |
const run = await f.settled((r) => r.commitOid === wrong.value.commitOid); |
| 1190 | 363 |
expect(run.state).toBe("failed"); |
| 1191 | 364 |
expect(run.summary).toBe( |
| 1192 | 365 |
"check failed: forge.json: check must be an object", |
| 1193 | 366 |
); |
| 1194 | 367 |
}); |
| 1195 | |
|
| 1196 | |
test("an open pull whose head was never checked is checked when the service boots", async () => { |
| 1197 | |
|
| 1198 | |
await checks.stop(); |
| 1199 | |
|
| 1200 | |
const gate = await commitToBranch(app, alice, repo, { |
| 1201 | |
message: "The base's check wants QUIET.md", |
| 1202 | |
files: [{ path: "forge.json", content: checkJson("test -f QUIET.md") }], |
| 1203 | |
}); |
| 1204 | |
if (!gate.ok) throw new Error(JSON.stringify(gate.error)); |
| 1205 | |
const branch = await commitToBranch(app, alice, repo, { |
| 1206 | |
branch: "quiet", |
| 1207 | |
message: "Quiet", |
| 1208 | |
files: [{ path: "QUIET.md", content: "quiet\n" }], |
| 1209 | |
}); |
| 1210 | |
if (!branch.ok) throw new Error(JSON.stringify(branch.error)); |
| 1211 | |
|
| 1212 | |
const base = await commitToBranch(app, alice, repo, { |
| 1213 | |
branch: "main", |
| 1214 | |
message: "The base's check", |
| 1215 | |
files: [{ path: "forge.json", content: checkJson("test -f QUIET.md") }], |
| 1216 | |
}); |
| 1217 | |
if (!base.ok) throw new Error(JSON.stringify(base.error)); |
| 1218 | |
|
| 1219 | |
const opened = await openPull(app, alice, repo, { |
| 1220 | |
title: "Quiet", |
| 1221 | |
baseBranch: "main", |
| 1222 | |
headBranch: "quiet", |
| 1223 | |
}); |
| 1224 | |
if (!opened.ok) throw new Error(String(opened.error)); |
| 1225 | |
expect( |
| 1226 | |
listRuns(app.db, repo.id, { kind: "check", limit: 50 }).filter( |
| 1227 | |
(r) => r.commitOid === opened.value.headOid, |
| 1228 | |
), |
| 1229 | |
).toHaveLength(0); |
| 1230 | |
checks = startChecks(app, { forgeUrl: servers.url, concurrency: 1 }); |
| 1231 | |
const done = await settled((r) => r.commitOid === opened.value.headOid); |
| 1232 | |
expect(done.reason).toBe("boot"); |
| 1233 | |
expect(done.state).toBe("ok"); |
| 1234 | |
}); |
| 1235 | |
}); |
| 1236 | |
|
| 1237 | |
describe("a check its forge died under", () => { |
| 1238 | |
let dataDir: string; |
| 1239 | |
let app: App; |
| 1240 | |
let alice: User; |
| 1241 | |
let repo: Repo; |
| 1242 | |
let pull: Pull; |
| 1243 | |
let stale: string; |
| 1244 | |
|
| 1245 | |
beforeAll(async () => { |
| 1246 | |
dataDir = await mkdtemp(join(tmpdir(), "forge-checks-abandoned-")); |
| 1247 | |
app = createApp({ dataDir, port: 0 }); |
| 1248 | |
const a = await createUser(app.db, { |
| 1249 | |
origin: "seed", |
| 1250 | |
trusted: true, |
| 1251 | |
username: "alice", |
| 1252 | |
email: "[email protected]", |
| 1253 | |
password: "a-good-passphrase", |
| 1254 | |
adminIfFirst: true, |
| 1255 | |
}); |
| 1256 | |
if (!a.ok) throw new Error(a.error); |
| 1257 | |
alice = a.value; |
| 1258 | |
const made = await createRepo(app, alice, { name: "lib" }); |
| 1259 | |
if (!made.ok) throw new Error(String(made.error)); |
| 1260 | |
repo = made.value; |
| 1261 | |
const seeded = await commitToBranch(app, alice, repo, { |
| 1262 | |
message: "Start", |
| 1263 | |
files: [{ path: "README.md", content: "# lib\n" }], |
| 1264 | |
}); |
| 1265 | |
if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); |
| 1266 | |
const first = await commitToBranch(app, alice, repo, { |
| 1267 | |
branch: "feature", |
| 1268 | |
message: "First", |
| 1269 | |
files: [{ path: "A.md", content: "a\n" }], |
| 1270 | |
}); |
| 1271 | |
if (!first.ok) throw new Error(JSON.stringify(first.error)); |
| 1272 | |
stale = first.value.commitOid; |
| 1273 | |
const second = await commitToBranch(app, alice, repo, { |
| 1274 | |
branch: "feature", |
| 1275 | |
message: "Second", |
| 1276 | |
files: [{ path: "B.md", content: "b\n" }], |
| 1277 | |
}); |
| 1278 | |
if (!second.ok) throw new Error(JSON.stringify(second.error)); |
| 1279 | |
const opened = await openPull(app, alice, repo, { |
| 1280 | |
title: "Feature", |
| 1281 | |
baseBranch: "main", |
| 1282 | |
headBranch: "feature", |
| 1283 | |
}); |
| 1284 | |
if (!opened.ok) throw new Error(String(opened.error)); |
| 1285 | |
pull = opened.value; |
| 1286 | |
}); |
| 1287 | |
|
| 1288 | |
afterAll(async () => { |
| 1289 | |
app.close(); |
| 1290 | |
await rm(dataDir, { recursive: true, force: true }); |
| 1291 | |
}); |
| 1292 | |
|
| 1293 | |
|
| 1294 | |
const abandoned = (commitOid: string, attempt = 0): Run => { |
| 1295 | |
const run = createRun(app.db, { |
| 1296 | |
repoId: repo.id, |
| 1297 | |
kind: "check", |
| 1298 | |
env: `pull-${pull.number}`, |
| 1299 | |
commitOid, |
| 1300 | |
reason: "push", |
| 1301 | |
issueId: pull.id, |
| 1302 | |
attempt, |
| 1303 | |
}); |
| 1304 | |
claimRun(app.db, run.id); |
| 1305 | |
app.db |
| 1306 | |
.query("UPDATE run SET heartbeat_at = ?2 WHERE id = ?1") |
| 1307 | |
.run(run.id, new Date(Date.now() - STALE_AFTER_MS - 1_000).toISOString()); |
| 1308 | |
return getRun(app.db, run.id)!; |
| 1309 | |
}; |
| 1310 | |
|
| 1311 | |
const word = () => |
| 1312 | |
pullWord( |
| 1313 | |
{ |
| 1314 | |
db: app.db, |
| 1315 | |
dir: repoDir(app.reposRoot, repo.ownerName, repo.name), |
| 1316 | |
runtime: undefined, |
| 1317 | |
record: undefined, |
| 1318 | |
history: servingHistory(), |
| 1319 | |
viewer: undefined, |
| 1320 | |
}, |
| 1321 | |
pull, |
| 1322 | |
); |
| 1323 | |
|
| 1324 | |
const queuedFor = (commitOid: string): Run[] => |
| 1325 | |
listRuns(app.db, repo.id, { kind: "check", limit: 50 }).filter( |
| 1326 | |
(r) => r.commitOid === commitOid && r.state === "queued", |
| 1327 | |
); |
| 1328 | |
|
| 1329 | |
test("a check for the head of an open pull is queued again, attempt + 1, and the change reads working", async () => { |
| 1330 | |
const run = abandoned(pull.headOid); |
| 1331 | |
expect((await word()).word).toBe("working"); |
| 1332 | |
expect(requeueAbandonedChecks(app, STALE_AFTER_MS)).toBe(1); |
| 1333 | |
|
| 1334 | |
expect(requeueAbandonedChecks(app, STALE_AFTER_MS)).toBe(0); |
| 1335 | |
const cut = getRun(app.db, run.id)!; |
| 1336 | |
expect(cut.state).toBe("cancelled"); |
| 1337 | |
expect(cut.summary).toBe(REQUEUED_SUMMARY); |
| 1338 | |
const again = queuedFor(pull.headOid); |
| 1339 | |
expect(again).toHaveLength(1); |
| 1340 | |
expect(again[0]!.attempt).toBe(1); |
| 1341 | |
expect(again[0]!.issueId).toBe(pull.id); |
| 1342 | |
expect(again[0]!.env).toBe(`pull-${pull.number}`); |
| 1343 | |
expect(await word()).toEqual({ word: "working", note: "queued to check" }); |
| 1344 | |
cancelRun(app.db, again[0]!.id); |
| 1345 | |
}); |
| 1346 | |
|
| 1347 | |
test("a check whose lease is still held is left alone", () => { |
| 1348 | |
const run = createRun(app.db, { |
| 1349 | |
repoId: repo.id, |
| 1350 | |
kind: "check", |
| 1351 | |
env: `pull-${pull.number}`, |
| 1352 | |
commitOid: pull.headOid, |
| 1353 | |
issueId: pull.id, |
| 1354 | |
}); |
| 1355 | |
claimRun(app.db, run.id); |
| 1356 | |
expect(requeueAbandonedChecks(app, STALE_AFTER_MS)).toBe(0); |
| 1357 | |
expect(getRun(app.db, run.id)?.state).toBe("running"); |
| 1358 | |
cancelRun(app.db, run.id); |
| 1359 | |
}); |
| 1360 | |
|
| 1361 | |
test("at the ceiling the check fails once, saying the forge kept going away", async () => { |
| 1362 | |
const run = abandoned(pull.headOid, CHECK_RESTART_LIMIT); |
| 1363 | |
expect(requeueAbandonedChecks(app, STALE_AFTER_MS)).toBe(1); |
| 1364 | |
const failed = getRun(app.db, run.id)!; |
| 1365 | |
expect(failed.state).toBe("failed"); |
| 1366 | |
expect(failed.summary).toContain( |
| 1367 | |
"the forge kept going away under this check", |
| 1368 | |
); |
| 1369 | |
expect(queuedFor(pull.headOid)).toHaveLength(0); |
| 1370 | |
expect((await word()).word).toBe("stopped"); |
| 1371 | |
}); |
| 1372 | |
|
| 1373 | |
test("a check for a commit that no longer heads an open pull is cancelled, not queued again", () => { |
| 1374 | |
const run = abandoned(stale); |
| 1375 | |
expect(requeueAbandonedChecks(app, STALE_AFTER_MS)).toBe(1); |
| 1376 | |
const cut = getRun(app.db, run.id)!; |
| 1377 | |
expect(cut.state).toBe("cancelled"); |
| 1378 | |
expect(cut.summary).toContain("no longer heads an open pull"); |
| 1379 | |
expect(queuedFor(stale)).toHaveLength(0); |
| 1380 | |
}); |
| 1381 | |
}); |
| 1382 | |
|
| 1383 | |
describe("startChecks: a booting forge removes only checkouts nobody is running (#1884)", () => { |
| 1384 | |
test("a live run's checkout survives, a lapsed run's and a stranger's go", async () => { |
| 1385 | |
|
| 1386 | |
const dir = await mkdtemp(join(tmpdir(), "forge-sweep-")); |
| 1387 | |
const db = createApp({ dataDir: dir, port: 0 }); |
| 1388 | |
try { |
| 1389 | |
const u = await createUser(db.db, { |
| 1390 | |
origin: "seed", |
| 1391 | |
trusted: true, |
| 1392 | |
username: "alice", |
| 1393 | |
email: "[email protected]", |
| 1394 | |
password: "a-good-passphrase", |
| 1395 | |
adminIfFirst: true, |
| 1396 | |
}); |
| 1397 | |
if (!u.ok) throw new Error(u.error); |
| 1398 | |
const made = await createRepo(db, u.value, { name: "lib" }); |
| 1399 | |
if (!made.ok) throw new Error(String(made.error)); |
| 1400 | |
const mk = (): Run => { |
| 1401 | |
const run = createRun(db.db, { repoId: made.value.id, kind: "check", env: "x" }); |
| 1402 | |
claimRun(db.db, run.id); |
| 1403 | |
return run; |
| 1404 | |
}; |
| 1405 | |
const live = mk(); |
| 1406 | |
const lapsed = mk(); |
| 1407 | |
db.db |
| 1408 | |
.query("UPDATE run SET heartbeat_at = ?2 WHERE id = ?1") |
| 1409 | |
.run(lapsed.id, new Date(Date.now() - STALE_AFTER_MS - 5_000).toISOString()); |
| 1410 | |
const where = (id: string): string => join(dir, "checks", id); |
| 1411 | |
for (const id of [live.id, lapsed.id, "no-such-run"]) |
| 1412 | |
mkdirSync(where(id), { recursive: true }); |
| 1413 | |
const handle = startChecks(db, { forgeUrl: "http://localhost" }); |
| 1414 | |
await sweepSoon(db); |
| 1415 | |
await handle.stop(); |
| 1416 | |
expect(existsSync(where(live.id))).toBe(true); |
| 1417 | |
expect(existsSync(where(lapsed.id))).toBe(false); |
| 1418 | |
expect(existsSync(where("no-such-run"))).toBe(false); |
| 1419 | |
} finally { |
| 1420 | |
db.close(); |
| 1421 | |
await rm(dir, { recursive: true, force: true }); |
| 1422 | |
} |
| 1423 | |
}); |
| 1424 | 368 |
}); |
| 1425 | 369 |
|
| 1426 | 370 |
describe("checks: a finished shard is announced as the runner writes it (#2889)", () => { |