import type { AgentResult, TestRunnerOptions, ValidationCheck } from "../utils.ts";
import { defineFixture, getAgentOutput, getStructuredOutput } from "../utils.ts";

/**
 * delegateEffort test - validates effort selection for delegation.
 *
 * the orchestrator delegates to Plan mode with mini effort.
 * validates that the subagent runs at mini effort (visible in agent logs
 * as "effort=mini" or sonnet model selection for claude).
 */

// orchestrator runs at "auto" (opus) while delegating with "mini" (sonnet).
// this tests that the delegate tool's effort parameter actually overrides
// the model selection — if it were ignored, the subagent would also run at auto.
const fixture = defineFixture(
  {
    prompt: `This is a simple task. Delegate to the Plan mode with MINI effort (this is a trivial task).
Pass these instructions to the subagent:
"Call set_output with the value 'EFFORT_TEST_PASSED'. Do not create plans or PRs. Just call set_output."`,
    effort: "auto",
    timeout: "5m",
  },
  { localOnly: true }
);

function validator(result: AgentResult): ValidationCheck[] {
  const output = getStructuredOutput(result);
  const agentOutput = getAgentOutput(result);

  const setOutputCalled = output !== null;
  const correctValue = setOutputCalled && /EFFORT_TEST_PASSED/i.test(output);

  // the orchestrator runs at auto (effort=auto in its log line).
  // the delegate tool should spawn the subagent at mini (effort=mini in its log line).
  // if effort override works, we should see BOTH effort=auto AND effort=mini in the output.
  const orchestratorEffort = /running \w+ with effort=auto/i.test(agentOutput);
  const subagentEffort = /running \w+ with effort=mini/i.test(agentOutput);

  return [
    { name: "set_output", passed: setOutputCalled },
    { name: "correct_value", passed: correctValue },
    { name: "orchestrator_auto", passed: orchestratorEffort },
    { name: "subagent_mini", passed: subagentEffort },
  ];
}

export const test: TestRunnerOptions = {
  name: "delegate-effort",
  fixture,
  validator,
  env: { GITHUB_REPOSITORY: "pullfrog/test-repo" },
  tags: ["agnostic"],
};
