44 lines
1.7 KiB
TypeScript
44 lines
1.7 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { HttpError } from "@/lib/security/http";
|
|
|
|
const requireAdmin = vi.fn();
|
|
const update = vi.fn();
|
|
const set = vi.fn();
|
|
const where = vi.fn();
|
|
const returning = vi.fn();
|
|
|
|
vi.mock("@/lib/auth/server", () => ({ requireAdmin }));
|
|
vi.mock("@/db", () => ({ getDb: () => ({ update }) }));
|
|
|
|
const { setCommissionAvailability } = await import("./actions");
|
|
|
|
describe("admin commission availability", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
requireAdmin.mockResolvedValue({ user: { id: "admin-1" } });
|
|
update.mockReturnValue({ set });
|
|
set.mockReturnValue({ where });
|
|
where.mockReturnValue({ returning });
|
|
returning.mockResolvedValue([{ isOpen: false }]);
|
|
});
|
|
|
|
it("requires admin authorization", async () => {
|
|
requireAdmin.mockRejectedValueOnce(new HttpError(401, "unauthorized"));
|
|
await expect(setCommissionAvailability(false, "Closed today")).rejects.toMatchObject({ status: 401 });
|
|
expect(update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("requires a closure message and saves the trimmed text", async () => {
|
|
expect(await setCommissionAvailability(false, " ")).toMatchObject({ status: "error" });
|
|
expect(update).not.toHaveBeenCalled();
|
|
expect(await setCommissionAvailability(false, " Closed today ")).toEqual({ status: "saved" });
|
|
expect(set).toHaveBeenCalledWith({ isOpen: false, closureMessage: "Closed today",
|
|
updatedAt: expect.any(Date) });
|
|
});
|
|
|
|
it("reopens without clearing the saved explanation", async () => {
|
|
expect(await setCommissionAvailability(true, "Previous reason")).toEqual({ status: "saved" });
|
|
expect(set).toHaveBeenCalledWith({ isOpen: true, updatedAt: expect.any(Date) });
|
|
});
|
|
});
|