import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; const h = vi.hoisted(() => ({ store: new Map(), callMobileApi: vi.fn() })); vi.mock("next/headers", () => ({ cookies: async () => ({ get: (n: string) => (h.store.has(n) ? { name: n, value: h.store.get(n)! } : undefined), set: (name: string, value: string) => { if (value) h.store.set(name, value); else h.store.delete(name); }, }), })); vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: h.callMobileApi })); import { keepSessionAlive } from "./sessionKeepAlive"; import { resetSessionPolicyCache } from "./sessionPolicy"; import { sealSession } from "@/app/lib/sessionSeal"; import { sessionCookieName } from "@/app/lib/sessionCookieName"; import { SIGNED_IN_ELSEWHERE_COOKIE } from "@/app/lib/signedInElsewhere"; // one-session-per-account: a check-in that reports a click also asks mobile-api whether the session is still live, so // a session ended by a sign-in elsewhere is noticed on the next click even when the click loads nothing. const NOW = 1_800_000_000; function jwt(exp: number): string { return `h.${Buffer.from(JSON.stringify({ sub: "admin1", exp })).toString("base64url")}.s`; } function upstream(sessionCheck: unknown) { h.callMobileApi.mockImplementation(async (path: string) => path === "/api/admin/auth/session-policy" ? { ok: true, data: { idleTtlSeconds: 3600, absoluteTtlSeconds: 28800 } } : path === "/api/admin/auth/session" ? sessionCheck : { ok: true, data: { token: jwt(NOW + 3600) } }, ); } const sessionChecks = () => h.callMobileApi.mock.calls.filter(([p]) => p === "/api/admin/auth/session"); beforeEach(async () => { h.store.clear(); h.callMobileApi.mockReset(); resetSessionPolicyCache(); vi.useFakeTimers(); vi.setSystemTime(NOW * 1000); h.store.set( sessionCookieName(), await sealSession({ token: jwt(NOW + 3000), signedInAt: NOW - 600, lastActivity: NOW - 30, idleTtl: 3600, absoluteExpiry: NOW + 28000 }), ); }); afterEach(() => vi.useRealTimers()); describe("keepSessionAlive: session replaced elsewhere", () => { it("a click on a replaced session ends it and leaves the marker", async () => { upstream({ ok: false, error: "Token revoked: signed in elsewhere", status: 401, code: "SESSION_REPLACED" }); expect(await keepSessionAlive(true)).toBe(false); expect(h.store.has(sessionCookieName())).toBe(false); expect(h.store.get(SIGNED_IN_ELSEWHERE_COOKIE)).toBe("1"); }); it("a click on a session revoked another way ends it without the marker", async () => { upstream({ ok: false, error: "Token revoked", status: 401 }); expect(await keepSessionAlive(true)).toBe(false); expect(h.store.has(sessionCookieName())).toBe(false); expect(h.store.has(SIGNED_IN_ELSEWHERE_COOKIE)).toBe(false); }); it("a click on a live session keeps it", async () => { upstream({ ok: true, data: null }); expect(await keepSessionAlive(true)).toBe(true); expect(sessionChecks()).toHaveLength(1); }); it("an unreachable check does not end the session", async () => { upstream({ ok: false, error: "Unable to reach the server. Please try again." }); expect(await keepSessionAlive(true)).toBe(true); }); it("a timer call does not check the session", async () => { upstream({ ok: false, error: "x", status: 401, code: "SESSION_REPLACED" }); expect(await keepSessionAlive(false)).toBe(true); expect(sessionChecks()).toHaveLength(0); }); }); describe("keepSessionAlive: a timer renewal refused because the account signed in elsewhere", () => { it("ends the session and leaves the marker", async () => { h.callMobileApi.mockImplementation(async (path: string) => path.endsWith("session-policy") ? { ok: true, data: { idleTtlSeconds: 3600, absoluteTtlSeconds: 28800 } } : path === "/api/admin/auth/refresh" ? { ok: false, error: "x", status: 401, code: "SESSION_REPLACED" } : { ok: true, data: null }, ); const store = h.store; const name = sessionCookieName(); const { openSession } = await import("@/app/lib/sessionSeal"); const current = await openSession(store.get(name)); store.set(name, await sealSession({ ...current!, token: jwt(NOW + 100) })); expect(await keepSessionAlive(false)).toBe(false); expect(store.has(name)).toBe(false); expect(store.get(SIGNED_IN_ELSEWHERE_COOKIE)).toBe("1"); }); });