import { describe, expect, it } from "vitest";
import { deriveControlsForActionPolicy } from "./provider-action-controls.js";

describe("deriveControlsForActionPolicy", () => {
  it("throws when phase_result is enabled without a phase name", () => {
    expect(() =>
      deriveControlsForActionPolicy(
        {
          allowPhaseResult: true,
        },
        {
          allowedActions: ["phase_result"],
          directResponseAllowed: false,
        },
      ),
    ).toThrow("phaseResultPhaseName is missing");
  });

  it("keeps phase_result controls when policy and phase name agree", () => {
    expect(
      deriveControlsForActionPolicy(
        {
          allowPhaseResult: true,
          phaseResultPhaseName: "verify",
        },
        {
          allowedActions: ["phase_result"],
          directResponseAllowed: false,
        },
      ),
    ).toEqual({
      allowPhaseResult: true,
      allowPlanUpdate: false,
      phaseResultPhaseName: "verify",
    });
  });

  it("closes phase_result when the action policy does not allow it", () => {
    expect(
      deriveControlsForActionPolicy(
        {
          allowPhaseResult: true,
          allowPlanUpdate: true,
          phaseResultPhaseName: "verify",
        },
        {
          allowedActions: ["tool"],
          directResponseAllowed: false,
        },
      ),
    ).toEqual({
      allowPhaseResult: false,
      allowPlanUpdate: false,
    });
  });
});
