import { describe, it, expect } from "vitest";
import { getTransition, canPerformAction, canReject } from "@/lib/appraisal-state-machine";

// Appraisal lifecycle (Crewing-Implementation-Spec §5.4).
describe("Appraisal state machine", () => {
  it("MPO verifies a SUBMITTED appraisal", () => {
    expect(getTransition("SUBMITTED", "verify")?.to).toBe("MPO_VERIFIED");
    expect(canPerformAction("SUBMITTED", "verify", "MANNING")).toBe(true);
    expect(canPerformAction("SUBMITTED", "verify", "MANAGER")).toBe(true);
    expect(canPerformAction("SUBMITTED", "verify", "SITE_STAFF")).toBe(false);
  });

  it("Manager approves an MPO_VERIFIED appraisal (not the MPO)", () => {
    expect(getTransition("MPO_VERIFIED", "approve")?.to).toBe("MANAGER_APPROVED");
    expect(canPerformAction("MPO_VERIFIED", "approve", "MANAGER")).toBe(true);
    expect(canPerformAction("MPO_VERIFIED", "approve", "MANNING")).toBe(false);
  });

  it("rejects out-of-order actions", () => {
    expect(getTransition("SUBMITTED", "approve")).toBeNull();
    expect(getTransition("MANAGER_APPROVED", "verify")).toBeNull();
  });

  it("is rejectable only while in review", () => {
    expect(canReject("SUBMITTED")).toBe(true);
    expect(canReject("MPO_VERIFIED")).toBe(true);
    expect(canReject("MANAGER_APPROVED")).toBe(false);
    expect(canReject("REJECTED")).toBe(false);
  });
});
