import { describe, it, expect } from "vitest"; import { NextRequest } from "next/server"; import { middleware } from "./middleware"; import { sealSession } from "@/app/lib/sessionSeal"; // sign-session-cookies (F32): the route guard reads only a signed session cookie. const SESSION = { token: "jwt", resetPasswordRequired: false, absoluteExpiry: 4102444800 }; function page(cookie: string): NextRequest { return new NextRequest(new URL("/dashboard", "https://portal.test"), { headers: { cookie: "__Host-colite_admin=" + cookie } }); } describe("middleware: signed session cookie", () => { it("lets a signed-in visitor through", async () => { const res = await middleware(page(await sealSession(SESSION))); expect(res.headers.get("x-middleware-next")).toBe("1"); }); it("sends a visitor with an edited cookie to login", async () => { const obj = JSON.parse(decodeURIComponent(await sealSession(SESSION))); obj.token = "forged"; const res = await middleware(page(encodeURIComponent(JSON.stringify(obj)))); expect(res.headers.get("location")).toContain("/login"); }); it("sends a visitor with an unsigned cookie to login", async () => { const res = await middleware(page(encodeURIComponent(JSON.stringify(SESSION)))); expect(res.headers.get("location")).toContain("/login"); }); });