Files
admin-panel/admin-panel-chart-harness.js
T
inference-admin c3f5a326c2
ci/woodpecker/push/woodpecker Pipeline was successful
fix: the panel served a page whose script never ran
The metric buttons were built with an inline handler inside PAGE_HTML:

    + " onclick=\"setTrendMetric('" + def.id + "')">" + ...

In a plain triple-quoted Python string `\"` is just `"`, so the SERVED JS read
    + " onclick="setTrendMetric('" + ...
— the string closes at `onclick=` and the rest is a syntax error. The whole
script died at that line: no fetches, no tables, no charts. Just the shell.

Two failures compounded:
1. The line itself (a Python-escape mistake, from a patch script that turned
   `\\"` into `\"`).
2. The gate certified it. tests/check-page-js.sh sliced PAGE_HTML out of the
   SOURCE TEXT between the triple quotes, so it checked the escapes as typed
   while the app served them INTERPRETED — a page nobody was ever served.
   node --check on that text says OK.

Fixes:
* buildMetricButtons() emits data-metric attributes and the row gets ONE
  delegated click listener. No inline handlers, so no quote escape can break
  the page again — and the wiring is testable.
* The gate now reads PAGE_HTML with ast.literal_eval: the string Python
  actually serves. Proof it works: with the new gate, the pre-fix source FAILS
  node --check for both apps; the fixed tree passes.
* The stub element gained stateful attributes and addEventListener/__fire, so
  the harness can prove a click reaches setTrendMetric (and that a miss does
  nothing).
* The chart harness's "series name is the chart title" assertion now compares
  against the shared definition instead of a title that no longer exists.

Found by rendering the served page in a real browser, which is the only check
that could have caught it: the console said
  Uncaught SyntaxError: Unexpected identifier 'setTrendMetric' @802:24
2026-11-13 21:14:03 +00:00

116 lines
6.6 KiB
JavaScript

// Harness for the admin panel's page JS. Run through the skill's checker —
// ~/.hermes/skills/research/inference-cooperative/scripts/check-page-js.sh \
// app/main.py admin-panel-chart-harness.js
// which evals stub + page JS + this file in ONE node scope, so the assertions
// below exercise the page's REAL functions (renderTrends, registerChartTips,
// showChartTip, moveChartTip) against a fixture shaped like the portal's
// /admin/metrics payload. Assert invariants, never the shape you happened to
// observe; when a check fails here, suspect this file first.
const assert = (cond, msg) => { if (!cond) { console.error("FAIL: " + msg); process.exit(1); } };
// Fixture: oldest-first daily rows, with a NULL day (unmeasured) to exercise
// the gap rule (NULL renders as a gap, never a zero — and gets no tooltip).
trendsData = [
{ day: "2026-11-01", members_total: 71, spend_usd: 12.34, requests: 2900, energy_wh: 8400.5, co2e_g: 2100.25 },
{ day: "2026-11-02", members_total: 72, spend_usd: null, requests: 3100, energy_wh: null, co2e_g: null },
{ day: "2026-11-03", members_total: 74, spend_usd: 5.01, requests: 1204, energy_wh: 9100, co2e_g: 2300 },
{ day: "2026-11-04", members_total: 75, spend_usd: 7.89, requests: 980, energy_wh: 6400, co2e_g: 1600 },
];
// ---- Bars (spend): every measured day is hoverable, gaps are not ----
trendMetric = "spend";
trendDays = 30;
renderTrends();
{
const host = document.getElementById("trend-body");
const pts = chartTipData["trend-body"];
assert(Array.isArray(pts) && pts.length === 4, "registry: one point per row (got " + (pts && pts.length) + ")");
assert(pts[0].label === "2026-11-01", "registry label is the row's exact day");
// The tooltip's series name is the SHARED definition's title (coop_chart.js
// line ~373: a single-series metric labels itself with def.title). Asserting
// the invariant, not the string that happened to be there before the port.
assert(pts[0].series[0][0] === metricDef("spend").title,
"the tooltip series name is the shared definition's title (got " + pts[0].series[0][0] + ")");
// EXACT values: the tooltip string must be the data value through the
// panel's own formatter — never a pixel-derived estimate.
assert(pts[0].series[0][1] === "$12.34", "exact spend for 11-01 (got " + pts[0].series[0][1] + ")");
assert(pts[2].series[0][1] === "$5.01", "exact spend for 11-03 (got " + pts[2].series[0][1] + ")");
assert(pts[3].series[0][1] === "$7.89", "exact spend for 11-04 (got " + pts[3].series[0][1] + ")");
assert(pts[1].series[0][1] === "not measured", "NULL day registers as not measured");
const marks = host.innerHTML.match(/data-tip-i="(\d+)"/g) || [];
const idxs = marks.map(s => parseInt(s.match(/\d+/)[0], 10)).sort((a, b) => a - b);
assert(JSON.stringify(idxs) === "[0,2,3]", "bars stamped only on measured days (got " + JSON.stringify(idxs) + ")");
assert((host.innerHTML.match(/data-tip-host="trend-body"/g) || []).length === 3, "every bar carries its host id");
assert(!/<title>/.test(host.innerHTML), "native <title> tooltips are gone (replaced by the shared helper)");
// Tooltip content for a hovered bar: exact date + exact value.
showChartTip("trend-body", 0, 500, 300);
const box = globalThis.__lastAppended;
assert(box && box.className === "chart-tip", "tooltip box created on first show");
assert(box.innerHTML.indexOf("2026-11-01") !== -1, "tooltip shows the exact date");
assert(box.innerHTML.indexOf("$12.34") !== -1, "tooltip shows the exact value");
assert(box.style.display === "block", "tooltip visible while over a point");
// Viewport clamping: near the right/bottom edges the box flips and lands
// fully inside the viewport; near the origin it clamps to the margin.
moveChartTip(1430, 890); // stub box is 120x48, viewport 1440x900
const x1 = parseInt(box.style.left, 10), y1 = parseInt(box.style.top, 10);
assert(x1 + 120 <= 1440 && y1 + 48 <= 900, "clamped inside the viewport (got " + x1 + "," + y1 + ")");
moveChartTip(0, 0);
assert(parseInt(box.style.left, 10) >= 4 && parseInt(box.style.top, 10) >= 4, "clamped to the top-left margin");
hideChartTip();
assert(box.style.display === "none", "tooltip hides off-chart");
}
// ---- Line (members): circles stamped, same registry contract ----
trendMetric = "members";
renderTrends();
{
const host = document.getElementById("trend-body");
const marks = (host.innerHTML.match(/data-tip-i="(\d+)"/g) || []).map(s => parseInt(s.match(/\d+/)[0], 10)).sort((a, b) => a - b);
assert(JSON.stringify(marks) === "[0,1,2,3]", "line points stamped on every measured day (got " + JSON.stringify(marks) + ")");
const pts = chartTipData["trend-body"];
assert(pts[1].series[0][1] === "72", "exact member count for 11-02 (got " + pts[1].series[0][1] + ")");
}
// ---- Multi-series contract (the shape the finance chart will register) ----
{
registerChartTips("finance-demo", [
{ label: "2026-11-04", series: [["Revenue", "$40.00"], ["Spend", "$12.34"]] },
]);
showChartTip("finance-demo", 0, 20, 20);
const box = globalThis.__lastAppended;
assert(box.innerHTML.indexOf("Revenue") !== -1 && box.innerHTML.indexOf("$40.00") !== -1, "multi-series: every series at the point is shown");
assert(box.innerHTML.indexOf("$12.34") !== -1, "multi-series: second series value shown");
}
// ---- Wiring: the shared delegated listeners are registered once ----
{
const L = globalThis.__listeners || {};
assert(!!L["pointerover"] && !!L["pointermove"] && !!L["pointerout"], "pointer listeners registered for all charts");
assert(!!L["pointercancel"], "touch cancel hides the tooltip");
}
// ---- the metric buttons are DELEGATED, so prove a click reaches setTrendMetric ----
// (An inline onclick inside PAGE_HTML is where a quote escape silently kills the
// whole page: it happened, and it shipped, because the gate read the raw source
// text instead of the string Python serves. Delegation removes that trap — and
// this is the check that the wiring still works.)
{
buildMetricButtons();
const rowEl = document.getElementById("trend-metrics");
assert(!!(rowEl.__handlers && rowEl.__handlers.click),
"the metric row has a delegated click listener");
setTrendMetric("members");
const fired = rowEl.__fire("click", { target: { closest: () => ({ getAttribute: () => "money" }) } });
assert(trendMetric === "money",
"clicking a metric button selects that metric (got " + trendMetric + ")");
setTrendMetric("members");
rowEl.__fire("click", { target: { closest: () => null } });
assert(trendMetric === "members", "a click that misses a button is ignored");
}
console.log("HARNESS OK: chart tooltips (bars, line, gaps, exact values, clamping, multi-series)");
process.exit(0);