Files
admin-panel/admin-panel-guests-harness.js
T
inference-bot 80acfaed3b oidc.py: single-sourced shared module; guest pill consumes the portal's state
- app/oidc.py is now a byte-identical copy of the canonical module
  (ops-docs: scripts/shared/oidc_client.py); this app's identity (logger
  name, session cookie, session salt) is set via oidc.configure() in
  main.py with the app's historical values, so existing sessions survive.
  tests/test_oidc_sync.py pins the copy to the canonical revision.
- Guests list: the pill CONSUMES the portal-served `state` (the single
  invited-vs-active derivation now lives in the portal and is tested
  there). The local active-flag re-derivation is deleted — the shim is
  first-served-field-wins (state → guest_status), so the previous payload
  shape renders identically during a portal rollback. Guests harness
  extended: invited-wins-over-ledger-active, served expired, and a
  payload with neither field renders NO derived pill text.
2026-11-11 05:23:05 +00:00

171 lines
8.9 KiB
JavaScript

// Harness for the admin panel's GUESTS list (guest accounts), run through the
// skill's checker alongside the chart harness:
// ~/.hermes/skills/research/inference-cooperative/scripts/check-page-js.sh \
// app/main.py admin-panel-guests-harness.js
// Exercises load() against both payload shapes — the portal's split payload
// (guests[]) and an older payload that still tags rows account_type='guest'
// inside members[] — and asserts the invariants that matter:
// * guests NEVER count as members, in either shape;
// * guests never render in the members table;
// * the guests list shows recipient, inviter, credits remaining, expiry
// and status, and hides entirely when there are no guests;
// * the Members-view switch rule keeps the section's visibility handled
// in switchView (the trap that stranded the Admin section on Models).
// The checker evals in CommonJS (no top-level await), so the body is wrapped.
const assert = (cond, msg) => { if (!cond) { console.error("FAIL: " + msg); process.exit(1); } };
const count = (s, re) => (String(s).match(re) || []).length;
const txt = (id) => String(document.getElementById(id).textContent); // stub keeps raw types; the DOM stringifies
const memberRow = (email, spend) => ({
email, username: email.split("@")[0], active: true, activated: true,
account_type: "member", spend, all_time_spend: spend * 10,
balance: 15, credit_balance: 0, credits_remaining: 0, remaining: 15 - spend,
});
async function runLoad(overview) {
globalThis.fetch = async (url) => ({
status: 200,
json: async () => (String(url).indexOf("/api/overview") !== -1 ? overview : { models: [] }),
});
await load();
}
(async () => {
// ---- Shape 1: the portal splits guests into their own list ----
await runLoad({
members: [memberRow("alice@example.com", 2), memberRow("bob@example.com", 1)],
guests: [{
email: "friend@example.com", username: "friend", active: true,
account_type: "guest", guest_inviter: "alice@example.com",
credit_balance: 5, credits_remaining: 3.5,
guest_expires_at: "2027-01-07T02:00:00", guest_status: "active",
spend: 1.5, all_time_spend: 1.5,
}],
totals: { members: 2, total_spend: 4.5, total_all_time_spend: 45,
operator_spend: 1, operator_all_time_spend: 10 },
});
assert(txt("tot-members") === "2",
"split payload: member count is 2 (got " + txt("tot-members") + ")");
assert(txt("tot-guests") === "1",
"split payload: guests count is 1");
assert(txt("tot-spend-cycle") === "$4.50",
"split payload: spend total unchanged by the guests split");
{
const rowsHtml = document.getElementById("rows").innerHTML;
assert(count(rowsHtml, /<tr>/g) === 2, "members table holds only the 2 members");
assert(rowsHtml.indexOf("friend@example.com") === -1, "guest never renders in the members table");
const g = document.getElementById("guest-rows").innerHTML;
assert(count(g, /<tr>/g) === 1, "guests list holds exactly the 1 guest");
assert(g.indexOf("friend@example.com") !== -1, "recipient email shown");
assert(g.indexOf("alice@example.com") !== -1, "inviting member shown from the ledger");
assert(g.indexOf("$3.50") !== -1, "credits remaining shown");
assert(g.indexOf("2027-01-07") !== -1, "time-box expiry date shown");
assert(/pill active[^>]*>active</.test(g), "status pill shows active");
assert(document.getElementById("guests-section").style.display === "",
"guests section visible when guests exist");
}
switchView("models");
assert(document.getElementById("guests-section").style.display === "none",
"guests section hides on the Models view (switchView owns visibility)");
switchView("members");
assert(document.getElementById("guests-section").style.display === "",
"guests section returns on the Members view");
// ---- Shape 2: older payload — guests tagged inside members[] ----
await runLoad({
members: [
memberRow("alice@example.com", 2),
memberRow("bob@example.com", 1),
{ email: "friend@example.com", username: "friend", active: true,
account_type: "guest", guest_inviter: "alice@example.com",
credit_balance: 5, credits_remaining: 3.5, guest_expires_at: "2027-01-07T02:00:00",
guest_status: "active", spend: 0.5, all_time_spend: 5 },
],
totals: { members: 3, total_spend: 3.5, total_all_time_spend: 35,
operator_spend: 0, operator_all_time_spend: 0 },
});
assert(txt("tot-members") === "2",
"old payload: guest row subtracted from the member count (got " + txt("tot-members") + ")");
assert(txt("tot-guests") === "1", "old payload: guest derived and counted");
assert(txt("tot-spend-cycle") === "$3.00",
"old payload: guest spend excluded from the spend total (3.5 - 0.5)");
assert(document.getElementById("rows").innerHTML.indexOf("friend@example.com") === -1,
"old payload: guest still never renders in the members table");
assert(document.getElementById("guest-rows").innerHTML.indexOf("friend@example.com") !== -1,
"old payload: guest renders in the guests list");
// ---- Status vocabulary: expired and converted keep their own pills ----
await runLoad({
members: [memberRow("alice@example.com", 2)],
guests: [
{ email: "lapsed@example.com", username: null, active: false, account_type: "guest",
guest_inviter: "alice@example.com", credit_balance: 2, credits_remaining: 2,
guest_expires_at: "2026-12-01T00:00:00", guest_status: "expired", spend: 3, all_time_spend: 3 },
{ email: "joined@example.com", username: "joined", active: true, account_type: "guest",
guest_inviter: "alice@example.com", credit_balance: 1, credits_remaining: 1,
guest_expires_at: null, guest_status: "converted", spend: 4, all_time_spend: 4 },
],
totals: { members: 1, total_spend: 2, total_all_time_spend: 20,
operator_spend: 0, operator_all_time_spend: 0 },
});
{
const g = document.getElementById("guest-rows").innerHTML;
assert(/pill inactive[^>]*>expired</.test(g), "expired status renders as an inactive-class pill");
assert(/pill org[^>]*>converted</.test(g), "converted status renders as a distinct pill");
assert(g.indexOf("lapsed@example.com") !== -1, "expired guest still listed (status column carries it)");
assert(g.indexOf("2026-12-01") !== -1, "expired guest keeps its expiry date");
}
// ---- The consolidation: the panel CONSUMES the portal's `state` ----
// The portal is the single derivation point (tested there); the panel derives
// nothing from flags. state='invited' wins even though the ledger underneath
// still says active — a provisioned-but-not-set-up guest is never 'active'.
await runLoad({
members: [memberRow("alice@example.com", 2)],
guests: [
{ email: "pending@example.com", username: null, active: true, account_type: "guest",
guest_inviter: "alice@example.com", credit_balance: 2, credits_remaining: 2,
guest_expires_at: "2027-02-01T00:00:00", guest_status: "active", state: "invited",
state_reason: "awaiting setup — invite sent 2026-11-10, not yet accepted",
spend: 0, all_time_spend: 0 },
{ email: "served-expired@example.com", username: null, active: true, account_type: "guest",
guest_inviter: "alice@example.com", credit_balance: 2, credits_remaining: 2,
guest_expires_at: "2026-12-01T00:00:00", state: "expired",
spend: 0, all_time_spend: 0 },
{ email: "flagonly@example.com", username: null, active: true, account_type: "guest",
guest_inviter: "alice@example.com", credit_balance: 2, credits_remaining: 2,
guest_expires_at: "2027-02-01T00:00:00",
spend: 0, all_time_spend: 0 },
],
totals: { members: 1, total_spend: 2, total_all_time_spend: 20,
operator_spend: 0, operator_all_time_spend: 0 },
});
{
const g = document.getElementById("guest-rows").innerHTML;
assert(/pill invited[^>]*>invited</.test(g),
"portal-served state='invited' wins over the ledger's active (never 'active' before setup)");
assert(g.indexOf("awaiting setup") !== -1,
"invited tooltip names the awaiting-setup reason");
assert(/pill inactive[^>]*>expired</.test(g),
"portal-served state='expired' renders without any local derivation");
const flagRow = g.slice(g.indexOf("flagonly@example.com"));
assert(!/>active</.test(flagRow),
"no active flag, no served state: NO pill text is derived from the flag (derivation deleted)");
}
// ---- Empty list: the section disappears entirely ----
await runLoad({
members: [memberRow("alice@example.com", 2)],
guests: [],
totals: { members: 1, total_spend: 2, total_all_time_spend: 20,
operator_spend: 0, operator_all_time_spend: 0 },
});
assert(document.getElementById("guest-rows").innerHTML === "", "no guests: list is empty");
assert(document.getElementById("guests-section").style.display === "none",
"no guests: the section is hidden");
assert(txt("tot-members") === "1", "no guests: member count is the members alone");
assert(txt("tot-guests") === "0", "no guests: counter reads 0");
console.log("GUESTS HARNESS OK: exclusion, both payload shapes, columns, visibility");
})().catch((e) => { console.error("FAIL: " + (e && e.stack || e)); process.exit(1); });