Files
archie/ui/test/session-retry.test.ts
T
sam 17b2e2a5cb feat(webui): give the dashboard an actionable auth failure state
An unauthenticated document request answered with a plain-text 401 -- and
index.html, the bundle and every asset sit behind requireToken -- so a
bookmarked or bare URL was a dead end: the dashboard's own authentication
banner could never load to explain it. Document requests now get a
self-contained 401 page whose one-field form reuses the ?t= exchange, so a
pasted token sets the cookie and the redirect lands on the dashboard. A
rejected token re-renders the page with the reason instead of answering a
document with text.

API and event-stream callers keep the plain-text 401: classifyActionError maps
it to "session-expired", and an EventSource error body is never seen.

The auth banner gains a Retry that re-runs the reads which report their own
401s, so a recovered session clears it without a document reload. The retry is
extracted to lib/session-retry.ts with an in-flight guard and unit tests.
2026-09-22 07:13:48 +10:00

75 lines
1.8 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
const { createSessionRetry } = await import("../src/lib/session-retry.ts");
test("session retry awaits every load before settling", async () => {
const order: string[] = [];
let release: (() => void) | undefined;
const retry = createSessionRetry([
() => {
order.push("first");
},
() =>
new Promise<void>((resolve) => {
release = () => {
order.push("second");
resolve();
};
}),
]);
const done = retry();
await Promise.resolve();
assert.deepEqual(order, ["first"], "the retry settled before the second read finished");
release?.();
await done;
assert.deepEqual(order, ["first", "second"]);
});
test("session retry ignores a second call while one is running", async () => {
let calls = 0;
let release: (() => void) | undefined;
const retry = createSessionRetry([
() =>
new Promise<void>((resolve) => {
calls += 1;
release = resolve;
}),
]);
const first = retry();
const second = retry();
release?.();
await Promise.all([first, second]);
assert.equal(calls, 1, "a double click re-ran the reads");
});
test("session retry runs again after a failure", async () => {
let calls = 0;
const retry = createSessionRetry([
() => {
calls += 1;
if (calls === 1) throw new Error("daemon unavailable");
},
]);
await assert.rejects(retry(), /daemon unavailable/);
await retry();
assert.equal(calls, 2, "a failed retry left the guard latched");
});
test("session retry runs again after it settles", async () => {
let calls = 0;
const retry = createSessionRetry([
() => {
calls += 1;
},
]);
await retry();
await retry();
assert.equal(calls, 2);
});