| 1 | import { jsx } from "hono/jsx"; |
| 2 | import { describe, expect, it } from "vitest"; |
| 3 | import type { Today } from "../../src/agentic/today.js"; |
| 4 | import type { TodayPull } from "../../src/agentic/today-repo.js"; |
| 5 | import { COLLAPSED_COOKIE, SHOWN, TodayView, collapsedSections } from "../../src/agentic/web.js"; |
| 6 | |
| 7 | const repo = { id: "web", fullName: "acme/web", url: "/acme/web", defaultBranch: "main", visibility: "private" }; |
| 8 | const pull = (n: number): TodayPull => ({ |
| 9 | number: n, |
| 10 | title: `Change ${n}`, |
| 11 | author: { id: "a-bot", login: "web-agent", kind: "agent" }, |
| 12 | head: `b${n}`, |
| 13 | base: "main", |
| 14 | draft: false, |
| 15 | requestedReviewers: [], |
| 16 | intent: null, |
| 17 | createdAt: "2026-10-22T08:00:00Z", |
| 18 | updatedAt: `2026-10-22T08:${String(n).padStart(2, "0")}:00Z`, |
| 19 | approvedBy: [], |
| 20 | approvedHead: false, |
| 21 | changesRequestedBy: [], |
| 22 | reviewerIds: [], |
| 23 | checks: "none", |
| 24 | failingChecks: [], |
| 25 | autoMerge: false, |
| 26 | queued: false, |
| 27 | mergeState: "clean", |
| 28 | }); |
| 29 | const today = (over: Partial<Today> = {}): Today => ({ |
| 30 | generatedAt: "2026-10-24T13:00:00Z", |
| 31 | since: "2026-10-23T13:00:00Z", |
| 32 | repositories: 1, |
| 33 | incomplete: false, |
| 34 | needsYou: [], |
| 35 | agentsAtWork: [], |
| 36 | landed: [], |
| 37 | releases: [], |
| 38 | failures: [], |
| 39 | deployments: [], |
| 40 | tasks: [], |
| 41 | ...over, |
| 42 | }); |
| 43 | const html = (t: Today, collapsed?: Set<string>) => String(jsx(TodayView, { today: t, name: "Can", collapsed })); |
| 44 | const section = (page: string, id: string) => page.slice(page.indexOf(`id="${id}"`), page.indexOf("</details>", page.lastIndexOf(`id="${id}"`))); |
| 45 | |
| 46 | describe("Today view", () => { |
| 47 | const eleven = today({ needsYou: Array.from({ length: 11 }, (_, i) => ({ kind: "agent_approval" as const, repo, pull: pull(11 - i) })) }); |
| 48 | |
| 49 | it("shows the newest SHOWN rows of a section and the rest behind Show N more", () => { |
| 50 | const page = html(eleven); |
| 51 | const needs = page.slice(page.indexOf('id="needs-you"'), page.indexOf('id="agents"')); |
| 52 | const [first, rest] = needs.split('class="today-more"'); |
| 53 | expect(SHOWN).toBe(3); |
| 54 | expect(first!.match(/class="title"/g)).toHaveLength(3); |
| 55 | expect(first).toContain("Change 11"); |
| 56 | expect(first).not.toContain("Change 8<"); |
| 57 | expect(rest).toContain("Show 8 more"); |
| 58 | expect(rest!.match(/class="title"/g)).toHaveLength(8); |
| 59 | expect(rest).toContain("data-show-fewer"); |
| 60 | expect(needs).toMatch(/<span class="count">11<\/span>/); |
| 61 | }); |
| 62 | |
| 63 | it("has no expander when a section fits", () => { |
| 64 | const page = html(today({ needsYou: [{ kind: "agent_approval", repo, pull: pull(1) }] })); |
| 65 | expect(page).not.toContain("today-more"); |
| 66 | expect(page).toContain("Agent PR needs a person's approval"); |
| 67 | }); |
| 68 | |
| 69 | it("renders every section open unless the viewer folded it", () => { |
| 70 | expect(html(eleven)).toMatch(/<details class="box today-sec" id="needs-you" data-today-sec="needs-you" open/); |
| 71 | const folded = html(eleven, collapsedSections("needs-you.landed")); |
| 72 | expect(folded).toMatch(/<details class="box today-sec" id="needs-you" data-today-sec="needs-you">/); |
| 73 | expect(folded).toMatch(/<details class="box today-sec" id="landed" data-today-sec="landed">/); |
| 74 | expect(folded).toMatch(/<details class="box today-sec" id="agents" data-today-sec="agents" open/); |
| 75 | }); |
| 76 | |
| 77 | it("lists what happened since yesterday newest first, whatever it is", () => { |
| 78 | const run = (id: string, at: string) => ({ repo, run: { id, runNumber: 1, workflow: "CI", title: id, ref: "refs/heads/main", event: "push", actor: { id: "u", login: "mia", kind: "user" }, conclusion: "failure", prNumber: null, at } }); |
| 79 | const merged = (n: number, mergedAt: string) => ({ repo, pull: { number: n, title: `Merged ${n}`, author: { id: "u", login: "mia", kind: "user" }, mergedAt, mergedBy: "mia" } }); |
| 80 | const page = html(today({ failures: [run("old failure", "2026-10-24T01:00:00Z")], landed: [merged(1, "2026-10-24T12:00:00Z"), merged(2, "2026-10-24T02:00:00Z"), merged(3, "2026-10-24T00:30:00Z")] })); |
| 81 | const landed = section(page, "landed"); |
| 82 | expect(landed.indexOf("Merged 1")).toBeLessThan(landed.indexOf("Merged 2")); |
| 83 | expect(landed.indexOf("Merged 2")).toBeLessThan(landed.indexOf("old failure")); |
| 84 | expect(landed.indexOf("old failure")).toBeLessThan(landed.indexOf("Show 1 more")); |
| 85 | expect(landed.indexOf("Show 1 more")).toBeLessThan(landed.indexOf("Merged 3")); |
| 86 | }); |
| 87 | |
| 88 | it("reads only known section ids from the cookie", () => { |
| 89 | expect(COLLAPSED_COOKIE).toBe("today_collapsed"); |
| 90 | expect([...collapsedSections("needs-you.bogus..agents")]).toEqual(["needs-you", "agents"]); |
| 91 | expect(collapsedSections(undefined).size).toBe(0); |
| 92 | }); |
| 93 | }); |