from pathlib import Path
import subprocess
import sys


def test_agent_builder_writes_yaml(tmp_path: Path) -> None:
    output_path = tmp_path / "arch.yaml"

    result = subprocess.run(
        [
            sys.executable,
            "agent_builder.py",
            "--description",
            "strict architect",
            "--name",
            "arch",
            "--role",
            "Architect",
            "--provider",
            "openai",
            "--model",
            "fake",
            "--format",
            "yaml",
            "--output",
            str(output_path),
        ],
        capture_output=True,
        text=True,
        cwd=Path(__file__).resolve().parents[1],
        env={**dict(), **{"MULTI_AGENT_FAKE_BUILDER": "1"}},
    )

    assert result.returncode == 0, result.stderr
    assert output_path.exists()
    assert "name: arch" in output_path.read_text(encoding="utf-8")


def test_agent_builder_writes_pm_contract_yaml(tmp_path: Path) -> None:
    output_path = tmp_path / "pm.yaml"

    result = subprocess.run(
        [
            sys.executable,
            "agent_builder.py",
            "--description",
            "route the meeting",
            "--name",
            "pm",
            "--role",
            "PM",
            "--provider",
            "openai",
            "--model",
            "fake",
            "--kind",
            "pm",
            "--format",
            "yaml",
            "--output",
            str(output_path),
        ],
        capture_output=True,
        text=True,
        cwd=Path(__file__).resolve().parents[1],
        env={**dict(), **{"MULTI_AGENT_FAKE_BUILDER": "1"}},
    )

    assert result.returncode == 0, result.stderr
    text = output_path.read_text(encoding="utf-8")
    assert "next_action" in text
    assert "final_report" in text
