import type { PullRequestCheck, PullRequestComment, ThreadPullRequestWatch, } from "@t3tools/contracts"; import { assert, describe, it } from "@effect/vitest"; import { PULL_REQUEST_WATCH_WAKE_LIMIT, evaluatePullRequestWatch, pullRequestWatchMessage, } from "./pullRequestWatch.ts"; const STARTED = "2026-10-02T12:00:00.000Z"; const watch = (overrides: Partial = {}): ThreadPullRequestWatch => ({ startedAt: STARTED, headSha: null, failedChecks: [], passed: false, passedChecks: [], remarksThrough: STARTED, remarkIds: [], conflicting: false, wakes: 0, ...overrides, }); const check = (name: string, status: PullRequestCheck["status"]): PullRequestCheck => ({ name, status, description: null, url: `https://ci.example/${name}`, }); type Detail = Parameters[1]; const detail = (overrides: Partial = {}): Detail => ({ headSha: "aaaaaaaaaa", checks: [check("lint", "success"), check("test", "pending")], mergeability: "mergeable", viewer: "agent-user", author: { login: "agent-user", name: null, avatarUrl: null }, ...overrides, }); const remark = ( login: string, createdAt: string, body = "Please rename this.", ): PullRequestComment => ({ id: `${login}-${createdAt}`, kind: "review-comment", author: { login, name: null, avatarUrl: null }, body, createdAt, url: `https://github.com/o/r/pull/1#${login}`, path: "src/index.ts", reviewState: null, }); const noRemarks: ReadonlyArray = []; describe("evaluatePullRequestWatch", () => { it("reports each failure at once, even while another check never finishes", () => { const bot = check("CodeRabbit", "pending"); const first = detail({ checks: [check("lint", "failure"), check("test", "pending"), bot] }); const lint = evaluatePullRequestWatch(watch(), first, noRemarks); assert.deepEqual(lint.changes, [{ kind: "checks-failed", failed: [check("lint", "failure")] }]); assert.deepEqual(evaluatePullRequestWatch(lint.next, first, noRemarks).changes, []); // A different job failing later is news of its own. const second = detail({ checks: [check("lint", "failure"), check("test", "failure"), bot] }); const test = evaluatePullRequestWatch(lint.next, second, noRemarks); assert.deepEqual(test.changes, [{ kind: "checks-failed", failed: [check("test", "failure")] }]); // A rerun leaves the list while it runs, so failing again is reported again. const rerun = evaluatePullRequestWatch(test.next, first, noRemarks); assert.equal(evaluatePullRequestWatch(rerun.next, second, noRemarks).changes.length, 1); // A push reports its failures, even ones that failed between two passes. const pushed = detail({ ...second, headSha: "bbbbbbbbbb" }); assert.equal(evaluatePullRequestWatch(test.next, pushed, noRemarks).changes.length, 1); }); it("reports passed once the required checks pass, whatever the others do", () => { const required = (name: string, status: PullRequestCheck["status"]) => ({ ...check(name, status), required: true, }); const green = detail({ checks: [required("test", "success"), required("lint", "success"), check("bot", "pending")], }); const passed = evaluatePullRequestWatch(watch(), green, noRemarks); assert.deepEqual(passed.changes, [{ kind: "checks-passed", count: 2, required: true }]); assert.deepEqual(evaluatePullRequestWatch(passed.next, green, noRemarks).changes, []); // Where nothing is marked required, every check has to pass. const plain = detail({ checks: [check("test", "success"), check("bot", "pending")] }); assert.deepEqual(evaluatePullRequestWatch(watch(), plain, noRemarks).changes, []); }); it("reports passed again when a required check shows up already passed", () => { const required = (name: string, status: PullRequestCheck["status"]) => ({ ...check(name, status), required: true, }); const tests = detail({ checks: [required("Tests", "success"), check("Smoke Tests", "pending")], }); const first = evaluatePullRequestWatch(watch(), tests, noRemarks); assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: true }]); // The gate job was created and finished between two passes, so it was never seen pending. const gated = detail({ checks: [ required("Tests", "success"), check("Smoke Tests", "success"), required("Smoke Tests Gate", "success"), ], }); const second = evaluatePullRequestWatch(first.next, gated, noRemarks); assert.deepEqual(second.changes, [{ kind: "checks-passed", count: 2, required: true }]); assert.deepEqual(evaluatePullRequestWatch(second.next, gated, noRemarks).changes, []); // Seen pending first, the gate is reported once it passes, as before. const pending = detail({ checks: [required("Tests", "success"), required("Smoke Tests Gate", "pending")], }); const waiting = evaluatePullRequestWatch(first.next, pending, noRemarks); assert.deepEqual(waiting.changes, []); assert.deepEqual(evaluatePullRequestWatch(waiting.next, gated, noRemarks).changes, [ { kind: "checks-passed", count: 2, required: true }, ]); }); it("does not report passed again for a new passed check where none is required", () => { const first = evaluatePullRequestWatch( watch(), detail({ checks: [check("test", "success")] }), noRemarks, ); assert.deepEqual(first.changes, [{ kind: "checks-passed", count: 1, required: false }]); const both = detail({ checks: [check("test", "success"), check("lint", "success")] }); assert.deepEqual(evaluatePullRequestWatch(first.next, both, noRemarks).changes, []); }); it("does not wake a watch saved before passed checks were recorded", () => { const green = detail({ checks: [{ ...check("test", "success"), required: true }] }); const told = watch({ headSha: "aaaaaaaaaa", passed: true }); const saved = evaluatePullRequestWatch(told, green, noRemarks); assert.deepEqual(saved.changes, []); assert.deepEqual(saved.next.passedChecks, ["test"]); }); it("keeps remarks for a later pass when the conversation was not read whole", () => { const comments = [remark("reviewer", "2026-10-02T12:06:00Z")]; const partial = evaluatePullRequestWatch(watch(), detail(), null); assert.deepEqual(partial.changes, []); assert.equal( evaluatePullRequestWatch(partial.next, detail(), comments).changes[0]?.kind, "remarks", ); }); it("reports a remark that shows up late with the same time as a reported one", () => { const first = remark("reviewer", "2026-10-02T12:06:00Z"); const late = { ...remark("bot", "2026-10-02T12:06:00Z"), id: "late" }; const reported = evaluatePullRequestWatch(watch(), detail(), [first]); const again = evaluatePullRequestWatch(reported.next, detail(), [first, late]); assert.deepEqual(again.changes, [{ kind: "remarks", remarks: [late] }]); assert.deepEqual(again.next.remarkIds, [first.id, "late"]); }); it("reports edits after the watermark once and counts them toward the wake limit", () => { const old = remark("greptile[bot]", "2026-10-02T11:00:00Z"); const watching = watch({ headSha: "aaaaaaaaaa", remarkIds: [old.id], wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1, }); assert.deepEqual(evaluatePullRequestWatch(watching, detail(), [old]).changes, []); const edited = { ...old, editedAt: "2026-10-02T12:06:00Z" }; const report = evaluatePullRequestWatch(watching, detail(), [edited]); assert.deepEqual(report.changes, [{ kind: "remarks", remarks: [edited] }]); assert.equal(report.next.remarksThrough, edited.editedAt); assert.deepEqual(report.next.remarkIds, [old.id]); assert.isTrue(report.exhausted); assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), [edited]).changes, []); const late = { ...edited, id: "late" }; assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), [edited, late]).changes, [ { kind: "remarks", remarks: [late] }, ]); }); it("does not treat a failed check read as a rerun", () => { const failed = detail({ checks: [check("lint", "failure")] }); const reported = evaluatePullRequestWatch(watch(), failed, noRemarks); assert.equal(reported.changes.length, 1); const unreadable = evaluatePullRequestWatch(reported.next, detail({ checks: [] }), noRemarks); assert.deepEqual(evaluatePullRequestWatch(unreadable.next, failed, noRemarks).changes, []); }); it("wakes for the pull request's author when the agent is someone else", () => { const contributor = detail({ author: { login: "contributor", name: null, avatarUrl: null } }); const reply = remark("contributor", "2026-10-02T12:06:00Z"); assert.deepEqual(evaluatePullRequestWatch(watch(), contributor, [reply]).changes, [ { kind: "remarks", remarks: [reply] }, ]); // Without a viewer, the author is taken to be the agent. const noViewer = detail({ viewer: undefined, author: contributor.author }); assert.deepEqual(evaluatePullRequestWatch(watch(), noViewer, [reply]).changes, []); }); it("reports remarks from others once and never the agent's own", () => { const comments = [ remark("agent-user", "2026-10-02T12:05:00Z", "Fixed in the latest push."), remark("macroscope-app[bot]", "2026-10-02T12:06:00Z"), remark("reviewer", "2026-10-02T11:00:00Z", "Older than the watch."), ]; const report = evaluatePullRequestWatch(watch(), detail(), comments); assert.deepEqual(report.changes, [{ kind: "remarks", remarks: [comments[1]!] }]); assert.equal(report.next.remarksThrough, "2026-10-02T12:06:00Z"); assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), comments).changes, []); }); it("reports a conflict once, until the branch is clean again", () => { const conflicting = detail({ mergeability: "conflicting" }); const first = evaluatePullRequestWatch(watch(), conflicting, noRemarks); assert.deepEqual(first.changes, [{ kind: "conflicting" }]); // GitHub answers "unknown" while it recomputes after a push; that is not a resolution. const recomputing = evaluatePullRequestWatch( first.next, detail({ mergeability: "unknown" }), noRemarks, ); assert.deepEqual( evaluatePullRequestWatch(recomputing.next, conflicting, noRemarks).changes, [], ); const clean = evaluatePullRequestWatch(first.next, detail(), noRemarks); assert.deepEqual(evaluatePullRequestWatch(clean.next, conflicting, noRemarks).changes, [ { kind: "conflicting" }, ]); }); it("does not spend the comment wake limit on check results", () => { const tired = watch({ headSha: "aaaaaaaaaa", wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1 }); const result = evaluatePullRequestWatch( tired, detail({ checks: [check("lint", "failure")] }), noRemarks, ); assert.isFalse(result.exhausted); assert.equal(result.next.wakes, 0); }); it("stops after the wake limit unless the head moves", () => { const comments = [remark("reviewer", "2026-10-02T12:10:00Z")]; const tired = watch({ headSha: "aaaaaaaaaa", wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1 }); assert.isTrue(evaluatePullRequestWatch(tired, detail(), comments).exhausted); const pushed = evaluatePullRequestWatch(tired, detail({ headSha: "cccccccccc" }), comments); assert.isFalse(pushed.exhausted); assert.equal(pushed.next.wakes, 1); }); }); describe("pullRequestWatchMessage", () => { it("tells the agent what changed and marks failures for the timeline", () => { const report = evaluatePullRequestWatch( watch(), detail({ checks: [check("lint", "failure")] }), [remark("reviewer", "2026-10-02T12:10:00Z", "Needs a test.")], ); const message = pullRequestWatchMessage({ number: 12, url: "https://github.com/o/r/pull/12", baseBranch: "main", headSha: report.next.headSha, report, }); assert.include(message.text, "- Checks failed on aaaaaaa:\n - lint https://ci.example/lint"); assert.include(message.text, ' - reviewer on src/index.ts: "Needs a test."'); assert.include(message.text, "unwatch_pull_request"); assert.deepEqual(message.notification, { source: { kind: "monitor" }, outcome: "failed", summary: "#12: checks failed, new comments", }); }); });