import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ apiGet: vi.fn(), apiPost: vi.fn() })); vi.mock("@/app/lib/clientApi", () => mocks); import { listEvents, getEvent, createEvent, updateEvent } from "./adminEvents"; const EVENT = { id: "1000000000000001", code: "KL-EVENT-01", name: "KL Pickup Event", salesFrom: "2026-09-01T09:00", salesTo: "2026-09-07T23:59", warehouseId: "2000000000000001", status: "Y" as const, }; beforeEach(() => { mocks.apiGet.mockReset(); mocks.apiPost.mockReset(); }); describe("listEvents", () => { it("GETs /api/events and returns the list", async () => { mocks.apiGet.mockResolvedValue([EVENT]); const result = await listEvents(); expect(mocks.apiGet).toHaveBeenCalledWith("/api/events"); expect(result).toEqual([EVENT]); }); }); describe("getEvent", () => { it("GETs /api/events/{id} and returns the event", async () => { mocks.apiGet.mockResolvedValue(EVENT); const result = await getEvent("1000000000000001"); expect(mocks.apiGet).toHaveBeenCalledWith("/api/events/1000000000000001"); expect(result).toEqual(EVENT); }); }); describe("createEvent", () => { it("POSTs /api/events with ISO-8601 dates and returns the created event", async () => { const input = { code: "KL-EVENT-01", name: "KL Pickup Event", salesFrom: "2026-09-01T09:00", salesTo: "2026-09-07T23:59", venue: "KL Convention Centre", }; mocks.apiPost.mockResolvedValue(EVENT); const result = await createEvent(input); expect(mocks.apiPost).toHaveBeenCalledWith("/api/events", input); expect(result).toEqual(EVENT); }); }); describe("updateEvent", () => { it("POSTs /api/events/{id} with the update payload and returns the updated event", async () => { const input = { name: "KL Pickup Event Updated", salesFrom: "2026-09-01T09:00", salesTo: "2026-09-08T23:59", venue: "KLCC", status: "Y" as const, }; const updated = { ...EVENT, ...input }; mocks.apiPost.mockResolvedValue(updated); const result = await updateEvent("1000000000000001", input); expect(mocks.apiPost).toHaveBeenCalledWith("/api/events/1000000000000001", input); expect(result).toEqual(updated); }); });