|
| 1 | +import { createExecutionContext, waitOnExecutionContext } from "cloudflare:test"; |
| 2 | +import { DateTime } from "luxon"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | +import { ACK_TEXT } from "../../../src/features/pr_management/pr_report"; |
| 5 | +import { GitHubApiError } from "../../../src/github"; |
| 6 | +import type { MessageBlock } from "../../../src/slack"; |
| 7 | +import { aPR, aPRHistory } from "../../builders/github"; |
| 8 | +import { aRecord } from "../../builders/pr_record"; |
| 9 | +import { type PRTestApp, prApp, REPO } from "../../helpers/pr"; |
| 10 | +import { commandBody, signedSlackRequest } from "../../helpers/slack"; |
| 11 | + |
| 12 | +const at = (iso: string) => DateTime.fromISO(iso, { zone: "utc" }); |
| 13 | + |
| 14 | +async function send(h: PRTestApp, body: string) { |
| 15 | + const ctx = createExecutionContext(); |
| 16 | + const response = await h.app.fetch(await signedSlackRequest(body), ctx); |
| 17 | + const text = await response.text(); |
| 18 | + await waitOnExecutionContext(ctx); |
| 19 | + return { status: response.status, text }; |
| 20 | +} |
| 21 | + |
| 22 | +const dms = (h: PRTestApp) => h.slack.dms; |
| 23 | +const PR_CHANNEL = "CPRS"; |
| 24 | +const channelReports = (h: PRTestApp) => |
| 25 | + h.slack.posts.filter((p) => p.channel === PR_CHANNEL && p.text.startsWith("PR report")); |
| 26 | + |
| 27 | +/** Every mrkdwn line from blocks. */ |
| 28 | +function sectionLines(blocks: readonly MessageBlock[]): string[] { |
| 29 | + return blocks.flatMap((block) => (block.type === "section" && block.text ? block.text.text.split("\n") : [])); |
| 30 | +} |
| 31 | + |
| 32 | +/** Ticks the scheduler at `iso` so a scheduled report is recorded. */ |
| 33 | +async function tickAt(h: PRTestApp, iso: string) { |
| 34 | + h.clock.set(iso); |
| 35 | + await h.app.scheduled(Date.parse(iso)); |
| 36 | +} |
| 37 | + |
| 38 | +describe("/nathan pr_report", () => { |
| 39 | + it("acks with a brief message and sends the report as a DM", async () => { |
| 40 | + const h = prApp(); |
| 41 | + h.github.upsert(aPR({ pendingReviewers: ["bob"] })); |
| 42 | + h.clock.set("2026-10-09T15:00:00Z"); |
| 43 | + await h.refresh(101); |
| 44 | + |
| 45 | + const result = await send(h, commandBody("pr_report")); |
| 46 | + |
| 47 | + expect(result.status).toBe(200); |
| 48 | + expect(result.text).toBe(ACK_TEXT); |
| 49 | + expect(dms(h)).toHaveLength(1); |
| 50 | + expect(dms(h)[0]?.userId).toBe("UALICE"); |
| 51 | + expect(dms(h)[0]?.message.text).toContain("PR report"); |
| 52 | + expect(dms(h)[0]?.message.text).toContain("1 open PR"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("does not post to the PR channel", async () => { |
| 56 | + const h = prApp(); |
| 57 | + h.clock.set("2026-10-09T15:00:00Z"); |
| 58 | + await send(h, commandBody("pr_report")); |
| 59 | + |
| 60 | + expect(channelReports(h)).toHaveLength(0); |
| 61 | + }); |
| 62 | + |
| 63 | + it("does not record the report, so the next scheduled one is unaffected", async () => { |
| 64 | + const h = prApp(); |
| 65 | + h.github.upsert(aPR({ number: 1, createdAt: at("2026-10-08T10:00:00Z") })); |
| 66 | + |
| 67 | + // A scheduled report at 09:30 records open count = 1. |
| 68 | + await tickAt(h, "2026-10-08T13:30:00Z"); |
| 69 | + expect(channelReports(h)).toHaveLength(1); |
| 70 | + |
| 71 | + // On-demand report at 14:00: a second PR exists now. |
| 72 | + h.github.upsert(aPR({ number: 2, createdAt: at("2026-10-08T14:00:00Z") })); |
| 73 | + h.clock.set("2026-10-08T18:00:00Z"); |
| 74 | + await send(h, commandBody("pr_report")); |
| 75 | + expect(dms(h)).toHaveLength(1); |
| 76 | + |
| 77 | + // Next scheduled report should still compare against the 09:30 report (open count = 1), |
| 78 | + // not the on-demand one. The blocks contain "+1 since the last report". |
| 79 | + await tickAt(h, "2026-10-09T13:30:00Z"); |
| 80 | + const lastReport = channelReports(h).at(-1); |
| 81 | + const lines = sectionLines((lastReport?.blocks ?? []) as MessageBlock[]); |
| 82 | + expect(lines.some((line) => line.includes("+1 since the last report"))).toBe(true); |
| 83 | + }); |
| 84 | + |
| 85 | + it("includes weekly sections on Monday", async () => { |
| 86 | + const h = prApp(); |
| 87 | + h.github.history.push( |
| 88 | + aPRHistory({ |
| 89 | + author: "bob", |
| 90 | + createdAt: at("2026-10-05T10:00:00Z"), |
| 91 | + mergedAt: at("2026-10-07T10:00:00Z"), |
| 92 | + closedAt: at("2026-10-07T10:00:00Z"), |
| 93 | + }), |
| 94 | + ); |
| 95 | + // Monday in New York (EDT, UTC-4). |
| 96 | + h.clock.set("2026-10-12T16:00:00Z"); |
| 97 | + await send(h, commandBody("pr_report")); |
| 98 | + |
| 99 | + const lines = sectionLines((dms(h)[0]?.message.blocks ?? []) as MessageBlock[]); |
| 100 | + expect(lines.some((line) => line.startsWith("*Trends*"))).toBe(true); |
| 101 | + expect(lines.some((line) => line.startsWith("*People*"))).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it("does not include weekly sections on a non-Monday", async () => { |
| 105 | + const h = prApp(); |
| 106 | + // Wednesday in New York. |
| 107 | + h.clock.set("2026-10-07T16:00:00Z"); |
| 108 | + await send(h, commandBody("pr_report")); |
| 109 | + |
| 110 | + const lines = sectionLines((dms(h)[0]?.message.blocks ?? []) as MessageBlock[]); |
| 111 | + expect(lines.some((line) => line.startsWith("*Trends*"))).toBe(false); |
| 112 | + }); |
| 113 | + |
| 114 | + it("threads multi-part reports in the DM", async () => { |
| 115 | + const h = prApp(); |
| 116 | + vi.spyOn(h.github.reader, "openPullRequests").mockRejectedValue(new GitHubApiError("down", 502)); |
| 117 | + for (let number = 1; number <= 900; number++) { |
| 118 | + await h.store.insert( |
| 119 | + aRecord({ |
| 120 | + repo: REPO, |
| 121 | + number, |
| 122 | + title: `A very long PR title that keeps going and going to fill the line ${"x".repeat(40)}`, |
| 123 | + category: "oss", |
| 124 | + author: "outsider", |
| 125 | + state: "awaiting_review", |
| 126 | + owners: ["bob"], |
| 127 | + stateSince: at("2026-10-09T13:00:00Z"), |
| 128 | + }), |
| 129 | + ); |
| 130 | + } |
| 131 | + h.clock.set("2026-10-09T15:00:00Z"); |
| 132 | + |
| 133 | + await send(h, commandBody("pr_report")); |
| 134 | + |
| 135 | + // First part is a DM. |
| 136 | + expect(dms(h)).toHaveLength(1); |
| 137 | + expect(dms(h)[0]?.userId).toBe("UALICE"); |
| 138 | + |
| 139 | + // Continuation parts are posted to the DM channel, threaded under the first message. |
| 140 | + const dmChannel = `DUALICE`; |
| 141 | + const threadParts = h.slack.posts.filter((p) => p.channel === dmChannel && p.thread_ts); |
| 142 | + expect(threadParts.length).toBeGreaterThan(0); |
| 143 | + // Nothing went to the PR channel. |
| 144 | + expect(channelReports(h)).toHaveLength(0); |
| 145 | + }); |
| 146 | + |
| 147 | + it("shows in /nathan help", async () => { |
| 148 | + const h = prApp(); |
| 149 | + const result = await send(h, commandBody("help")); |
| 150 | + expect(result.text).toContain("pr_report"); |
| 151 | + expect(result.text).toContain("PR report"); |
| 152 | + }); |
| 153 | +}); |
0 commit comments