Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestWatch.test.ts

1,368 lines57.6 KB

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