// Harness for the admin panel's MEMBERS table after the allowance/dues change, // run through the checker alongside the other harnesses: // tests/check-page-js.sh app/main.py admin-panel-dues-harness.js // // Invariants asserted (relationships, not the shape of one lucky payload): // * the Allowance cell is (balance - spend) floored at 0 — it does NOT print // the served `balance` (the uniform total) and does NOT print the served // `remaining` credit figure (which also counts purchased credits); // * a member with no readable OC dues renders '—', never the allowance; // * a live OC dues level renders '$/' ('$10/mo'); // * every member row has exactly the header's column count, in BOTH modes — // the offset-reset-dates defect came from a cell/header count mismatch; // * the Dues column stays present in all-time mode (it is not balance-derived), // while Allowance left still carries its allowance-only class. const assert = (cond, msg) => { if (!cond) { console.error("FAIL: " + msg); process.exit(1); } }; const rowsHtml = () => String(document.getElementById("rows").innerHTML); const rowCells = () => rowsHtml().split("").filter(s => s.indexOf(" (s.match(/ Object.assign({ email: "x@example.com", username: "x", active: true, activated: true, account_type: "member", balance: 15, credit_balance: 0, credits_remaining: 0, spend: 0, all_time_spend: 0, remaining: 15, dues_amount: null, dues_frequency: null, }, over); // alice: $15 allowance, $2.50 spent this cycle, $10/mo OC dues, and 10 bought // credits -> the OLD Allowance cell printed balance ($15.00); the OLD // Remaining cell printed $22.50 (allowance + credits - spend). Neither // is "allowance left" ($12.50). // bob: spent MORE than the allowance -> floored at $0.00, and no OC dues. // carol: untouched allowance, $120/yr OC dues (yearly is NOT divided by 12). // dave: no dues row at all (field absent) — an older portal payload must still // render '—' rather than 'undefined' or a blank. const PAYLOAD = { members: [ member({ email: "alice@example.com", username: "alice", spend: 2.5, all_time_spend: 2.5, credit_balance: 10, credits_remaining: 10, remaining: 22.5, dues_amount: 10, dues_frequency: "MONTHLY" }), member({ email: "bob@example.com", username: "bob", spend: 20, all_time_spend: 20, remaining: 0 }), member({ email: "carol@example.com", username: "carol", spend: 0, all_time_spend: 0, dues_amount: 120, dues_frequency: "YEARLY" }), { email: "dave@example.com", username: "dave", active: true, activated: true, account_type: "member", balance: 15, spend: 5, all_time_spend: 5, credit_balance: 0, credits_remaining: 0, remaining: 10 }, ], guests: [], totals: { members: 4, total_spend: 27.5, total_all_time_spend: 27.5, operator_spend: 0, operator_all_time_spend: 0 }, }; async function runLoad() { globalThis.fetch = async (url) => ({ status: 200, json: async () => (String(url).indexOf("/api/overview") !== -1 ? PAYLOAD : { models: [] }), }); await load(); } // Cell text of one rendered row, tags stripped: [4] allowance, [7] dues. const rowOf = (email) => rowsHtml().split("").find(s => s.indexOf(email) !== -1) || ""; const cellsOf = (email) => (rowOf(email).match(/]*>[\s\S]*?(?=)/g) || []) .map(s => s.replace(/<[^>]*>/g, "").trim()); (async () => { await runLoad(); const html = rowsHtml(); // ---- Allowance cell = balance - spend, floored at 0 ---- assert(cellsOf("alice@example.com")[4] === "$12.50", "alice's allowance left is $12.50 (15.00 - 2.50), not her $15.00 balance (got " + cellsOf("alice@example.com")[4] + ")"); assert(cellsOf("bob@example.com")[4] === "$0.00", "bob spent $20 against a $15 allowance: floored at $0.00, never negative (got " + cellsOf("bob@example.com")[4] + ")"); assert(cellsOf("carol@example.com")[4] === "$15.00", "an unspent member reads the full allowance left, $15.00"); assert(cellsOf("dave@example.com")[4] === "$10.00", "dave: $15 - $5 spent = $10.00"); assert(cellsOf("alice@example.com")[4] !== cellsOf("alice@example.com")[5], "the allowance cell is not the Credits cell"); assert(html.indexOf("-$5.00") === -1 && html.indexOf("-5.00") === -1, "no negative allowance is rendered"); // ---- Dues cell ---- assert(cellsOf("alice@example.com")[7] === "$10/mo", "alice's dues render as $10/mo (got " + cellsOf("alice@example.com")[7] + ")"); assert(cellsOf("carol@example.com")[7] === "$120/yr", "carol's dues render as $120/yr — yearly is not divided into months"); assert(cellsOf("bob@example.com")[7] === "—" && cellsOf("dave@example.com")[7] === "—", "members with no readable dues render — (never the allowance)"); assert(html.indexOf("undefined") === -1 && html.indexOf("NaN") === -1, "no dues field renders 'undefined'/'NaN'"); assert(html.indexOf("Remaining") === -1, "the Remaining column no longer renders"); // ---- Column count in balance mode ---- const counts = rowCells(); assert(counts.length === 4, "4 member rows rendered (got " + counts.length + ")"); assert(counts.every(n => n === 10), "every row carries the 10 header columns in balance mode (got " + counts.join(",") + ")"); // ---- Allowance cell keeps its balance-only class; Dues does not ---- assert(html.indexOf('class="num balance-col allowance-only"') !== -1, "Allowance left still carries balance-col allowance-only (hides in all-time)"); assert(html.indexOf('class="num balance-col remaining-col') === -1, "no member cell carries the retired remaining-col class"); assert(/\$10\/mo/.test(html) || /\$10\/mo/.test(html), "the Dues cell carries no balance-col class (stays visible in all-time mode)"); // ---- Sorting by dues: ascending puts $10 before $120, unknown LAST ---- sortMembers("dues_amount"); const asc = rowsHtml(); const iAlice = asc.indexOf("alice@example.com"), iCarol = asc.indexOf("carol@example.com"), iBob = asc.indexOf("bob@example.com"), iDave = asc.indexOf("dave@example.com"); assert(iAlice < iCarol && iCarol < iBob && iCarol < iDave, "dues ascending: $10/mo, then $120/yr, then the two unknown dues members last"); sortMembers("dues_amount"); const desc = rowsHtml(); assert(desc.indexOf("carol@example.com") < desc.indexOf("alice@example.com"), "dues descending starts with the $120/yr member"); assert(desc.indexOf("bob@example.com") > desc.indexOf("alice@example.com"), "unknown dues stay LAST when the sort direction flips"); // ---- All-time mode: column count holds; dues stays, allowance hides ---- setMemberMode("all"); const allHtml = rowsHtml(); const allCounts = rowCells(); assert(allCounts.every(n => n === 10), "all-time mode keeps 10 cells per row — no offset columns (got " + allCounts.join(",") + ")"); assert(allHtml.indexOf("$10/mo") !== -1, "Dues stays visible in all-time mode"); assert(document.getElementById("table-members").parentElement.classList.contains("members-all-time"), "the table's parent carries members-all-time so balance-only columns hide"); assert(document.getElementById("table-members").parentElement.classList.contains("members-all-time"), "the mode class is observable (stub classList)"); console.log("dues/allowance harness: all checks passed"); })();