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

pullRequestWatch.test.ts

255 lines10.8 KB

1import type {
2 PullRequestCheck,
3 PullRequestComment,
4 ThreadPullRequestWatch,
5} from "@t3tools/contracts";
6import { assert, describe, it } from "@effect/vitest";
7​
8import { PULL_REQUEST_WATCH_WAKE_LIMIT, evaluatePullRequestWatch } from "./pullRequestWatch.ts";
9​
10const STARTED = "2026-10-02T12:00:00.000Z";
11​
12const watch = (overrides: Partial<ThreadPullRequestWatch> = {}): ThreadPullRequestWatch => ({
13 startedAt: STARTED,
14 headSha: null,
15 failedChecks: [],
16 passed: false,
17 passedChecks: [],
18 remarksThrough: STARTED,
19 remarkIds: [],
20 conflicting: false,
21 wakes: 0,
22 ...overrides,
23});
24​
25const check = (name: string, status: PullRequestCheck["status"]): PullRequestCheck => ({
26 name,
27 status,
28 description: null,
29 url: `https://ci.example/${name}`,
30});
31​
32type Detail = Parameters<typeof evaluatePullRequestWatch>[1];
33​
34const detail = (overrides: Partial<Detail> = {}): Detail => ({
35 headSha: "aaaaaaaaaa",
36 checks: [check("lint", "success"), check("test", "pending")],
37 mergeability: "mergeable",
38 viewer: "agent-user",
39 author: { login: "agent-user", name: null, avatarUrl: null },
40 ...overrides,
41});
42​
43const remark = (
44 login: string,
45 createdAt: string,
46 body = "Please rename this.",
47): PullRequestComment => ({
48 id: `${login}-${createdAt}`,
49 kind: "review-comment",
50 author: { login, name: null, avatarUrl: null },
51 body,
52 createdAt,
53 url: `https://github.com/o/r/pull/1#${login}`,
54 path: "src/index.ts",
55 reviewState: null,
56});
57​
58const noRemarks: ReadonlyArray<PullRequestComment> = [];
59​
60describe("evaluatePullRequestWatch", () => {
61 it("reports each failure at once, even while another check never finishes", () => {
62 const bot = check("CodeRabbit", "pending");
63 const first = detail({ checks: [check("lint", "failure"), check("test", "pending"), bot] });
64 const lint = evaluatePullRequestWatch(watch(), first, noRemarks);
65 assert.deepEqual(lint.changes, [{ kind: "checks-failed", failed: [check("lint", "failure")] }]);
66 assert.deepEqual(evaluatePullRequestWatch(lint.next, first, noRemarks).changes, []);
67​
68 const second = detail({ checks: [check("lint", "failure"), check("test", "failure"), bot] });
69 const test = evaluatePullRequestWatch(lint.next, second, noRemarks);
70 assert.deepEqual(test.changes, [{ kind: "checks-failed", failed: [check("test", "failure")] }]);
71​
72 const rerun = evaluatePullRequestWatch(test.next, first, noRemarks);
73 assert.equal(evaluatePullRequestWatch(rerun.next, second, noRemarks).changes.length, 1);
74​
75 const pushed = detail({ ...second, headSha: "bbbbbbbbbb" });
76 assert.equal(evaluatePullRequestWatch(test.next, pushed, noRemarks).changes.length, 1);
77 });
78​
79 it("reports passed once the required checks pass, whatever the others do", () => {
80 const required = (name: string, status: PullRequestCheck["status"]) => ({
81 ...check(name, status),
82 required: true,
83 });
84 const green = detail({
85 checks: [required("test", "success"), required("lint", "success"), check("bot", "pending")],
86 });
87 const passed = evaluatePullRequestWatch(watch(), green, noRemarks);
88 assert.deepEqual(passed.changes, [{ kind: "checks-passed", count: 2, required: true }]);
89 assert.deepEqual(evaluatePullRequestWatch(passed.next, green, noRemarks).changes, []);
90​
91 const plain = detail({ checks: [check("test", "success"), check("bot", "pending")] });
92 assert.deepEqual(evaluatePullRequestWatch(watch(), plain, noRemarks).changes, []);
93 });
94​
95 it("reports passed again when a required check shows up already passed", () => {
96 const required = (name: string, status: PullRequestCheck["status"]) => ({
97 ...check(name, status),
98 required: true,
99 });
100 const tests = detail({
101 checks: [required("Tests", "success"), check("Smoke Tests", "pending")],
102 });
103 const first = evaluatePullRequestWatch(watch(), tests, noRemarks);
104 assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: true }]);
105​
106 const gated = detail({
107 checks: [
108 required("Tests", "success"),
109 check("Smoke Tests", "success"),
110 required("Smoke Tests Gate", "success"),
111 ],
112 });
113 const second = evaluatePullRequestWatch(first.next, gated, noRemarks);
114 assert.deepEqual(second.changes, [{ kind: "checks-passed", count: 2, required: true }]);
115 assert.deepEqual(evaluatePullRequestWatch(second.next, gated, noRemarks).changes, []);
116​
117 const pending = detail({
118 checks: [required("Tests", "success"), required("Smoke Tests Gate", "pending")],
119 });
120 const waiting = evaluatePullRequestWatch(first.next, pending, noRemarks);
121 assert.deepEqual(waiting.changes, []);
122 assert.deepEqual(evaluatePullRequestWatch(waiting.next, gated, noRemarks).changes, [
123 { kind: "checks-passed", count: 2, required: true },
124 ]);
125 });
126​
127 // regression: #16208 PR watches burned GitHub's rate limit and gave up
128 it("does not report passed again for a new passed check where none is required", () => {
129 const first = evaluatePullRequestWatch(
130 watch(),
131 detail({ checks: [check("test", "success")] }),
132 noRemarks,
133 );
134 assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: false }]);
135 const both = detail({ checks: [check("test", "success"), check("lint", "success")] });
136 assert.deepEqual(evaluatePullRequestWatch(first.next, both, noRemarks).changes, []);
137 });
138​
139 it("does not wake a watch saved before passed checks were recorded", () => {
140 const green = detail({ checks: [{ ...check("test", "success"), required: true }] });
141 const told = watch({ headSha: "aaaaaaaaaa", passed: true });
142 const saved = evaluatePullRequestWatch(told, green, noRemarks);
143 assert.deepEqual(saved.changes, []);
144 assert.deepEqual(saved.next.passedChecks, ["test"]);
145 });
146​
147 it("keeps remarks for a later pass when the conversation was not read whole", () => {
148 const comments = [remark("reviewer", "2026-10-02T12:06:00Z")];
149 const partial = evaluatePullRequestWatch(watch(), detail(), null);
150 assert.deepEqual(partial.changes, []);
151 assert.equal(
152 evaluatePullRequestWatch(partial.next, detail(), comments).changes[0]?.kind,
153 "remarks",
154 );
155 });
156​
157 it("reports a remark that shows up late with the same time as a reported one", () => {
158 const first = remark("reviewer", "2026-10-02T12:06:00Z");
159 const late = { ...remark("bot", "2026-10-02T12:06:00Z"), id: "late" };
160 const reported = evaluatePullRequestWatch(watch(), detail(), [first]);
161 const again = evaluatePullRequestWatch(reported.next, detail(), [first, late]);
162 assert.deepEqual(again.changes, [{ kind: "remarks", remarks: [late] }]);
163 assert.deepEqual(again.next.remarkIds, [first.id, "late"]);
164 });
165​
166 it("reports edits after the watermark once and counts them toward the wake limit", () => {
167 const old = remark("greptile[bot]", "2026-10-02T11:00:00Z");
168 const watching = watch({
169 headSha: "aaaaaaaaaa",
170 remarkIds: [old.id],
171 wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1,
172 });
173 assert.deepEqual(evaluatePullRequestWatch(watching, detail(), [old]).changes, []);
174 const edited = { ...old, editedAt: "2026-10-02T12:06:00Z" };
175 const report = evaluatePullRequestWatch(watching, detail(), [edited]);
176 assert.deepEqual(report.changes, [{ kind: "remarks", remarks: [edited] }]);
177 assert.equal(report.next.remarksThrough, edited.editedAt);
178 assert.deepEqual(report.next.remarkIds, [old.id]);
179 assert.isTrue(report.exhausted);
180 assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), [edited]).changes, []);
181 const late = { ...edited, id: "late" };
182 assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), [edited, late]).changes, [
183 { kind: "remarks", remarks: [late] },
184 ]);
185 });
186​
187 it("does not treat a failed check read as a rerun", () => {
188 const failed = detail({ checks: [check("lint", "failure")] });
189 const reported = evaluatePullRequestWatch(watch(), failed, noRemarks);
190 assert.equal(reported.changes.length, 1);
191 const unreadable = evaluatePullRequestWatch(reported.next, detail({ checks: [] }), noRemarks);
192 assert.deepEqual(evaluatePullRequestWatch(unreadable.next, failed, noRemarks).changes, []);
193 });
194​
195 it("wakes for the pull request's author when the agent is someone else", () => {
196 const contributor = detail({ author: { login: "contributor", name: null, avatarUrl: null } });
197 const reply = remark("contributor", "2026-10-02T12:06:00Z");
198 assert.deepEqual(evaluatePullRequestWatch(watch(), contributor, [reply]).changes, [
199 { kind: "remarks", remarks: [reply] },
200 ]);
201 const noViewer = detail({ viewer: undefined, author: contributor.author });
202 assert.deepEqual(evaluatePullRequestWatch(watch(), noViewer, [reply]).changes, []);
203 });
204​
205 it("reports remarks from others once and never the agent's own", () => {
206 const comments = [
207 remark("agent-user", "2026-10-02T12:05:00Z", "Fixed in the latest push."),
208 remark("macroscope-app[bot]", "2026-10-02T12:06:00Z"),
209 remark("reviewer", "2026-10-02T11:00:00Z", "Older than the watch."),
210 ];
211 const report = evaluatePullRequestWatch(watch(), detail(), comments);
212 assert.deepEqual(report.changes, [{ kind: "remarks", remarks: [comments[1]!] }]);
213 assert.equal(report.next.remarksThrough, "2026-10-02T12:06:00Z");
214 assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), comments).changes, []);
215 });
216​
217 it("reports a conflict once, until the branch is clean again", () => {
218 const conflicting = detail({ mergeability: "conflicting" });
219 const first = evaluatePullRequestWatch(watch(), conflicting, noRemarks);
220 assert.deepEqual(first.changes, [{ kind: "conflicting" }]);
221 const recomputing = evaluatePullRequestWatch(
222 first.next,
223 detail({ mergeability: "unknown" }),
224 noRemarks,
225 );
226 assert.deepEqual(
227 evaluatePullRequestWatch(recomputing.next, conflicting, noRemarks).changes,
228 [],
229 );
230 const clean = evaluatePullRequestWatch(first.next, detail(), noRemarks);
231 assert.deepEqual(evaluatePullRequestWatch(clean.next, conflicting, noRemarks).changes, [
232 { kind: "conflicting" },
233 ]);
234 });
235​
236 it("does not spend the comment wake limit on check results", () => {
237 const tired = watch({ headSha: "aaaaaaaaaa", wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1 });
238 const result = evaluatePullRequestWatch(
239 tired,
240 detail({ checks: [check("lint", "failure")] }),
241 noRemarks,
242 );
243 assert.isFalse(result.exhausted);
244 assert.equal(result.next.wakes, 0);
245 });
246​
247 it("stops after the wake limit unless the head moves", () => {
248 const comments = [remark("reviewer", "2026-10-02T12:10:00Z")];
249 const tired = watch({ headSha: "aaaaaaaaaa", wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1 });
250 assert.isTrue(evaluatePullRequestWatch(tired, detail(), comments).exhausted);
251 const pushed = evaluatePullRequestWatch(tired, detail({ headSha: "cccccccccc" }), comments);
252 assert.isFalse(pushed.exhausted);
253 assert.equal(pushed.next.wakes, 1);
254 });
255});