from __future__ import annotations

import importlib.util
from pathlib import Path
import sys


def _load_case_module():
    scripts_dir = Path(__file__).resolve().parents[2] / "scripts"
    if str(scripts_dir) not in sys.path:
        sys.path.insert(0, str(scripts_dir))
    file_path = scripts_dir / "auto_test_domains" / "project_detail_data_case.py"
    spec = importlib.util.spec_from_file_location("project_detail_data_case_for_test", file_path)
    if spec is None or spec.loader is None:
        raise RuntimeError(f"Unable to load module from {file_path}")
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


def test_project_detail_case_excludes_production_from_unified_rounds(monkeypatch, tmp_path: Path) -> None:
    module = _load_case_module()
    recorded_rounds: list[str] = []

    def _fake_run_pipeline_round(**kwargs):
        recorded_rounds.append(kwargs["round_name"])
        return {"name": kwargs["round_name"], "passed": True, "issues": []}

    def _fake_build_domain_report(**kwargs):
        return kwargs

    monkeypatch.setattr(module, "run_pipeline_round", _fake_run_pipeline_round)
    monkeypatch.setattr(module, "build_domain_report", _fake_build_domain_report)

    report = module.run_case(config={"project_root": str(tmp_path)}, report_root=tmp_path)

    assert recorded_rounds == [
        "round_1_create",
        "round_2_update",
        "round_3_plan_construction_create",
        "round_5_ensure_production_push",
        "round_6_climb_production_push",
        "round_7_challenge_production_push",
    ]
    assert "production" in report["description"]
    assert "不混入本组统一回归" in report["description"]
