"""Regression guards for the generate.py CLI rewire (Phase 6 / Bug B1).

Pre-Phase-6 state: generate.py raised
    NotImplementedError("rewire to dispatch() / EpisodeRunner — phase-21-22 deferred")
on the live-run branch. Post-Phase-6 state: the live-run branch constructs an
EpisodeRunner and dispatches.

These tests do NOT exercise a real dispatch — they only assert the stub is
gone and the imports are wired.
"""

import subprocess
import sys
from pathlib import Path

REPO = Path(__file__).resolve().parents[2]


def test_dry_run_completes_without_notimplementederror():
    """--dry-run should NOT raise NotImplementedError (B1 regression guard)."""
    result = subprocess.run(
        [sys.executable, "recoil/pipeline/cli/generate.py",
         "--project", "tartarus", "--episode", "1",
         "--pass", "EP001_PASS_008_SH16_17_18_A_WREN",
         "--budget", "4", "--dry-run"],
        cwd=REPO, capture_output=True, text=True, timeout=120,
        env={"PYTHONPATH": str(REPO), "PATH": ""},
    )
    # The --dry-run path returns early with the cost-estimate dict (no live dispatch).
    # We only assert that NotImplementedError is NOT in stderr.
    assert "NotImplementedError" not in result.stderr, result.stderr
    assert "rewire to dispatch" not in result.stderr


def test_generate_imports_episode_runner():
    """Static-import assertion — generate.py must import EpisodeRunner."""
    text = (REPO / "recoil/pipeline/cli/generate.py").read_text()
    assert "EpisodeRunner" in text
    # The phase-21-22 stub must be gone.
    assert "phase-21-22 deferred" not in text


def test_generate_no_notimplementederror_in_source():
    """The NotImplementedError stub must be deleted from the source."""
    text = (REPO / "recoil/pipeline/cli/generate.py").read_text()
    assert "raise NotImplementedError" not in text


def test_generate_no_productionloop_references():
    """Dead BatchConfig / ProductionLoop commented-out code must be removed."""
    text = (REPO / "recoil/pipeline/cli/generate.py").read_text()
    assert "ProductionLoop" not in text
    assert "BatchConfig" not in text
