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

pullRequestWatch.test.ts

283 lines11.7 KB

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