from __future__ import annotations

import json
import subprocess
import sys
from pathlib import Path


def test_validate_dotnet_domain_parity_artifact_passes() -> None:
    root = Path(__file__).resolve().parents[2]
    temp = root / "Temp" / "dotnet_domain_parity_v1.json"
    temp.write_text('{"gate":"PASS","total":44,"passed":44}', encoding="utf-8")

    proc = subprocess.run(
        [sys.executable, str(root / "tools" / "validate_dotnet_domain_parity_artifact_v1.py")],
        cwd=root,
        capture_output=True,
        text=True,
    )
    assert proc.returncode == 0, proc.stdout + proc.stderr
    payload = json.loads(proc.stdout)
    assert payload["gate"] == "PASS"


def test_validate_dotnet_domain_parity_artifact_reports_bad_total() -> None:
    root = Path(__file__).resolve().parents[2]
    temp = root / "Temp" / "test_dotnet_domain_parity_bad.json"
    temp.write_text('{"gate":"PASS","total":39,"passed":39}', encoding="utf-8")

    proc = subprocess.run(
        [sys.executable, str(root / "tools" / "validate_dotnet_domain_parity_artifact_v1.py"), "--artifact", str(temp)],
        cwd=root,
        capture_output=True,
        text=True,
    )
    assert proc.returncode != 0
    payload = json.loads(proc.stdout)
    assert payload["gate"] == "FAIL"
    assert "total" in payload["missing"]
