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
103 changes: 102 additions & 1 deletion dashboard/e2e/learning.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test, type Page, type Route } from "@playwright/test";
import fs from "node:fs";
import path from "node:path";
import { learningFixture } from "./wide-data/learning";
import { learningFixture, learningHealthFixture } from "./wide-data/learning";
import { pickOption } from "./select";

// The Learning page: what Coderhelm learned from outcomes, the behavior it
Expand All @@ -22,6 +22,8 @@ interface Backend {
pinned: string;
weights: Record<string, number>;
calls: { method: string; path: string; body: unknown }[];
/** The health check's answer, or its error status; left out, a gateway without it (404). */
health?: unknown;
}

async function mockApi(page: Page, init: Partial<Backend> = {}): Promise<Backend> {
Expand All @@ -44,6 +46,12 @@ async function mockApi(page: Page, init: Partial<Backend> = {}): Promise<Backend
const data = learningFixture({ repo: REPO, repo2: REPO_2, text: "Ask for a test of the retry path in the payments client.", file: "src/payments/client.ts", canManage: db.canManage, empty: db.empty, reverted: db.reverted, pinned: db.pinned, weights: db.weights });
return json(route, { ...data, days: Number(url.searchParams.get("days") || 30), repo: url.searchParams.get("repo") || "" });
}
if (p === "/api/learning/health" && method === "GET") {
db.calls.push({ method, path: p, body });
if (db.health === undefined) return json(route, { error: "Not found" }, 404);
if (typeof db.health === "number") return json(route, { error: "Internal error" }, db.health);
return json(route, db.health);
}
if (p.startsWith("/api/learning/")) {
db.calls.push({ method, path: p, body });
if (db.writeStatus !== 200) return json(route, {}, db.writeStatus);
Expand Down Expand Up @@ -440,3 +448,96 @@ test("examples show a skeleton while another repository loads", async ({ page })
release();
await expect(page.locator("#exemplars").getByTestId("list-skeleton")).toHaveCount(0);
});

// ── Learning health: is each loop still learning? ─────────────────────────

test("a quiet loop turns the health check red and says what to check", async ({ page }) => {
await mockApi(page, { health: learningHealthFixture({ repo: REPO, text: "", quiet: ["pr_outcomes"], idle: ["coder_lessons", "stop_outcomes"] }) });
await page.goto("/learning");
const health = page.getByTestId("learning-health");
await expect(health).toHaveAttribute("data-state", "red");
await expect(health.getByText("1 loop stopped learning")).toBeVisible();

// The red loop comes first, in plain words: what it learns from, when it last learned, what to check.
const red = page.getByTestId("loop-pr_outcomes");
await expect(health.locator("li[data-state]").first()).toHaveAttribute("data-testid", "loop-pr_outcomes");
await expect(red).toHaveAttribute("data-state", "quiet");
await expect(red).toContainText("Not learning");
await expect(red).toContainText("Learns from: Whether Coderhelm's pull requests were merged as written");
await expect(red).toContainText(/Last learned: .*ago/);
await expect(red).toContainText("3 of 5 this week");
await expect(red).toContainText(`2 closed pull request(s) were never scored: ${REPO}#12345`);
await expect(page.getByTestId("loop-pr_outcomes-check")).toContainText("What to check: Pull requests closed this week were never scored.");

// An idle loop had nothing to learn from: no red, never learned.
const idle = page.getByTestId("loop-coder_lessons");
await expect(idle).toHaveAttribute("data-state", "idle");
await expect(idle).toContainText("Nothing to learn from yet");
await expect(idle).toContainText("Last learned: never");
await expect(idle).toContainText("0 this week");
await expect(idle).toContainText("Learns from: Repeated setbacks in one area of the code");
await expect(page.getByTestId("loop-coder_lessons-check")).toHaveCount(0);

// Titles are the dashboard's own words, never the API's names.
await expect(red.getByRole("heading")).toHaveText("What happens to Coderhelm's pull requests");
await expect(health.getByText("Outcomes of CoderHelm's pull requests")).toHaveCount(0);
await expect(health.getByText(/CoderHelm/)).toHaveCount(0);
await expect(page.getByTestId("loop-review_outcomes")).toContainText("4 of 6 this week");
await expect(page.getByTestId("loop-review_outcomes").getByRole("heading")).toHaveText("What people do with review comments");

// A working loop with no expected count shows its count alone.
const ok = page.getByTestId("loop-calibration");
await expect(ok).toContainText("Learning");
await expect(ok).toContainText("4 this week");
await expect(health.locator("li[data-state]")).toHaveCount(9);
await noSideways(page);
fs.mkdirSync(SHOTS_DIR, { recursive: true });
await page.locator("#health").screenshot({ path: path.join(SHOTS_DIR, "learning-health-desktop.png") });
});

test("every loop learning reads as fine, with no red", async ({ page }) => {
await mockApi(page, { health: learningHealthFixture({ repo: REPO, text: "", quiet: [], idle: ["stop_outcomes"] }) });
await page.goto("/learning");
const health = page.getByTestId("learning-health");
await expect(health).toHaveAttribute("data-state", "ok");
await expect(health).toContainText("Every loop that had something to learn from learned from it.");
await expect(health.getByText(/stopped learning/)).toHaveCount(0);
await expect(page.getByTestId("loop-stop_outcomes")).toContainText("Nothing to learn from yet");
await expect(health.getByText("What to check:")).toHaveCount(0);
});

test("a health check that fails says so and retries, and the rest of the page still loads", async ({ page }) => {
const db = await mockApi(page, { health: 500 });
await page.goto("/learning");
await expect(page.getByText("Couldn't check the learning loops")).toBeVisible();
await expect(page.getByTestId("tile-events")).toBeVisible();
db.health = learningHealthFixture({ repo: REPO, text: "" });
await page.getByRole("button", { name: "Try again" }).click();
await expect(page.getByTestId("learning-health")).toHaveAttribute("data-state", "red");
});

test("a gateway without the health check shows no health section", async ({ page }) => {
await mockApi(page);
await page.goto("/learning");
await expect(page.getByTestId("tile-events")).toBeVisible();
await expect(page.getByRole("heading", { name: "Is it still learning?" })).toHaveCount(0);
});

test("the health check fits a phone at 375 wide", async ({ page }) => {
await page.setViewportSize({ width: 375, height: 844 });
await mockApi(page, { health: learningHealthFixture({ repo: "your-team/payments-reconciliation-ledger-service", text: "packages/payments-reconciliation-ledger/src/adapters/stripe/webhooks/handlers/charge-refunded-partially.ts" }) });
await page.goto("/learning");
await expect(page.getByTestId("loop-pr_outcomes-check")).toBeVisible();
await noSideways(page);
fs.mkdirSync(SHOTS_DIR, { recursive: true });
await page.locator("#health").screenshot({ path: path.join(SHOTS_DIR, "learning-health-phone.png") });
});

test("a loop this dashboard has no title for reads as its id in words", async ({ page }) => {
await mockApi(page, { health: learningHealthFixture({ repo: REPO, text: "", quiet: [], extra: true }) });
await page.goto("/learning");
const extra = page.getByTestId("loop-planner_lessons");
await expect(extra.getByRole("heading")).toHaveText("Planner lessons");
await expect(page.getByText("Planner lessons (API name)")).toHaveCount(0);
await expect(extra).toContainText("1 of 2 this week");
});
181 changes: 181 additions & 0 deletions dashboard/e2e/run-draft-hold.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import { expect, test, type Page, type Route } from "@playwright/test";
import fs from "node:fs";
import path from "node:path";

// A run that finished with its pull request still a draft (`draft_hold`) says
// "Finished, PR held" at the top, with the summary and each reason with what
// to do. A run waiting out GitHub's hourly limit (`github_wait`) says when it
// goes on, and that nothing is needed. Every API call is answered here.

const SHOTS_DIR = process.env.E2E_SHOTS_DIR || path.join("test-results", "shots");

const now = Date.now();
const iso = (minsAgo: number) => new Date(now - minsAgo * 60_000).toISOString();

type Run = Record<string, unknown> & { run_id: string; status: string };

const SECURITY = {
what: "Security items are open under Open items",
todo: "Fix them on this branch (Coderhelm checks again on the next push), or delete each security line once a person has checked it.",
};
const SANDBOX = {
what: "The sandbox could not run the end-to-end tests",
todo: "Add the missing BASE_URL to the repository's test settings, then push to the branch.",
};

function run(over: Partial<Run> = {}): Run {
return {
run_id: "01RUNHOLD",
status: "needs_input",
ticket_source: "jira",
ticket_id: "ABC-9",
title: "Add rate limits to the sign-up form",
repo: "your-team/web",
pr_url: "https://github.com/your-team/web/pull/318",
pr_number: 318,
current_pass: "review",
tokens_in: 1200,
tokens_out: 300,
duration_s: 900,
created_at: iso(60),
updated_at: iso(5),
pass_history: [
{ pass: "triage", started_at: iso(60) },
{ pass: "implement", started_at: iso(50) },
{ pass: "pr", started_at: iso(20) },
{ pass: "review", started_at: iso(10) },
],
progress_notes: [],
waiting_on: { action: "Review pull request #318", detail: "It stays a draft until the items below are dealt with.", url: "https://github.com/your-team/web/pull/318", since: iso(5) },
draft_hold: {
kind: "held",
summary: `Finished, but pull request #318 stays a draft: ${SECURITY.what}. ${SECURITY.todo}`,
reasons: [SECURITY, SANDBOX],
pr_number: 318,
since: iso(5),
},
...over,
};
}

async function mockApi(page: Page, r: Run) {
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 url = new URL(route.request().url());
const p = url.pathname;
if (p === "/api/me") return json(route, { user_id: "u_1", team_id: "t_1", github_login: "pat", email: "pat@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/teams") return json(route, { teams: [{ team_id: "t_1", org: "your-team", status: "active", current: true }] });
if (p === "/api/repos") return json(route, { repos: [{ name: "your-team/web", enabled: true, ticket_source: "jira" }] });
if (p === "/api/plugins/catalog") return json(route, { plugins: [] });
const m = p.match(/^\/api\/runs\/([^/]+)(?:\/(.+))?$/);
if (m) {
if (decodeURIComponent(m[1]) !== r.run_id) return json(route, { error: "No run with this id in the team.", code: "not_found" }, 404);
const action = m[2];
if (!action) return json(route, r);
if (action === "openspec") return json(route, {});
if (action === "traces") return json(route, { traces: [] });
if (action === "agent-log") return json(route, { passes: [], live_events: [] });
if (action === "asks") return json(route, { asks: [] });
}
return json(route, { error: `Not mocked: ${p}` }, 404);
});
}

async function noSideways(page: Page) {
const overflow = await page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth);
expect(overflow, "the page scrolls sideways").toBeLessThanOrEqual(0);
}

async function shot(page: Page, name: string) {
fs.mkdirSync(SHOTS_DIR, { recursive: true });
await page.screenshot({ path: path.join(SHOTS_DIR, `${name}.png`), fullPage: true });
}

test("a held run reads Finished, PR held, with each reason and what to do", async ({ page }) => {
await mockApi(page, run());
await page.goto("/runs/detail?id=01RUNHOLD");

const outcome = page.getByTestId("run-outcome");
await expect(outcome).toBeVisible();
await expect(outcome).toHaveAttribute("data-tone", "waiting");
await expect(outcome.getByText("Finished, PR held", { exact: true })).toBeVisible();
// The status beside the title says the same, not "Needs your answer".
await expect(page.getByText("Finished, PR held", { exact: true })).toHaveCount(2);
await expect(page.getByText("Needs your answer")).toHaveCount(0);

await expect(page.getByTestId("draft-hold-summary")).toHaveText(/pull request #318 stays a draft: Security items are open/);
const reasons = page.getByTestId("draft-hold-reasons");
await expect(reasons.getByRole("listitem").filter({ hasText: SECURITY.what })).toContainText(`What to do: ${SECURITY.todo}`);
await expect(reasons.getByRole("listitem").filter({ hasText: SANDBOX.what })).toContainText(`What to do: ${SANDBOX.todo}`);
await expect(reasons).toContainText("Held since");
await expect(outcome.getByRole("link", { name: /Open pull request #318/ })).toHaveAttribute("href", "https://github.com/your-team/web/pull/318");
// The named wait under it is the same pull request; the hold says it once.
await expect(page.getByText("Review pull request #318")).toHaveCount(0);
await shot(page, "run-draft-hold");
});

test("a pull request GitHub refused to mark ready reads as needing attention", async ({ page }) => {
await mockApi(page, run({
status: "completed",
waiting_on: null,
draft_hold: { kind: "failed", summary: "Finished, but GitHub refused every try to mark pull request #318 ready.", reasons: [{ what: "GitHub refused to mark it ready (403)", todo: "Mark it ready on GitHub, or check the GitHub App's permissions on the repository." }], pr_number: 318, since: null },
}));
await page.goto("/runs/detail?id=01RUNHOLD");
const outcome = page.getByTestId("run-outcome");
await expect(outcome).toHaveAttribute("data-tone", "attention");
await expect(outcome).toContainText("Finished, PR held: GitHub refused to mark it ready");
await expect(outcome).toContainText("What to do: Mark it ready on GitHub");
await expect(outcome).not.toContainText("Held since");
// Not "Done. The change is ready for review."
await expect(page.getByText("Done. The change is ready for review.")).toHaveCount(0);
});

test("a run with no hold still reads as before", async ({ page }) => {
await mockApi(page, run({ status: "completed", waiting_on: null, draft_hold: null }));
await page.goto("/runs/detail?id=01RUNHOLD");
await expect(page.getByTestId("run-outcome")).toContainText("Done. The change is ready for review.");
await expect(page.getByText("Finished, PR held")).toHaveCount(0);
});

test("a run waiting for GitHub's hourly limit says when it goes on", async ({ page }) => {
await mockApi(page, run({
status: "running",
current_pass: "implement",
waiting_on: null,
draft_hold: null,
github_wait: "Waiting for GitHub's hourly limit to reset at 21:05 UTC; continues by itself",
}));
await page.goto("/runs/detail?id=01RUNHOLD");
const outcome = page.getByTestId("run-outcome");
await expect(outcome).toHaveAttribute("data-tone", "waiting");
await expect(outcome).toContainText("Waiting for GitHub");
await expect(page.getByTestId("github-wait")).toHaveText("Waiting for GitHub's hourly limit to reset at 21:05 UTC; continues by itself. Nothing is needed from you.");
await expect(page.getByText("Coderhelm is working on this")).toHaveCount(0);
});

test("a finished run ignores a stale GitHub wait", async ({ page }) => {
await mockApi(page, run({ status: "completed", waiting_on: null, draft_hold: null, github_wait: "Waiting for GitHub's hourly limit to reset at 21:05 UTC; continues by itself" }));
await page.goto("/runs/detail?id=01RUNHOLD");
await expect(page.getByTestId("run-outcome")).toContainText("Done. The change is ready for review.");
await expect(page.getByTestId("github-wait")).toHaveCount(0);
});

test("the hold and the GitHub wait fit a phone at 375 wide", async ({ page }) => {
await page.setViewportSize({ width: 375, height: 844 });
const long = "packages/payments-reconciliation-ledger/src/adapters/stripe/webhooks/handlers/charge-refunded-partially.ts";
await mockApi(page, run({ draft_hold: { kind: "held", summary: `Finished, but pull request #318 stays a draft: ${long}`, reasons: [{ what: long, todo: `Fix ${long} on branch coderhelm/abc-9-add-rate-limits-to-the-sign-up-form-and-the-password-reset-form` }], pr_number: 318, since: iso(5) } }));
await page.goto("/runs/detail?id=01RUNHOLD");
await expect(page.getByTestId("draft-hold-reasons")).toBeVisible();
await noSideways(page);
await shot(page, "run-draft-hold-phone");

await page.unrouteAll({ behavior: "ignoreErrors" });
await mockApi(page, run({ run_id: "01RUNWAIT", status: "running", waiting_on: null, draft_hold: null, github_wait: `Waiting for GitHub's hourly limit to reset at 21:05 UTC; continues by itself ${long}` }));
await page.goto("/runs/detail?id=01RUNWAIT");
await expect(page.getByTestId("github-wait")).toBeVisible();
await noSideways(page);
});
Loading
Loading