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

pullRequestWatch.test.ts

374 lines16.8 KB

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