import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ keepSessionAlive: vi.fn() })); vi.mock("@/app/lib/server/sessionKeepAlive", () => ({ keepSessionAlive: mocks.keepSessionAlive })); import { POST } from "./route"; function req(body: unknown) { return new Request("http://localhost/api/auth/keepalive", { method: "POST", headers: { "content-type": "application/json" }, body: typeof body === "string" ? body : JSON.stringify(body), }) as unknown as Parameters[0]; } beforeEach(() => mocks.keepSessionAlive.mockReset()); describe("POST /api/auth/keepalive", () => { it("passes real input through as activity and answers 204", async () => { mocks.keepSessionAlive.mockResolvedValue(true); const res = await POST(req({ active: true })); expect(res.status).toBe(204); expect(mocks.keepSessionAlive).toHaveBeenCalledWith(true); }); it("anything but active:true is not activity", async () => { mocks.keepSessionAlive.mockResolvedValue(true); await POST(req({ active: "yes" })); await POST(req("not json")); expect(mocks.keepSessionAlive).toHaveBeenNthCalledWith(1, false); expect(mocks.keepSessionAlive).toHaveBeenNthCalledWith(2, false); }); it("answers 401 when the session has ended", async () => { mocks.keepSessionAlive.mockResolvedValue(false); expect((await POST(req({ active: true }))).status).toBe(401); }); });