import { describe, expect, it } from "vitest"; import { CatalogError } from "./errors.js"; import { assertMassActionAllowed, evaluateMassAction, massActionErrors, previewMassAction } from "./mass-action.js"; const policy = { confirmThreshold: 50, hardLimit: 1000 }; describe("previewMassAction", () => { it("returns a bounded sample and the confirmation/limit flags", () => { const items = Array.from({ length: 20 }, (_, index) => index); const preview = previewMassAction({ affected: 1400, items, policy: { confirmThreshold: 50, hardLimit: 5000 } }); expect(preview.affected).toBe(1400); expect(preview.sample).toEqual([0, 1, 2, 3, 4]); expect(preview.requiresConfirmation).toBe(true); expect(preview.exceedsHardLimit).toBe(false); }); it("honours a custom sample size and flags a hard-limit breach", () => { const preview = previewMassAction({ affected: 6000, items: [1, 2, 3], sampleSize: 2, policy: { confirmThreshold: 50, hardLimit: 5000 } }); expect(preview.sample).toEqual([1, 2]); expect(preview.exceedsHardLimit).toBe(true); expect(preview.requiresConfirmation).toBe(false); }); }); describe("evaluateMassAction", () => { it("allows actions at or below the confirmation threshold", () => { expect(evaluateMassAction({ affected: 50, policy })).toEqual({ status: "allowed", affected: 50, confirmed: false }); }); it("requires confirmation above the threshold", () => { expect(evaluateMassAction({ affected: 200, policy })).toEqual({ status: "requires_confirmation", affected: 200, confirmThreshold: 50 }); }); it("allows once the user confirms the exact affected count", () => { expect(evaluateMassAction({ affected: 200, policy, confirmedAffected: 200 })).toEqual({ status: "allowed", affected: 200, confirmed: true }); }); it("re-requires confirmation on scope drift (confirmed count differs)", () => { expect(evaluateMassAction({ affected: 250, policy, confirmedAffected: 200 })).toEqual({ status: "requires_confirmation", affected: 250, confirmThreshold: 50, }); }); it("blocks above the hard limit", () => { expect(evaluateMassAction({ affected: 1001, policy, confirmedAffected: 1001 })).toEqual({ status: "blocked", reason: "exceeds_hard_limit", affected: 1001, hardLimit: 1000, }); }); it("treats an absent hardLimit as no cap", () => { expect(evaluateMassAction({ affected: 1_000_000, policy: { confirmThreshold: 50 }, confirmedAffected: 1_000_000 })).toMatchObject({ status: "allowed", confirmed: true, }); }); }); describe("assertMassActionAllowed", () => { it("returns the decision when allowed", () => { expect(assertMassActionAllowed({ affected: 10, policy })).toMatchObject({ status: "allowed" }); }); it("throws a 409 confirmation-required CatalogError above the threshold", () => { try { assertMassActionAllowed({ affected: 200, policy }); throw new Error("expected throw"); } catch (error) { expect(error).toBeInstanceOf(CatalogError); expect((error as CatalogError).code).toBe(massActionErrors.confirmationRequired.code); expect((error as CatalogError).status).toBe(409); } }); it("throws a 422 exceeds-limit CatalogError above the hard limit", () => { try { assertMassActionAllowed({ affected: 5000, policy }); throw new Error("expected throw"); } catch (error) { expect(error).toBeInstanceOf(CatalogError); expect((error as CatalogError).code).toBe(massActionErrors.exceedsLimit.code); expect((error as CatalogError).status).toBe(422); } }); });