import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), getToken: vi.fn() })); vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi })); vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken })); import { GET } from "./route"; const req = (url: string) => new Request(url) as unknown as Parameters[0]; const ctx = (path?: string[]) => ({ params: Promise.resolve({ path }) }); beforeEach(() => { mocks.callMobileApi.mockReset(); mocks.getToken.mockReset().mockResolvedValue("jwt"); }); describe("GET /api/emails/[[...path]]", () => { it("returns 401 without a session", async () => { mocks.getToken.mockResolvedValue(null); expect((await GET(req("http://localhost/api/emails"), ctx())).status).toBe(401); }); it("forwards the list query", async () => { mocks.callMobileApi.mockResolvedValue({ ok: true, data: { content: [] } }); await GET(req("http://localhost/api/emails?recipient=s&page=0&size=20"), ctx()); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/emails?recipient=s&page=0&size=20", { token: "jwt" }); }); it("forwards the detail id", async () => { mocks.callMobileApi.mockResolvedValue({ ok: true, data: { id: 7 } }); await GET(req("http://localhost/api/emails/7"), ctx(["7"])); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/emails/7", { token: "jwt" }); }); it("refuses dot segments", async () => { expect((await GET(req("http://localhost/api/emails/.."), ctx([".."]))).status).toBe(400); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("passes a refusal through with its code", async () => { mocks.callMobileApi.mockResolvedValue({ ok: false, status: 403, error: "Only a super admin can read this email", code: "SUPER_ADMIN_REQUIRED" }); const res = await GET(req("http://localhost/api/emails/3"), ctx(["3"])); expect(res.status).toBe(403); expect(await res.json()).toMatchObject({ code: "SUPER_ADMIN_REQUIRED" }); }); });