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
325 changes: 325 additions & 0 deletions dashboard/e2e/reviewer-session-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,325 @@
// Made-up review sessions of a made-up bookstore, for the session page's
// tests and screenshots: one per state the page draws. Every name, path and
// number here is invented.

export const SESSION_ID = "7c1d2e3f-4a5b-4c6d-8e7f-9a0b1c2d3e4f";
export const SHA = "4e1a2b0c9d8f7e6a5b4c3d2e1f0a9b8c7d6e5f4a";
export const REVIEW_SK = "REVIEW#acme/bookstore#000412#2026-11-30T15:04:41Z";
const REPO = "acme/bookstore";

/** Times relative to `now`, so a running review reads as running. */
export function times(now = Date.now()) {
const iso = (secsAgo: number) => new Date(now - secsAgo * 1000).toISOString();
return { iso };
}

export type Json = Record<string, unknown>;

const overview = {
summary: "Lets a shopper pay part of an order with a gift card and the rest by card.",
changes: [
"Checkout takes the gift card's balance off the total before charging the card.",
"A new `gift_card_redemptions` table records each use, so a refund can put the balance back.",
],
files: [
{ path: "src/checkout/giftcard.ts", description: "Reads the card's balance and takes the order's share of it." },
{ path: "src/checkout/total.ts", description: "Subtracts the gift card amount before tax is worked out." },
{ path: "src/checkout/refund.ts", description: "Puts a redeemed amount back on the card when an order is refunded." },
{ path: "db/migrations/0042_gift_card_redemptions.sql", description: "Adds the `gift_card_redemptions` table." },
{ path: "test/checkout/giftcard.test.ts", description: "Covers a partial and a full redemption." },
],
files_total: 5,
};

function activity(iso: (s: number) => string, done: boolean) {
const list: Json[] = [
{
kind: "note",
title: "Checking how the balance is taken",
started_at: iso(150),
ended_at: iso(150),
text: "The balance is read, then written back as a separate step. Two orders paying with one card at once could both see the full balance.",
items: [],
},
{
kind: "search",
title: "Search 3 times",
started_at: iso(146),
ended_at: iso(140),
text: "",
items: [
{ at: iso(146), label: "redeemGiftCard", detail: "", failed: false },
{ at: iso(143), label: "callers of applyBalance", detail: "", failed: false },
{ at: iso(140), label: "FOR UPDATE", detail: "", failed: false },
],
},
{
kind: "view",
title: "View 3 files",
started_at: iso(138),
ended_at: iso(120),
text: "",
items: [
{ at: iso(138), label: "src/checkout/giftcard.ts:60-120", detail: "", failed: false },
{ at: iso(130), label: "src/checkout/refund.ts:1-80", detail: "", failed: false },
{ at: iso(120), label: "src/db/tx.ts:1-40", detail: "", failed: false },
],
},
{
kind: "note",
title: "Checking the rounding of split payments",
started_at: iso(110),
ended_at: iso(110),
text: "The card is charged the total less the gift card amount; both are in cents, so nothing is lost.",
items: [],
},
{
kind: "view",
title: "View 2 files",
started_at: iso(105),
ended_at: iso(96),
text: "",
items: [
{ at: iso(105), label: "src/checkout/total.ts:1-64", detail: "", failed: false },
{ at: iso(96), label: "test/checkout/giftcard.test.ts:1-90", detail: "", failed: false },
],
},
];
if (done) {
list.push({
kind: "comment",
title: "Keep 3 comments",
started_at: iso(60),
ended_at: iso(60),
text: "",
items: [
{ at: iso(60), label: "src/checkout/giftcard.ts:88", detail: "Two orders can spend the same balance", failed: false },
{ at: iso(60), label: "src/checkout/refund.ts:41", detail: "A refund of a split order puts back the whole order", failed: false },
{ at: iso(60), label: "src/checkout/total.ts:22", detail: "Name the cents variable", failed: false },
],
});
}
return list;
}

const steps = (iso: (s: number) => string, done: boolean) => [
{ at: iso(180), text: "Started reviewing `4e1a2b0`", detail: "" },
{ at: iso(170), text: "Read the change: 5 files changed", detail: overview.files.map((f) => f.path).join("\n") },
{ at: iso(160), text: "Reviewing the code", detail: "Against the repository's rules, the team's standards and what earlier reviews taught." },
...(done
? [
{ at: iso(70), text: "Checked the findings: kept 3 of 5", detail: "A second pass drops findings it can't back up." },
{ at: iso(20), text: "Posted the review: changes requested", detail: "" },
]
: []),
];

/** The session of a review that asked for changes, done. */
export function changesRequested(now = Date.now()): Json {
const { iso } = times(now);
return {
id: SESSION_ID,
repo: REPO,
pr_number: 412,
pr_title: "Apply gift card balance at checkout",
head_sha: SHA,
trigger: "push",
kind: "review",
status: "done",
in_flight: false,
attempt: 0,
started_at: iso(180),
finished_at: iso(15),
reason: "",
verdict: "REQUEST_CHANGES",
findings: 3,
review_url: `https://github.com/${REPO}/pull/412#pullrequestreview-7781`,
check_run_id: 52210001,
steps: steps(iso, true),
activity: activity(iso, true),
activity_dropped: 0,
overview,
review_sk: REVIEW_SK,
cost_usd: 0.84,
verification: {
outcome: "incomplete",
checks: [
{ kind: "install", dir: ".", cmd: "npm ci", result: "passed", why: "" },
{ kind: "check", dir: ".", cmd: "npx tsc --noEmit", result: "passed", why: "" },
{ kind: "test", dir: ".", cmd: "npx vitest run test/checkout/giftcard.test.ts", result: "passed", why: "" },
{
kind: "check",
dir: "admin",
cmd: "npm run typecheck",
result: "couldnt_run",
why: "the admin package has no typecheck script",
},
],
note: "",
left_out: ["`e2e/checkout.spec.ts`: end-to-end tests run against a preview, so the pull request's CI result stands for them"],
},
};
}

const reviewBody = `## Summary
Lets a shopper pay part of an order with a gift card. One change holds the merge: two orders paying with the same card at the same moment can both spend its full balance.

- [blocking] src/checkout/giftcard.ts:88 — Two orders can spend the same balance
\`redeemGiftCard\` reads the balance, then writes the new one in a second query. Two checkouts that run together both read \`$50\` and both take it, so the card pays out \`$100\`.

Take the balance in one statement inside the order's transaction:

\`\`\`ts
await tx.query("UPDATE gift_cards SET balance = balance - $1 WHERE id = $2 AND balance >= $1", [amount, card.id]);
\`\`\`
- [medium] src/checkout/refund.ts:41 — A refund of a split order puts back the whole order
The refund credits the order total to the gift card, not the part the card paid. A \`$80\` order paid \`$30\` by gift card gives the card \`$80\` back.
- [nit] src/checkout/total.ts:22 — Name the cents variable
\`n\` holds cents; \`amountCents\` says so.

#### 🧪 Verification
✅ Ran \`npm ci\` in the sandbox: passed.
✅ Ran \`npx tsc --noEmit\` in the sandbox: passed.
✅ Ran \`npx vitest run test/checkout/giftcard.test.ts\` in the sandbox: passed.
`;

/** The review that session posted (`/api/reviewer/review`). */
export function review(now = Date.now()): Json {
const { iso } = times(now);
return {
sk: REVIEW_SK,
repo: REPO,
pr_number: 412,
head_sha: SHA,
verdict: "REQUEST_CHANGES",
risk: "high",
body: reviewBody,
posted_as: "REQUEST_CHANGES",
trigger: "synchronize",
action_summary: "",
thumbs_up: 0,
thumbs_down: 0,
rating_comments: [],
created_at: iso(15),
pr_title: "Apply gift card balance at checkout",
verdict_reason: "Changes requested for 1 blocking finding: two orders can spend the same gift card balance.",
session_id: SESSION_ID,
applied_standards: [],
findings: [
{
severity: "blocking",
kind: "defect",
blocks: true,
file: "src/checkout/giftcard.ts",
line: 88,
title: "Two orders can spend the same balance",
body:
"`redeemGiftCard` reads the balance, then writes the new one in a second query. Two checkouts that run together both read `$50` and both take it, so the card pays out `$100`.\n\nTake the balance in one statement inside the order's transaction:\n\n```ts\nawait tx.query(\"UPDATE gift_cards SET balance = balance - $1 WHERE id = $2 AND balance >= $1\", [amount, card.id]);\n```",
},
{
severity: "medium",
kind: "defect",
blocks: false,
file: "src/checkout/refund.ts",
line: 41,
title: "A refund of a split order puts back the whole order",
body: "The refund credits the order total to the gift card, not the part the card paid. A `$80` order paid `$30` by gift card gives the card `$80` back.",
},
{
severity: "nit",
kind: "suggestion",
blocks: false,
file: "src/checkout/total.ts",
line: 22,
title: "Name the cents variable",
body: "`n` holds cents; `amountCents` says so.",
},
],
};
}

/** The same PR approved on a later commit: nothing holds the merge. */
export function approved(now = Date.now()): Json {
const s = changesRequested(now);
return {
...s,
verdict: "APPROVE",
findings: 1,
review_url: `https://github.com/${REPO}/pull/412#pullrequestreview-7790`,
steps: (s.steps as Json[]).map((st, i, all) => (i === all.length - 1 ? { ...st, text: "Posted the review: approved" } : st)),
verification: {
outcome: "passed",
checks: (changesRequested(now).verification as { checks: Json[] }).checks.slice(0, 3),
note: "",
left_out: [],
},
};
}

export function approvedReview(now = Date.now()): Json {
const r = review(now);
return {
...r,
verdict: "APPROVE",
posted_as: "APPROVE",
risk: "medium",
verdict_reason: "Approved: nothing holds the merge. 1 note is worth a look.",
body: "## Summary\nLets a shopper pay part of an order with a gift card. The balance is now taken in one statement, so two orders can't spend it twice.\n",
findings: [(r.findings as Json[])[2]],
};
}

/** Still reading: no verdict yet, the newest step running. */
export function inProgress(now = Date.now()): Json {
const { iso } = times(now);
const s = changesRequested(now);
return {
...s,
status: "reviewing",
in_flight: true,
finished_at: "",
verdict: "",
findings: null,
review_url: "",
review_sk: null,
overview: null,
verification: null,
cost_usd: null,
steps: steps(iso, false),
activity: activity(iso, false),
};
}

/** Ended without a review. */
export function failed(now = Date.now()): Json {
const s = inProgress(now);
const { iso } = times(now);
return {
...s,
status: "failed",
in_flight: false,
finished_at: iso(30),
reason: "The model stopped answering partway through the review, twice.",
cost_usd: 0.31,
};
}

/** A large PR read in parts: this session read the first part and handed on. */
export function inParts(now = Date.now()): Json {
const { iso } = times(now);
const s = inProgress(now);
return {
...s,
status: "done",
in_flight: false,
finished_at: iso(10),
reason: "Reviewing 4e1a2b0 in 3 parts: 1 done, the next one starts now",
goes_on: true,
steps: [
...(s.steps as Json[]),
{ at: iso(60), text: "Reading part 1 of 3", detail: "38 files (code, tests), the full pass." },
{ at: iso(12), text: "Read this part; the review goes on in its next part: Reviewing 4e1a2b0 in 3 parts: 1 done, the next one starts now", detail: "" },
],
cost_usd: 0.52,
};
}
63 changes: 63 additions & 0 deletions dashboard/e2e/reviewer-session-shots.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { test, type Page, type Route } from "@playwright/test";
import fs from "node:fs";
import path from "node:path";
import { approved, approvedReview, changesRequested, failed, inParts, inProgress, review, SESSION_ID, type Json } from "./reviewer-session-data";

// Screenshots of the review session page in each state, at desktop and phone
// width, for a design review. Runs only when E2E_SHOTS_DIR is set; every API
// call is answered here with a made-up bookstore's review.

const SHOTS_DIR = process.env.E2E_SHOTS_DIR || "";
const LABEL = process.env.SHOTS_LABEL || "after";

async function mock(page: Page, session: Json, rev: Json | null) {
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 p = new URL(route.request().url()).pathname;
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: "member" });
if (p === "/api/usage") return json(route, { month: "2026-11", 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/security/overview") return json(route, { enabled: false, dry_run: true, counts: { open: 0, p0: 0, p1: 0 } });
if (p === "/api/setup") return json(route, { repos: [], new_recommendations: [], auto_setup_new_repos: false });
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/bookstore", enabled: true, ticket_source: "github" }] });
if (p === "/api/aws-connections") return json(route, { connections: [] });
if (p === "/api/reviewer/session") return json(route, session);
if (p === "/api/reviewer/review") return rev ? json(route, rev) : json(route, { error: "nope" }, 404);
return json(route, { error: `Not mocked: ${p}` }, 404);
});
}

async function shot(page: Page, name: string) {
fs.mkdirSync(SHOTS_DIR, { recursive: true });
await page.addStyleTag({ content: "nextjs-portal{display:none!important}" });
for (const width of [1440, 390]) {
await page.setViewportSize({ width, height: width < 640 ? 844 : 900 });
await page.waitForTimeout(300);
await page.screenshot({ path: path.join(SHOTS_DIR, `${LABEL}-${name}-${width}.png`), fullPage: true });
}
}

const STATES: [string, () => Json, () => Json | null][] = [
["changes-requested", () => changesRequested(), () => review()],
["approved", () => approved(), () => approvedReview()],
["in-progress", () => inProgress(), () => null],
["failed", () => failed(), () => null],
["in-parts", () => inParts(), () => null],
];

for (const [name, session, rev] of STATES) {
test(`session page screenshot: ${name}`, async ({ page }) => {
test.skip(!SHOTS_DIR, "Screenshots only when E2E_SHOTS_DIR is set");
await mock(page, session(), rev());
await page.goto(`/reviewer/session?id=${SESSION_ID}`);
await page.getByTestId("review-session").waitFor();
await page.waitForTimeout(800);
await shot(page, name);
});
}
Loading
Loading