import { afterEach, describe, expect, it, vi } from "vitest"; import { getCaptcha, login } from "@/api/client"; describe("admin auth client", () => { afterEach(() => vi.restoreAllMocks()); it("loads a captcha challenge without an access token", async () => { const fetchMock = vi.fn().mockResolvedValueOnce(jsonResponse(200, { captchaEnabled: true, captchaId: "captcha-1", image: "data:image/svg+xml;base64,PHN2Zy8+", expiresIn: 120, })); vi.stubGlobal("fetch", fetchMock); await expect(getCaptcha()).resolves.toMatchObject({ captchaId: "captcha-1", captchaEnabled: true }); expect(fetchMock).toHaveBeenCalledWith("/api/admin/auth/captcha", expect.objectContaining({ credentials: "include" })); }); it("sends the captcha fields with login", async () => { const fetchMock = vi.fn().mockResolvedValueOnce(jsonResponse(200, { token: "access-token", accessToken: "access-token", expiresIn: 900, user: { id: "admin-1", email: "admin@example.com", name: "Admin", role: "admin" }, })); vi.stubGlobal("fetch", fetchMock); await login("admin@example.com", "password", "captcha-1", "ABCD", true); expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({ email: "admin@example.com", password: "password", captchaId: "captcha-1", captchaCode: "ABCD", rememberMe: true, }); }); }); function jsonResponse(code: number, data: unknown) { return { status: code, json: async () => ({ code, msg: "success", data }) }; }