import { describe, expect, it } from "vitest"; import type { AuthUser } from "@/auth/types"; import { canApproveContractStep } from "./permissions"; const withPositionType = (typeKey: string): AuthUser => ({ employee: [{ positions: [{ positionType: { key: typeKey } }] }], }); const withRole = (roleKey: string): AuthUser => ({ roles: [{ key: roleKey }] }); const withPermission = (permKey: string): AuthUser => ({ permissionKeys: [permKey], }); describe("canApproveContractStep", () => { it("shows to the matching position type only", () => { const chief = withPositionType("-marketing-chief"); expect(canApproveContractStep(chief, "-marketing-chief")).toBe(true); // a marketing officer must NOT see the chief step's buttons expect(canApproveContractStep(chief, "-marketing-director-")).toBe(false); }); it("lets super/org admins action any step", () => { expect(canApproveContractStep(withRole("super_admin"), "anything")).toBe( true, ); expect( canApproveContractStep(withRole("organization_admin"), "-marketing-chief"), ).toBe(true); }); it("resolves legacy chain roles via their position-type aliases", () => { const director = withPositionType("operation-director"); expect(canApproveContractStep(director, "DIRECTOR")).toBe(true); expect(canApproveContractStep(director, "CEO")).toBe(false); }); it("honours the role's own legacy approve permission", () => { const staff = withPermission( "edr_freight_app:contracts:approve_director", ); expect(canApproveContractStep(staff, "DIRECTOR")).toBe(true); }); it("does NOT show to holders of an unrelated approve permission", () => { // the dropped blanket fallback: a line-staff approver is not a chief const lineStaff = withPermission( "edr_freight_app:contracts:approve_line_staff", ); expect(canApproveContractStep(lineStaff, "-marketing-chief")).toBe(false); }); it("returns false without a user or role", () => { expect(canApproveContractStep(null, "-marketing-chief")).toBe(false); expect(canApproveContractStep(withPositionType("x"), null)).toBe(false); }); });