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

pullRequestWatch.test.ts

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