The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { |
| 2 | type PullRequestCheck, |
| 3 | PullRequestComment, |
| 4 | ThreadPullRequestWatch, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { assert, describe, it } from "@effect/vitest"; |
| 7 | |
| 8 | import { |
| 9 | PULL_REQUEST_WATCH_WAKE_LIMIT, |
| 10 | evaluatePullRequestWatch as evaluate, |
| 11 | pullRequestWatchEndMessage, |
| 12 | pullRequestWatchMessage, |
| 13 | } from "./pullRequestWatch.ts"; |
| 14 | |
| 15 | // These cases read the head once or at one moment, so the no-checks notice never comes due. |
| 16 | const evaluatePullRequestWatch = ( |
| 17 | ...args: Parameters<typeof evaluate> extends [...infer Head, number] ? Head : never |
| 18 | ) => evaluate(...args, 0); |
| 19 | |
| 20 | const STARTED = "2026-10-02T12:00:00.000Z"; |
| 21 | |
| 22 | const watch = (overrides: Partial<ThreadPullRequestWatch> = {}): ThreadPullRequestWatch => ({ |
| 23 | startedAt: STARTED, |
| 24 | headSha: null, |
| 25 | failedChecks: [], |
| 26 | passed: false, |
| 27 | passedChecks: [], |
| 28 | remarksThrough: STARTED, |
| 29 | remarkIds: [], |
| 30 | conflicting: false, |
| 31 | wakes: 0, |
| 32 | ...overrides, |
| 33 | }); |
| 34 | |
| 35 | const run = (id: number) => `https://github.com/o/r/actions/runs/${id}`; |
| 36 | |
| 37 | const check = (name: string, status: PullRequestCheck["status"]): PullRequestCheck => ({ |
| 38 | name, |
| 39 | status, |
| 40 | description: null, |
| 41 | url: `https://ci.example/${name}`, |
| 42 | }); |
| 43 | |
| 44 | type Detail = Parameters<typeof evaluatePullRequestWatch>[1]; |
| 45 | |
| 46 | const capabilities = (checksAlwaysReport: boolean): Detail["capabilities"] => ({ |
| 47 | diff: true, |
| 48 | comment: true, |
| 49 | actions: [], |
| 50 | mergeMethods: [], |
| 51 | search: false, |
| 52 | review: { inlineComment: false, reply: false, resolve: false, verdicts: [] }, |
| 53 | reviewers: { request: false, listCandidates: false }, |
| 54 | checksAlwaysReport, |
| 55 | }); |
| 56 | |
| 57 | const detail = (overrides: Partial<Detail> = {}): Detail => ({ |
| 58 | capabilities: capabilities(true), |
| 59 | headSha: "aaaaaaaaaa", |
| 60 | checks: [check("lint", "success"), check("test", "pending")], |
| 61 | mergeability: "mergeable", |
| 62 | viewer: "agent-user", |
| 63 | author: { login: "agent-user", name: null, avatarUrl: null }, |
| 64 | ...overrides, |
| 65 | }); |
| 66 | |
| 67 | const remark = ( |
| 68 | login: string, |
| 69 | createdAt: string, |
| 70 | body = "Please rename this.", |
| 71 | ): PullRequestComment => ({ |
| 72 | id: `${login}-${createdAt}`, |
| 73 | kind: "review-comment", |
| 74 | author: { login, name: null, avatarUrl: null }, |
| 75 | body, |
| 76 | createdAt, |
| 77 | url: `https://github.com/o/r/pull/1#${login}`, |
| 78 | path: "src/index.ts", |
| 79 | reviewState: null, |
| 80 | }); |
| 81 | |
| 82 | const noRemarks: ReadonlyArray<PullRequestComment> = []; |
| 83 | |
| 84 | describe("evaluatePullRequestWatch", () => { |
| 85 | it.each(["pending", "success"] as const)( |
| 86 | "reports a required rebase once with %s CI", |
| 87 | (status) => { |
| 88 | const blocked = detail({ |
| 89 | mergeability: "unknown", |
| 90 | rebaseRequired: true, |
| 91 | checks: [check("test", status)], |
| 92 | }); |
| 93 | const first = evaluatePullRequestWatch(watch(), blocked, noRemarks); |
| 94 | assert.include( |
| 95 | first.changes.map((change) => change.kind), |
| 96 | "needs-rebase", |
| 97 | ); |
| 98 | assert.deepEqual(evaluatePullRequestWatch(first.next, blocked, noRemarks).changes, []); |
| 99 | const unknown = evaluatePullRequestWatch( |
| 100 | first.next, |
| 101 | { ...blocked, rebaseRequired: undefined }, |
| 102 | noRemarks, |
| 103 | ); |
| 104 | assert.deepEqual(evaluatePullRequestWatch(unknown.next, blocked, noRemarks).changes, []); |
| 105 | const clean = evaluatePullRequestWatch( |
| 106 | first.next, |
| 107 | { ...blocked, rebaseRequired: false }, |
| 108 | noRemarks, |
| 109 | ); |
| 110 | assert.deepEqual(evaluatePullRequestWatch(clean.next, blocked, noRemarks).changes, [ |
| 111 | { kind: "needs-rebase" }, |
| 112 | ]); |
| 113 | assert.include( |
| 114 | evaluatePullRequestWatch( |
| 115 | first.next, |
| 116 | { ...blocked, headSha: "bbbbbbbbbb" }, |
| 117 | noRemarks, |
| 118 | ).changes.map((change) => change.kind), |
| 119 | "needs-rebase", |
| 120 | ); |
| 121 | }, |
| 122 | ); |
| 123 | |
| 124 | it.each([false, true])("baselines already-passed checks (required: %s)", (required) => { |
| 125 | const green = detail({ checks: [{ ...check("test", "success"), required }] }); |
| 126 | const baseline = evaluatePullRequestWatch(watch({ awaitingBaseline: "all" }), green, noRemarks); |
| 127 | assert.deepEqual(baseline.changes, []); |
| 128 | assert.equal(baseline.next.headSha, green.headSha); |
| 129 | assert.isTrue(baseline.next.passed); |
| 130 | assert.deepEqual(baseline.next.passedChecks, ["test"]); |
| 131 | assert.isFalse(baseline.exhausted); |
| 132 | assert.deepEqual(evaluatePullRequestWatch(baseline.next, green, noRemarks).changes, []); |
| 133 | |
| 134 | const pending = evaluatePullRequestWatch( |
| 135 | baseline.next, |
| 136 | detail({ checks: [{ ...check("test", "pending"), required }] }), |
| 137 | noRemarks, |
| 138 | ); |
| 139 | assert.deepEqual(pending.changes, []); |
| 140 | const failed = evaluatePullRequestWatch( |
| 141 | pending.next, |
| 142 | detail({ checks: [{ ...check("test", "failure"), required }] }), |
| 143 | noRemarks, |
| 144 | ); |
| 145 | assert.equal(failed.changes[0]?.kind, "checks-failed"); |
| 146 | const passed = evaluatePullRequestWatch(failed.next, green, noRemarks); |
| 147 | assert.deepEqual(passed.changes, [{ kind: "checks-passed", count: 1, required }]); |
| 148 | assert.deepEqual( |
| 149 | evaluatePullRequestWatch(passed.next, { ...green, headSha: "bbbbbbbbbb" }, noRemarks).changes, |
| 150 | [{ kind: "checks-passed", count: 1, required }], |
| 151 | ); |
| 152 | }); |
| 153 | |
| 154 | it("baselines existing failures and conflicts, then reports new ones", () => { |
| 155 | const broken = detail({ |
| 156 | checks: [check("lint", "failure"), check("test", "pending")], |
| 157 | mergeability: "conflicting", |
| 158 | rebaseRequired: true, |
| 159 | }); |
| 160 | const baseline = evaluatePullRequestWatch( |
| 161 | watch({ awaitingBaseline: "all" }), |
| 162 | broken, |
| 163 | noRemarks, |
| 164 | ); |
| 165 | assert.deepEqual(baseline.changes, []); |
| 166 | assert.deepEqual(baseline.next.failedChecks, ["lint"]); |
| 167 | assert.isFalse(baseline.next.passed); |
| 168 | assert.isTrue(baseline.next.conflicting); |
| 169 | assert.isTrue(baseline.next.rebaseRequired); |
| 170 | assert.deepEqual(evaluatePullRequestWatch(baseline.next, broken, noRemarks).changes, []); |
| 171 | |
| 172 | const failed = evaluatePullRequestWatch( |
| 173 | baseline.next, |
| 174 | { ...broken, checks: [check("lint", "failure"), check("test", "failure")] }, |
| 175 | noRemarks, |
| 176 | ); |
| 177 | assert.deepEqual(failed.changes, [ |
| 178 | { kind: "checks-failed", failed: [check("test", "failure")] }, |
| 179 | { |
| 180 | kind: "checks-finished", |
| 181 | passed: 0, |
| 182 | failing: [check("lint", "failure"), check("test", "failure")], |
| 183 | }, |
| 184 | ]); |
| 185 | const clean = evaluatePullRequestWatch( |
| 186 | failed.next, |
| 187 | detail({ rebaseRequired: false }), |
| 188 | noRemarks, |
| 189 | ); |
| 190 | assert.deepEqual(clean.changes, []); |
| 191 | assert.deepEqual(evaluatePullRequestWatch(clean.next, broken, noRemarks).changes, [ |
| 192 | { kind: "checks-failed", failed: [check("lint", "failure")] }, |
| 193 | { kind: "conflicting" }, |
| 194 | { kind: "needs-rebase" }, |
| 195 | ]); |
| 196 | }); |
| 197 | |
| 198 | it("reports failures on the first read only when CI was pending at the start", () => { |
| 199 | const red = detail({ checks: [check("lint", "failure"), check("test", "pending")] }); |
| 200 | const pending = evaluatePullRequestWatch( |
| 201 | watch({ awaitingBaseline: "all", checksPending: true }), |
| 202 | red, |
| 203 | noRemarks, |
| 204 | ); |
| 205 | assert.deepEqual(pending.changes, [ |
| 206 | { kind: "checks-failed", failed: [check("lint", "failure")] }, |
| 207 | ]); |
| 208 | assert.deepEqual( |
| 209 | evaluatePullRequestWatch( |
| 210 | pending.next, |
| 211 | detail({ checks: [check("lint", "failure"), check("test", "success")] }), |
| 212 | noRemarks, |
| 213 | ).changes, |
| 214 | [{ kind: "checks-finished", passed: 1, failing: [check("lint", "failure")] }], |
| 215 | ); |
| 216 | assert.deepEqual( |
| 217 | evaluatePullRequestWatch( |
| 218 | watch({ awaitingBaseline: "all", checksPending: false }), |
| 219 | red, |
| 220 | noRemarks, |
| 221 | ).changes, |
| 222 | [], |
| 223 | ); |
| 224 | |
| 225 | // A read the host could not finish keeps the baseline and must keep the seed with it. |
| 226 | const placeholder = { ...check("Workflow approval status", "pending"), placeholder: true }; |
| 227 | const incomplete = evaluatePullRequestWatch( |
| 228 | watch({ awaitingBaseline: "all", checksPending: true }), |
| 229 | detail({ checks: [check("lint", "success"), placeholder] }), |
| 230 | noRemarks, |
| 231 | ); |
| 232 | assert.deepEqual(incomplete.changes, []); |
| 233 | assert.deepEqual( |
| 234 | evaluatePullRequestWatch( |
| 235 | incomplete.next, |
| 236 | detail({ checks: [check("lint", "success"), check("gated", "failure")] }), |
| 237 | noRemarks, |
| 238 | ).changes, |
| 239 | [{ kind: "checks-failed", failed: [check("gated", "failure")] }], |
| 240 | ); |
| 241 | }); |
| 242 | |
| 243 | it("baselines checks already awaiting approval", () => { |
| 244 | const gated = detail({ checks: [{ ...check("CI", "action-required"), url: run(1) }] }); |
| 245 | const baseline = evaluatePullRequestWatch(watch({ awaitingBaseline: "all" }), gated, noRemarks); |
| 246 | assert.deepEqual(baseline.changes, []); |
| 247 | assert.deepEqual(evaluatePullRequestWatch(baseline.next, gated, noRemarks).changes, []); |
| 248 | }); |
| 249 | |
| 250 | it("reports remarks after the watch started while baselining checks and conflicts", () => { |
| 251 | const green = detail({ |
| 252 | checks: [check("test", "success")], |
| 253 | mergeability: "conflicting", |
| 254 | }); |
| 255 | const comments = [ |
| 256 | remark("reviewer", "2026-10-02T11:00:00Z"), |
| 257 | remark("reviewer", "2026-10-02T12:06:00Z"), |
| 258 | ]; |
| 259 | const baseline = evaluatePullRequestWatch(watch({ awaitingBaseline: "all" }), green, comments); |
| 260 | assert.deepEqual(baseline.changes, [{ kind: "remarks", remarks: [comments[1]!] }]); |
| 261 | assert.isTrue(baseline.next.passed); |
| 262 | assert.isTrue(baseline.next.conflicting); |
| 263 | assert.equal(baseline.next.wakes, 1); |
| 264 | assert.deepEqual(evaluatePullRequestWatch(baseline.next, green, comments).changes, []); |
| 265 | }); |
| 266 | |
| 267 | it("reports a failure once the head SHA appears after a baseline without one", () => { |
| 268 | const waiting = evaluatePullRequestWatch( |
| 269 | watch({ awaitingBaseline: "all" }), |
| 270 | detail({ headSha: undefined, checks: [check("test", "pending")] }), |
| 271 | noRemarks, |
| 272 | ); |
| 273 | assert.deepEqual(waiting.changes, []); |
| 274 | const failed = evaluatePullRequestWatch( |
| 275 | waiting.next, |
| 276 | detail({ checks: [check("test", "failure")] }), |
| 277 | noRemarks, |
| 278 | ); |
| 279 | assert.deepEqual(failed.changes, [ |
| 280 | { kind: "checks-failed", failed: [check("test", "failure")] }, |
| 281 | ]); |
| 282 | }); |
| 283 | |
| 284 | it("keeps a baseline taken without a head SHA once the SHA appears", () => { |
| 285 | const green = detail({ checks: [check("test", "success")] }); |
| 286 | const baseline = evaluatePullRequestWatch( |
| 287 | watch({ awaitingBaseline: "all" }), |
| 288 | { ...green, headSha: undefined }, |
| 289 | noRemarks, |
| 290 | ); |
| 291 | const known = evaluatePullRequestWatch(baseline.next, green, noRemarks); |
| 292 | assert.deepEqual(known.changes, []); |
| 293 | assert.equal(known.next.headSha, green.headSha); |
| 294 | const unknown = evaluatePullRequestWatch( |
| 295 | known.next, |
| 296 | { ...green, headSha: undefined }, |
| 297 | noRemarks, |
| 298 | ); |
| 299 | assert.equal(unknown.next.headSha, green.headSha); |
| 300 | assert.deepEqual( |
| 301 | evaluatePullRequestWatch(unknown.next, { ...green, headSha: "bbbbbbbbbb" }, noRemarks) |
| 302 | .changes, |
| 303 | [{ kind: "checks-passed", count: 1, required: false }], |
| 304 | ); |
| 305 | }); |
| 306 | |
| 307 | it.each(["success", "failure"] as const)( |
| 308 | "reports %s on checks that appear after a first read without any", |
| 309 | (status) => { |
| 310 | const empty = evaluatePullRequestWatch( |
| 311 | watch({ awaitingBaseline: "all" }), |
| 312 | detail({ checks: [] }), |
| 313 | noRemarks, |
| 314 | ); |
| 315 | assert.deepEqual(empty.changes, []); |
| 316 | assert.isUndefined(empty.next.awaitingBaseline); |
| 317 | assert.isTrue(empty.next.checksPending); |
| 318 | assert.deepEqual( |
| 319 | evaluatePullRequestWatch(empty.next, detail({ checks: [check("test", status)] }), noRemarks) |
| 320 | .changes, |
| 321 | status === "success" |
| 322 | ? [{ kind: "checks-passed", count: 1, required: false }] |
| 323 | : [{ kind: "checks-failed", failed: [check("test", "failure")] }], |
| 324 | ); |
| 325 | }, |
| 326 | ); |
| 327 | |
| 328 | it("keeps the check baseline through a read whose approval lookup failed", () => { |
| 329 | const bot = check("bot", "success"); |
| 330 | const unavailable = { ...check("Workflow approval status", "pending"), placeholder: true }; |
| 331 | const partial = evaluatePullRequestWatch( |
| 332 | watch({ awaitingBaseline: "all" }), |
| 333 | detail({ checks: [bot, unavailable] }), |
| 334 | noRemarks, |
| 335 | ); |
| 336 | assert.deepEqual(partial.changes, []); |
| 337 | assert.equal(partial.next.awaitingBaseline, "checks"); |
| 338 | const approval = { ...check("CI", "action-required"), url: run(1) }; |
| 339 | for (const checks of [[bot], [bot, approval]]) { |
| 340 | assert.deepEqual( |
| 341 | evaluatePullRequestWatch(partial.next, detail({ checks }), noRemarks).changes, |
| 342 | [], |
| 343 | ); |
| 344 | } |
| 345 | }); |
| 346 | |
| 347 | it("reports pending advisory checks finishing after a required-check baseline", () => { |
| 348 | const initial = detail({ |
| 349 | checks: [{ ...check("test", "success"), required: true }, check("bot", "pending")], |
| 350 | }); |
| 351 | const baseline = evaluatePullRequestWatch( |
| 352 | watch({ awaitingBaseline: "all" }), |
| 353 | initial, |
| 354 | noRemarks, |
| 355 | ); |
| 356 | assert.deepEqual(baseline.changes, []); |
| 357 | const settled = evaluatePullRequestWatch( |
| 358 | baseline.next, |
| 359 | { |
| 360 | ...initial, |
| 361 | checks: [initial.checks[0]!, check("bot", "success")], |
| 362 | }, |
| 363 | noRemarks, |
| 364 | ); |
| 365 | assert.deepEqual(settled.changes, [{ kind: "checks-passed", count: 2, required: false }]); |
| 366 | assert.deepEqual( |
| 367 | evaluatePullRequestWatch( |
| 368 | settled.next, |
| 369 | { |
| 370 | ...initial, |
| 371 | checks: [initial.checks[0]!, check("bot", "success")], |
| 372 | }, |
| 373 | noRemarks, |
| 374 | ).changes, |
| 375 | [], |
| 376 | ); |
| 377 | }); |
| 378 | |
| 379 | it("waits for workflow approval once, through reads that miss it, until the run finishes", () => { |
| 380 | const bot = check("bot", "success"); |
| 381 | const approval = { ...check("CI", "action-required"), url: run(1) }; |
| 382 | const gated = detail({ checks: [bot, approval] }); |
| 383 | const first = evaluatePullRequestWatch(watch(), gated, noRemarks); |
| 384 | assert.deepEqual(first.changes, [{ kind: "checks-awaiting-approval", checks: [approval] }]); |
| 385 | |
| 386 | // GitHub's approval read can answer empty while the rest of the checks read fine. |
| 387 | const missing = evaluatePullRequestWatch(first.next, detail({ checks: [bot] }), noRemarks); |
| 388 | assert.deepEqual(missing.changes, []); |
| 389 | assert.isFalse(missing.next.passed); |
| 390 | assert.deepEqual(evaluatePullRequestWatch(missing.next, gated, noRemarks).changes, []); |
| 391 | const later = { ...check("Preview", "action-required"), url: run(2) }; |
| 392 | assert.deepEqual( |
| 393 | evaluatePullRequestWatch(missing.next, detail({ checks: [bot, approval, later] }), noRemarks) |
| 394 | .changes, |
| 395 | [], |
| 396 | ); |
| 397 | const unnamed = { ...check("Docs", "action-required"), url: null }; |
| 398 | const named = detail({ checks: [bot, unnamed] }); |
| 399 | const alone = evaluatePullRequestWatch(watch(), named, noRemarks); |
| 400 | assert.deepEqual(alone.changes, [{ kind: "checks-awaiting-approval", checks: [unnamed] }]); |
| 401 | const placeholder = { ...unnamed, name: "Workflow approval status", placeholder: true }; |
| 402 | const unlisted = evaluatePullRequestWatch( |
| 403 | alone.next, |
| 404 | detail({ checks: [bot, placeholder] }), |
| 405 | noRemarks, |
| 406 | ); |
| 407 | assert.deepEqual(unlisted.changes, []); |
| 408 | assert.isFalse(unlisted.next.passed); |
| 409 | assert.deepEqual(evaluatePullRequestWatch(unlisted.next, named, noRemarks).changes, []); |
| 410 | const dropped = evaluatePullRequestWatch(alone.next, detail({ checks: [bot] }), noRemarks); |
| 411 | assert.deepEqual(dropped.changes, []); |
| 412 | assert.isFalse(dropped.next.passed); |
| 413 | assert.deepEqual(dropped.next.awaitingApproval, [unnamed.name]); |
| 414 | assert.deepEqual(evaluatePullRequestWatch(dropped.next, named, noRemarks).changes, []); |
| 415 | const namedPending = evaluatePullRequestWatch( |
| 416 | dropped.next, |
| 417 | detail({ |
| 418 | checks: [ |
| 419 | { ...bot, required: true }, |
| 420 | { ...unnamed, status: "pending" }, |
| 421 | ], |
| 422 | }), |
| 423 | noRemarks, |
| 424 | ); |
| 425 | assert.deepEqual(namedPending.changes, []); |
| 426 | assert.isFalse(namedPending.next.passed); |
| 427 | const namedDone = detail({ checks: [bot, { ...unnamed, status: "success" }] }); |
| 428 | const namedFinished = evaluatePullRequestWatch(namedPending.next, namedDone, noRemarks); |
| 429 | assert.deepEqual(namedFinished.changes, [{ kind: "checks-passed", count: 2, required: false }]); |
| 430 | assert.deepEqual(namedFinished.next.awaitingApproval, []); |
| 431 | assert.deepEqual( |
| 432 | evaluatePullRequestWatch(namedFinished.next, namedDone, noRemarks).changes, |
| 433 | [], |
| 434 | ); |
| 435 | const pushed = detail({ headSha: "bbbbbbbbbb", checks: [bot] }); |
| 436 | assert.deepEqual(evaluatePullRequestWatch(missing.next, pushed, noRemarks).changes, [ |
| 437 | { kind: "checks-passed", count: 1, required: false }, |
| 438 | ]); |
| 439 | |
| 440 | // Approved, the run's jobs carry their own names under the run's URL. |
| 441 | const job = { ...check("lint", "success"), url: `${run(1)}/job/7` }; |
| 442 | const pending = { ...check("test", "pending"), url: `${run(1)}/job/8` }; |
| 443 | const required = { ...bot, required: true }; |
| 444 | const running = evaluatePullRequestWatch( |
| 445 | missing.next, |
| 446 | detail({ checks: [required, job, pending] }), |
| 447 | noRemarks, |
| 448 | ); |
| 449 | assert.deepEqual(running.changes, []); |
| 450 | assert.isFalse(running.next.passed); |
| 451 | assert.deepEqual(running.next.awaitingApproval, [run(1)]); |
| 452 | const overlap = evaluatePullRequestWatch( |
| 453 | running.next, |
| 454 | detail({ checks: [required, job, { ...pending, url: `${run(2)}/job/9` }] }), |
| 455 | noRemarks, |
| 456 | ); |
| 457 | assert.deepEqual(overlap.changes, []); |
| 458 | assert.isFalse(overlap.next.passed); |
| 459 | assert.deepEqual(overlap.next.awaitingApproval, [run(1)]); |
| 460 | const unlinked = evaluatePullRequestWatch( |
| 461 | overlap.next, |
| 462 | detail({ checks: [required, job, { ...pending, url: null }] }), |
| 463 | noRemarks, |
| 464 | ); |
| 465 | assert.deepEqual(unlinked.changes, []); |
| 466 | assert.isFalse(unlinked.next.passed); |
| 467 | assert.deepEqual(unlinked.next.awaitingApproval, [run(1)]); |
| 468 | const advisory = evaluatePullRequestWatch( |
| 469 | unlinked.next, |
| 470 | detail({ checks: [required, job, { ...pending, url: "https://ci.example/test" }] }), |
| 471 | noRemarks, |
| 472 | ); |
| 473 | assert.deepEqual(advisory.changes, [{ kind: "checks-passed", count: 1, required: true }]); |
| 474 | assert.deepEqual(advisory.next.awaitingApproval, []); |
| 475 | const completed = detail({ checks: [required, job, { ...pending, status: "success" }] }); |
| 476 | const finished = evaluatePullRequestWatch(unlinked.next, completed, noRemarks); |
| 477 | assert.deepEqual(finished.changes, [{ kind: "checks-passed", count: 1, required: true }]); |
| 478 | assert.deepEqual(finished.next.awaitingApproval, []); |
| 479 | assert.deepEqual(evaluatePullRequestWatch(finished.next, completed, noRemarks).changes, []); |
| 480 | |
| 481 | const done = evaluatePullRequestWatch(missing.next, detail({ checks: [bot, job] }), noRemarks); |
| 482 | assert.deepEqual(done.changes, [{ kind: "checks-passed", count: 2, required: false }]); |
| 483 | }); |
| 484 | |
| 485 | it("does not pass or fail while the host cannot tell whether workflows await approval", () => { |
| 486 | const bot = check("bot", "success"); |
| 487 | const unknown = { |
| 488 | ...check("Workflow approval status", "action-required"), |
| 489 | url: null, |
| 490 | placeholder: true, |
| 491 | }; |
| 492 | const unread = evaluatePullRequestWatch(watch(), detail({ checks: [bot, unknown] }), noRemarks); |
| 493 | assert.deepEqual(unread.changes, []); |
| 494 | assert.isFalse(unread.next.passed); |
| 495 | const read = evaluatePullRequestWatch(unread.next, detail({ checks: [bot] }), noRemarks); |
| 496 | assert.deepEqual(read.changes, [{ kind: "checks-passed", count: 1, required: false }]); |
| 497 | const blocked = evaluatePullRequestWatch( |
| 498 | read.next, |
| 499 | detail({ checks: [{ ...bot, status: "pending" }, unknown] }), |
| 500 | noRemarks, |
| 501 | ); |
| 502 | assert.isFalse(blocked.next.passed); |
| 503 | assert.deepEqual( |
| 504 | evaluatePullRequestWatch(blocked.next, detail({ checks: [bot] }), noRemarks).changes, |
| 505 | [{ kind: "checks-passed", count: 1, required: false }], |
| 506 | ); |
| 507 | }); |
| 508 | |
| 509 | it("reports each failure at once, even while another check never finishes", () => { |
| 510 | const bot = check("CodeRabbit", "pending"); |
| 511 | const first = detail({ checks: [check("lint", "failure"), check("test", "pending"), bot] }); |
| 512 | const lint = evaluatePullRequestWatch(watch(), first, noRemarks); |
| 513 | assert.deepEqual(lint.changes, [{ kind: "checks-failed", failed: [check("lint", "failure")] }]); |
| 514 | assert.deepEqual(evaluatePullRequestWatch(lint.next, first, noRemarks).changes, []); |
| 515 | |
| 516 | const second = detail({ checks: [check("lint", "failure"), check("test", "failure"), bot] }); |
| 517 | const test = evaluatePullRequestWatch(lint.next, second, noRemarks); |
| 518 | assert.deepEqual(test.changes, [{ kind: "checks-failed", failed: [check("test", "failure")] }]); |
| 519 | |
| 520 | const rerun = evaluatePullRequestWatch(test.next, first, noRemarks); |
| 521 | assert.equal(evaluatePullRequestWatch(rerun.next, second, noRemarks).changes.length, 1); |
| 522 | |
| 523 | const pushed = detail({ ...second, headSha: "bbbbbbbbbb" }); |
| 524 | assert.equal(evaluatePullRequestWatch(test.next, pushed, noRemarks).changes.length, 1); |
| 525 | |
| 526 | const approval = { ...check("CI", "action-required"), url: run(1) }; |
| 527 | const gated = detail({ checks: [approval, check("lint", "failure")] }); |
| 528 | assert.deepEqual(evaluatePullRequestWatch(watch(), gated, noRemarks).changes, [ |
| 529 | { kind: "checks-failed", failed: [check("lint", "failure")] }, |
| 530 | { kind: "checks-awaiting-approval", checks: [approval] }, |
| 531 | ]); |
| 532 | }); |
| 533 | |
| 534 | it("reports passed once the required checks pass, whatever the others do", () => { |
| 535 | const required = (name: string, status: PullRequestCheck["status"]) => ({ |
| 536 | ...check(name, status), |
| 537 | required: true, |
| 538 | }); |
| 539 | const green = detail({ |
| 540 | checks: [required("test", "success"), required("lint", "success"), check("bot", "pending")], |
| 541 | }); |
| 542 | const passed = evaluatePullRequestWatch(watch(), green, noRemarks); |
| 543 | assert.deepEqual(passed.changes, [{ kind: "checks-passed", count: 2, required: true }]); |
| 544 | assert.deepEqual(evaluatePullRequestWatch(passed.next, green, noRemarks).changes, []); |
| 545 | |
| 546 | const plain = detail({ checks: [check("test", "success"), check("bot", "pending")] }); |
| 547 | assert.deepEqual(evaluatePullRequestWatch(watch(), plain, noRemarks).changes, []); |
| 548 | }); |
| 549 | |
| 550 | it("reports passed again when a required check shows up already passed", () => { |
| 551 | const required = (name: string, status: PullRequestCheck["status"]) => ({ |
| 552 | ...check(name, status), |
| 553 | required: true, |
| 554 | }); |
| 555 | const tests = detail({ |
| 556 | checks: [required("Tests", "success"), check("Smoke Tests", "pending")], |
| 557 | }); |
| 558 | const first = evaluatePullRequestWatch(watch(), tests, noRemarks); |
| 559 | assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: true }]); |
| 560 | |
| 561 | const gated = detail({ |
| 562 | checks: [ |
| 563 | required("Tests", "success"), |
| 564 | check("Smoke Tests", "success"), |
| 565 | required("Smoke Tests Gate", "success"), |
| 566 | ], |
| 567 | }); |
| 568 | const second = evaluatePullRequestWatch(first.next, gated, noRemarks); |
| 569 | assert.deepEqual(second.changes, [{ kind: "checks-passed", count: 2, required: true }]); |
| 570 | assert.deepEqual(evaluatePullRequestWatch(second.next, gated, noRemarks).changes, []); |
| 571 | |
| 572 | const pending = detail({ |
| 573 | checks: [required("Tests", "success"), required("Smoke Tests Gate", "pending")], |
| 574 | }); |
| 575 | const waiting = evaluatePullRequestWatch(first.next, pending, noRemarks); |
| 576 | assert.deepEqual(waiting.changes, []); |
| 577 | assert.deepEqual(evaluatePullRequestWatch(waiting.next, gated, noRemarks).changes, [ |
| 578 | { kind: "checks-passed", count: 2, required: true }, |
| 579 | ]); |
| 580 | }); |
| 581 | |
| 582 | // regression: #23712 required checks woke the agent early and the rest finishing never did |
| 583 | it("wakes again when the other checks finish after the required ones passed", () => { |
| 584 | const required = (name: string, status: PullRequestCheck["status"]) => ({ |
| 585 | ...check(name, status), |
| 586 | required: true, |
| 587 | }); |
| 588 | const early = detail({ |
| 589 | checks: [required("test", "success"), check("e2e", "pending")], |
| 590 | }); |
| 591 | const first = evaluatePullRequestWatch(watch(), early, noRemarks); |
| 592 | assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: true }]); |
| 593 | |
| 594 | const done = detail({ |
| 595 | checks: [required("test", "success"), check("e2e", "success")], |
| 596 | }); |
| 597 | const final = evaluatePullRequestWatch(first.next, done, noRemarks); |
| 598 | assert.deepEqual( |
| 599 | final.changes.map((change) => change.kind), |
| 600 | ["checks-passed"], |
| 601 | ); |
| 602 | assert.deepEqual(evaluatePullRequestWatch(final.next, done, noRemarks).changes, []); |
| 603 | // A later advisory job running and passing is not more news. |
| 604 | const botPending = detail({ checks: [...done.checks, check("bot", "pending")] }); |
| 605 | const churn = evaluatePullRequestWatch(final.next, botPending, noRemarks); |
| 606 | assert.deepEqual(churn.changes, []); |
| 607 | const botDone = detail({ checks: [...done.checks, check("bot", "success")] }); |
| 608 | assert.deepEqual(evaluatePullRequestWatch(churn.next, botDone, noRemarks).changes, []); |
| 609 | |
| 610 | // A pending check that turns required on the next read still finishes with one wake. |
| 611 | const promoted = detail({ |
| 612 | checks: [required("test", "success"), required("e2e", "success")], |
| 613 | }); |
| 614 | const gated = evaluatePullRequestWatch(first.next, promoted, noRemarks); |
| 615 | assert.deepEqual(gated.changes, [{ kind: "checks-passed", count: 2, required: true }]); |
| 616 | assert.deepEqual(evaluatePullRequestWatch(gated.next, promoted, noRemarks).changes, []); |
| 617 | |
| 618 | // Nothing else was pending when the required ones passed, so nothing more is owed. |
| 619 | const green = evaluatePullRequestWatch(watch(), done, noRemarks); |
| 620 | assert.deepEqual(green.changes, [{ kind: "checks-passed", count: 1, required: true }]); |
| 621 | assert.deepEqual(evaluatePullRequestWatch(green.next, botDone, noRemarks).changes, []); |
| 622 | }); |
| 623 | |
| 624 | it("reports an owed non-required failure once after the required notice", () => { |
| 625 | const required = { ...check("test", "success"), required: true }; |
| 626 | const early = detail({ checks: [required, check("e2e", "pending")] }); |
| 627 | const first = evaluatePullRequestWatch(watch(), early, noRemarks); |
| 628 | assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: true }]); |
| 629 | const failed = detail({ checks: [required, check("e2e", "failure")] }); |
| 630 | const failure = evaluatePullRequestWatch(first.next, failed, noRemarks); |
| 631 | assert.deepEqual(failure.changes, [ |
| 632 | { kind: "checks-failed", failed: [check("e2e", "failure")] }, |
| 633 | ]); |
| 634 | const repeated = evaluatePullRequestWatch(failure.next, failed, noRemarks); |
| 635 | assert.deepEqual(repeated.changes, []); |
| 636 | const recovered = detail({ checks: [required, check("e2e", "success")] }); |
| 637 | const final = evaluatePullRequestWatch(repeated.next, recovered, noRemarks); |
| 638 | assert.deepEqual(final.changes, [{ kind: "checks-passed", count: 2, required: false }]); |
| 639 | assert.deepEqual(evaluatePullRequestWatch(final.next, recovered, noRemarks).changes, []); |
| 640 | }); |
| 641 | |
| 642 | it("clears old-head owed checks when a new head is pushed", () => { |
| 643 | const required = { ...check("test", "success"), required: true }; |
| 644 | const early = detail({ checks: [required, check("e2e", "pending")] }); |
| 645 | const first = evaluatePullRequestWatch(watch(), early, noRemarks); |
| 646 | assert.deepEqual(first.next.owedChecks, ["e2e"]); |
| 647 | const unreadHead = detail({ headSha: "bbbbbbbbbb", checks: [] }); |
| 648 | const unread = evaluatePullRequestWatch(first.next, unreadHead, noRemarks); |
| 649 | assert.deepEqual(unread.changes, []); |
| 650 | assert.deepEqual(unread.next.owedChecks, []); |
| 651 | const pushed = detail({ ...unreadHead, checks: [required] }); |
| 652 | const newHead = evaluatePullRequestWatch(unread.next, pushed, noRemarks); |
| 653 | assert.deepEqual(newHead.changes, [{ kind: "checks-passed", count: 1, required: true }]); |
| 654 | assert.deepEqual(newHead.next.owedChecks, []); |
| 655 | const lateResult = detail({ ...pushed, checks: [required, check("e2e", "success")] }); |
| 656 | const final = evaluatePullRequestWatch(newHead.next, lateResult, noRemarks); |
| 657 | assert.deepEqual(final.changes, []); |
| 658 | assert.deepEqual(evaluatePullRequestWatch(final.next, lateResult, noRemarks).changes, []); |
| 659 | }); |
| 660 | |
| 661 | // regression: #30530 a failure told while other checks ran left the agent waiting for them forever |
| 662 | it.each([true, false])( |
| 663 | "wakes once when the other checks finish after one failed (required: %s)", |
| 664 | (required) => { |
| 665 | const gated = (name: string, status: PullRequestCheck["status"]) => ({ |
| 666 | ...check(name, status), |
| 667 | required, |
| 668 | }); |
| 669 | const labels = gated("labels", "failure"); |
| 670 | const running = detail({ |
| 671 | checks: [labels, gated("test", "pending"), gated("docs", "pending")], |
| 672 | }); |
| 673 | const failure = evaluatePullRequestWatch(watch(), running, noRemarks); |
| 674 | assert.deepEqual(failure.changes, [{ kind: "checks-failed", failed: [labels] }]); |
| 675 | assert.deepEqual(evaluatePullRequestWatch(failure.next, running, noRemarks).changes, []); |
| 676 | |
| 677 | const done = detail({ checks: [labels, gated("test", "success"), gated("docs", "skipped")] }); |
| 678 | const finished = evaluatePullRequestWatch(failure.next, done, noRemarks); |
| 679 | assert.deepEqual(finished.changes, [ |
| 680 | { kind: "checks-finished", passed: 1, failing: [labels] }, |
| 681 | ]); |
| 682 | assert.equal( |
| 683 | pullRequestWatchMessage({ |
| 684 | number: 1, |
| 685 | url: "https://github.com/o/r/pull/1", |
| 686 | baseBranch: "main", |
| 687 | headSha: finished.next.headSha, |
| 688 | report: finished, |
| 689 | }).notification.outcome, |
| 690 | "failed", |
| 691 | ); |
| 692 | assert.deepEqual(evaluatePullRequestWatch(finished.next, done, noRemarks).changes, []); |
| 693 | |
| 694 | // A later job running and passing is not more news. |
| 695 | const botPending = detail({ checks: [...done.checks, check("bot", "pending")] }); |
| 696 | const churn = evaluatePullRequestWatch(finished.next, botPending, noRemarks); |
| 697 | assert.deepEqual(churn.changes, []); |
| 698 | const botDone = detail({ checks: [...done.checks, check("bot", "success")] }); |
| 699 | assert.deepEqual(evaluatePullRequestWatch(churn.next, botDone, noRemarks).changes, []); |
| 700 | |
| 701 | // A watch started after the failure is still owed the news that the rest finished. |
| 702 | const late = evaluatePullRequestWatch(watch({ awaitingBaseline: "all" }), running, noRemarks); |
| 703 | assert.deepEqual(late.changes, []); |
| 704 | assert.deepEqual( |
| 705 | evaluatePullRequestWatch(late.next, done, noRemarks).changes, |
| 706 | finished.changes, |
| 707 | ); |
| 708 | }, |
| 709 | ); |
| 710 | |
| 711 | it("says the checks finished only once every check has, a maintainer's approval included", () => { |
| 712 | const required = (name: string, status: PullRequestCheck["status"]) => ({ |
| 713 | ...check(name, status), |
| 714 | required: true, |
| 715 | }); |
| 716 | const lint = required("lint", "failure"); |
| 717 | const unit = required("unit", "success"); |
| 718 | const failure = evaluatePullRequestWatch( |
| 719 | watch(), |
| 720 | detail({ checks: [lint, required("unit", "pending"), check("e2e", "pending")] }), |
| 721 | noRemarks, |
| 722 | ); |
| 723 | assert.deepEqual(failure.changes, [{ kind: "checks-failed", failed: [lint] }]); |
| 724 | |
| 725 | const advisory = detail({ checks: [lint, unit, check("e2e", "pending")] }); |
| 726 | const waiting = evaluatePullRequestWatch(failure.next, advisory, noRemarks); |
| 727 | assert.deepEqual(waiting.changes, []); |
| 728 | |
| 729 | const approval = { ...check("deploy", "action-required"), url: run(1) }; |
| 730 | const held = detail({ checks: [lint, unit, check("e2e", "success"), approval] }); |
| 731 | const holding = evaluatePullRequestWatch(waiting.next, held, noRemarks); |
| 732 | assert.deepEqual(holding.changes, [{ kind: "checks-awaiting-approval", checks: [approval] }]); |
| 733 | |
| 734 | const unknown = { |
| 735 | ...check("Workflow approval status", "action-required"), |
| 736 | url: null, |
| 737 | placeholder: true, |
| 738 | }; |
| 739 | const deployed = { ...approval, status: "success" as const }; |
| 740 | const e2e = check("e2e", "failure"); |
| 741 | const unread = evaluatePullRequestWatch( |
| 742 | holding.next, |
| 743 | detail({ checks: [lint, unit, check("e2e", "success"), deployed, unknown] }), |
| 744 | noRemarks, |
| 745 | ); |
| 746 | assert.deepEqual(unread.changes, []); |
| 747 | |
| 748 | // A failure that arrives with the last result shares its wake. |
| 749 | const done = detail({ checks: [lint, unit, e2e, deployed] }); |
| 750 | const finished = evaluatePullRequestWatch(unread.next, done, noRemarks); |
| 751 | assert.deepEqual(finished.changes, [ |
| 752 | { kind: "checks-failed", failed: [e2e] }, |
| 753 | { kind: "checks-finished", passed: 2, failing: [lint, e2e] }, |
| 754 | ]); |
| 755 | assert.deepEqual(evaluatePullRequestWatch(finished.next, done, noRemarks).changes, []); |
| 756 | |
| 757 | // The wake is owed just the same when only an approval the read omits is outstanding. |
| 758 | const gated = { ...check("deploy", "action-required"), url: run(2) }; |
| 759 | const asked = evaluatePullRequestWatch( |
| 760 | watch(), |
| 761 | detail({ checks: [required("lint", "pending"), gated] }), |
| 762 | noRemarks, |
| 763 | ); |
| 764 | const omitted = evaluatePullRequestWatch(asked.next, detail({ checks: [lint] }), noRemarks); |
| 765 | assert.deepEqual(omitted.changes, [{ kind: "checks-failed", failed: [lint] }]); |
| 766 | const job = { ...check("deploy-job", "success"), url: `${run(2)}/job/1` }; |
| 767 | assert.deepEqual( |
| 768 | evaluatePullRequestWatch(omitted.next, detail({ checks: [lint, job] }), noRemarks).changes, |
| 769 | [{ kind: "checks-finished", passed: 1, failing: [lint] }], |
| 770 | ); |
| 771 | }); |
| 772 | |
| 773 | it("wakes when a rerun finishes while another failure still blocks the checks", () => { |
| 774 | const labels = check("labels", "failure"); |
| 775 | const test = check("test", "failure"); |
| 776 | const both = detail({ checks: [labels, test] }); |
| 777 | // Nothing else ran when these failed, so their own wake is the last word. |
| 778 | const failed = evaluatePullRequestWatch(watch(), both, noRemarks); |
| 779 | assert.deepEqual(failed.changes, [{ kind: "checks-failed", failed: [labels, test] }]); |
| 780 | assert.deepEqual(evaluatePullRequestWatch(failed.next, both, noRemarks).changes, []); |
| 781 | |
| 782 | const rerun = evaluatePullRequestWatch( |
| 783 | failed.next, |
| 784 | detail({ checks: [labels, check("test", "pending")] }), |
| 785 | noRemarks, |
| 786 | ); |
| 787 | assert.deepEqual(rerun.changes, []); |
| 788 | const recovered = detail({ checks: [labels, check("test", "success")] }); |
| 789 | assert.deepEqual(evaluatePullRequestWatch(rerun.next, recovered, noRemarks).changes, [ |
| 790 | { kind: "checks-finished", passed: 1, failing: [labels] }, |
| 791 | ]); |
| 792 | |
| 793 | // A rerun that starts and ends between two reads is the same news. |
| 794 | assert.deepEqual(evaluatePullRequestWatch(failed.next, recovered, noRemarks).changes, [ |
| 795 | { kind: "checks-finished", passed: 1, failing: [labels] }, |
| 796 | ]); |
| 797 | |
| 798 | const pushed = detail({ ...recovered, headSha: "bbbbbbbbbb" }); |
| 799 | assert.deepEqual(evaluatePullRequestWatch(rerun.next, pushed, noRemarks).changes, [ |
| 800 | { kind: "checks-failed", failed: [labels] }, |
| 801 | ]); |
| 802 | }); |
| 803 | |
| 804 | // regression: #16208 PR watches burned GitHub's rate limit and gave up |
| 805 | it("does not report passed again for a new passed check where none is required", () => { |
| 806 | const first = evaluatePullRequestWatch( |
| 807 | watch(), |
| 808 | detail({ checks: [check("test", "success")] }), |
| 809 | noRemarks, |
| 810 | ); |
| 811 | assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: false }]); |
| 812 | const both = detail({ checks: [check("test", "success"), check("lint", "success")] }); |
| 813 | assert.deepEqual(evaluatePullRequestWatch(first.next, both, noRemarks).changes, []); |
| 814 | }); |
| 815 | |
| 816 | it("does not wake a watch saved before passed checks were recorded", () => { |
| 817 | const green = detail({ checks: [{ ...check("test", "success"), required: true }] }); |
| 818 | const told = watch({ headSha: "aaaaaaaaaa", passed: true }); |
| 819 | const saved = evaluatePullRequestWatch(told, green, noRemarks); |
| 820 | assert.deepEqual(saved.changes, []); |
| 821 | assert.deepEqual(saved.next.passedChecks, ["test"]); |
| 822 | }); |
| 823 | |
| 824 | it("keeps remarks for a later pass when the conversation was not read whole", () => { |
| 825 | const comments = [remark("reviewer", "2026-10-02T12:06:00Z")]; |
| 826 | const partial = evaluatePullRequestWatch(watch(), detail(), null); |
| 827 | assert.deepEqual(partial.changes, []); |
| 828 | assert.equal( |
| 829 | evaluatePullRequestWatch(partial.next, detail(), comments).changes[0]?.kind, |
| 830 | "remarks", |
| 831 | ); |
| 832 | }); |
| 833 | |
| 834 | it("reports a remark that shows up late with the same time as a reported one", () => { |
| 835 | const first = remark("reviewer", "2026-10-02T12:06:00Z"); |
| 836 | const late = { ...remark("bot", "2026-10-02T12:06:00Z"), id: "late" }; |
| 837 | const reported = evaluatePullRequestWatch(watch(), detail(), [first]); |
| 838 | const again = evaluatePullRequestWatch(reported.next, detail(), [first, late]); |
| 839 | assert.deepEqual(again.changes, [{ kind: "remarks", remarks: [late] }]); |
| 840 | assert.deepEqual(again.next.remarkIds, [first.id, "late"]); |
| 841 | }); |
| 842 | |
| 843 | it("reports edits after the watermark once and counts them toward the wake limit", () => { |
| 844 | const old = remark("greptile[bot]", "2026-10-02T11:00:00Z"); |
| 845 | const watching = watch({ |
| 846 | headSha: "aaaaaaaaaa", |
| 847 | remarkIds: [old.id], |
| 848 | wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1, |
| 849 | }); |
| 850 | assert.deepEqual(evaluatePullRequestWatch(watching, detail(), [old]).changes, []); |
| 851 | const edited = { ...old, editedAt: "2026-10-02T12:06:00Z" }; |
| 852 | const report = evaluatePullRequestWatch(watching, detail(), [edited]); |
| 853 | assert.deepEqual(report.changes, [{ kind: "remarks", remarks: [edited] }]); |
| 854 | assert.equal(report.next.remarksThrough, edited.editedAt); |
| 855 | assert.deepEqual(report.next.remarkIds, [old.id]); |
| 856 | assert.isTrue(report.exhausted); |
| 857 | assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), [edited]).changes, []); |
| 858 | const late = { ...edited, id: "late" }; |
| 859 | assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), [edited, late]).changes, [ |
| 860 | { kind: "remarks", remarks: [late] }, |
| 861 | ]); |
| 862 | }); |
| 863 | |
| 864 | it("wakes once per edit of a remark seen at the watermark, never for the agent's own", () => { |
| 865 | const seen = remark("ci-bot", "2026-10-10T19:52:40.000Z"); |
| 866 | const watching = watch({ |
| 867 | headSha: "aaaaaaaaaa", |
| 868 | remarksThrough: seen.createdAt, |
| 869 | remarkIds: [seen.id], |
| 870 | wakes: 2, |
| 871 | }); |
| 872 | assert.deepEqual(evaluatePullRequestWatch(watching, detail(), [seen]).changes, []); |
| 873 | const edited = { ...seen, editedAt: "2026-10-10T20:04:50.000Z" }; |
| 874 | const first = evaluatePullRequestWatch(watching, detail(), [edited]); |
| 875 | assert.deepEqual(first.changes, [{ kind: "remarks", remarks: [edited] }]); |
| 876 | assert.strictEqual(first.next.remarksThrough, edited.editedAt); |
| 877 | assert.deepEqual(evaluatePullRequestWatch(first.next, detail(), [edited]).changes, []); |
| 878 | const again = { ...seen, editedAt: "2026-10-10T20:05:50.000Z" }; |
| 879 | const second = evaluatePullRequestWatch(first.next, detail(), [again]); |
| 880 | assert.deepEqual(second.changes, [{ kind: "remarks", remarks: [again] }]); |
| 881 | assert.deepEqual(evaluatePullRequestWatch(second.next, detail(), [again]).changes, []); |
| 882 | const own = { ...edited, author: { login: "agent-user", name: null, avatarUrl: null } }; |
| 883 | assert.deepEqual(evaluatePullRequestWatch(watching, detail(), [own]).changes, []); |
| 884 | }); |
| 885 | |
| 886 | it("does not treat a failed check read as a rerun", () => { |
| 887 | const failed = detail({ checks: [check("lint", "failure")] }); |
| 888 | const reported = evaluatePullRequestWatch(watch(), failed, noRemarks); |
| 889 | assert.equal(reported.changes.length, 1); |
| 890 | const unreadable = evaluatePullRequestWatch(reported.next, detail({ checks: [] }), noRemarks); |
| 891 | assert.deepEqual(evaluatePullRequestWatch(unreadable.next, failed, noRemarks).changes, []); |
| 892 | }); |
| 893 | |
| 894 | it("wakes for the pull request's author when the agent is someone else", () => { |
| 895 | const contributor = detail({ author: { login: "contributor", name: null, avatarUrl: null } }); |
| 896 | const reply = remark("contributor", "2026-10-02T12:06:00Z"); |
| 897 | assert.deepEqual(evaluatePullRequestWatch(watch(), contributor, [reply]).changes, [ |
| 898 | { kind: "remarks", remarks: [reply] }, |
| 899 | ]); |
| 900 | const noViewer = detail({ viewer: undefined, author: contributor.author }); |
| 901 | assert.deepEqual(evaluatePullRequestWatch(watch(), noViewer, [reply]).changes, []); |
| 902 | }); |
| 903 | |
| 904 | it("reports remarks from others once and never the agent's own", () => { |
| 905 | const comments = [ |
| 906 | remark("agent-user", "2026-10-02T12:05:00Z", "Fixed in the latest push."), |
| 907 | remark("macroscope-app[bot]", "2026-10-02T12:06:00Z"), |
| 908 | remark("reviewer", "2026-10-02T11:00:00Z", "Older than the watch."), |
| 909 | ]; |
| 910 | const report = evaluatePullRequestWatch(watch(), detail(), comments); |
| 911 | assert.deepEqual(report.changes, [{ kind: "remarks", remarks: [comments[1]!] }]); |
| 912 | assert.equal(report.next.remarksThrough, "2026-10-02T12:06:00Z"); |
| 913 | assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), comments).changes, []); |
| 914 | }); |
| 915 | |
| 916 | it("cuts a long remark in the wake message without splitting an emoji", () => { |
| 917 | const body = `${"x".repeat(196)}🚀 and more`; |
| 918 | const report = evaluatePullRequestWatch(watch(), detail(), [ |
| 919 | remark("reviewer", "2026-10-02T12:06:00Z", body), |
| 920 | ]); |
| 921 | const { text } = pullRequestWatchMessage({ |
| 922 | number: 1, |
| 923 | url: "https://github.com/o/r/pull/1", |
| 924 | baseBranch: "main", |
| 925 | headSha: null, |
| 926 | report, |
| 927 | }); |
| 928 | assert.isTrue(text.isWellFormed()); |
| 929 | assert.include(text, `"${"x".repeat(196)}..."`); |
| 930 | }); |
| 931 | |
| 932 | it.each([ |
| 933 | [ |
| 934 | "a bot's leading badge", |
| 935 | '<a href="https://app.coderabbit.ai/change-stack/o/r/pull/1?cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://coderabbit.ai/badge-dark.svg"><img alt="Severity > `Minor`" src="https://coderabbit.ai/badge.svg"></picture></a>\r\n\r\n_⚠️ Potential issue_ | _🟠 Major_\r\n\r\n**Guard the empty list.**', |
| 936 | "_⚠️ Potential issue_ | _🟠 Major_ **Guard the empty list.**", |
| 937 | ], |
| 938 | [ |
| 939 | "tags in prose and a hidden comment", |
| 940 | "<!-- state -->\n<details>\n<summary>Why</summary>\n\n<p>Guard this</p>, then <br/>retry.\n</details>", |
| 941 | "Why Guard this , then retry.", |
| 942 | ], |
| 943 | [ |
| 944 | "type arguments", |
| 945 | "Return Promise<void>, not Promise<A> or Vec<code>.", |
| 946 | "Return Promise<void>, not Promise<A> or Vec<code>.", |
| 947 | ], |
| 948 | [ |
| 949 | "code spans and fences", |
| 950 | "Replace `<div>` with ``<span>``:\n~~~html\n<p>Done</p>\n~~~\r\nor\r\n```html\r\n<b>Done</b>\r\n```", |
| 951 | "Replace `<div>` with ``<span>``: ~~~html <p>Done</p> ~~~ or ```html <b>Done</b> ```", |
| 952 | ], |
| 953 | [ |
| 954 | "indented code", |
| 955 | "Use this replacement:\n\n <div>\n <span>Ready</span>\n </div>\n\nKeep the <b>wrapper</b>.", |
| 956 | "Use this replacement: <div> <span>Ready</span> </div> Keep the wrapper .", |
| 957 | ], |
| 958 | ["an indented continuation of an HTML block", "<div>\n <span>Ready</span>\n</div>", "Ready"], |
| 959 | ])("quotes a remark's text without its HTML tags: %s", (_name, body, quoted) => { |
| 960 | const report = evaluatePullRequestWatch(watch(), detail(), [ |
| 961 | remark("reviewer", "2026-10-02T12:06:00Z", body), |
| 962 | ]); |
| 963 | const { text } = pullRequestWatchMessage({ |
| 964 | number: 1, |
| 965 | url: "https://github.com/o/r/pull/1", |
| 966 | baseBranch: "main", |
| 967 | headSha: null, |
| 968 | report, |
| 969 | }); |
| 970 | assert.include(text, `: "${quoted}" `); |
| 971 | }); |
| 972 | |
| 973 | it("reports a conflict once, until the branch is clean again", () => { |
| 974 | const conflicting = detail({ mergeability: "conflicting" }); |
| 975 | const first = evaluatePullRequestWatch(watch(), conflicting, noRemarks); |
| 976 | assert.deepEqual(first.changes, [{ kind: "conflicting" }]); |
| 977 | const { headSha: _unreadHead, ...headUnread } = conflicting; |
| 978 | const unread = evaluatePullRequestWatch(first.next, headUnread, noRemarks); |
| 979 | assert.deepEqual(unread.changes, []); |
| 980 | assert.deepEqual(evaluatePullRequestWatch(unread.next, conflicting, noRemarks).changes, []); |
| 981 | const recomputing = evaluatePullRequestWatch( |
| 982 | first.next, |
| 983 | detail({ mergeability: "unknown" }), |
| 984 | noRemarks, |
| 985 | ); |
| 986 | assert.deepEqual( |
| 987 | evaluatePullRequestWatch(recomputing.next, conflicting, noRemarks).changes, |
| 988 | [], |
| 989 | ); |
| 990 | const clean = evaluatePullRequestWatch(first.next, detail(), noRemarks); |
| 991 | assert.deepEqual(evaluatePullRequestWatch(clean.next, conflicting, noRemarks).changes, [ |
| 992 | { kind: "conflicting" }, |
| 993 | ]); |
| 994 | const pushed = { headSha: "bbbbbbbbbb" }; |
| 995 | const recomputed = evaluatePullRequestWatch( |
| 996 | first.next, |
| 997 | detail({ ...pushed, mergeability: "unknown" }), |
| 998 | noRemarks, |
| 999 | ); |
| 1000 | assert.deepEqual( |
| 1001 | evaluatePullRequestWatch( |
| 1002 | recomputed.next, |
| 1003 | detail({ ...pushed, mergeability: "conflicting" }), |
| 1004 | noRemarks, |
| 1005 | ).changes, |
| 1006 | [{ kind: "conflicting" }], |
| 1007 | ); |
| 1008 | }); |
| 1009 | |
| 1010 | it("reports each merge queue removal once while the head checks stay green", () => { |
| 1011 | const green = detail({ checks: [check("test", "success")] }); |
| 1012 | const watching = evaluatePullRequestWatch(watch(), green, noRemarks).next; |
| 1013 | const removed = (id: string, removedAt: string) => |
| 1014 | detail({ ...green, mergeQueueRemoval: { id, removedAt, reason: "Merge conflict" } }); |
| 1015 | |
| 1016 | const dropped = removed("first", "2026-10-02T12:10:00Z"); |
| 1017 | const first = evaluatePullRequestWatch(watching, dropped, noRemarks); |
| 1018 | assert.deepEqual(first.changes, [{ kind: "merge-queue-removed", reason: "Merge conflict" }]); |
| 1019 | const wake = pullRequestWatchMessage({ |
| 1020 | number: 1, |
| 1021 | url: "https://github.com/o/r/pull/1", |
| 1022 | baseBranch: "main", |
| 1023 | headSha: first.next.headSha, |
| 1024 | report: first, |
| 1025 | }); |
| 1026 | assert.deepEqual(wake.notification, { |
| 1027 | source: { kind: "monitor" }, |
| 1028 | outcome: "failed", |
| 1029 | summary: "#1: removed from merge queue", |
| 1030 | }); |
| 1031 | assert.deepEqual(evaluatePullRequestWatch(first.next, dropped, noRemarks).changes, []); |
| 1032 | // A read without queue news keeps the removal already told. |
| 1033 | const quiet = evaluatePullRequestWatch(first.next, green, noRemarks); |
| 1034 | assert.deepEqual(evaluatePullRequestWatch(quiet.next, dropped, noRemarks).changes, []); |
| 1035 | |
| 1036 | const again = removed("second", "2026-10-02T12:20:00Z"); |
| 1037 | assert.equal(evaluatePullRequestWatch(first.next, again, noRemarks).changes.length, 1); |
| 1038 | const before = removed("before", "2026-10-02T11:00:00Z"); |
| 1039 | const baseline = evaluatePullRequestWatch( |
| 1040 | watch({ headSha: watching.headSha, passed: true, passedChecks: watching.passedChecks }), |
| 1041 | before, |
| 1042 | noRemarks, |
| 1043 | ); |
| 1044 | assert.deepEqual(baseline.changes, []); |
| 1045 | for (const prior of [watching, baseline.next]) { |
| 1046 | const skewed = evaluatePullRequestWatch( |
| 1047 | prior, |
| 1048 | removed("new", "2026-10-02T11:59:58Z"), |
| 1049 | noRemarks, |
| 1050 | ); |
| 1051 | assert.deepEqual(skewed.changes, [{ kind: "merge-queue-removed", reason: "Merge conflict" }]); |
| 1052 | assert.deepEqual( |
| 1053 | evaluatePullRequestWatch(skewed.next, removed("new", "2026-10-02T11:59:58Z"), noRemarks) |
| 1054 | .changes, |
| 1055 | [], |
| 1056 | ); |
| 1057 | } |
| 1058 | }); |
| 1059 | |
| 1060 | it("reports a merge queue removal stamped in the second the watch started", () => { |
| 1061 | const watching = watch({ startedAt: "2026-10-02T12:00:00.500Z" }); |
| 1062 | const removed = (removedAt: string) => |
| 1063 | detail({ mergeQueueRemoval: { id: "first", removedAt, reason: "CI failed" } }); |
| 1064 | assert.deepEqual( |
| 1065 | evaluatePullRequestWatch(watching, removed("2026-10-02T12:00:00Z"), noRemarks).changes, |
| 1066 | [{ kind: "merge-queue-removed", reason: "CI failed" }], |
| 1067 | ); |
| 1068 | assert.deepEqual( |
| 1069 | evaluatePullRequestWatch(watching, removed("2026-10-02T11:59:59Z"), noRemarks).changes, |
| 1070 | [], |
| 1071 | ); |
| 1072 | }); |
| 1073 | |
| 1074 | it("stays quiet when the queue lets go of a pull request by merging it", () => { |
| 1075 | const watching = evaluatePullRequestWatch(watch(), detail(), noRemarks).next; |
| 1076 | const merging = detail({ |
| 1077 | mergeQueueRemoval: { id: "landed", removedAt: "2026-10-02T12:10:00Z", reason: "merged" }, |
| 1078 | }); |
| 1079 | const read = evaluatePullRequestWatch(watching, merging, noRemarks); |
| 1080 | assert.deepEqual(read.changes, []); |
| 1081 | assert.equal(read.next.mergeQueueRemovalId, "landed"); |
| 1082 | }); |
| 1083 | |
| 1084 | it("does not spend the comment wake limit on check results", () => { |
| 1085 | const tired = watch({ headSha: "aaaaaaaaaa", wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1 }); |
| 1086 | const result = evaluatePullRequestWatch( |
| 1087 | tired, |
| 1088 | detail({ checks: [check("lint", "failure")] }), |
| 1089 | noRemarks, |
| 1090 | ); |
| 1091 | assert.isFalse(result.exhausted); |
| 1092 | assert.equal(result.next.wakes, 0); |
| 1093 | }); |
| 1094 | |
| 1095 | it("stops after the wake limit unless the head moves", () => { |
| 1096 | const comments = [remark("reviewer", "2026-10-02T12:10:00Z")]; |
| 1097 | const tired = watch({ headSha: "aaaaaaaaaa", wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1 }); |
| 1098 | assert.isTrue(evaluatePullRequestWatch(tired, detail(), comments).exhausted); |
| 1099 | const pushed = evaluatePullRequestWatch(tired, detail({ headSha: "cccccccccc" }), comments); |
| 1100 | assert.isFalse(pushed.exhausted); |
| 1101 | assert.equal(pushed.next.wakes, 1); |
| 1102 | }); |
| 1103 | |
| 1104 | const anHourLater = ( |
| 1105 | first: ThreadPullRequestWatch, |
| 1106 | read: Detail, |
| 1107 | ): ReadonlyArray<ReadonlyArray<string>> => { |
| 1108 | const kinds: Array<ReadonlyArray<string>> = []; |
| 1109 | let current = first; |
| 1110 | for (let minute = 0; minute <= 60; minute += 10) { |
| 1111 | const { changes, next } = evaluate(current, read, noRemarks, minute * 60_000); |
| 1112 | kinds.push(changes.map((change) => change.kind)); |
| 1113 | current = next; |
| 1114 | } |
| 1115 | return kinds; |
| 1116 | }; |
| 1117 | |
| 1118 | it("does not tell a watch saved before the no-checks clock that its seen head has none", () => { |
| 1119 | // Such a watch may have seen this head's checks pending, which records nothing. |
| 1120 | const legacy = watch({ headSha: "aaaaaaaaaa" }); |
| 1121 | assert.isTrue(anHourLater(legacy, detail({ checks: [] })).every((kinds) => kinds.length === 0)); |
| 1122 | const pushed = anHourLater(legacy, detail({ headSha: "bbbbbbbbbb", checks: [] })); |
| 1123 | assert.deepEqual(pushed.flat(), ["no-checks"]); |
| 1124 | const unknown = detail({ |
| 1125 | checks: [{ ...check("Workflow approval status", "action-required"), placeholder: true }], |
| 1126 | }); |
| 1127 | const fresh = watch({ awaitingBaseline: "all" }); |
| 1128 | assert.deepEqual(anHourLater(fresh, unknown).flat(), []); |
| 1129 | const unread = evaluate(fresh, unknown, noRemarks, 0); |
| 1130 | assert.deepEqual( |
| 1131 | evaluate(unread.next, detail({ checks: [] }), noRemarks, 60 * 60_000).changes, |
| 1132 | [{ kind: "no-checks", minutes: 60 }], |
| 1133 | ); |
| 1134 | }); |
| 1135 | |
| 1136 | it("starts the no-checks clock while a fresh watch awaits its baseline", () => { |
| 1137 | assert.deepEqual( |
| 1138 | anHourLater(watch({ awaitingBaseline: "all" }), detail({ checks: [] })).flat(), |
| 1139 | ["no-checks"], |
| 1140 | ); |
| 1141 | }); |
| 1142 | |
| 1143 | it("never reports missing checks on a host where an empty list says nothing", () => { |
| 1144 | const silent = detail({ capabilities: capabilities(false), checks: [] }); |
| 1145 | assert.isTrue(anHourLater(watch(), silent).every((kinds) => kinds.length === 0)); |
| 1146 | }); |
| 1147 | |
| 1148 | it("counts checks as pending until every one, a review bot's included, has finished", () => { |
| 1149 | const pending = (checks: ReadonlyArray<PullRequestCheck>, from = watch()) => |
| 1150 | evaluatePullRequestWatch(from, detail({ checks }), noRemarks).next.checksPending; |
| 1151 | const ci = { ...check("test", "success"), required: true }; |
| 1152 | assert.isTrue(pending([ci, check("CodeRabbit", "pending")])); |
| 1153 | assert.isFalse(pending([ci, check("CodeRabbit", "success")])); |
| 1154 | assert.isFalse(pending([ci, check("CodeRabbit", "failure")])); |
| 1155 | assert.isFalse(pending([ci, check("deploy", "action-required")])); |
| 1156 | // A failed read keeps the last answer, as does one with only an unreadable approval check. |
| 1157 | assert.isFalse(pending([], watch({ headSha: "aaaaaaaaaa", checksPending: false }))); |
| 1158 | const unreadable = { ...check("approvals", "action-required"), placeholder: true }; |
| 1159 | assert.isTrue(pending([unreadable], watch({ headSha: "aaaaaaaaaa", checksPending: true }))); |
| 1160 | }); |
| 1161 | |
| 1162 | it("waits a while for a new head's checks on every host", () => { |
| 1163 | const pushed = (checksAlwaysReport: boolean) => |
| 1164 | evaluatePullRequestWatch( |
| 1165 | watch({ headSha: "aaaaaaaaaa", checksPending: false }), |
| 1166 | detail({ |
| 1167 | headSha: "bbbbbbbbbb", |
| 1168 | capabilities: capabilities(checksAlwaysReport), |
| 1169 | checks: [], |
| 1170 | }), |
| 1171 | noRemarks, |
| 1172 | ).next.checksPending; |
| 1173 | assert.isTrue(pushed(true)); |
| 1174 | assert.isTrue(pushed(false)); |
| 1175 | |
| 1176 | // Once the no-checks window passes, the watch stops waiting for them. |
| 1177 | for (const checksAlwaysReport of [true, false]) { |
| 1178 | let current = watch({ headSha: "aaaaaaaaaa" }); |
| 1179 | for (const minute of [0, 20]) { |
| 1180 | current = evaluate( |
| 1181 | current, |
| 1182 | detail({ |
| 1183 | headSha: "bbbbbbbbbb", |
| 1184 | capabilities: capabilities(checksAlwaysReport), |
| 1185 | checks: [], |
| 1186 | }), |
| 1187 | noRemarks, |
| 1188 | minute * 60_000, |
| 1189 | ).next; |
| 1190 | } |
| 1191 | assert.isFalse(current.checksPending); |
| 1192 | } |
| 1193 | |
| 1194 | // A watch with no answer waits on an empty list only while the no-checks clock runs. |
| 1195 | const unanswered = (checksAwaitedSince: string | null) => |
| 1196 | evaluatePullRequestWatch( |
| 1197 | watch({ headSha: "bbbbbbbbbb", checksAwaitedSince }), |
| 1198 | detail({ headSha: "bbbbbbbbbb", checks: [] }), |
| 1199 | noRemarks, |
| 1200 | ).next.checksPending; |
| 1201 | assert.isFalse(unanswered(null)); |
| 1202 | assert.isTrue(unanswered(STARTED)); |
| 1203 | }); |
| 1204 | }); |
| 1205 | |
| 1206 | describe("pullRequestWatchEndMessage", () => { |
| 1207 | const url = "https://git.cafe/pingdotgg/t3code/pull/7"; |
| 1208 | const end = (overrides: Partial<Parameters<typeof pullRequestWatchEndMessage>[0]["detail"]>) => |
| 1209 | pullRequestWatchEndMessage({ |
| 1210 | number: 7, |
| 1211 | url, |
| 1212 | detail: { state: "merged", baseBranch: "main", ...overrides }, |
| 1213 | }); |
| 1214 | const mergeCommitSha = "3b2f958603a51d8c42c4ede02eee7b8cb310b2d9"; |
| 1215 | |
| 1216 | it("says how the pull request ended, with the merge commit and merger the host reports", () => { |
| 1217 | const merged = end({ |
| 1218 | mergeCommitSha, |
| 1219 | mergedBy: { login: "juliusmarminge", name: null, avatarUrl: null }, |
| 1220 | }); |
| 1221 | assert.equal( |
| 1222 | merged.text, |
| 1223 | `Pull request #7 (${url}) was merged into main by juliusmarminge as 3b2f958, so T3 Code stopped watching it.`, |
| 1224 | ); |
| 1225 | assert.deepEqual(merged.notification, { |
| 1226 | source: { kind: "monitor" }, |
| 1227 | outcome: "completed", |
| 1228 | summary: "#7: merged, stopped watching", |
| 1229 | }); |
| 1230 | // GitCafe reports a merge made on GitHub without who made it. |
| 1231 | assert.equal( |
| 1232 | end({ mergeCommitSha }).text, |
| 1233 | `Pull request #7 (${url}) was merged into main as 3b2f958, so T3 Code stopped watching it.`, |
| 1234 | ); |
| 1235 | |
| 1236 | const closed = end({ state: "closed" }); |
| 1237 | assert.equal( |
| 1238 | closed.text, |
| 1239 | `Pull request #7 (${url}) was closed without merging, so T3 Code stopped watching it. If it is reopened, call watch_pull_request to watch it again.`, |
| 1240 | ); |
| 1241 | assert.equal(closed.notification.summary, "#7: closed, stopped watching"); |
| 1242 | }); |
| 1243 | }); |