import json
import subprocess
import sys
from pathlib import Path


def test_cli_runs(tmp_path):
    manifest = Path(".mindmodel/manifest.yaml")
    assert manifest.exists(), "expected .mindmodel/manifest.yaml to exist in repo"

    report_path = tmp_path / "report.json"

    # Try module mode first, fallback to direct script invocation
    cmds = [
        [
            sys.executable,
            "-m",
            "scripts.validate_mindmodel",
            str(manifest),
            "--report",
            str(report_path),
        ],
        [
            sys.executable,
            "scripts/validate_mindmodel.py",
            str(manifest),
            "--report",
            str(report_path),
        ],
    ]

    result = None
    for cmd in cmds:
        try:
            result = subprocess.run(cmd, check=False, capture_output=True, text=True)
            # if process ran (any exit code), break and use this result
            break
        except FileNotFoundError:
            continue

    assert result is not None, "Failed to run script (no suitable invocation)"
    # CLI should exit with 0 (report-only)
    assert result.returncode == 0, (
        f"CLI exited non-zero: {result.returncode}\nstderr: {result.stderr}"
    )

    assert report_path.exists(), f"Report file was not created at {report_path}"

    data = json.loads(report_path.read_text(encoding="utf-8"))
    # top-level keys expected from validator
    for key in ("missing_files", "truncated_evidence", "potential_secrets"):
        assert key in data, f"Report JSON missing key: {key}"
