Files
admin-panel/admin-panel-invite-harness.js
T
inference-bot aabee20534 Guest-invite form: outcome shown in the card, double-submit guard server-side
The operator could never tell what a submit did: the only outcome message was
the top flash, off-screen from the mid-page form, and a portal refusal left
the fields filled with nothing visible where the operator was looking.

- invite card now carries its own persistent status line (same palette as
  .flash): success 'Invite sent to <email> — expires <date>.' with the fields
  cleared; any refusal/failure shown plainly with the fields KEPT.
- button held disabled ('Sending…') for the whole request, restored after.
- flash(): clear the previous timer so a message shown soon after another is
  not hidden by the first message's timeout.
- SERVER-SIDE duplicate guard (guest_duplicate_refusal, pure + unit-tested):
  /api/provision-guest refuses 409 before any portal call when an ACTIVE
  guest for the email exists ('A guest account for <email> already exists')
  or a PENDING request awaits the decision ('awaits activation'). Holds for
  any client; the portal's own one-guest-per-recipient guard stays behind it.
- admin-panel-invite-harness.js pins the whole behavior.
2026-11-08 03:12:30 +00:00

109 lines
6.0 KiB
JavaScript

// Harness for the admin panel's GUEST-INVITE form (sendGuestAccess), run via
// the skill's checker:
// ~/.hermes/skills/research/inference-cooperative/scripts/check-page-js.sh \
// app/main.py admin-panel-invite-harness.js
// The operator could never tell what a submit did (no clearing, no message),
// so the harness pins the behavior the fix promises:
// * the button is DISABLED for the whole request ("Sending…") — a double
// click cannot fire a second invite mid-flight;
// * SUCCESS: the fields are cleared and the card states
// 'Invite sent to <email> — expires <date>.' (and the guests list reloads);
// * REFUSAL (duplicate guard): the error is shown plainly in the card and
// the fields are KEPT (no silent swallow, no accidental re-clear);
// * the button is re-enabled afterwards in every path.
const assert = (cond, msg) => { if (!cond) { console.error("FAIL: " + msg); process.exit(1); } };
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const txt = (id) => String(document.getElementById(id).textContent);
const cls = (id) => String(document.getElementById(id).className);
const val = (id) => String(document.getElementById(id).value);
function typeInvite(email, name) {
document.getElementById("guest-email").value = email;
document.getElementById("guest-name").value = name || "";
}
// Responder queue: each fetch consumes the next entry — a function that,
// given the resolved body, returns the response object {status, json}.
// `during` runs INSIDE the fetch promise — the moment the request is in
// flight — to observe the form state.
let responders = [];
let duringFlight = null;
let loadCalled = 0;
globalThis.fetch = async (url, opts) => {
if (String(url).indexOf("/api/overview") !== -1) {
loadCalled++;
return { status: 200, json: async () => ({ members: [], guests: [], totals: { members: 0 } }) };
}
const body = JSON.parse((opts && opts.body) || "{}");
const r = responders.shift();
if (duringFlight) duringFlight();
return r(body);
};
const ok200 = (payload) => ({ status: 200, ok: true, json: async () => payload });
const bad = (status, payload) => ({ status, ok: false, json: async () => payload });
(async () => {
// ---- SUCCESS: fields cleared, dated success message, button held+released --
responders = [() => ok200({ status: "provisioned", email: "friend@example.com", expires_in_days: 60 })];
typeInvite("friend@example.com", "Friend");
let sawDisabled = false;
duringFlight = () => {
sawDisabled = document.getElementById("guest-send").disabled === true;
};
await sendGuestAccess({ preventDefault() {} });
duringFlight = null;
assert(sawDisabled, "button is disabled while the request is in flight");
assert(txt("guest-send") === "Send invite", "button label restored after success (got " + txt("guest-send") + ")");
assert(val("guest-email") === "", "email field cleared after success (got " + val("guest-email") + ")");
assert(val("guest-name") === "", "name field cleared after success (got " + val("guest-name") + ")");
const okMsg = txt("guest-invite-status");
assert(okMsg.indexOf("Invite sent to friend@example.com") === 0,
"success message names the recipient (got " + JSON.stringify(okMsg) + ")");
assert(okMsg.indexOf("— expires ") !== -1 && okMsg.endsWith("."),
"success message carries an expiry date (got " + JSON.stringify(okMsg) + ")");
assert(cls("guest-invite-status").indexOf("ok") !== -1, "success status styled ok");
assert(cls("flash").indexOf("ok") !== -1, "top flash also reports success");
assert(loadCalled >= 1, "the guests list reloads after a successful invite");
// ---- REFUSAL (duplicate guard): error shown plainly, fields KEPT -----------
responders = [() => bad(409, { error: "A guest account for friend@example.com already exists — no second account was created" })];
typeInvite("friend@example.com", "Friend");
await sendGuestAccess({ preventDefault() {} });
const errMsg = txt("guest-invite-status");
assert(errMsg.indexOf("A guest account for friend@example.com already exists") === 0,
"duplicate refusal shown verbatim in the card (got " + JSON.stringify(errMsg) + ")");
assert(cls("guest-invite-status").indexOf("err") !== -1, "refusal styled as an error");
assert(val("guest-email") === "friend@example.com", "email KEPT on refusal");
assert(val("guest-name") === "Friend", "name KEPT on refusal");
assert(txt("guest-send") === "Send invite", "button re-enabled after refusal");
assert(txt("flash").indexOf("already exists") !== -1, "top flash carries the refusal too");
// ---- HTTP-level failure (no error body): message, fields kept --------------
responders = [() => bad(502, {})];
typeInvite("other@example.com", "");
await sendGuestAccess({ preventDefault() {} });
assert(txt("guest-invite-status").indexOf("Could not send guest access") === 0,
"transport failure reported in the card (got " + JSON.stringify(txt("guest-invite-status")) + ")");
assert(val("guest-email") === "other@example.com", "email kept on transport failure");
// ---- Network error: reported, fields kept ----------------------------------
responders = [() => { throw new Error("boom"); }];
typeInvite("third@example.com", "");
await sendGuestAccess({ preventDefault() {} });
assert(txt("guest-invite-status").indexOf("Guest access failed: boom") === 0,
"network failure reported in the card (got " + JSON.stringify(txt("guest-invite-status")) + ")");
assert(val("guest-email") === "third@example.com", "email kept on network error");
// ---- Missing email: refused locally, nothing sent --------------------------
responders = []; // any fetch here would be an unwanted POST
typeInvite("", "");
await sendGuestAccess({ preventDefault() {} });
assert(txt("guest-invite-status").indexOf("Enter the guest's email") === 0,
"blank email refused locally (got " + JSON.stringify(txt("guest-invite-status")) + ")");
assert(responders.length === 0, "no request fired for a blank email");
console.log("invite harness: all assertions passed");
})().catch((e) => { console.error("FAIL: " + (e && e.stack || e)); process.exit(1); });