import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; import { apiGet, ApiError } from "./clientApi"; let assign: ReturnType; function stubFetch(status: number, body: unknown) { vi.stubGlobal( "fetch", vi.fn().mockResolvedValue({ ok: status >= 200 && status < 300, status, text: async () => JSON.stringify(body), } as Response), ); } function stubLocation(pathname: string) { assign = vi.fn(); vi.stubGlobal("window", { location: { assign, pathname } }); } beforeEach(() => stubLocation("/account")); afterEach(() => vi.unstubAllGlobals()); describe("clientFetch phone-verification redirect", () => { it("403 with phoneVerificationRequired redirects to /register/verify and throws ApiError", async () => { stubFetch(403, { error: "Phone verification required.", phoneVerificationRequired: true }); await expect(apiGet("/api/member/profile")).rejects.toBeInstanceOf(ApiError); expect(assign).toHaveBeenCalledWith("/register/verify"); }); it("does not redirect again when already on /register/verify, still throws", async () => { stubLocation("/register/verify"); stubFetch(403, { error: "Phone verification required.", phoneVerificationRequired: true }); await expect(apiGet("/api/member/profile")).rejects.toBeInstanceOf(ApiError); expect(assign).not.toHaveBeenCalled(); }); it("403 without the flag does not redirect", async () => { stubFetch(403, { error: "Forbidden" }); await expect(apiGet("/api/member/profile")).rejects.toBeInstanceOf(ApiError); expect(assign).not.toHaveBeenCalled(); }); it("401 still redirects to /login", async () => { stubFetch(401, { error: "Not authenticated" }); await expect(apiGet("/api/member/profile")).rejects.toBeInstanceOf(ApiError); expect(assign).toHaveBeenCalledWith("/login"); }); });