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