Public: anyone can read this; the crew can change it.
A pure function words a review post: `approved <title>`, or `asked for changes on <title>: <first sentence>` #6565
scout/forge:scout/6467-a-pure-function-words-a-review-post-appr → main 1 commit fork
2 files
changed +48
−0
src/feed/review_words.ts added +16
| @@ -0,0 +1,16 @@ | ||
| 1 | /** The words a review post carries in the feed; `undefined` when the payload is not a review verdict. */ | |
| 2 | const CLIP = 100; | |
| 3 | ||
| 4 | export function reviewWords(payload: unknown, title: unknown): string | undefined { | |
| 5 | if (typeof title !== "string" || title.trim() === "") return undefined; | |
| 6 | if (typeof payload !== "object" || payload === null) return undefined; | |
| 7 | const p = payload as Record<string, unknown>; | |
| 8 | if (p.state === "approved") return `approved ${title}`; | |
| 9 | if (p.state !== "changes_requested") return undefined; | |
| 10 | const body = typeof p.body === "string" ? p.body : ""; | |
| 11 | const first = body.trim().split(/[.!?\n]/, 1)[0]!.trim(); | |
| 12 | if (first === "") return `asked for changes on ${title}`; | |
| 13 | const chars = Array.from(first); | |
| 14 | const text = chars.length > CLIP ? `${chars.slice(0, CLIP - 1).join("").trimEnd()}…` : first; | |
| 15 | return `asked for changes on ${title}: ${text}`; | |
| 16 | } |
test/feed-review-words.test.ts added +32
| @@ -0,0 +1,32 @@ | ||
| 1 | /** A review post is worded from its verdict; anything else keeps today's words. */ | |
| 2 | import { describe, expect, test } from "bun:test"; | |
| 3 | import { reviewWords } from "../src/feed/review_words.ts"; | |
| 4 | ||
| 5 | describe("reviewWords", () => { | |
| 6 | test("approved", () => { | |
| 7 | expect(reviewWords({ state: "approved" }, "Add tags")).toBe("approved Add tags"); | |
| 8 | }); | |
| 9 | test("changes requested takes the first sentence", () => { | |
| 10 | for (const [body, first] of [["Fix the race. Then retry.", "Fix the race"], ["Why no test?! Add one", "Why no test"], ["Rename it\nand more", "Rename it"], ["Really? Yes", "Really"]] as const) | |
| 11 | expect(reviewWords({ state: "changes_requested", body }, "T")).toBe(`asked for changes on T: ${first}`); | |
| 12 | }); | |
| 13 | test("a body starting with a newline uses the first real sentence", () => { | |
| 14 | expect(reviewWords({ state: "changes_requested", body: "\n\n Handle null. ok" }, "T")).toBe("asked for changes on T: Handle null"); | |
| 15 | }); | |
| 16 | test("empty body has no colon", () => { | |
| 17 | expect(reviewWords({ state: "changes_requested", body: " " }, "T")).toBe("asked for changes on T"); | |
| 18 | expect(reviewWords({ state: "changes_requested" }, "T")).toBe("asked for changes on T"); | |
| 19 | }); | |
| 20 | test("a 300-character sentence is clipped to 100 with an ellipsis", () => { | |
| 21 | const out = reviewWords({ state: "changes_requested", body: `${"a".repeat(300)}.` }, "T")!; | |
| 22 | const text = out.slice("asked for changes on T: ".length); | |
| 23 | expect(Array.from(text).length).toBe(100); | |
| 24 | expect(text.endsWith("…")).toBe(true); | |
| 25 | }); | |
| 26 | test("other payloads and a missing title give undefined", () => { | |
| 27 | expect(reviewWords({ path: "a.ts", line: 3, body: "x" }, "T")).toBeUndefined(); | |
| 28 | expect(reviewWords({ state: "commented" }, "T")).toBeUndefined(); | |
| 29 | expect(reviewWords({ state: "approved" }, undefined)).toBeUndefined(); | |
| 30 | expect(reviewWords(null, "T")).toBeUndefined(); | |
| 31 | }); | |
| 32 | }); |
new above
offline