from __future__ import annotations

import json
import subprocess
import sys
from pathlib import Path


def test_validate_dotnet_domain_parity_backlog_passes() -> None:
    root = Path(__file__).resolve().parents[2]
    proc = subprocess.run(
        [sys.executable, str(root / "tools" / "validate_dotnet_domain_parity_backlog_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_backlog_reports_missing_target() -> None:
    root = Path(__file__).resolve().parents[2]
    temp = root / "Temp" / "test_wbs_10_domain_parity_backlog_bad.yaml"
    temp.write_text(
        "formula_id: WBS_10_DOTNET_DOMAIN_PARITY_BACKLOG_V1\ncoverage_min: 1.0\ntolerance_policy:\n  numeric_default: 0\n  text_default: exact\nparity_targets:\n  - target_id: formula_engine_timing\n    priority: 1\n  - target_id: formula_engine_sell\n    priority: 2\n",
        encoding="utf-8",
    )
    proc = subprocess.run(
        [sys.executable, str(root / "tools" / "validate_dotnet_domain_parity_backlog_v1.py"), "--backlog", str(temp)],
        cwd=root,
        capture_output=True,
        text=True,
    )
    assert proc.returncode != 0
    payload = json.loads(proc.stdout)
    assert payload["gate"] == "FAIL"
    assert "exit_stop_price missing" in payload["missing"]
