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