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

pullRequestWatch.test.ts

438 lines19.5 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 check = (name: string, status: PullRequestCheck["status"]): PullRequestCheck => ({
35 name,
36 status,
37 description: null,
38 url: `https://ci.example/${name}`,
39});
40​
41type Detail = Parameters<typeof evaluatePullRequestWatch>[1];
42​
43const capabilities = (checksAlwaysReport: boolean): Detail["capabilities"] => ({
44 diff: true,
45 comment: true,
46 actions: [],
47 mergeMethods: [],
48 search: false,
49 review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
50 reviewers: { request: false, listCandidates: false },
51 checksAlwaysReport,
52});
53​
54const detail = (overrides: Partial<Detail> = {}): Detail => ({
55 capabilities: capabilities(true),
56 headSha: "aaaaaaaaaa",
57 checks: [check("lint", "success"), check("test", "pending")],
58 mergeability: "mergeable",
59 viewer: "agent-user",
60 author: { login: "agent-user", name: null, avatarUrl: null },
61 ...overrides,
62});
63​
64const remark = (
65 login: string,
66 createdAt: string,
67 body = "Please rename this.",
68): PullRequestComment => ({
69 id: `${login}-${createdAt}`,
70 kind: "review-comment",
71 author: { login, name: null, avatarUrl: null },
72 body,
73 createdAt,
74 url: `https://github.com/o/r/pull/1#${login}`,
75 path: "src/index.ts",
76 reviewState: null,
77});
78​
79const noRemarks: ReadonlyArray<PullRequestComment> = [];
80​
81describe("evaluatePullRequestWatch", () => {
82 it("reports each failure at once, even while another check never finishes", () => {
83 const bot = check("CodeRabbit", "pending");
84 const first = detail({ checks: [check("lint", "failure"), check("test", "pending"), bot] });
85 const lint = evaluatePullRequestWatch(watch(), first, noRemarks);
86 assert.deepEqual(lint.changes, [{ kind: "checks-failed", failed: [check("lint", "failure")] }]);
87 assert.deepEqual(evaluatePullRequestWatch(lint.next, first, noRemarks).changes, []);
88​
89 const second = detail({ checks: [check("lint", "failure"), check("test", "failure"), bot] });
90 const test = evaluatePullRequestWatch(lint.next, second, noRemarks);
91 assert.deepEqual(test.changes, [{ kind: "checks-failed", failed: [check("test", "failure")] }]);
92​
93 const rerun = evaluatePullRequestWatch(test.next, first, noRemarks);
94 assert.equal(evaluatePullRequestWatch(rerun.next, second, noRemarks).changes.length, 1);
95​
96 const pushed = detail({ ...second, headSha: "bbbbbbbbbb" });
97 assert.equal(evaluatePullRequestWatch(test.next, pushed, noRemarks).changes.length, 1);
98 });
99​
100 it("reports passed once the required checks pass, whatever the others do", () => {
101 const required = (name: string, status: PullRequestCheck["status"]) => ({
102 ...check(name, status),
103 required: true,
104 });
105 const green = detail({
106 checks: [required("test", "success"), required("lint", "success"), check("bot", "pending")],
107 });
108 const passed = evaluatePullRequestWatch(watch(), green, noRemarks);
109 assert.deepEqual(passed.changes, [{ kind: "checks-passed", count: 2, required: true }]);
110 assert.deepEqual(evaluatePullRequestWatch(passed.next, green, noRemarks).changes, []);
111​
112 const plain = detail({ checks: [check("test", "success"), check("bot", "pending")] });
113 assert.deepEqual(evaluatePullRequestWatch(watch(), plain, noRemarks).changes, []);
114 });
115​
116 it("reports passed again when a required check shows up already passed", () => {
117 const required = (name: string, status: PullRequestCheck["status"]) => ({
118 ...check(name, status),
119 required: true,
120 });
121 const tests = detail({
122 checks: [required("Tests", "success"), check("Smoke Tests", "pending")],
123 });
124 const first = evaluatePullRequestWatch(watch(), tests, noRemarks);
125 assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: true }]);
126​
127 const gated = detail({
128 checks: [
129 required("Tests", "success"),
130 check("Smoke Tests", "success"),
131 required("Smoke Tests Gate", "success"),
132 ],
133 });
134 const second = evaluatePullRequestWatch(first.next, gated, noRemarks);
135 assert.deepEqual(second.changes, [{ kind: "checks-passed", count: 2, required: true }]);
136 assert.deepEqual(evaluatePullRequestWatch(second.next, gated, noRemarks).changes, []);
137​
138 const pending = detail({
139 checks: [required("Tests", "success"), required("Smoke Tests Gate", "pending")],
140 });
141 const waiting = evaluatePullRequestWatch(first.next, pending, noRemarks);
142 assert.deepEqual(waiting.changes, []);
143 assert.deepEqual(evaluatePullRequestWatch(waiting.next, gated, noRemarks).changes, [
144 { kind: "checks-passed", count: 2, required: true },
145 ]);
146 });
147​
148 // regression: #23712 required checks woke the agent early and the rest finishing never did
149 it("wakes again when the other checks finish after the required ones passed", () => {
150 const required = (name: string, status: PullRequestCheck["status"]) => ({
151 ...check(name, status),
152 required: true,
153 });
154 const early = detail({
155 checks: [required("test", "success"), check("e2e", "pending")],
156 });
157 const first = evaluatePullRequestWatch(watch(), early, noRemarks);
158 assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: true }]);
159​
160 const done = detail({
161 checks: [required("test", "success"), check("e2e", "success")],
162 });
163 const final = evaluatePullRequestWatch(first.next, done, noRemarks);
164 assert.deepEqual(
165 final.changes.map((change) => change.kind),
166 ["checks-passed"],
167 );
168 assert.deepEqual(evaluatePullRequestWatch(final.next, done, noRemarks).changes, []);
169 // A later advisory job running and passing is not more news.
170 const botPending = detail({ checks: [...done.checks, check("bot", "pending")] });
171 const churn = evaluatePullRequestWatch(final.next, botPending, noRemarks);
172 assert.deepEqual(churn.changes, []);
173 const botDone = detail({ checks: [...done.checks, check("bot", "success")] });
174 assert.deepEqual(evaluatePullRequestWatch(churn.next, botDone, noRemarks).changes, []);
175​
176 // A pending check that turns required on the next read still finishes with one wake.
177 const promoted = detail({
178 checks: [required("test", "success"), required("e2e", "success")],
179 });
180 const gated = evaluatePullRequestWatch(first.next, promoted, noRemarks);
181 assert.deepEqual(gated.changes, [{ kind: "checks-passed", count: 2, required: true }]);
182 assert.deepEqual(evaluatePullRequestWatch(gated.next, promoted, noRemarks).changes, []);
183​
184 // Nothing else was pending when the required ones passed, so nothing more is owed.
185 const green = evaluatePullRequestWatch(watch(), done, noRemarks);
186 assert.deepEqual(green.changes, [{ kind: "checks-passed", count: 1, required: true }]);
187 assert.deepEqual(evaluatePullRequestWatch(green.next, botDone, noRemarks).changes, []);
188 });
189​
190 it("reports an owed non-required failure once after the required notice", () => {
191 const required = { ...check("test", "success"), required: true };
192 const early = detail({ checks: [required, check("e2e", "pending")] });
193 const first = evaluatePullRequestWatch(watch(), early, noRemarks);
194 assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: true }]);
195 const failed = detail({ checks: [required, check("e2e", "failure")] });
196 const failure = evaluatePullRequestWatch(first.next, failed, noRemarks);
197 assert.deepEqual(failure.changes, [
198 { kind: "checks-failed", failed: [check("e2e", "failure")] },
199 ]);
200 const repeated = evaluatePullRequestWatch(failure.next, failed, noRemarks);
201 assert.deepEqual(repeated.changes, []);
202 const recovered = detail({ checks: [required, check("e2e", "success")] });
203 const final = evaluatePullRequestWatch(repeated.next, recovered, noRemarks);
204 assert.deepEqual(final.changes, [{ kind: "checks-passed", count: 2, required: false }]);
205 assert.deepEqual(evaluatePullRequestWatch(final.next, recovered, noRemarks).changes, []);
206 });
207​
208 it("clears old-head owed checks when a new head is pushed", () => {
209 const required = { ...check("test", "success"), required: true };
210 const early = detail({ checks: [required, check("e2e", "pending")] });
211 const first = evaluatePullRequestWatch(watch(), early, noRemarks);
212 assert.deepEqual(first.next.owedChecks, ["e2e"]);
213 const unreadHead = detail({ headSha: "bbbbbbbbbb", checks: [] });
214 const unread = evaluatePullRequestWatch(first.next, unreadHead, noRemarks);
215 assert.deepEqual(unread.changes, []);
216 assert.deepEqual(unread.next.owedChecks, []);
217 const pushed = detail({ ...unreadHead, checks: [required] });
218 const newHead = evaluatePullRequestWatch(unread.next, pushed, noRemarks);
219 assert.deepEqual(newHead.changes, [{ kind: "checks-passed", count: 1, required: true }]);
220 assert.deepEqual(newHead.next.owedChecks, []);
221 const lateResult = detail({ ...pushed, checks: [required, check("e2e", "success")] });
222 const final = evaluatePullRequestWatch(newHead.next, lateResult, noRemarks);
223 assert.deepEqual(final.changes, []);
224 assert.deepEqual(evaluatePullRequestWatch(final.next, lateResult, noRemarks).changes, []);
225 });
226​
227 // regression: #16208 PR watches burned GitHub's rate limit and gave up
228 it("does not report passed again for a new passed check where none is required", () => {
229 const first = evaluatePullRequestWatch(
230 watch(),
231 detail({ checks: [check("test", "success")] }),
232 noRemarks,
233 );
234 assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: false }]);
235 const both = detail({ checks: [check("test", "success"), check("lint", "success")] });
236 assert.deepEqual(evaluatePullRequestWatch(first.next, both, noRemarks).changes, []);
237 });
238​
239 it("does not wake a watch saved before passed checks were recorded", () => {
240 const green = detail({ checks: [{ ...check("test", "success"), required: true }] });
241 const told = watch({ headSha: "aaaaaaaaaa", passed: true });
242 const saved = evaluatePullRequestWatch(told, green, noRemarks);
243 assert.deepEqual(saved.changes, []);
244 assert.deepEqual(saved.next.passedChecks, ["test"]);
245 });
246​
247 it("keeps remarks for a later pass when the conversation was not read whole", () => {
248 const comments = [remark("reviewer", "2026-10-02T12:06:00Z")];
249 const partial = evaluatePullRequestWatch(watch(), detail(), null);
250 assert.deepEqual(partial.changes, []);
251 assert.equal(
252 evaluatePullRequestWatch(partial.next, detail(), comments).changes[0]?.kind,
253 "remarks",
254 );
255 });
256​
257 it("reports a remark that shows up late with the same time as a reported one", () => {
258 const first = remark("reviewer", "2026-10-02T12:06:00Z");
259 const late = { ...remark("bot", "2026-10-02T12:06:00Z"), id: "late" };
260 const reported = evaluatePullRequestWatch(watch(), detail(), [first]);
261 const again = evaluatePullRequestWatch(reported.next, detail(), [first, late]);
262 assert.deepEqual(again.changes, [{ kind: "remarks", remarks: [late] }]);
263 assert.deepEqual(again.next.remarkIds, [first.id, "late"]);
264 });
265​
266 it("reports edits after the watermark once and counts them toward the wake limit", () => {
267 const old = remark("greptile[bot]", "2026-10-02T11:00:00Z");
268 const watching = watch({
269 headSha: "aaaaaaaaaa",
270 remarkIds: [old.id],
271 wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1,
272 });
273 assert.deepEqual(evaluatePullRequestWatch(watching, detail(), [old]).changes, []);
274 const edited = { ...old, editedAt: "2026-10-02T12:06:00Z" };
275 const report = evaluatePullRequestWatch(watching, detail(), [edited]);
276 assert.deepEqual(report.changes, [{ kind: "remarks", remarks: [edited] }]);
277 assert.equal(report.next.remarksThrough, edited.editedAt);
278 assert.deepEqual(report.next.remarkIds, [old.id]);
279 assert.isTrue(report.exhausted);
280 assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), [edited]).changes, []);
281 const late = { ...edited, id: "late" };
282 assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), [edited, late]).changes, [
283 { kind: "remarks", remarks: [late] },
284 ]);
285 });
286​
287 it("wakes once per edit of a remark seen at the watermark, never for the agent's own", () => {
288 const seen = remark("ci-bot", "2026-10-10T19:52:40.000Z");
289 const watching = watch({
290 headSha: "aaaaaaaaaa",
291 remarksThrough: seen.createdAt,
292 remarkIds: [seen.id],
293 wakes: 2,
294 });
295 assert.deepEqual(evaluatePullRequestWatch(watching, detail(), [seen]).changes, []);
296 const edited = { ...seen, editedAt: "2026-10-10T20:04:50.000Z" };
297 const first = evaluatePullRequestWatch(watching, detail(), [edited]);
298 assert.deepEqual(first.changes, [{ kind: "remarks", remarks: [edited] }]);
299 assert.strictEqual(first.next.remarksThrough, edited.editedAt);
300 assert.deepEqual(evaluatePullRequestWatch(first.next, detail(), [edited]).changes, []);
301 const again = { ...seen, editedAt: "2026-10-10T20:05:50.000Z" };
302 const second = evaluatePullRequestWatch(first.next, detail(), [again]);
303 assert.deepEqual(second.changes, [{ kind: "remarks", remarks: [again] }]);
304 assert.deepEqual(evaluatePullRequestWatch(second.next, detail(), [again]).changes, []);
305 const own = { ...edited, author: { login: "agent-user", name: null, avatarUrl: null } };
306 assert.deepEqual(evaluatePullRequestWatch(watching, detail(), [own]).changes, []);
307 });
308​
309 it("does not treat a failed check read as a rerun", () => {
310 const failed = detail({ checks: [check("lint", "failure")] });
311 const reported = evaluatePullRequestWatch(watch(), failed, noRemarks);
312 assert.equal(reported.changes.length, 1);
313 const unreadable = evaluatePullRequestWatch(reported.next, detail({ checks: [] }), noRemarks);
314 assert.deepEqual(evaluatePullRequestWatch(unreadable.next, failed, noRemarks).changes, []);
315 });
316​
317 it("wakes for the pull request's author when the agent is someone else", () => {
318 const contributor = detail({ author: { login: "contributor", name: null, avatarUrl: null } });
319 const reply = remark("contributor", "2026-10-02T12:06:00Z");
320 assert.deepEqual(evaluatePullRequestWatch(watch(), contributor, [reply]).changes, [
321 { kind: "remarks", remarks: [reply] },
322 ]);
323 const noViewer = detail({ viewer: undefined, author: contributor.author });
324 assert.deepEqual(evaluatePullRequestWatch(watch(), noViewer, [reply]).changes, []);
325 });
326​
327 it("reports remarks from others once and never the agent's own", () => {
328 const comments = [
329 remark("agent-user", "2026-10-02T12:05:00Z", "Fixed in the latest push."),
330 remark("macroscope-app[bot]", "2026-10-02T12:06:00Z"),
331 remark("reviewer", "2026-10-02T11:00:00Z", "Older than the watch."),
332 ];
333 const report = evaluatePullRequestWatch(watch(), detail(), comments);
334 assert.deepEqual(report.changes, [{ kind: "remarks", remarks: [comments[1]!] }]);
335 assert.equal(report.next.remarksThrough, "2026-10-02T12:06:00Z");
336 assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), comments).changes, []);
337 });
338​
339 it("cuts a long remark in the wake message without splitting an emoji", () => {
340 const body = `${"x".repeat(196)}🚀 and more`;
341 const report = evaluatePullRequestWatch(watch(), detail(), [
342 remark("reviewer", "2026-10-02T12:06:00Z", body),
343 ]);
344 const { text } = pullRequestWatchMessage({
345 number: 1,
346 url: "https://github.com/o/r/pull/1",
347 baseBranch: "main",
348 headSha: null,
349 report,
350 });
351 assert.isTrue(text.isWellFormed());
352 assert.include(text, `"${"x".repeat(196)}..."`);
353 });
354​
355 it("reports a conflict once, until the branch is clean again", () => {
356 const conflicting = detail({ mergeability: "conflicting" });
357 const first = evaluatePullRequestWatch(watch(), conflicting, noRemarks);
358 assert.deepEqual(first.changes, [{ kind: "conflicting" }]);
359 const { headSha: _unreadHead, ...headUnread } = conflicting;
360 const unread = evaluatePullRequestWatch(first.next, headUnread, noRemarks);
361 assert.deepEqual(unread.changes, []);
362 assert.deepEqual(evaluatePullRequestWatch(unread.next, conflicting, noRemarks).changes, []);
363 const recomputing = evaluatePullRequestWatch(
364 first.next,
365 detail({ mergeability: "unknown" }),
366 noRemarks,
367 );
368 assert.deepEqual(
369 evaluatePullRequestWatch(recomputing.next, conflicting, noRemarks).changes,
370 [],
371 );
372 const clean = evaluatePullRequestWatch(first.next, detail(), noRemarks);
373 assert.deepEqual(evaluatePullRequestWatch(clean.next, conflicting, noRemarks).changes, [
374 { kind: "conflicting" },
375 ]);
376 const pushed = { headSha: "bbbbbbbbbb" };
377 const recomputed = evaluatePullRequestWatch(
378 first.next,
379 detail({ ...pushed, mergeability: "unknown" }),
380 noRemarks,
381 );
382 assert.deepEqual(
383 evaluatePullRequestWatch(
384 recomputed.next,
385 detail({ ...pushed, mergeability: "conflicting" }),
386 noRemarks,
387 ).changes,
388 [{ kind: "conflicting" }],
389 );
390 });
391​
392 it("does not spend the comment wake limit on check results", () => {
393 const tired = watch({ headSha: "aaaaaaaaaa", wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1 });
394 const result = evaluatePullRequestWatch(
395 tired,
396 detail({ checks: [check("lint", "failure")] }),
397 noRemarks,
398 );
399 assert.isFalse(result.exhausted);
400 assert.equal(result.next.wakes, 0);
401 });
402​
403 it("stops after the wake limit unless the head moves", () => {
404 const comments = [remark("reviewer", "2026-10-02T12:10:00Z")];
405 const tired = watch({ headSha: "aaaaaaaaaa", wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1 });
406 assert.isTrue(evaluatePullRequestWatch(tired, detail(), comments).exhausted);
407 const pushed = evaluatePullRequestWatch(tired, detail({ headSha: "cccccccccc" }), comments);
408 assert.isFalse(pushed.exhausted);
409 assert.equal(pushed.next.wakes, 1);
410 });
411​
412 const anHourLater = (
413 first: ThreadPullRequestWatch,
414 read: Detail,
415 ): ReadonlyArray<ReadonlyArray<string>> => {
416 const kinds: Array<ReadonlyArray<string>> = [];
417 let current = first;
418 for (let minute = 0; minute <= 60; minute += 10) {
419 const { changes, next } = evaluate(current, read, noRemarks, minute * 60_000);
420 kinds.push(changes.map((change) => change.kind));
421 current = next;
422 }
423 return kinds;
424 };
425​
426 it("does not tell a watch saved before the no-checks clock that its seen head has none", () => {
427 // Such a watch may have seen this head's checks pending, which records nothing.
428 const legacy = watch({ headSha: "aaaaaaaaaa" });
429 assert.isTrue(anHourLater(legacy, detail({ checks: [] })).every((kinds) => kinds.length === 0));
430 const pushed = anHourLater(legacy, detail({ headSha: "bbbbbbbbbb", checks: [] }));
431 assert.deepEqual(pushed.flat(), ["no-checks"]);
432 });
433​
434 it("never reports missing checks on a host where an empty list says nothing", () => {
435 const silent = detail({ capabilities: capabilities(false), checks: [] });
436 assert.isTrue(anHourLater(watch(), silent).every((kinds) => kinds.length === 0));
437 });
438});