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

pullRequestWatch.test.ts

865 lines37.4 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 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([false, true])("baselines already-passed checks (required: %s)", (required) => {
85 const green = detail({ checks: [{ ...check("test", "success"), required }] });
86 const baseline = evaluatePullRequestWatch(watch({ awaitingBaseline: "all" }), green, noRemarks);
87 assert.deepEqual(baseline.changes, []);
88 assert.equal(baseline.next.headSha, green.headSha);
89 assert.isTrue(baseline.next.passed);
90 assert.deepEqual(baseline.next.passedChecks, ["test"]);
91 assert.isFalse(baseline.exhausted);
92 assert.deepEqual(evaluatePullRequestWatch(baseline.next, green, noRemarks).changes, []);
93​
94 const pending = evaluatePullRequestWatch(
95 baseline.next,
96 detail({ checks: [{ ...check("test", "pending"), required }] }),
97 noRemarks,
98 );
99 assert.deepEqual(pending.changes, []);
100 const failed = evaluatePullRequestWatch(
101 pending.next,
102 detail({ checks: [{ ...check("test", "failure"), required }] }),
103 noRemarks,
104 );
105 assert.equal(failed.changes[0]?.kind, "checks-failed");
106 const passed = evaluatePullRequestWatch(failed.next, green, noRemarks);
107 assert.deepEqual(passed.changes, [{ kind: "checks-passed", count: 1, required }]);
108 assert.deepEqual(
109 evaluatePullRequestWatch(passed.next, { ...green, headSha: "bbbbbbbbbb" }, noRemarks).changes,
110 [{ kind: "checks-passed", count: 1, required }],
111 );
112 });
113​
114 it("baselines existing failures and conflicts, then reports new ones", () => {
115 const broken = detail({
116 checks: [check("lint", "failure"), check("test", "pending")],
117 mergeability: "conflicting",
118 });
119 const baseline = evaluatePullRequestWatch(
120 watch({ awaitingBaseline: "all" }),
121 broken,
122 noRemarks,
123 );
124 assert.deepEqual(baseline.changes, []);
125 assert.deepEqual(baseline.next.failedChecks, ["lint"]);
126 assert.isFalse(baseline.next.passed);
127 assert.isTrue(baseline.next.conflicting);
128 assert.deepEqual(evaluatePullRequestWatch(baseline.next, broken, noRemarks).changes, []);
129​
130 const failed = evaluatePullRequestWatch(
131 baseline.next,
132 { ...broken, checks: [check("lint", "failure"), check("test", "failure")] },
133 noRemarks,
134 );
135 assert.deepEqual(failed.changes, [
136 { kind: "checks-failed", failed: [check("test", "failure")] },
137 ]);
138 const clean = evaluatePullRequestWatch(failed.next, detail(), noRemarks);
139 assert.deepEqual(clean.changes, []);
140 assert.deepEqual(evaluatePullRequestWatch(clean.next, broken, noRemarks).changes, [
141 { kind: "checks-failed", failed: [check("lint", "failure")] },
142 { kind: "conflicting" },
143 ]);
144 });
145​
146 it("baselines checks already awaiting approval", () => {
147 const gated = detail({ checks: [{ ...check("CI", "action-required"), url: run(1) }] });
148 const baseline = evaluatePullRequestWatch(watch({ awaitingBaseline: "all" }), gated, noRemarks);
149 assert.deepEqual(baseline.changes, []);
150 assert.deepEqual(evaluatePullRequestWatch(baseline.next, gated, noRemarks).changes, []);
151 });
152​
153 it("reports remarks after the watch started while baselining checks and conflicts", () => {
154 const green = detail({
155 checks: [check("test", "success")],
156 mergeability: "conflicting",
157 });
158 const comments = [
159 remark("reviewer", "2026-10-02T11:00:00Z"),
160 remark("reviewer", "2026-10-02T12:06:00Z"),
161 ];
162 const baseline = evaluatePullRequestWatch(watch({ awaitingBaseline: "all" }), green, comments);
163 assert.deepEqual(baseline.changes, [{ kind: "remarks", remarks: [comments[1]!] }]);
164 assert.isTrue(baseline.next.passed);
165 assert.isTrue(baseline.next.conflicting);
166 assert.equal(baseline.next.wakes, 1);
167 assert.deepEqual(evaluatePullRequestWatch(baseline.next, green, comments).changes, []);
168 });
169​
170 it("reports a failure once the head SHA appears after a baseline without one", () => {
171 const waiting = evaluatePullRequestWatch(
172 watch({ awaitingBaseline: "all" }),
173 detail({ headSha: undefined, checks: [check("test", "pending")] }),
174 noRemarks,
175 );
176 assert.deepEqual(waiting.changes, []);
177 const failed = evaluatePullRequestWatch(
178 waiting.next,
179 detail({ checks: [check("test", "failure")] }),
180 noRemarks,
181 );
182 assert.deepEqual(failed.changes, [
183 { kind: "checks-failed", failed: [check("test", "failure")] },
184 ]);
185 });
186​
187 it("keeps a baseline taken without a head SHA once the SHA appears", () => {
188 const green = detail({ checks: [check("test", "success")] });
189 const baseline = evaluatePullRequestWatch(
190 watch({ awaitingBaseline: "all" }),
191 { ...green, headSha: undefined },
192 noRemarks,
193 );
194 const known = evaluatePullRequestWatch(baseline.next, green, noRemarks);
195 assert.deepEqual(known.changes, []);
196 assert.equal(known.next.headSha, green.headSha);
197 const unknown = evaluatePullRequestWatch(
198 known.next,
199 { ...green, headSha: undefined },
200 noRemarks,
201 );
202 assert.equal(unknown.next.headSha, green.headSha);
203 assert.deepEqual(
204 evaluatePullRequestWatch(unknown.next, { ...green, headSha: "bbbbbbbbbb" }, noRemarks)
205 .changes,
206 [{ kind: "checks-passed", count: 1, required: false }],
207 );
208 });
209​
210 it.each(["success", "failure"] as const)(
211 "reports %s on checks that appear after a first read without any",
212 (status) => {
213 const empty = evaluatePullRequestWatch(
214 watch({ awaitingBaseline: "all" }),
215 detail({ checks: [] }),
216 noRemarks,
217 );
218 assert.deepEqual(empty.changes, []);
219 assert.isUndefined(empty.next.awaitingBaseline);
220 assert.deepEqual(
221 evaluatePullRequestWatch(empty.next, detail({ checks: [check("test", status)] }), noRemarks)
222 .changes,
223 status === "success"
224 ? [{ kind: "checks-passed", count: 1, required: false }]
225 : [{ kind: "checks-failed", failed: [check("test", "failure")] }],
226 );
227 },
228 );
229​
230 it("keeps the check baseline through a read whose approval lookup failed", () => {
231 const bot = check("bot", "success");
232 const unavailable = { ...check("Workflow approval status", "pending"), placeholder: true };
233 const partial = evaluatePullRequestWatch(
234 watch({ awaitingBaseline: "all" }),
235 detail({ checks: [bot, unavailable] }),
236 noRemarks,
237 );
238 assert.deepEqual(partial.changes, []);
239 assert.equal(partial.next.awaitingBaseline, "checks");
240 const approval = { ...check("CI", "action-required"), url: run(1) };
241 for (const checks of [[bot], [bot, approval]]) {
242 assert.deepEqual(
243 evaluatePullRequestWatch(partial.next, detail({ checks }), noRemarks).changes,
244 [],
245 );
246 }
247 });
248​
249 it("reports pending advisory checks finishing after a required-check baseline", () => {
250 const initial = detail({
251 checks: [{ ...check("test", "success"), required: true }, check("bot", "pending")],
252 });
253 const baseline = evaluatePullRequestWatch(
254 watch({ awaitingBaseline: "all" }),
255 initial,
256 noRemarks,
257 );
258 assert.deepEqual(baseline.changes, []);
259 const settled = evaluatePullRequestWatch(
260 baseline.next,
261 {
262 ...initial,
263 checks: [initial.checks[0]!, check("bot", "success")],
264 },
265 noRemarks,
266 );
267 assert.deepEqual(settled.changes, [{ kind: "checks-passed", count: 2, required: false }]);
268 assert.deepEqual(
269 evaluatePullRequestWatch(
270 settled.next,
271 {
272 ...initial,
273 checks: [initial.checks[0]!, check("bot", "success")],
274 },
275 noRemarks,
276 ).changes,
277 [],
278 );
279 });
280​
281 it("waits for workflow approval once, through reads that miss it, until the run finishes", () => {
282 const bot = check("bot", "success");
283 const approval = { ...check("CI", "action-required"), url: run(1) };
284 const gated = detail({ checks: [bot, approval] });
285 const first = evaluatePullRequestWatch(watch(), gated, noRemarks);
286 assert.deepEqual(first.changes, [{ kind: "checks-awaiting-approval", checks: [approval] }]);
287​
288 // GitHub's approval read can answer empty while the rest of the checks read fine.
289 const missing = evaluatePullRequestWatch(first.next, detail({ checks: [bot] }), noRemarks);
290 assert.deepEqual(missing.changes, []);
291 assert.isFalse(missing.next.passed);
292 assert.deepEqual(evaluatePullRequestWatch(missing.next, gated, noRemarks).changes, []);
293 const later = { ...check("Preview", "action-required"), url: run(2) };
294 assert.deepEqual(
295 evaluatePullRequestWatch(missing.next, detail({ checks: [bot, approval, later] }), noRemarks)
296 .changes,
297 [],
298 );
299 const unnamed = { ...check("Docs", "action-required"), url: null };
300 const named = detail({ checks: [bot, unnamed] });
301 const alone = evaluatePullRequestWatch(watch(), named, noRemarks);
302 assert.deepEqual(alone.changes, [{ kind: "checks-awaiting-approval", checks: [unnamed] }]);
303 const placeholder = { ...unnamed, name: "Workflow approval status", placeholder: true };
304 const unlisted = evaluatePullRequestWatch(
305 alone.next,
306 detail({ checks: [bot, placeholder] }),
307 noRemarks,
308 );
309 assert.deepEqual(unlisted.changes, []);
310 assert.isFalse(unlisted.next.passed);
311 assert.deepEqual(evaluatePullRequestWatch(unlisted.next, named, noRemarks).changes, []);
312 const dropped = evaluatePullRequestWatch(alone.next, detail({ checks: [bot] }), noRemarks);
313 assert.deepEqual(dropped.changes, []);
314 assert.isFalse(dropped.next.passed);
315 assert.deepEqual(dropped.next.awaitingApproval, [unnamed.name]);
316 assert.deepEqual(evaluatePullRequestWatch(dropped.next, named, noRemarks).changes, []);
317 const namedPending = evaluatePullRequestWatch(
318 dropped.next,
319 detail({
320 checks: [
321 { ...bot, required: true },
322 { ...unnamed, status: "pending" },
323 ],
324 }),
325 noRemarks,
326 );
327 assert.deepEqual(namedPending.changes, []);
328 assert.isFalse(namedPending.next.passed);
329 const namedDone = detail({ checks: [bot, { ...unnamed, status: "success" }] });
330 const namedFinished = evaluatePullRequestWatch(namedPending.next, namedDone, noRemarks);
331 assert.deepEqual(namedFinished.changes, [{ kind: "checks-passed", count: 2, required: false }]);
332 assert.deepEqual(namedFinished.next.awaitingApproval, []);
333 assert.deepEqual(
334 evaluatePullRequestWatch(namedFinished.next, namedDone, noRemarks).changes,
335 [],
336 );
337 const pushed = detail({ headSha: "bbbbbbbbbb", checks: [bot] });
338 assert.deepEqual(evaluatePullRequestWatch(missing.next, pushed, noRemarks).changes, [
339 { kind: "checks-passed", count: 1, required: false },
340 ]);
341​
342 // Approved, the run's jobs carry their own names under the run's URL.
343 const job = { ...check("lint", "success"), url: `${run(1)}/job/7` };
344 const pending = { ...check("test", "pending"), url: `${run(1)}/job/8` };
345 const required = { ...bot, required: true };
346 const running = evaluatePullRequestWatch(
347 missing.next,
348 detail({ checks: [required, job, pending] }),
349 noRemarks,
350 );
351 assert.deepEqual(running.changes, []);
352 assert.isFalse(running.next.passed);
353 assert.deepEqual(running.next.awaitingApproval, [run(1)]);
354 const overlap = evaluatePullRequestWatch(
355 running.next,
356 detail({ checks: [required, job, { ...pending, url: `${run(2)}/job/9` }] }),
357 noRemarks,
358 );
359 assert.deepEqual(overlap.changes, []);
360 assert.isFalse(overlap.next.passed);
361 assert.deepEqual(overlap.next.awaitingApproval, [run(1)]);
362 const unlinked = evaluatePullRequestWatch(
363 overlap.next,
364 detail({ checks: [required, job, { ...pending, url: null }] }),
365 noRemarks,
366 );
367 assert.deepEqual(unlinked.changes, []);
368 assert.isFalse(unlinked.next.passed);
369 assert.deepEqual(unlinked.next.awaitingApproval, [run(1)]);
370 const advisory = evaluatePullRequestWatch(
371 unlinked.next,
372 detail({ checks: [required, job, { ...pending, url: "https://ci.example/test" }] }),
373 noRemarks,
374 );
375 assert.deepEqual(advisory.changes, [{ kind: "checks-passed", count: 1, required: true }]);
376 assert.deepEqual(advisory.next.awaitingApproval, []);
377 const completed = detail({ checks: [required, job, { ...pending, status: "success" }] });
378 const finished = evaluatePullRequestWatch(unlinked.next, completed, noRemarks);
379 assert.deepEqual(finished.changes, [{ kind: "checks-passed", count: 1, required: true }]);
380 assert.deepEqual(finished.next.awaitingApproval, []);
381 assert.deepEqual(evaluatePullRequestWatch(finished.next, completed, noRemarks).changes, []);
382​
383 const done = evaluatePullRequestWatch(missing.next, detail({ checks: [bot, job] }), noRemarks);
384 assert.deepEqual(done.changes, [{ kind: "checks-passed", count: 2, required: false }]);
385 });
386​
387 it("does not pass or fail while the host cannot tell whether workflows await approval", () => {
388 const bot = check("bot", "success");
389 const unknown = {
390 ...check("Workflow approval status", "action-required"),
391 url: null,
392 placeholder: true,
393 };
394 const unread = evaluatePullRequestWatch(watch(), detail({ checks: [bot, unknown] }), noRemarks);
395 assert.deepEqual(unread.changes, []);
396 assert.isFalse(unread.next.passed);
397 const read = evaluatePullRequestWatch(unread.next, detail({ checks: [bot] }), noRemarks);
398 assert.deepEqual(read.changes, [{ kind: "checks-passed", count: 1, required: false }]);
399 const blocked = evaluatePullRequestWatch(
400 read.next,
401 detail({ checks: [{ ...bot, status: "pending" }, unknown] }),
402 noRemarks,
403 );
404 assert.isFalse(blocked.next.passed);
405 assert.deepEqual(
406 evaluatePullRequestWatch(blocked.next, detail({ checks: [bot] }), noRemarks).changes,
407 [{ kind: "checks-passed", count: 1, required: false }],
408 );
409 });
410​
411 it("reports each failure at once, even while another check never finishes", () => {
412 const bot = check("CodeRabbit", "pending");
413 const first = detail({ checks: [check("lint", "failure"), check("test", "pending"), bot] });
414 const lint = evaluatePullRequestWatch(watch(), first, noRemarks);
415 assert.deepEqual(lint.changes, [{ kind: "checks-failed", failed: [check("lint", "failure")] }]);
416 assert.deepEqual(evaluatePullRequestWatch(lint.next, first, noRemarks).changes, []);
417​
418 const second = detail({ checks: [check("lint", "failure"), check("test", "failure"), bot] });
419 const test = evaluatePullRequestWatch(lint.next, second, noRemarks);
420 assert.deepEqual(test.changes, [{ kind: "checks-failed", failed: [check("test", "failure")] }]);
421​
422 const rerun = evaluatePullRequestWatch(test.next, first, noRemarks);
423 assert.equal(evaluatePullRequestWatch(rerun.next, second, noRemarks).changes.length, 1);
424​
425 const pushed = detail({ ...second, headSha: "bbbbbbbbbb" });
426 assert.equal(evaluatePullRequestWatch(test.next, pushed, noRemarks).changes.length, 1);
427​
428 const approval = { ...check("CI", "action-required"), url: run(1) };
429 const gated = detail({ checks: [approval, check("lint", "failure")] });
430 assert.deepEqual(evaluatePullRequestWatch(watch(), gated, noRemarks).changes, [
431 { kind: "checks-failed", failed: [check("lint", "failure")] },
432 { kind: "checks-awaiting-approval", checks: [approval] },
433 ]);
434 });
435​
436 it("reports passed once the required checks pass, whatever the others do", () => {
437 const required = (name: string, status: PullRequestCheck["status"]) => ({
438 ...check(name, status),
439 required: true,
440 });
441 const green = detail({
442 checks: [required("test", "success"), required("lint", "success"), check("bot", "pending")],
443 });
444 const passed = evaluatePullRequestWatch(watch(), green, noRemarks);
445 assert.deepEqual(passed.changes, [{ kind: "checks-passed", count: 2, required: true }]);
446 assert.deepEqual(evaluatePullRequestWatch(passed.next, green, noRemarks).changes, []);
447​
448 const plain = detail({ checks: [check("test", "success"), check("bot", "pending")] });
449 assert.deepEqual(evaluatePullRequestWatch(watch(), plain, noRemarks).changes, []);
450 });
451​
452 it("reports passed again when a required check shows up already passed", () => {
453 const required = (name: string, status: PullRequestCheck["status"]) => ({
454 ...check(name, status),
455 required: true,
456 });
457 const tests = detail({
458 checks: [required("Tests", "success"), check("Smoke Tests", "pending")],
459 });
460 const first = evaluatePullRequestWatch(watch(), tests, noRemarks);
461 assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: true }]);
462​
463 const gated = detail({
464 checks: [
465 required("Tests", "success"),
466 check("Smoke Tests", "success"),
467 required("Smoke Tests Gate", "success"),
468 ],
469 });
470 const second = evaluatePullRequestWatch(first.next, gated, noRemarks);
471 assert.deepEqual(second.changes, [{ kind: "checks-passed", count: 2, required: true }]);
472 assert.deepEqual(evaluatePullRequestWatch(second.next, gated, noRemarks).changes, []);
473​
474 const pending = detail({
475 checks: [required("Tests", "success"), required("Smoke Tests Gate", "pending")],
476 });
477 const waiting = evaluatePullRequestWatch(first.next, pending, noRemarks);
478 assert.deepEqual(waiting.changes, []);
479 assert.deepEqual(evaluatePullRequestWatch(waiting.next, gated, noRemarks).changes, [
480 { kind: "checks-passed", count: 2, required: true },
481 ]);
482 });
483​
484 // regression: #23712 required checks woke the agent early and the rest finishing never did
485 it("wakes again when the other checks finish after the required ones passed", () => {
486 const required = (name: string, status: PullRequestCheck["status"]) => ({
487 ...check(name, status),
488 required: true,
489 });
490 const early = detail({
491 checks: [required("test", "success"), check("e2e", "pending")],
492 });
493 const first = evaluatePullRequestWatch(watch(), early, noRemarks);
494 assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: true }]);
495​
496 const done = detail({
497 checks: [required("test", "success"), check("e2e", "success")],
498 });
499 const final = evaluatePullRequestWatch(first.next, done, noRemarks);
500 assert.deepEqual(
501 final.changes.map((change) => change.kind),
502 ["checks-passed"],
503 );
504 assert.deepEqual(evaluatePullRequestWatch(final.next, done, noRemarks).changes, []);
505 // A later advisory job running and passing is not more news.
506 const botPending = detail({ checks: [...done.checks, check("bot", "pending")] });
507 const churn = evaluatePullRequestWatch(final.next, botPending, noRemarks);
508 assert.deepEqual(churn.changes, []);
509 const botDone = detail({ checks: [...done.checks, check("bot", "success")] });
510 assert.deepEqual(evaluatePullRequestWatch(churn.next, botDone, noRemarks).changes, []);
511​
512 // A pending check that turns required on the next read still finishes with one wake.
513 const promoted = detail({
514 checks: [required("test", "success"), required("e2e", "success")],
515 });
516 const gated = evaluatePullRequestWatch(first.next, promoted, noRemarks);
517 assert.deepEqual(gated.changes, [{ kind: "checks-passed", count: 2, required: true }]);
518 assert.deepEqual(evaluatePullRequestWatch(gated.next, promoted, noRemarks).changes, []);
519​
520 // Nothing else was pending when the required ones passed, so nothing more is owed.
521 const green = evaluatePullRequestWatch(watch(), done, noRemarks);
522 assert.deepEqual(green.changes, [{ kind: "checks-passed", count: 1, required: true }]);
523 assert.deepEqual(evaluatePullRequestWatch(green.next, botDone, noRemarks).changes, []);
524 });
525​
526 it("reports an owed non-required failure once after the required notice", () => {
527 const required = { ...check("test", "success"), required: true };
528 const early = detail({ checks: [required, check("e2e", "pending")] });
529 const first = evaluatePullRequestWatch(watch(), early, noRemarks);
530 assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: true }]);
531 const failed = detail({ checks: [required, check("e2e", "failure")] });
532 const failure = evaluatePullRequestWatch(first.next, failed, noRemarks);
533 assert.deepEqual(failure.changes, [
534 { kind: "checks-failed", failed: [check("e2e", "failure")] },
535 ]);
536 const repeated = evaluatePullRequestWatch(failure.next, failed, noRemarks);
537 assert.deepEqual(repeated.changes, []);
538 const recovered = detail({ checks: [required, check("e2e", "success")] });
539 const final = evaluatePullRequestWatch(repeated.next, recovered, noRemarks);
540 assert.deepEqual(final.changes, [{ kind: "checks-passed", count: 2, required: false }]);
541 assert.deepEqual(evaluatePullRequestWatch(final.next, recovered, noRemarks).changes, []);
542 });
543​
544 it("clears old-head owed checks when a new head is pushed", () => {
545 const required = { ...check("test", "success"), required: true };
546 const early = detail({ checks: [required, check("e2e", "pending")] });
547 const first = evaluatePullRequestWatch(watch(), early, noRemarks);
548 assert.deepEqual(first.next.owedChecks, ["e2e"]);
549 const unreadHead = detail({ headSha: "bbbbbbbbbb", checks: [] });
550 const unread = evaluatePullRequestWatch(first.next, unreadHead, noRemarks);
551 assert.deepEqual(unread.changes, []);
552 assert.deepEqual(unread.next.owedChecks, []);
553 const pushed = detail({ ...unreadHead, checks: [required] });
554 const newHead = evaluatePullRequestWatch(unread.next, pushed, noRemarks);
555 assert.deepEqual(newHead.changes, [{ kind: "checks-passed", count: 1, required: true }]);
556 assert.deepEqual(newHead.next.owedChecks, []);
557 const lateResult = detail({ ...pushed, checks: [required, check("e2e", "success")] });
558 const final = evaluatePullRequestWatch(newHead.next, lateResult, noRemarks);
559 assert.deepEqual(final.changes, []);
560 assert.deepEqual(evaluatePullRequestWatch(final.next, lateResult, noRemarks).changes, []);
561 });
562​
563 // regression: #16208 PR watches burned GitHub's rate limit and gave up
564 it("does not report passed again for a new passed check where none is required", () => {
565 const first = evaluatePullRequestWatch(
566 watch(),
567 detail({ checks: [check("test", "success")] }),
568 noRemarks,
569 );
570 assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: false }]);
571 const both = detail({ checks: [check("test", "success"), check("lint", "success")] });
572 assert.deepEqual(evaluatePullRequestWatch(first.next, both, noRemarks).changes, []);
573 });
574​
575 it("does not wake a watch saved before passed checks were recorded", () => {
576 const green = detail({ checks: [{ ...check("test", "success"), required: true }] });
577 const told = watch({ headSha: "aaaaaaaaaa", passed: true });
578 const saved = evaluatePullRequestWatch(told, green, noRemarks);
579 assert.deepEqual(saved.changes, []);
580 assert.deepEqual(saved.next.passedChecks, ["test"]);
581 });
582​
583 it("keeps remarks for a later pass when the conversation was not read whole", () => {
584 const comments = [remark("reviewer", "2026-10-02T12:06:00Z")];
585 const partial = evaluatePullRequestWatch(watch(), detail(), null);
586 assert.deepEqual(partial.changes, []);
587 assert.equal(
588 evaluatePullRequestWatch(partial.next, detail(), comments).changes[0]?.kind,
589 "remarks",
590 );
591 });
592​
593 it("reports a remark that shows up late with the same time as a reported one", () => {
594 const first = remark("reviewer", "2026-10-02T12:06:00Z");
595 const late = { ...remark("bot", "2026-10-02T12:06:00Z"), id: "late" };
596 const reported = evaluatePullRequestWatch(watch(), detail(), [first]);
597 const again = evaluatePullRequestWatch(reported.next, detail(), [first, late]);
598 assert.deepEqual(again.changes, [{ kind: "remarks", remarks: [late] }]);
599 assert.deepEqual(again.next.remarkIds, [first.id, "late"]);
600 });
601​
602 it("reports edits after the watermark once and counts them toward the wake limit", () => {
603 const old = remark("greptile[bot]", "2026-10-02T11:00:00Z");
604 const watching = watch({
605 headSha: "aaaaaaaaaa",
606 remarkIds: [old.id],
607 wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1,
608 });
609 assert.deepEqual(evaluatePullRequestWatch(watching, detail(), [old]).changes, []);
610 const edited = { ...old, editedAt: "2026-10-02T12:06:00Z" };
611 const report = evaluatePullRequestWatch(watching, detail(), [edited]);
612 assert.deepEqual(report.changes, [{ kind: "remarks", remarks: [edited] }]);
613 assert.equal(report.next.remarksThrough, edited.editedAt);
614 assert.deepEqual(report.next.remarkIds, [old.id]);
615 assert.isTrue(report.exhausted);
616 assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), [edited]).changes, []);
617 const late = { ...edited, id: "late" };
618 assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), [edited, late]).changes, [
619 { kind: "remarks", remarks: [late] },
620 ]);
621 });
622​
623 it("wakes once per edit of a remark seen at the watermark, never for the agent's own", () => {
624 const seen = remark("ci-bot", "2026-10-10T19:52:40.000Z");
625 const watching = watch({
626 headSha: "aaaaaaaaaa",
627 remarksThrough: seen.createdAt,
628 remarkIds: [seen.id],
629 wakes: 2,
630 });
631 assert.deepEqual(evaluatePullRequestWatch(watching, detail(), [seen]).changes, []);
632 const edited = { ...seen, editedAt: "2026-10-10T20:04:50.000Z" };
633 const first = evaluatePullRequestWatch(watching, detail(), [edited]);
634 assert.deepEqual(first.changes, [{ kind: "remarks", remarks: [edited] }]);
635 assert.strictEqual(first.next.remarksThrough, edited.editedAt);
636 assert.deepEqual(evaluatePullRequestWatch(first.next, detail(), [edited]).changes, []);
637 const again = { ...seen, editedAt: "2026-10-10T20:05:50.000Z" };
638 const second = evaluatePullRequestWatch(first.next, detail(), [again]);
639 assert.deepEqual(second.changes, [{ kind: "remarks", remarks: [again] }]);
640 assert.deepEqual(evaluatePullRequestWatch(second.next, detail(), [again]).changes, []);
641 const own = { ...edited, author: { login: "agent-user", name: null, avatarUrl: null } };
642 assert.deepEqual(evaluatePullRequestWatch(watching, detail(), [own]).changes, []);
643 });
644​
645 it("does not treat a failed check read as a rerun", () => {
646 const failed = detail({ checks: [check("lint", "failure")] });
647 const reported = evaluatePullRequestWatch(watch(), failed, noRemarks);
648 assert.equal(reported.changes.length, 1);
649 const unreadable = evaluatePullRequestWatch(reported.next, detail({ checks: [] }), noRemarks);
650 assert.deepEqual(evaluatePullRequestWatch(unreadable.next, failed, noRemarks).changes, []);
651 });
652​
653 it("wakes for the pull request's author when the agent is someone else", () => {
654 const contributor = detail({ author: { login: "contributor", name: null, avatarUrl: null } });
655 const reply = remark("contributor", "2026-10-02T12:06:00Z");
656 assert.deepEqual(evaluatePullRequestWatch(watch(), contributor, [reply]).changes, [
657 { kind: "remarks", remarks: [reply] },
658 ]);
659 const noViewer = detail({ viewer: undefined, author: contributor.author });
660 assert.deepEqual(evaluatePullRequestWatch(watch(), noViewer, [reply]).changes, []);
661 });
662​
663 it("reports remarks from others once and never the agent's own", () => {
664 const comments = [
665 remark("agent-user", "2026-10-02T12:05:00Z", "Fixed in the latest push."),
666 remark("macroscope-app[bot]", "2026-10-02T12:06:00Z"),
667 remark("reviewer", "2026-10-02T11:00:00Z", "Older than the watch."),
668 ];
669 const report = evaluatePullRequestWatch(watch(), detail(), comments);
670 assert.deepEqual(report.changes, [{ kind: "remarks", remarks: [comments[1]!] }]);
671 assert.equal(report.next.remarksThrough, "2026-10-02T12:06:00Z");
672 assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), comments).changes, []);
673 });
674​
675 it("cuts a long remark in the wake message without splitting an emoji", () => {
676 const body = `${"x".repeat(196)}🚀 and more`;
677 const report = evaluatePullRequestWatch(watch(), detail(), [
678 remark("reviewer", "2026-10-02T12:06:00Z", body),
679 ]);
680 const { text } = pullRequestWatchMessage({
681 number: 1,
682 url: "https://github.com/o/r/pull/1",
683 baseBranch: "main",
684 headSha: null,
685 report,
686 });
687 assert.isTrue(text.isWellFormed());
688 assert.include(text, `"${"x".repeat(196)}..."`);
689 });
690​
691 it("reports a conflict once, until the branch is clean again", () => {
692 const conflicting = detail({ mergeability: "conflicting" });
693 const first = evaluatePullRequestWatch(watch(), conflicting, noRemarks);
694 assert.deepEqual(first.changes, [{ kind: "conflicting" }]);
695 const { headSha: _unreadHead, ...headUnread } = conflicting;
696 const unread = evaluatePullRequestWatch(first.next, headUnread, noRemarks);
697 assert.deepEqual(unread.changes, []);
698 assert.deepEqual(evaluatePullRequestWatch(unread.next, conflicting, noRemarks).changes, []);
699 const recomputing = evaluatePullRequestWatch(
700 first.next,
701 detail({ mergeability: "unknown" }),
702 noRemarks,
703 );
704 assert.deepEqual(
705 evaluatePullRequestWatch(recomputing.next, conflicting, noRemarks).changes,
706 [],
707 );
708 const clean = evaluatePullRequestWatch(first.next, detail(), noRemarks);
709 assert.deepEqual(evaluatePullRequestWatch(clean.next, conflicting, noRemarks).changes, [
710 { kind: "conflicting" },
711 ]);
712 const pushed = { headSha: "bbbbbbbbbb" };
713 const recomputed = evaluatePullRequestWatch(
714 first.next,
715 detail({ ...pushed, mergeability: "unknown" }),
716 noRemarks,
717 );
718 assert.deepEqual(
719 evaluatePullRequestWatch(
720 recomputed.next,
721 detail({ ...pushed, mergeability: "conflicting" }),
722 noRemarks,
723 ).changes,
724 [{ kind: "conflicting" }],
725 );
726 });
727​
728 it("reports each merge queue removal once while the head checks stay green", () => {
729 const green = detail({ checks: [check("test", "success")] });
730 const watching = evaluatePullRequestWatch(watch(), green, noRemarks).next;
731 const removed = (id: string, removedAt: string) =>
732 detail({ ...green, mergeQueueRemoval: { id, removedAt, reason: "Merge conflict" } });
733​
734 const dropped = removed("first", "2026-10-02T12:10:00Z");
735 const first = evaluatePullRequestWatch(watching, dropped, noRemarks);
736 assert.deepEqual(first.changes, [{ kind: "merge-queue-removed", reason: "Merge conflict" }]);
737 const wake = pullRequestWatchMessage({
738 number: 1,
739 url: "https://github.com/o/r/pull/1",
740 baseBranch: "main",
741 headSha: first.next.headSha,
742 report: first,
743 });
744 assert.deepEqual(wake.notification, {
745 source: { kind: "monitor" },
746 outcome: "failed",
747 summary: "#1: removed from merge queue",
748 });
749 assert.deepEqual(evaluatePullRequestWatch(first.next, dropped, noRemarks).changes, []);
750 // A read without queue news keeps the removal already told.
751 const quiet = evaluatePullRequestWatch(first.next, green, noRemarks);
752 assert.deepEqual(evaluatePullRequestWatch(quiet.next, dropped, noRemarks).changes, []);
753​
754 const again = removed("second", "2026-10-02T12:20:00Z");
755 assert.equal(evaluatePullRequestWatch(first.next, again, noRemarks).changes.length, 1);
756 const before = removed("before", "2026-10-02T11:00:00Z");
757 const baseline = evaluatePullRequestWatch(
758 watch({ headSha: watching.headSha, passed: true, passedChecks: watching.passedChecks }),
759 before,
760 noRemarks,
761 );
762 assert.deepEqual(baseline.changes, []);
763 for (const prior of [watching, baseline.next]) {
764 const skewed = evaluatePullRequestWatch(
765 prior,
766 removed("new", "2026-10-02T11:59:58Z"),
767 noRemarks,
768 );
769 assert.deepEqual(skewed.changes, [{ kind: "merge-queue-removed", reason: "Merge conflict" }]);
770 assert.deepEqual(
771 evaluatePullRequestWatch(skewed.next, removed("new", "2026-10-02T11:59:58Z"), noRemarks)
772 .changes,
773 [],
774 );
775 }
776 });
777​
778 it("reports a merge queue removal stamped in the second the watch started", () => {
779 const watching = watch({ startedAt: "2026-10-02T12:00:00.500Z" });
780 const removed = (removedAt: string) =>
781 detail({ mergeQueueRemoval: { id: "first", removedAt, reason: "CI failed" } });
782 assert.deepEqual(
783 evaluatePullRequestWatch(watching, removed("2026-10-02T12:00:00Z"), noRemarks).changes,
784 [{ kind: "merge-queue-removed", reason: "CI failed" }],
785 );
786 assert.deepEqual(
787 evaluatePullRequestWatch(watching, removed("2026-10-02T11:59:59Z"), noRemarks).changes,
788 [],
789 );
790 });
791​
792 it("stays quiet when the queue lets go of a pull request by merging it", () => {
793 const watching = evaluatePullRequestWatch(watch(), detail(), noRemarks).next;
794 const merging = detail({
795 mergeQueueRemoval: { id: "landed", removedAt: "2026-10-02T12:10:00Z", reason: "merged" },
796 });
797 const read = evaluatePullRequestWatch(watching, merging, noRemarks);
798 assert.deepEqual(read.changes, []);
799 assert.equal(read.next.mergeQueueRemovalId, "landed");
800 });
801​
802 it("does not spend the comment wake limit on check results", () => {
803 const tired = watch({ headSha: "aaaaaaaaaa", wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1 });
804 const result = evaluatePullRequestWatch(
805 tired,
806 detail({ checks: [check("lint", "failure")] }),
807 noRemarks,
808 );
809 assert.isFalse(result.exhausted);
810 assert.equal(result.next.wakes, 0);
811 });
812​
813 it("stops after the wake limit unless the head moves", () => {
814 const comments = [remark("reviewer", "2026-10-02T12:10:00Z")];
815 const tired = watch({ headSha: "aaaaaaaaaa", wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1 });
816 assert.isTrue(evaluatePullRequestWatch(tired, detail(), comments).exhausted);
817 const pushed = evaluatePullRequestWatch(tired, detail({ headSha: "cccccccccc" }), comments);
818 assert.isFalse(pushed.exhausted);
819 assert.equal(pushed.next.wakes, 1);
820 });
821​
822 const anHourLater = (
823 first: ThreadPullRequestWatch,
824 read: Detail,
825 ): ReadonlyArray<ReadonlyArray<string>> => {
826 const kinds: Array<ReadonlyArray<string>> = [];
827 let current = first;
828 for (let minute = 0; minute <= 60; minute += 10) {
829 const { changes, next } = evaluate(current, read, noRemarks, minute * 60_000);
830 kinds.push(changes.map((change) => change.kind));
831 current = next;
832 }
833 return kinds;
834 };
835​
836 it("does not tell a watch saved before the no-checks clock that its seen head has none", () => {
837 // Such a watch may have seen this head's checks pending, which records nothing.
838 const legacy = watch({ headSha: "aaaaaaaaaa" });
839 assert.isTrue(anHourLater(legacy, detail({ checks: [] })).every((kinds) => kinds.length === 0));
840 const pushed = anHourLater(legacy, detail({ headSha: "bbbbbbbbbb", checks: [] }));
841 assert.deepEqual(pushed.flat(), ["no-checks"]);
842 const unknown = detail({
843 checks: [{ ...check("Workflow approval status", "action-required"), placeholder: true }],
844 });
845 const fresh = watch({ awaitingBaseline: "all" });
846 assert.deepEqual(anHourLater(fresh, unknown).flat(), []);
847 const unread = evaluate(fresh, unknown, noRemarks, 0);
848 assert.deepEqual(
849 evaluate(unread.next, detail({ checks: [] }), noRemarks, 60 * 60_000).changes,
850 [{ kind: "no-checks", minutes: 60 }],
851 );
852 });
853​
854 it("starts the no-checks clock while a fresh watch awaits its baseline", () => {
855 assert.deepEqual(
856 anHourLater(watch({ awaitingBaseline: "all" }), detail({ checks: [] })).flat(),
857 ["no-checks"],
858 );
859 });
860​
861 it("never reports missing checks on a host where an empty list says nothing", () => {
862 const silent = detail({ capabilities: capabilities(false), checks: [] });
863 assert.isTrue(anHourLater(watch(), silent).every((kinds) => kinds.length === 0));
864 });
865});