import { describe, it, expect, beforeEach, vi } from "vitest"; // sign-session-cookies (F32): the server session reader only trusts a signed cookie. const h = vi.hoisted(() => ({ store: new Map() })); 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) => { h.store.set(name, value); }, delete: (n: string) => { h.store.delete(n); }, }), })); import { encodeSession } from "@/app/lib/sessionCodec"; import { setSession, readSession } from "./session"; beforeEach(() => h.store.clear()); describe("session cookie signature", () => { it("writes a signed cookie that reads back", async () => { await setSession("jwt-xyz", true); expect(JSON.parse(decodeURIComponent(h.store.get("__Host-colite_admin")!)).sig).toEqual(expect.any(String)); expect((await readSession())?.token).toBe("jwt-xyz"); }); it("treats an edited cookie as signed out", async () => { await setSession("jwt-xyz", true); const obj = JSON.parse(decodeURIComponent(h.store.get("__Host-colite_admin")!)); obj.resetPasswordRequired = false; h.store.set("__Host-colite_admin", encodeURIComponent(JSON.stringify(obj))); expect(await readSession()).toBeNull(); }); it("treats an unsigned cookie as signed out", async () => { await setSession("jwt-xyz", true); const { sig, ...unsigned } = JSON.parse(decodeURIComponent(h.store.get("__Host-colite_admin")!)); expect(sig).toEqual(expect.any(String)); h.store.set("__Host-colite_admin", encodeSession(unsigned)); expect(await readSession()).toBeNull(); }); });