// Live grade. POST /api/run starts the same suites start.py runs; the stream draws each case as it finishes. import { useEffect, useRef, useState } from "react"; import { Icon } from "../lib/icons.jsx"; import { useData } from "../lib/data.jsx"; import { useUi } from "../lib/ui.jsx"; import { CardHead } from "./common.jsx"; import { ResetButton } from "./ResetButton.jsx"; import { tr } from "../lib/i18n.js"; const EMPTY = { seq: 0, running: false, reader: null, model: "", ollama: false, why: "", passed: 0, failed: 0, total: 0, ok: null, error: null, tail: [], suites: [], }; function tally(job) { const passed = job.suites.reduce((a, s) => a + s.passed, 0); const failed = job.suites.reduce((a, s) => a + s.failed, 0); return { ...job, passed, failed }; } export function reduceRun(job, ev) { if (!ev || ev.type === "ping") return job; if (ev.type === "reset") return ev.job || job; const base = job || EMPTY; if (ev.type === "log") return { ...base, tail: [...(base.tail || []), ev.text].slice(-12) }; if (ev.type === "suite") { const suites = base.suites.map((s) => (s.id === ev.id ? { ...s, status: ev.status, total: ev.total ?? s.total, passed: ev.passed ?? s.passed, failed: ev.failed ?? s.failed } : s)); return tally({ ...base, suites }); } if (ev.type === "case") { const suites = base.suites.map((s) => (s.id === ev.suite ? { ...s, passed: ev.passed, failed: ev.failed, cases: [...s.cases, ev.case] } : s)); return tally({ ...base, suites }); } if (ev.type === "done") { return { ...base, running: false, ok: ev.ok, error: ev.error, passed: ev.passed ?? base.passed, failed: ev.failed ?? base.failed, total: ev.total ?? base.total, }; } return base; } function folderFor(label) { return label === "rules" ? "rules" : `llm_${(label || "").replace(":", "_")}`; } function blurb(job, mode, demo) { if (job?.running) { const seen = job.passed + job.failed; const who = demo ? tr("AI model") : job.reader === "rules" ? tr("rule reader") : tr("{model} + rule check", { model: job.model }); return tr("Running with the {who}. {seen} of {total} cases finished.", { who: who, seen: seen, total: job.total }); } if (job?.suites?.length) { if (job.ok) return tr("Finished. {passed} passed{failed}", { passed: job.passed, failed: job.failed ? }), ${job.failed} failed` : ""}.`; return job.error ? tr("Stopped. {error}.", { error: job.error }) : tr("Finished with {failed} failed.", { failed: job.failed }); } if (demo) return tr("Grades the {n} demo events with the AI model. Each case shows up as soon as it finishes.", { n: demo }); if (mode === "both") return "Grades the 15 given events with the model, then checks every reading against the rules. Each case shows up when it finishes."; return "Grades the 15 given events with the rule reader. Each case shows up as soon as it finishes. This does not re-run the event feed."; } export function RunTests() { // Online (demoSize > 0) the grade is the visitor's own: the AI model alone, on the demo's events. const { reload, demoSize } = useData(); const { toast } = useUi(); const [job, setJob] = useState(null); const [mode, setMode] = useState("rules"); const seq = useRef(-1); const sawRun = useRef(false); const feed = useRef(null); const stick = useRef(true); const accept = (data) => { if (!data || typeof data.seq !== "number" || data.seq <= seq.current) return; seq.current = data.seq; setJob(data); }; useEffect(() => { let stop = false; const es = new EventSource("/api/run/stream"); es.addEventListener("state", (e) => { if (!stop) accept(JSON.parse(e.data)); }); es.addEventListener("update", (e) => { if (stop) return; const ev = JSON.parse(e.data); if (typeof ev.seq === "number" && ev.seq <= seq.current) return; if (typeof ev.seq === "number") seq.current = ev.seq; setJob((cur) => reduceRun(cur, ev)); }); const poll = setInterval(async () => { if (stop) return; try { const res = await fetch("/api/run", { cache: "no-store" }); if (res.ok) accept(await res.json()); } catch { /* the stream is the live path; the next poll retries */ } }, 1000); return () => { stop = true; clearInterval(poll); es.close(); }; }, []); useEffect(() => { if (!job) return; if (job.running) sawRun.current = true; else if (sawRun.current) { sawRun.current = false; reload(); } }, [job, job?.running, reload]); const seen = (job?.passed || 0) + (job?.failed || 0); useEffect(() => { const el = feed.current; if (el && stick.current) el.scrollTop = el.scrollHeight; }, [seen]); const start = async () => { const body = demoSize ? { reader: "llm", suites: ["given"] } : mode === "both" ? { reader: "both", suites: ["given"] } : { reader: "rules" }; try { const res = await fetch("/api/run", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); const data = await res.json().catch(() => ({})); if (!res.ok) toast(data.busy ? tr("Someone else is running the tests right now. Try again in a minute.") : data.error || "Could not start the run"); } catch { toast(tr("Could not start the run")); } }; const stop = async () => { try { const res = await fetch("/api/run/stop", { method: "POST" }); if (!res.ok) toast(tr("Could not stop the run")); } catch { toast(tr("Could not stop the run")); } }; const running = !!job?.running; const pct = job?.total ? Math.min(100, Math.round((100 * seen) / job.total)) : 0; const bar = job?.failed ? "var(--bad)" : running ? "var(--accent)" : "var(--good)"; const rows = []; for (const suite of job?.suites || []) { suite.cases.forEach((c, i) => rows.push({ ...c, suite, key: `${suite.id}-${c.reader}-${c.id}-${i}` })); } const last = rows.length - 1; return (
{!demoSize &&
} {running && }
{!!job?.suites?.length && ( <>
{seen} / {job.total}
{running ? "Live" : job.failed ? tr("{failed} failed", { failed: job.failed }) : "Passed"}
{job.suites.map((s) => (
{s.status === "running" && } {s.title} {s.passed + s.failed}/{s.total}
))}
{ const el = e.currentTarget; stick.current = el.scrollHeight - el.scrollTop - el.clientHeight < 48; }}> {rows.map((c, i) => { const open = c.suite.status === "done" || c.suite.status === "failed"; const href = `#/tests/${c.suite.id}/${folderFor(c.reader)}/${c.id}`; return (
{c.verdict === "PASS" ? tr("PASS") : tr("FAIL")} {open ? {c.id} : {c.id}} {demoSize ? tr("AI model") : c.reader} {c.category} {c.suite.title}
); })} {!rows.length &&
{tr("Starting the grader…")}
}
{running && !!job.tail?.length &&
{job.tail[job.tail.length - 1]}
} )}
); }