import { describe, expect, it, vi } from "vitest"; import { executeWithGatewayRetry } from "@/lib/gateway-retry"; describe("lib/gateway-retry", () => { it("首次 503 第二次 200 返回 200", async () => { const handler = vi .fn() .mockResolvedValueOnce({ status: 503, body: { error: "down" } }) .mockResolvedValueOnce({ status: 200, body: { ok: true } }); const result = await executeWithGatewayRetry(handler); expect(result.status).toBe(200); expect(result.body).toEqual({ ok: true }); expect(handler).toHaveBeenCalledTimes(2); }); it("连续 503 返回 INTERNAL_ERROR", async () => { const handler = vi.fn().mockResolvedValue({ status: 503, body: null }); const result = await executeWithGatewayRetry(handler); expect(result.status).toBe(503); expect(result.body).toEqual({ code: "INTERNAL_ERROR", message: "服务暂时不可用,请稍后重试", data: null, }); expect(handler).toHaveBeenCalledTimes(2); }); it("首次 200 不重试", async () => { const handler = vi.fn().mockResolvedValue({ status: 200, body: { ok: true } }); const result = await executeWithGatewayRetry(handler); expect(result.status).toBe(200); expect(handler).toHaveBeenCalledTimes(1); }); });