import { describe, expect, it, vi } from "vitest"; import { readServerVersion, withVersionConflictRetry, } from "@/utils/mail-submit-version"; describe("readServerVersion", () => { it("reads integer server_version from error details", () => { expect(readServerVersion({ server_version: 8 })).toBe(8); expect(readServerVersion({ server_version: "9" })).toBe(9); expect(readServerVersion({ server_version: 0 })).toBeNull(); expect(readServerVersion(null)).toBeNull(); }); }); describe("withVersionConflictRetry", () => { it("retries once with server_version after VERSION_CONFLICT", async () => { const submit = vi .fn() .mockResolvedValueOnce({ ok: false, error: { code: "VERSION_CONFLICT", message: "版本冲突,请刷新后重试", details: { server_version: 4 }, }, }) .mockResolvedValueOnce({ ok: true, data: { mail_status: "FAILED", version: 5 }, }); const onVersion = vi.fn(); const res = await withVersionConflictRetry({ version: 3, submit, onVersion, }); expect(submit).toHaveBeenCalledTimes(2); expect(submit).toHaveBeenNthCalledWith(1, 3); expect(submit).toHaveBeenNthCalledWith(2, 4); expect(res.ok).toBe(true); expect(onVersion).toHaveBeenCalledWith(4); expect(onVersion).toHaveBeenCalledWith(5); }); it("does not retry other errors, but still keeps server_version", async () => { const submit = vi.fn().mockResolvedValue({ ok: false, error: { code: "CC_FAILED", message: "写入失败", details: { server_version: 6 }, }, }); const onVersion = vi.fn(); const res = await withVersionConflictRetry({ version: 5, submit, onVersion, }); expect(submit).toHaveBeenCalledTimes(1); expect(res.ok).toBe(false); expect(onVersion).toHaveBeenCalledWith(6); }); });