from __future__ import annotations

import sys
import types
from pathlib import Path

from evals.importers import cli as importers_cli


def _install_fake_debugbench_importer(monkeypatch):
    calls: dict[str, object] = {}

    fake_module = types.ModuleType("evals.importers.debugbench_importer")

    def _fake_import_debugbench(output_dir, max_cases, validate, dry_run):
        calls["output_dir"] = output_dir
        calls["max_cases"] = max_cases
        calls["validate"] = validate
        calls["dry_run"] = dry_run
        return []

    fake_module.import_debugbench = _fake_import_debugbench
    monkeypatch.setitem(sys.modules, "evals.importers.debugbench_importer", fake_module)
    return calls


def test_importers_cli_accepts_execution_flags_after_subcommand(monkeypatch) -> None:
    calls = _install_fake_debugbench_importer(monkeypatch)
    exit_code = importers_cli.main(["debugbench", "--executor", "local", "--dry-run"])
    assert exit_code == 0
    assert calls["dry_run"] is True
    assert isinstance(calls["output_dir"], Path)


def test_importers_cli_accepts_execution_flags_before_subcommand(monkeypatch) -> None:
    calls = _install_fake_debugbench_importer(monkeypatch)
    exit_code = importers_cli.main(["--executor", "local", "debugbench", "--dry-run"])
    assert exit_code == 0
    assert calls["dry_run"] is True
    assert isinstance(calls["output_dir"], Path)
