money chart: cumulative OC revenue, token spend, and OC balance
New Money tab beside Trends, fed by the portal's /admin/money-series (hourly-cached server-side). One fetch at full range; period (30/90/all) and spend-mode (cumulative/daily) toggles re-render client-side. - Cumulative mode: three lines — Revenue (OC contributions, green), Token spend (blue), OC balance (amber). NULL balance days render as a gap (the OC API exposes only the CURRENT balance; history accrues from daily snapshots from the deploy date onward — stated in the caption and in the balance legend key). - Daily mode: token spend per day as bars (spend-only; the other series are cumulative and would need a second axis). - Money ticks use the 1/2/5 x 10^k rule; tooltips reuse the shared chart-tip helper (all three series listed per day). - The caption is a deliberate exception to the no-caption chart rule: the operator asked for the honest data-history limits in the UI. - Harness: tests/money-chart-harness.js (axis invariants, gap handling, daily bars, period slice, single-point balance).
This commit is contained in:
1 parent
bdb5a9c6eb
commit
90294df4c6
2 files changed
+343
-1
No files matched your search
@@ -0,0 +1,91 @@
|
||||
// Money-chart harness: exercises the real renderMoney 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 observed shape.
|
||||
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;
|
||||
}
|
||||
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(),
|
||||
};
|
||||
moneyLoaded = true;
|
||||
|
||||
// --- cumulative mode (default) ---
|
||||
renderMoney();
|
||||
let svg = $("money-body").innerHTML;
|
||||
const count = (re) => (svg.match(re) || []).length;
|
||||
const polys = count(/<polyline /g);
|
||||
const rects = count(/<rect /g);
|
||||
if (polys !== 2) throw new Error("expected 2 cumulative polylines (revenue+spend; balance is single-point), got " + polys);
|
||||
if (rects !== 0) throw new Error("cumulative mode must not draw bars, got " + rects);
|
||||
if (!count(/<circle cx="[^"]+" cy="[^"]+" r="3" fill="#a97a2a"/)) throw new Error("single balance point marker missing");
|
||||
if (!count(/data-tip-host="money-body"/)) throw new Error("hover targets missing");
|
||||
const legend = $("money-legend").innerHTML;
|
||||
for (const frag of ["Revenue — OC contributions (cumulative)", "Token spend — cumulative", "OC balance — history begins 2026-11-08"]) {
|
||||
if (!legend.includes(frag)) throw new Error("legend missing: " + frag);
|
||||
}
|
||||
if ($("money-caption").textContent !== moneyData.caveat) throw new Error("caption not the caveat");
|
||||
|
||||
// Axis: tick labels bottom-first — values ascend while their y descends; the
|
||||
// top tick must clear the data max. Date labels share text-anchor="end" with
|
||||
// the y labels, so filter dash-shaped strings before numeric parsing.
|
||||
const texts = [...svg.matchAll(/<text x="([^"]+)" y="([^"]+)"[^>]*>([^<]*)<\/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");
|
||||
|
||||
// --- daily mode: spend bars only, legend relabels, no lines ---
|
||||
setMoneySpendMode("daily");
|
||||
svg = $("money-body").innerHTML;
|
||||
const rects2 = (svg.match(/<rect /g) || []).length;
|
||||
if (rects2 !== 66) throw new Error("daily mode: expected 66 bars, got " + rects2);
|
||||
if ((svg.match(/<polyline /g) || []).length !== 0) throw new Error("daily mode must draw no lines");
|
||||
if (!$("money-legend").innerHTML.includes("Token spend — daily")) throw new Error("legend not relabelled in daily mode");
|
||||
if ($("money-legend").innerHTML.includes("OC balance")) throw new Error("balance must not be legend-listed in daily mode");
|
||||
|
||||
// --- period slice: 30 days → 30 bars, no refetch ---
|
||||
setMoneyPeriod(30);
|
||||
svg = $("money-body").innerHTML;
|
||||
const rects3 = (svg.match(/<rect /g) || []).length;
|
||||
if (rects3 !== 30) throw new Error("30-day slice: expected 30 bars, got " + rects3);
|
||||
setMoneyPeriod(90);
|
||||
setMoneySpendMode("cum");
|
||||
|
||||
// --- balance-only deploy-day shape: one point renders as a marker, no crash ---
|
||||
moneyData.series = [{ day: "2026-11-08", revenue_cum: null, spend_day: null, spend_cum: null, oc_balance: 1160.25 }];
|
||||
renderMoney();
|
||||
const svg2 = $("money-body").innerHTML;
|
||||
if (svg2.indexOf("trend-empty") === -1 && svg2.indexOf("<circle") === -1 && svg2.indexOf("<polyline") === -1) {
|
||||
throw new Error("single-row series rendered nothing");
|
||||
}
|
||||
moneyData.series = mkSeries();
|
||||
|
||||
console.log("MONEY CHART HARNESS: ALL CHECKS PASSED");
|
||||
Reference in new issue
Block a user