import { beforeEach, describe, expect, it, vi } from "vitest"; const prismaMock = vi.hoisted(() => ({ alertLog: { create: vi.fn(), }, })); vi.mock("@/lib/prisma", () => ({ prisma: prismaMock })); import { writeAlert } from "@/modules/alert/service"; describe("writeAlert", () => { beforeEach(() => { vi.clearAllMocks(); prismaMock.alertLog.create.mockResolvedValue({ id: BigInt(1) }); }); it("写入 alert_log 成功", async () => { await writeAlert("RPA_FAILED", { quoteId: "QTE_001", cargoHash: "abc123", detail: { reason: "timeout" }, }); expect(prismaMock.alertLog.create).toHaveBeenCalledWith({ data: expect.objectContaining({ alertType: "RPA_FAILED", quoteId: "QTE_001", cargoHash: "abc123", status: "open", }), }); }); it("DB 写失败不抛异常", async () => { prismaMock.alertLog.create.mockRejectedValue(new Error("db down")); const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {}); await expect( writeAlert("SECURITY", { detail: { reason: "test" } }), ).resolves.toBeUndefined(); expect(consoleSpy).toHaveBeenCalled(); consoleSpy.mockRestore(); }); });