"""Le CLI expose `--engines` : diagnostic honnête des moteurs embarqués (code 0).

On charge le module CLI par chemin (il n'est pas packagé) et on vérifie que
`--engines` liste les moteurs et sort 0, sans exiger d'argument `input`.
"""

from __future__ import annotations

import importlib.util
from pathlib import Path

import pytest

_CLI_PATH = Path(__file__).resolve().parents[2] / "scripts" / "anonymize_cli.py"


def _load_cli():
    spec = importlib.util.spec_from_file_location("anonymize_cli_undertest", _CLI_PATH)
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
    return mod


def test_engines_flag_lists_engines_and_exits_zero(capsys):
    cli = _load_cli()
    rc = cli.main(["--engines"])
    assert rc == 0
    out = capsys.readouterr().out
    assert "Moteurs d'anonymisation" in out
    # les 3 moteurs connus apparaissent dans le diagnostic
    assert "CamemBERT-bio" in out
    assert "EDS-Pseudo" in out
    assert "GLiNER" in out


def test_no_input_without_engines_errors(capsys):
    cli = _load_cli()
    with pytest.raises(SystemExit) as exc:  # argparse error => exit 2
        cli.main([])
    assert exc.value.code == 2
