import { afterEach, describe, expect, it, vi } from "vitest"; // P2:真正断言——mock openapi-fetch,验证 client 用正确 baseUrl 初始化。 // createClient 在模块加载时被调用一次,故每个用例 resetModules + 重新 import。 const createClientMock = vi.fn(() => ({ GET: vi.fn(), POST: vi.fn() })); vi.mock("openapi-fetch", () => ({ default: createClientMock, })); afterEach(() => { vi.resetModules(); vi.unstubAllEnvs(); createClientMock.mockClear(); }); describe("api client baseUrl", () => { it("uses NEXT_PUBLIC_API_BASE when set", async () => { vi.stubEnv("NEXT_PUBLIC_API_BASE", "https://api.example.com"); await import("./client"); expect(createClientMock).toHaveBeenCalledTimes(1); expect(createClientMock).toHaveBeenCalledWith({ baseUrl: "https://api.example.com", }); }); it("falls back to localhost when env unset", async () => { vi.stubEnv("NEXT_PUBLIC_API_BASE", undefined); await import("./client"); expect(createClientMock).toHaveBeenCalledWith({ baseUrl: "http://localhost:8000", }); }); it("exposes the constructed client as `api`", async () => { const mod = await import("./client"); expect(mod.api).toBeDefined(); expect(typeof mod.api.GET).toBe("function"); }); });