import { describe, expect, it } from "vitest";
import type { RequiredToolConstraint } from "./provider-turn/tool-constraint-types.js";
import { resolveProviderToolUsePolicy } from "./tool-use-policy.js";

function requiredToolConstraint(
  input: Partial<RequiredToolConstraint> = {},
): RequiredToolConstraint {
  return {
    forbiddenToolNames: [],
    reason: "explicit-required-tool",
    requiredReleasePolicy: "after-required-tools-succeeded",
    requiredToolNames: [],
    restrictionScope: "turn",
    ...input,
  };
}

describe("provider tool use policy", () => {
  it("treats structured phase-result turns as required-tool-only", () => {
    expect(
      resolveProviderToolUsePolicy(
        { allowPhaseResult: true },
        { phaseName: "change", profileName: "build" },
      ),
    ).toBe("required-tool-only");

    expect(resolveProviderToolUsePolicy({ allowPhaseResult: true }, { phaseName: "verify" })).toBe(
      "required-tool-only",
    );
  });

  it("keeps explicit control turns required-tool-only", () => {
    expect(resolveProviderToolUsePolicy({ allowPlanUpdate: true }, {})).toBe("required-tool-only");
  });

  it("does not let required-tool constraints force tool-only turns", () => {
    expect(
      resolveProviderToolUsePolicy(
        { requiredToolConstraint: requiredToolConstraint({ requiredToolNames: ["shell"] }) },
        {},
      ),
    ).toBe("answer-when-sufficient");
    expect(
      resolveProviderToolUsePolicy(
        { requiredToolConstraint: requiredToolConstraint({ requiredToolNames: [] }) },
        {},
      ),
    ).toBe("answer-when-sufficient");
  });

  it("does not let required any-tool groups force tool-only turns", () => {
    expect(
      resolveProviderToolUsePolicy(
        {
          requiredToolConstraint: requiredToolConstraint({
            requiredAnyToolGroups: [["read-file", "workspace-search"]],
            requiredToolNames: ["read-file", "workspace-search"],
          }),
        },
        {},
      ),
    ).toBe("answer-when-sufficient");
  });

  it("does not force implementation contexts into a tool-first policy", () => {
    expect(
      resolveProviderToolUsePolicy(
        {},
        { artifactObligations: [{}], phaseName: "change", profileName: "build" },
      ),
    ).toBe("answer-when-sufficient");
  });
});
