import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn() })); vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi })); import { POST } from "./route"; function req(body: unknown) { return new Request("http://localhost/api/auth/forgot-password", { method: "POST", body: typeof body === "string" ? body : JSON.stringify(body), }) as unknown as Parameters[0]; } beforeEach(() => { mocks.callMobileApi.mockReset(); }); describe("POST /api/auth/forgot-password", () => { it("forwards trimmed username and email with no token and answers ok", async () => { mocks.callMobileApi.mockResolvedValue({ ok: true, data: { result: "true" } }); const res = await POST(req({ username: " jane ", email: " jane@example.com ", extra: "x" })); expect(res.status).toBe(200); expect(await res.json()).toEqual({ ok: true }); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/auth/forgot-password", { method: "POST", body: { username: "jane", email: "jane@example.com" }, }); }); it.each([ ["404", { ok: false, error: "nope", status: 404 }], ["500", { ok: false, error: "boom", status: 500 }], ["network failure", { ok: false, error: "Unable to reach the server." }], ])("answers 200 ok whatever upstream returns (%s)", async (_n, upstream) => { mocks.callMobileApi.mockResolvedValue(upstream); const res = await POST(req({ username: "jane", email: "jane@example.com" })); expect(res.status).toBe(200); expect(await res.json()).toEqual({ ok: true }); }); it("drops non-string values and does not call upstream for missing ones", async () => { const res = await POST(req({ username: { a: 1 }, email: 5 })); expect(res.status).toBe(200); expect(await res.json()).toEqual({ ok: true }); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("answers 200 ok on an unparseable body", async () => { const res = await POST(req("not json")); expect(res.status).toBe(200); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); });