From 2ae98689667f506a9ca07669493dff37995ef7d5 Mon Sep 17 00:00:00 2001 From: Inferencebot Date: Tue, 17 Nov 2026 09:02:25 +0000 Subject: [PATCH] panel: Allowance shows what's left; Remaining becomes Dues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Members table's Allowance column printed the uniform $15 balance, which is the total, not what the member still has. It now computes max(balance - spend, 0) client-side from the two served fields (no series summed in the browser), keeps its allowance-only class so it still hides in All-time mode, and reads "Allowance left" with a title saying it is a budget, not OC dues. The Remaining column (allowance + credits - spend, a credit figure) is replaced by Dues: the member's current Open Collective contribution level, rendered $10/mo / $120/yr and '—' when the portal has no readable level. Dues sorts by dues_amount with unknowns last, and carries no balance-col, so it stays visible in both modes. tests/check-page-js.sh now stubs classList/parentElement so a harness can observe the mode toggle; admin-panel-dues-harness.js pins the subtraction, floor, dues text, null handling, sort and 10-cell column count in both modes; tests/test_member_table_columns.py pins the header/row alignment. --- admin-panel-dues-harness.js | 136 +++++++++++++++++++++++++++++ app/main.py | 49 +++++++++-- tests/check-page-js.sh | 11 +++ tests/test_member_table_columns.py | 75 ++++++++++++++++ 4 files changed, 264 insertions(+), 7 deletions(-) create mode 100644 admin-panel-dues-harness.js create mode 100644 tests/test_member_table_columns.py diff --git a/admin-panel-dues-harness.js b/admin-panel-dues-harness.js new file mode 100644 index 0000000..301982c --- /dev/null +++ b/admin-panel-dues-harness.js @@ -0,0 +1,136 @@ +// 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"); +})(); diff --git a/app/main.py b/app/main.py index 57f7092..071dd90 100644 --- a/app/main.py +++ b/app/main.py @@ -465,8 +465,7 @@ PAGE_HTML = """ .range-label { font-size: 12px; color: var(--muted); margin-right: 2px; } .range-btn { cursor: pointer; border: 1px solid var(--border); background: #fff; color: var(--muted); border-radius: 8px; font-size: 12px; font-weight: 600; padding: 5px 12px; } .range-btn.active { background: var(--green-mid); color: #fff; border-color: var(--green-mid); } - .members-all-time .balance-col.allowance-only, - .members-all-time .balance-col.remaining-only { display: none; } + .members-all-time .balance-col.allowance-only { display: none; } .members-all-time .credits-col-head .mode-note::after, .table-scroll { overflow-x: auto; border-radius: 12px; } .table-scroll table { min-width: 980px; } @@ -517,10 +516,10 @@ PAGE_HTML = """ Status Activated Source - Allowance + Allowance left Credits Spend - Remaining + Dues Reset Adjust @@ -968,7 +967,10 @@ function setMemberMode(mode) { // must not claim a mode the reader is not in — it said "All-time spend" while // the table was in balance mode (ntnsndr, 2026-11-13). Just "Spend". if (adminLabel) adminLabel.textContent = 'Spend'; - // Balance-only columns (Allowance/Credits/Remaining/Reset) hide in all-time mode + // Balance-only columns (Allowance left/Credits/Reset) hide in all-time mode. + // Dues is NOT balance-derived — it is the member's current OC subscription + // level, so it stays visible in both modes (and its header/cells never carry + // balance-col, which keeps header and row cells in step). $('table-members').parentElement.classList.toggle('members-all-time', mode === 'all'); const adminTable = document.getElementById('table-admins'); if (adminTable) adminTable.parentElement.classList.toggle('admins-all-time', mode === 'all'); @@ -985,6 +987,31 @@ function memberSpend(m) { return memberMode === 'all' ? (m.all_time_spend ?? m.spend ?? 0) : (m.spend ?? 0); } +// Allowance left: what the member can still spend from the uniform monthly +// allowance. ONE subtraction of two served fields — never a series summed in +// the browser. Floored at zero so an overdrawn cycle reads $0.00, not negative. +function allowanceLeft(m) { + return Math.max(Number(m.balance ?? 0) - memberSpend(m), 0); +} + +// Dues: the member's CURRENT Open Collective contribution level, e.g. $10/mo. +// The portal persists dues_amount/dues_frequency from the OC orders its +// reconciliation matched. null means "no live recurring contribution we could +// read" and renders '—' — never the allowance, never a guessed tier. +function duesCell(m) { + const raw = m.dues_amount; + const amt = (raw === null || raw === undefined) ? null : Number(raw); + if (amt === null || !isFinite(amt)) return '—'; + const per = { MONTHLY: 'mo', YEARLY: 'yr', ONETIME: 'once' }[String(m.dues_frequency || '').toUpperCase()]; + const money = '$' + (Number.isInteger(amt) ? String(amt) : amt.toFixed(2)); + return '' + esc(per ? money + '/' + per : money) + ''; +} + +function duesSortValue(m) { + const raw = m.dues_amount; + return (raw === null || raw === undefined) ? null : Number(raw); +} + function sortModels(key) { modelSort = { key, dir: modelSort.key === key ? -modelSort.dir : -1 }; clearArrows('models', modelSort); @@ -1000,6 +1027,14 @@ function renderMembers() { const rows = [...membersData].sort((a, b) => { let va = a[memberSort.key], vb = b[memberSort.key]; if (memberSort.key === 'spend') { va = memberSpend(a); vb = memberSpend(b); } + if (memberSort.key === 'allowance_left') { va = allowanceLeft(a); vb = allowanceLeft(b); } + if (memberSort.key === 'dues_amount') { + va = duesSortValue(a); vb = duesSortValue(b); + // Members with no readable dues sort LAST in either direction — they are + // not a $0 subscription, they are unknown. + if ((va === null) !== (vb === null)) return va === null ? 1 : -1; + va = va ?? 0; vb = vb ?? 0; + } if (memberSort.key === 'active' || memberSort.key === 'activated') { va = va ? 1 : 0; vb = vb ? 1 : 0; } if (typeof va === 'string' || typeof vb === 'string') { return String(va ?? '').localeCompare(String(vb ?? '')) * memberSort.dir; @@ -1059,10 +1094,10 @@ function renderMembers() { '' + statusCell + '' + '' + act + '' + '' + src + '' + - '' + fmt(m.balance) + '' + + '' + fmt(allowanceLeft(m)) + '' + '' + creditsCell + '' + '' + fmt(memberSpend(m)) + '' + - '' + fmt(m.remaining) + '' + + '' + duesCell(m) + '' + '' + esc(reset) + '' + '
' + '' + diff --git a/tests/check-page-js.sh b/tests/check-page-js.sh index b3c46e3..68dd15d 100755 --- a/tests/check-page-js.sh +++ b/tests/check-page-js.sh @@ -83,6 +83,10 @@ const mkEl = (id) => { const el = { id, style: {}, textContent: "", innerHTML: "", className: "", offsetWidth: 120, offsetHeight: 48, + // classList/parentElement, because the tables toggle their mode class on + // the table's PARENT (members-all-time / admins-all-time) and a harness + // must be able to observe that toggle rather than crash on it. + classList: null, parentElement: null, __classes: {}, // Stateful attributes, like a real element: a stub that always answers null // cannot tell "wired once" from "wired on every call". getAttribute: (k) => (k in attrs ? attrs[k] : null), @@ -98,6 +102,13 @@ const mkEl = (id) => { // (buildMetricButtons inserts the SHARED metric list), so the stub has to // support it or the harness cannot exercise that path at all. el.insertAdjacentHTML = (pos, html) => { el.innerHTML += html; }; + el.classList = { + toggle: (c, on) => { el.__classes[c] = (on === undefined ? !el.__classes[c] : !!on); }, + add: (c) => { el.__classes[c] = true; }, + remove: (c) => { el.__classes[c] = false; }, + contains: (c) => !!el.__classes[c], + }; + el.parentElement = { classList: el.classList }; return el; }; globalThis.document = { diff --git a/tests/test_member_table_columns.py b/tests/test_member_table_columns.py new file mode 100644 index 0000000..b31e32a --- /dev/null +++ b/tests/test_member_table_columns.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 +"""Structural guard for the admin panel's MEMBERS table columns. + +WHY THIS EXISTS + A column was removed from the body once while its header stayed, and every + column after it sat under the wrong heading (reset dates rendered under + "Remaining"). The interface must keep header cells and row cells in step, and + the retired "Remaining" column must not creep back. + +Pins: + * the members thead has exactly 10 cells, and the loading row spans 10; + * the headers read "Allowance left" and "Dues", and no "Remaining" header; + * the member row template emits exactly 10 cells; + * Allowance sorts by the computed allowance-left value, Dues by dues_amount; + * the Allowance cell computes `balance - memberSpend`, never `m.balance`. + + python3 tests/test_member_table_columns.py +""" +import pathlib +import re +import sys + +SRC = (pathlib.Path(__file__).resolve().parent.parent / "app" / "main.py").read_text() +FAILS = [] + + +def check(cond, msg): + print((" ok " if cond else " FAIL ") + msg) + if not cond: + FAILS.append(msg) + + +table = SRC[SRC.index(''):] +table = table[:table.index("
")] +thead = table[:table.index("")] +headers = re.findall(r"]*>(.*?)", thead, re.S) +header_text = [re.sub(r"<[^>]*>", "", h).strip() for h in headers] + +check(len(headers) == 10, f"members table has 10 header cells (got {len(headers)})") +check(header_text == ["Member", "Status", "Activated", "Source", "Allowance left", + "Credits", "Spend", "Dues", "Reset", "Adjust"], + f"headers are exactly the expected 10 (got {header_text})") +check("Remaining" not in " ".join(header_text), "no 'Remaining' header remains") +check('colspan="10"' in table, 'the loading row spans all 10 columns') + +check("sortMembers('allowance_left')" in thead, + "Allowance sorts by the computed allowance-left value, not the raw balance") +check("sortMembers('dues_amount')" in thead, + "Dues sorts by dues_amount") +check('class="sortable balance-col allowance-only"' in thead + and 'class="sortable" onclick="sortMembers(\'dues_amount\')"' in thead, + "Allowance keeps balance-col allowance-only; Dues carries no balance-col") + +body = SRC[SRC.index("function renderMembers()"):] +body = body[:body.index("clearArrows('members', memberSort)")] +tds = len(re.findall(r"' cells (got {tds})") +check("remaining-col" not in body, "no member cell carries the retired remaining-col class") +check("fmt(m.balance)" not in body, + "the Allowance cell no longer prints the uniform balance") +check("fmt(m.remaining)" not in body, + "the Remaining cell no longer prints the credit figure") +check("fmt(allowanceLeft(m))" in body, + "the Allowance cell prints allowanceLeft(m)") +check("function allowanceLeft(m)" in SRC + and "Math.max(Number(m.balance ?? 0) - memberSpend(m), 0)" in SRC, + "allowanceLeft is max(balance - spend, 0)") +check("duesCell(m)" in body and "function duesCell(m)" in SRC, + "the Dues cell renders from the portal's dues fields") + +print() +if FAILS: + print(f"member table columns: {len(FAILS)} FAILURE(S)") + sys.exit(1) +print("member table columns: all checks passed")