// Money-chart harness: exercises the REAL shared renderer through the panel's // renderTrends() with the money metric selected, against a realistic fixture // (spend from 09-04, revenue from 09-06, balance only on the last day — the // deploy-day shape). Asserts chart invariants, not the shape we happened to see. // // WHAT CHANGED AND WHY THIS HARNESS IS DIFFERENT // Money used to be its own tab with its own renderers (renderMoney, // renderMoneyCumulative, renderMoneyDaily, renderMoneyLegend) and its own // period/spend-mode toggles. It is now one metric among the others, defined in // the shared module (CoOpChart.CHART_METRICS) and drawn by the one shared // renderer — the same chart the member dashboard draws. So this harness drives // the metric the way the page does, and asserts the invariants that must hold // wherever the chart is drawn. function mkSeries() { const rows = []; const start = new Date(Date.UTC(2026, 8, 4)); // 2026-09-04 for (let i = 0; i < 66; i++) { const d = new Date(start.getTime() + i * 86400000); const key = d.toISOString().slice(0, 10); const rev = key >= "2026-09-06" ? 100 + i * 2 : 0; // cumulative climbs rows.push({ day: key, revenue_cum: rev, spend_day: 5 + i, spend_cum: (5 + i) * (i + 1) / 2, oc_balance: i === 65 ? 1160.25 : null, }); } return rows; } function mkMetrics() { return mkSeries().map((r, i) => ({ day: r.day, members_total: 100 + i, requests: 10 + i, spend_usd: 1.5 + (i % 7), energy_wh: 40 + i, co2e_g: 12 + i, operator_spend_usd: 0.5, })); } moneyData = { caveat: "Balance history begins 2026-11-08: the Open Collective API exposes only the current balance.", balance_history_start: "2026-11-08", balance_current: { value: 1160.25, currency: "USD" }, errors: [], series: mkSeries(), }; trendsData = mkMetrics(); trendDays = 0; const count = (re) => (svg.match(re) || []).length; // --- the metric buttons come from the SHARED list (Money included) --- buildMetricButtons(); for (const def of CHART_METRICS) { if (!$("tm-" + def.id)) throw new Error("metric button missing for the shared metric: " + def.id); } if (!CHART_METRICS.some(d => d.id === "money")) { throw new Error("the shared metric list must carry Money — it is one of the charts now"); } // --- money metric: cumulative lines, exact tooltips, gap-safe balance --- trendMetric = "money"; renderTrends(); svg = $("trend-body").innerHTML; const polys = count(/]*>([^<]*)<\/text>/g)].map(m => ({ x: m[1], y: m[2], t: m[3] })); const yTicks = texts.filter(o => !/^\d{2}-\d{2}$/.test(o.t) && o.t.startsWith("$")); if (yTicks.length !== 5) throw new Error("expected 5 y ticks, got " + yTicks.length + ": " + JSON.stringify(yTicks)); const vals = yTicks.map(o => parseFloat(o.t.replace(/[$,]/g, ""))); for (let k = 1; k < vals.length; k++) { if (!(vals[k] > vals[k - 1])) throw new Error("tick values not ascending bottom-first: " + vals); if (!(parseFloat(yTicks[k].y) < parseFloat(yTicks[k - 1].y))) throw new Error("tick positions not descending"); } if (vals[vals.length - 1] < 1828) throw new Error("top tick " + vals[4] + " does not clear max cumulative 1828"); const xTicks = texts.filter(o => /^\d{2}-\d{2}$/.test(o.t)); if (xTicks.length !== 3) throw new Error("expected 3 date marks, got " + xTicks.length); // Zero tick must sit lowest. const ys = yTicks.map(o => parseFloat(o.y)); if (Math.min.apply(null, ys) !== Math.max.apply(null, ys) && ys[0] !== Math.max.apply(null, ys)) throw new Error("zero tick not lowest"); // --- the daily token-spend view is now the shared `spend` metric --- trendMetric = "spend"; renderTrends(); svg = $("trend-body").innerHTML; const bars = count(/