"""Tests for `kernbench list` subcommand and `--bench <index>` resolution."""
from __future__ import annotations

import kernbench.cli.main as cli_main
from kernbench.benches import registry


def test_cli_list_outputs_all_benches(capsys):
    rc = cli_main.main(["list"])
    assert rc == 0
    out = capsys.readouterr().out
    for spec in registry.list_all():
        assert spec.name in out
    assert "DESCRIPTION" in out


def test_cli_run_by_index(monkeypatch):
    """CLI accepts numeric index for --bench; same callable as the name."""
    qkv_spec = registry.resolve("qkv-gemm")

    captured = {}

    def fake_run_bench(*, topology, bench_fn, device, engine_factory):
        captured["bench_fn"] = bench_fn

        class _R:
            traces = []
            engine = None

            class completion:
                ok = True

            def summary_text(self):
                return ""
        return _R()

    monkeypatch.setattr(cli_main, "run_bench", fake_run_bench)
    rc = cli_main.main([
        "run", "--topology", "topology.yaml",
        "--bench", str(qkv_spec.index),
        "--device", "sip:0",
    ])
    assert rc == 0
    assert captured["bench_fn"] is qkv_spec.run
