usage charts on the shared chart module: my-usage chart + co-op trend tooltips

Both usage views get charts. The co-op trend refactor onto the shared
coop_chart.js module replaces its native <title> tooltips with the panel's
exact-value hover tooltips (registerChartTips contract). NEW: the my-usage
view gets a per-day chart (Spend/Requests metric toggle, 30/90/all-time
period toggle) fed by /broker/usage's new daily series, same shared module,
same tooltip contract. No chart library, no CDN.
CI: .woodpecker.yml (oidc + chart sync pins, both harnesses via the vendored
check-page-js.sh, which splices the module as the app does).
Verified: rendering captured across all chart states matches the panel's
shared-module output; harnesses exercise exact values, zero days, gaps,
clamping, no-refetch toggles.
This commit is contained in:
inference-bot committed 2026-11-13 16:11:23 +00:00
1 parent 622982a371
commit 8c469c1242
6 files changed
+750 -67

No files matched your search

+128
View File
@@ -0,0 +1,128 @@
// Harness for the member dashboard's USAGE CHARTS (my-usage + co-op trend),
// run through the vendored checker:
// bash tests/check-page-js.sh app/main.py dashboard-chart-harness.js
//
// Both charts render through the SHARED module (ops-docs
// scripts/shared/coop_chart.js). Assert the invariants that matter:
// * my-usage chart: bars render from the portal's `daily` series, hidden
// entirely when the member has no usage rows yet; tooltips carry the
// EXACT values through the page's formatters; the 30/90/all toggles
// re-render client-side without refetching;
// * co-op trend: native <title> tooltips are gone (replaced by the shared
// helper), NULL days are stamped with no tooltip (the gap rule);
// * clamping: the tooltip box stays inside the viewport at the edges.
// The checker evals in CommonJS (no top-level await), so the body is wrapped.
const assert = (cond, msg) => { if (!cond) { console.error("FAIL: " + msg); process.exit(1); } };
function mkDaily() {
// 10 days, one with zero usage (a real zero, not an unmeasured gap) and a
// span long enough to exercise the 30/90 slicing.
const rows = [];
for (let i = 0; i < 10; i++) {
rows.push({
day: "2026-11-0" + (i + 1),
spend: i === 4 ? 0 : +(1.25 * (i + 1)).toFixed(2),
requests: i === 4 ? 0 : 10 * (i + 1),
});
}
return rows;
}
(async () => {
// Drive load() with a realistic /api/usage payload.
globalThis.fetch = async () => ({
status: 200,
json: async () => ({
email: "member@example.com", guest: false,
balance: 15, allowance: 15, credits: 0, credits_remaining: 0,
spend: 6.25, all_time_spend: 68.75, remaining: 8.75,
models: [{ model: "greenpt/glm-5.3-flash", spend: 6.25, tokens: 900, calls: 41 }],
key_spend: [], daily: mkDaily(),
coop_models: [],
}),
});
await load();
// ---- my-usage chart ----
{
const block = document.getElementById("mine-chart");
assert(block.style.display === "", "chart block shown when the member has usage rows");
const body = document.getElementById("mine-chart-body").innerHTML;
assert(/<svg /.test(body), "my-usage chart renders an SVG");
assert(!/<title>/.test(body), "native <title> tooltips are gone (shared helper)");
const bars = (body.match(/data-tip-host="mine-chart-body"/g) || []).length;
assert(bars === 10, "one stamped bar per day incl. the zero day (got " + bars + ")");
// EXACT tooltip values: straight from the payload through the page formatters.
const pts = chartTipData["mine-chart-body"];
assert(Array.isArray(pts) && pts.length === 10, "registry: one point per day");
assert(pts[0].label === "2026-11-01", "registry label is the exact day");
assert(pts[0].series[0][1] === "$1.25", "exact spend for 11-01 (got " + pts[0].series[0][1] + ")");
assert(pts[4].series[0][1] === "$0.00", "a zero-usage day reads $0.00, not not-measured");
assert(pts[9].series[0][1] === "$12.50", "exact spend for 11-10 (got " + pts[9].series[0][1] + ")");
// Metric toggle re-renders from the SAME data (no refetch): exact counts.
setMineMetric("requests");
const ptsReq = chartTipData["mine-chart-body"];
assert(ptsReq[0].series[0][1] === "10", "requests tooltip exact (got " + ptsReq[0].series[0][1] + ")");
const bodyReq = document.getElementById("mine-chart-body").innerHTML;
assert((bodyReq.match(/data-tip-host="mine-chart-body"/g) || []).length === 10,
"requests mode still stamps every day");
setMineMetric("spend");
// Period toggles slice client-side: 30 keeps all 10 rows here; verify the
// all-time toggle does not refetch by watching the fetch counter.
let fetches = 0;
globalThis.fetch = async () => { fetches++; return { status: 200, json: async () => ({}) }; };
setMineDays(30);
setMineDays(0);
setMineDays(90);
assert(fetches === 0, "period toggles re-render client-side, never refetch");
assert(document.getElementById("md-all").className.indexOf("active") === -1,
"the 90d button is active again after the round trip");
}
// ---- my-usage chart hidden for a member with no usage ----
{
mineData = [];
renderMineChart();
assert(document.getElementById("mine-chart").style.display === "none",
"no usage rows: the chart block is hidden, not an empty frame");
// restore
mineData = mkDaily();
renderMineChart();
}
// ---- co-op trend (shared module; portal-shaped rows with a NULL day) ----
{
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: null, 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 },
];
setTrendMetric("spend");
setTrendDays(90);
const body = document.getElementById("trend-body").innerHTML;
assert(!/<title>/.test(body), "co-op trend: native <title> tooltips are gone");
assert((body.match(/data-tip-host="trend-body"/g) || []).length === 2,
"gap day carries no bar/stamp (2 measured days of 3)");
const pts = chartTipData["trend-body"];
assert(pts[1].series[0][1] === "not measured", "NULL day registers as not measured");
assert(pts[0].series[0][1] === "$12.34" && pts[2].series[0][1] === "$5.01",
"co-op trend tooltips carry exact spend");
// Tooltip box behavior through the shared helper.
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");
moveChartTip(1430, 890);
const x = parseInt(box.style.left, 10), y = parseInt(box.style.top, 10);
assert(x + 120 <= 1440 && y + 48 <= 900, "clamped inside the viewport (got " + x + "," + y + ")");
hideChartTip();
assert(box.style.display === "none", "tooltip hides off-chart");
}
console.log("DASHBOARD CHART HARNESS OK: shared module tooltips, exact values, gaps, toggles");
})().catch((e) => { console.error("FAIL: " + (e && e.stack || e)); process.exit(1); });