Files
admin-panel/tests/money-chart-harness.js
T
inference-bot 2904a4c2f8 money chart: drop the caveat caption and the history-start legend note
The operator removed all caveats from the finance chart. Gone: the
money-caption element, the caveat/stale/errors caption rendering, the
.legend 'history begins' suffix and the orphaned CSS. Chart, legend keys,
series and toggles untouched; balance_history_start stays in the payload.
Harness now asserts no caveat text renders in any mode.
2026-11-08 01:55:23 +00:00

103 lines
5.1 KiB
JavaScript

// 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"]) {
if (!legend.includes(frag)) throw new Error("legend missing: " + frag);
}
// No caveat anywhere, in any rendering state — even though the fixture still
// carries the payload's caveat field, which the panel must ignore.
function assertNoCaveat() {
const rendered = $("money-body").innerHTML + " " + $("money-legend").innerHTML;
for (const bad of ["Balance history begins", "history begins", "Open Collective API exposes", "money-caption"]) {
if (rendered.includes(bad)) throw new Error("caveat/caption text rendered: " + bad);
}
}
assertNoCaveat();
// 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");
renderMoney();
assertNoCaveat();
// --- 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");