Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
285 changes: 285 additions & 0 deletions dashboard/e2e/reviewer-session.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
import { expect, test, type Page, type Route } from "@playwright/test";
import {
findingsLabel,
isSessionId,
kindLabel,
sessionHref,
sessionLook,
sessionSeconds,
statusNote,
stepOffset,
triggerLabel,
} from "../src/app/reviewer/session/session";

// A review's session page: where a PR's "Coderhelm review" check links. Every
// API call is answered here; nothing reaches a real backend.

const ID = "3f2c9a1e-5b7d-4c8e-9a0b-1c2d3e4f5a6b";
const now = Date.now();
const iso = (secsAgo: number) => new Date(now - secsAgo * 1000).toISOString();

// ---- The helpers ------------------------------------------------------------

test.describe("session helpers", () => {
test("only a job id (a UUID) is a session id", () => {
expect(isSessionId(ID)).toBe(true);
expect(isSessionId(ID.toUpperCase())).toBe(true);
for (const bad of ["", "abc", `${ID}x`, "../REVIEW#acme/web", "3f2c9a1e_5b7d_4c8e_9a0b_1c2d3e4f5a6b"]) {
expect(isSessionId(bad), bad).toBe(false);
}
expect(sessionHref(ID)).toBe(`/reviewer/session?id=${ID}`);
});

test("every status reads as words with its tone", () => {
expect(sessionLook("reviewing")).toEqual({ label: "Reviewing", tone: "live", live: true });
expect(sessionLook("reviewing", "question").label).toBe("Answering");
expect(sessionLook("done")).toEqual({ label: "Done", tone: "success" });
expect(sessionLook("skipped").tone).toBe("muted");
expect(sessionLook("retrying").label).toBe("Sent again");
expect(sessionLook("failed").tone).toBe("danger");
expect(sessionLook("stalled")).toEqual({ label: "Stalled", tone: "danger" });
expect(sessionLook("some_new_state")).toEqual({ label: "Some new state", tone: "muted" });
expect(statusNote("retrying")).toMatch(/own session/);
expect(statusNote("done")).toBeNull();
});

test("trigger, kind and findings read as plain words", () => {
expect(triggerLabel("label")).toBe("The review label");
expect(triggerLabel("push")).toBe("New commits");
expect(triggerLabel("rerequest")).toBe("Asked for again");
expect(triggerLabel("reply")).toBe("A reply to the reviewer");
expect(triggerLabel("new_kind")).toBe("New kind");
expect(triggerLabel("")).toBe("Unknown");
expect(kindLabel("question")).toBe("Answer to a comment");
expect(kindLabel("review")).toBe("Review");
expect(findingsLabel(undefined)).toBeNull();
expect(findingsLabel(null)).toBeNull();
expect(findingsLabel(0)).toBe("No findings");
expect(findingsLabel(1)).toBe("1 finding");
expect(findingsLabel(3)).toBe("3 findings");
});

test("duration runs to now while in flight and to the finish after", () => {
const start = "2026-10-22T14:18:00+00:00";
const end = "2026-10-22T14:20:05+00:00";
expect(sessionSeconds({ started_at: start, finished_at: end, in_flight: false })).toBe(125);
const t = new Date(start).getTime() + 42_000;
expect(sessionSeconds({ started_at: start, finished_at: "", in_flight: true }, t)).toBe(42);
expect(sessionSeconds({ started_at: start, finished_at: "", in_flight: false })).toBeNull();
expect(sessionSeconds({ started_at: "", finished_at: end, in_flight: false })).toBeNull();
expect(stepOffset(start, "2026-10-22T14:18:12+00:00")).toBe(12);
expect(stepOffset(start, "not a time")).toBeNull();
});
});

// ---- The page ---------------------------------------------------------------

type Session = Record<string, unknown>;

function session(over: Session = {}): Session {
return {
id: ID,
repo: "acme/web",
pr_number: 318,
pr_title: "Rate-limit the login form",
head_sha: "9fceb02d0ae598e95dc970b74767f19372d61af8",
trigger: "push",
kind: "review",
status: "reviewing",
in_flight: true,
attempt: 0,
started_at: iso(90),
finished_at: "",
reason: "",
verdict: "",
findings: null,
review_url: "",
check_run_id: 31337000,
steps: [
{ at: iso(90), text: "Started reviewing `9fceb02`", detail: "" },
{ at: iso(80), text: "Read the change: 2 files changed", detail: "src/auth/login.ts\nsrc/auth/limits.ts" },
],
...over,
};
}

interface Backend {
calls: string[];
/** Answers to /api/reviewer/session, in turn; the last one repeats. */
sessions: (Session | number)[];
role: string;
gate?: Record<string, unknown>;
}

async function mockApi(page: Page, init: Partial<Backend> = {}): Promise<Backend> {
const db: Backend = { calls: [], sessions: [session()], role: "member", ...init };
let served = 0;
const json = (route: Route, body: unknown, status = 200) =>
route.fulfill({ status, contentType: "application/json", body: JSON.stringify(body) });
await page.route((url) => url.hostname !== "localhost", (route) => route.abort());
await page.route((url) => url.hostname === "localhost" && /^\/(api|auth)\//.test(url.pathname), async (route) => {
const req = route.request();
const url = new URL(req.url());
const p = url.pathname;
db.calls.push(`${req.method()} ${p}${url.search}`);
if (p === "/api/me") return json(route, { user_id: "u_1", team_id: "t_1", github_login: "a-maintainer", email: "someone@example.com", avatar_url: "", role: db.role });
if (p === "/api/usage") return json(route, { month: "2026-01", total_tokens: 0, tokens_in: 0, tokens_out: 0, total_runs: 0, max_tokens: 0 });
if (p === "/api/banners") return json(route, { banners: [] });
if (p === "/api/support/badge") return json(route, { waiting: 0, queue_open: null });
if (p === "/api/integrations") return json(route, { can_edit: false, items: [], migrated: 0 });
if (p === "/api/skills/used") return json(route, { skills: [], agents: [], sources: [], standards: [] });
if (p === "/api/teams") return json(route, { teams: [{ team_id: "t_1", org: "acme", status: "active", current: true }] });
if (p === "/api/repos") return json(route, { repos: [{ name: "acme/web", enabled: true, ticket_source: "github" }] });
if (p === "/api/aws-connections") return json(route, { connections: [] });
if (p === "/api/reviewer/session") {
const answer = db.sessions[Math.min(served, db.sessions.length - 1)];
served += 1;
if (typeof answer === "number") return json(route, { error: "nope" }, answer);
return json(route, answer);
}
if (p === "/api/reviewer/gate" && db.gate) return json(route, db.gate);
return json(route, { error: `Not mocked: ${req.method()} ${p}` }, 404);
});
return db;
}

function watchConsole(page: Page, allowFailedLoads = false) {
const errors: string[] = [];
page.on("console", (msg) => {
if (msg.type() !== "error") return;
const text = msg.text();
if (text.includes("net::ERR_ABORTED") || text.includes("net::ERR_FAILED")) return;
if (allowFailedLoads && text.startsWith("Failed to load resource")) return;
errors.push(text);
});
page.on("pageerror", (err) => errors.push(err.message));
return errors;
}

const sessionCalls = (db: Backend) => db.calls.filter((c) => c.startsWith("GET /api/reviewer/session"));

test("a running review shows its steps live, then its verdict and the posted review", async ({ page }) => {
const errors = watchConsole(page);
const done = session({
status: "done",
in_flight: false,
finished_at: iso(5),
verdict: "REQUEST_CHANGES",
findings: 2,
review_url: "https://github.com/acme/web/pull/318#pullrequestreview-2201",
steps: [
...(session().steps as unknown[]),
{ at: iso(5), text: "Posted the review: 2 findings", detail: "" },
],
});
const db = await mockApi(page, { sessions: [session(), done] });
await page.goto(`/reviewer/session?id=${ID}`);
await expect(page.getByRole("heading", { level: 1, name: "Rate-limit the login form" })).toBeVisible();
await expect(page.getByTestId("session-status")).toHaveText("Reviewing");
await expect(page.getByRole("link", { name: /Open on GitHub/ })).toHaveAttribute("href", "https://github.com/acme/web/pull/318");
await expect(page.getByTestId("session-facts")).toContainText("New commits");
await expect(page.getByTestId("session-facts")).toContainText("Still running");
await expect(page.getByRole("link", { name: /9fceb02/ })).toHaveAttribute("href", "https://github.com/acme/web/commit/9fceb02d0ae598e95dc970b74767f19372d61af8");
const steps = page.getByTestId("session-steps").getByRole("listitem");
await expect(steps).toHaveCount(2);
await expect(steps.nth(1)).toContainText("Read the change: 2 files changed");
// A step's detail opens on demand.
await expect(page.getByText("src/auth/limits.ts")).toBeHidden();
await steps.nth(1).getByText("More").click();
await expect(page.getByText(/src\/auth\/limits\.ts/)).toBeVisible();
await expect(page.getByTestId("session-outcome")).toHaveCount(0);

// The page refreshes on its own while the review runs, and stops when it ends.
await expect(page.getByTestId("session-status")).toHaveText("Done", { timeout: 10_000 });
await expect(steps).toHaveCount(3);
const outcome = page.getByTestId("session-outcome");
await expect(outcome).toContainText("Changes requested");
await expect(page.getByTestId("session-findings")).toHaveText("2 findings");
await expect(outcome.getByRole("link", { name: /See the review on GitHub/ })).toHaveAttribute(
"href",
"https://github.com/acme/web/pull/318#pullrequestreview-2201",
);
await expect(page.getByTestId("session-duration")).toHaveText("1m 25s");
const calls = sessionCalls(db).length;
await page.waitForTimeout(5_000);
expect(sessionCalls(db).length).toBe(calls);

// Phone width: nothing scrolls sideways.
await page.setViewportSize({ width: 390, height: 800 });
const overflow = await page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth);
expect(overflow).toBeLessThanOrEqual(0);
expect(errors).toEqual([]);
});

test("a hidden tab stops refreshing until it is visible again", async ({ page }) => {
const db = await mockApi(page);
await page.goto(`/reviewer/session?id=${ID}`);
await expect(page.getByTestId("session-status")).toHaveText("Reviewing");
await page.evaluate(() => {
Object.defineProperty(document, "visibilityState", { configurable: true, get: () => "hidden" });
document.dispatchEvent(new Event("visibilitychange"));
});
const calls = sessionCalls(db).length;
await page.waitForTimeout(9_000);
expect(sessionCalls(db).length).toBe(calls);
await page.evaluate(() => {
Object.defineProperty(document, "visibilityState", { configurable: true, get: () => "visible" });
document.dispatchEvent(new Event("visibilitychange"));
});
await expect.poll(() => sessionCalls(db).length).toBeGreaterThan(calls);
});

test("a skipped or retried review says why", async ({ page }) => {
await mockApi(page, {
sessions: [session({ status: "skipped", in_flight: false, finished_at: iso(60), reason: "Only lockfile changes." })],
});
await page.goto(`/reviewer/session?id=${ID}`);
await expect(page.getByTestId("session-status")).toHaveText("Skipped");
await expect(page.getByTestId("session-notice")).toContainText("Only lockfile changes.");
await expect(page.getByTestId("session-outcome")).toHaveCount(0);

await mockApi(page, { sessions: [session({ status: "retrying", in_flight: false, finished_at: iso(60), attempt: 1 })] });
await page.goto(`/reviewer/session?id=${ID}`);
await expect(page.getByTestId("session-status")).toHaveText("Sent again");
await expect(page.getByTestId("session-notice")).toContainText("The new try has its own session.");
await expect(page.getByTestId("session-facts")).toContainText("Review, try 2");
});

test("a missing, malformed or forbidden session shows nothing to read", async ({ page }) => {
const errors = watchConsole(page, true);
const db = await mockApi(page, { sessions: [404] });
await page.goto(`/reviewer/session?id=${ID}`);
await expect(page.getByText("Review not found")).toBeVisible();
// A malformed id never reaches the API.
const before = sessionCalls(db).length;
await page.goto("/reviewer/session?id=../REVIEW%23acme");
await expect(page.getByText("Review not found")).toBeVisible();
expect(sessionCalls(db).length).toBe(before);

await mockApi(page, { sessions: [403] });
await page.goto(`/reviewer/session?id=${ID}`);
await expect(page.getByText("You can't open this review")).toBeVisible();

await mockApi(page, { sessions: [500, session()] });
await page.goto(`/reviewer/session?id=${ID}`);
await expect(page.getByText("Could not load the review")).toBeVisible();
await page.getByRole("button", { name: "Try again" }).click();
await expect(page.getByTestId("session-status")).toHaveText("Reviewing");
expect(errors).toEqual([]);
});

test("the PR page links to its latest review session", async ({ page }) => {
const gate = { repo: "acme/web", pr_number: 318, pr_title: "Rate-limit the login form", auto_merge: false, status: "reviewing", group: "active", can_control: true, gate: null };
await mockApi(page, { gate: { ...gate, session_id: ID } });
await page.goto("/reviewer/pr?repo=acme/web&pr=318");
const link = page.getByRole("link", { name: "View session" });
await expect(link).toHaveAttribute("href", new RegExp(`/reviewer/session/?\\?id=${ID}`));
await link.click();
await expect(page.getByTestId("session-status")).toHaveText("Reviewing");

// No session yet: no link.
await mockApi(page, { gate: { ...gate, session_id: "" } });
await page.goto("/reviewer/pr?repo=acme/web&pr=318");
await expect(page.getByRole("heading", { level: 1, name: "Rate-limit the login form" })).toBeVisible();
await expect(page.getByRole("link", { name: "View session" })).toHaveCount(0);
});
3 changes: 2 additions & 1 deletion dashboard/e2e/wide-data/addresses.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { REPO } from "./long";
import { ALERT_ID, DRAFT_ID, PLAN_ID, PLUGIN_ID, PLUGIN_RUN_ID, REVIEW_SK, RUN_ID, SUPPORT_TICKET_ID, TEST_ID, TEST_RUN_KEY, TOOL_NAME } from "./ids";
import { ALERT_ID, DRAFT_ID, PLAN_ID, PLUGIN_ID, PLUGIN_RUN_ID, REVIEW_SK, RUN_ID, SESSION_ID, SUPPORT_TICKET_ID, TEST_ID, TEST_RUN_KEY, TOOL_NAME } from "./ids";

/**
* The query an item's page (`detail: true` in lib/routes.ts) is opened with,
Expand All @@ -12,6 +12,7 @@ export const SAMPLE_QUERY: Record<string, string> = {
// Not an item's page, but its settings form only shows for one repo.
"/reviewer/config": `?repo=${encodeURIComponent(REPO)}`,
"/reviewer/pr": `?repo=${encodeURIComponent(REPO)}&pr=12345`,
"/reviewer/session": `?id=${SESSION_ID}`,
"/tests/run": `?key=${encodeURIComponent(TEST_RUN_KEY)}`,
"/tests/test": `?id=${encodeURIComponent(TEST_ID)}`,
"/plans/detail": `?id=${PLAN_ID}`,
Expand Down
1 change: 1 addition & 0 deletions dashboard/e2e/wide-data/ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { REPO } from "./long";

// The ids the sample addresses point at; the fixtures answer for these.
export const RUN_ID = "01RUNWIDE00000000000000001";
export const SESSION_ID = "3f2c9a1e-5b7d-4c8e-9a0b-1c2d3e4f5a6b";
export const REVIEW_SK = `REVIEW#${REPO}#012345#2026-01-03T10:00:00Z`;
export const TEST_RUN_KEY = "testrun-01WIDE0000000000000001";
export const TEST_ID = "test-01WIDE0000000000000001";
Expand Down
17 changes: 15 additions & 2 deletions dashboard/e2e/wide-data/review.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Handler } from "./index";
import { ALERT_ID, REVIEW_SK, SUPPORT_TICKET_ID, TEST_ID, TEST_RUN_KEY } from "./ids";
import { ALERT_ID, REVIEW_SK, SESSION_ID, SUPPORT_TICKET_ID, TEST_ID, TEST_RUN_KEY } from "./ids";
import {
ARN, BRANCH, EMAIL, FILE, FILE_2, iso, LOG_GROUP, LONG_URL, MARKDOWN, NOTES, ORG, PR_URL, REPO, REPO_2, SENTENCE, SHA, TICKET, TITLE, TOKEN,
} from "./long";
Expand Down Expand Up @@ -308,7 +308,20 @@ export const reviewData: Handler = ({ method, path, url }) => {
if (path === "/api/reviewer/gate") {
return {
repo: url.searchParams.get("repo") ?? REPO, pr_number: Number(url.searchParams.get("pr") ?? 12345), pr_title: TITLE,
auto_merge: true, status: "stopped", group: "waiting", can_control: true, gate: GATE,
session_id: SESSION_ID, auto_merge: true, status: "stopped", group: "waiting", can_control: true, gate: GATE,
};
}
if (path === "/api/reviewer/session") {
return {
id: url.searchParams.get("id") ?? SESSION_ID, repo: REPO, pr_number: 12345, pr_title: TITLE, head_sha: SHA,
trigger: "a_trigger_word_the_dashboard_does_not_know_yet_with_a_long_name", kind: "review", status: "done", in_flight: false,
attempt: 2, started_at: iso(30), finished_at: iso(20), reason: `${SENTENCE} ${TOKEN}`, verdict: "REQUEST_CHANGES", findings: 12,
review_url: `${PR_URL}#pullrequestreview-2201`, check_run_id: 31337000,
steps: [
{ at: iso(30), text: `Started reviewing \`${SHA.slice(0, 7)}\``, detail: "" },
{ at: iso(28), text: `${TITLE} ${TOKEN}`, detail: [FILE, FILE_2, LONG_URL, TOKEN].join("\n") },
{ at: iso(20), text: "Posted the review: 12 findings", detail: SENTENCE },
],
};
}
if (path === "/api/reviewer/org-config") return { instructions: NOTES, web_domains: ["staging.web-platform-frontend-monorepo.example.com", LONG_URL] };
Expand Down
18 changes: 13 additions & 5 deletions dashboard/src/app/reviewer/pr/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Problem } from "@/components/problem";
import { Page, PageHeader, cardClass } from "@/components/page";
import { statusLook, stopperName } from "../status";
import { GATE_ACTION_LABEL } from "../gate-control";
import { isSessionId, sessionHref } from "../session/session";

// One PR's auto-merge: what it waits on, who stopped it, every stop and
// re-arm, and one button to stop or re-arm it. The PR's status comment links
Expand Down Expand Up @@ -154,11 +155,18 @@ function GatePage() {
status={!stopped && (gateState || gate.status) ? <StatusText look={look} /> : undefined}
lead={<span className="[overflow-wrap:anywhere]">{gate.repo} #{gate.pr_number}</span>}
actions={
<a href={prUrl} target="_blank" rel="noreferrer" className={`${secondaryButton} gap-1.5`}>
Open on GitHub
<span aria-hidden="true" className="inline-flex"><ExternalLinkIcon /></span>
<span className="sr-only"> (opens in a new tab)</span>
</a>
<>
{gate.session_id && isSessionId(gate.session_id) && (
<Link href={sessionHref(gate.session_id)} className={secondaryButton}>
View session
</Link>
)}
<a href={prUrl} target="_blank" rel="noreferrer" className={`${secondaryButton} gap-1.5`}>
Open on GitHub
<span aria-hidden="true" className="inline-flex"><ExternalLinkIcon /></span>
<span className="sr-only"> (opens in a new tab)</span>
</a>
</>
}
/>

Expand Down
Loading
Loading