// When the swarm UI reads a response as "the login ended". Run by `npm test`. import assert from "node:assert/strict"; import { test } from "node:test"; import { isSessionExpired, mayReload } from "../src/ui/session/session.ts"; const ORIGIN = "https://swarm.example"; test("a gateway 401 on an api address is an ended session", () => { assert.equal(isSessionExpired("/api/agents/status", 401, true, ORIGIN), true); assert.equal( isSessionExpired(`${ORIGIN}/api/jobq/graph`, 401, true, ORIGIN), true, ); }); test("a 401 without the gateway's challenge is the controller's own", () => { assert.equal(isSessionExpired("/api/agents", 401, false, ORIGIN), false); }); test("other statuses and other addresses are not an ended session", () => { assert.equal(isSessionExpired("/api/agents", 403, true, ORIGIN), false); assert.equal(isSessionExpired("/api/agents", 500, true, ORIGIN), false); assert.equal(isSessionExpired("/static/main.js", 401, true, ORIGIN), false); assert.equal( isSessionExpired("https://other.example/api/x", 401, true, ORIGIN), false, ); }); test("a reload is skipped when one just happened", () => { assert.equal(mayReload(null, 1_000_000), true); assert.equal(mayReload(1_000_000, 1_005_000), false); assert.equal(mayReload(1_000_000, 1_010_000), true); }); test("a clock that went backwards does not block the reload", () => { assert.equal(mayReload(2_000_000, 1_000_000), true); });