import { describe, expect, it, vi, beforeEach } from "vitest"; const mockError = vi.hoisted(() => vi.fn()); vi.mock("@/lib/logger", () => ({ logger: { error: mockError }, })); import { wrapAction, actionError } from "./action-helper"; beforeEach(() => { mockError.mockReset(); }); describe("wrapAction", () => { it("returns success with data when fn resolves", async () => { const result = await wrapAction(async () => "hello", "testAction"); expect(result).toEqual({ success: true, data: "hello" }); }); it("returns failure and logs when fn throws", async () => { const result = await wrapAction(async () => { throw new Error("boom"); }, "testAction"); expect(result).toEqual({ success: false, error: "boom" }); expect(mockError).toHaveBeenCalledWith("Action failed: testAction", { action: "testAction", error: "boom", }); }); }); describe("actionError", () => { it("returns failure result and logs", () => { const result = actionError("myAction", "something went wrong"); expect(result).toEqual({ success: false, error: "something went wrong" }); expect(mockError).toHaveBeenCalledWith("Action failed: myAction", { action: "myAction", error: "something went wrong", }); }); });