"""Tests for T02 — markidocx evidence CLI commands (FR-1409, FR-814).""" from __future__ import annotations import json from pathlib import Path import pytest @pytest.fixture() def store_with_run(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): """Create an EvidenceStore with one sample run and monkeypatch the default dir.""" from markidocx.evidence import EvidenceStore, ReportContext ev_dir = tmp_path / "evidence" store = EvidenceStore(base_dir=ev_dir) run_id = store.new_run_id() store.save_report( run_id, "build", {"status": "ok", "warnings": [], "errors": []}, ReportContext(project="test"), ) _orig_init = EvidenceStore.__init__ def _patched_init(self, base_dir=None): _orig_init(self, base_dir=ev_dir) monkeypatch.setattr(EvidenceStore, "__init__", _patched_init) return store, run_id class TestEvidenceListCommand: def test_list_shows_runs(self, store_with_run) -> None: from typer.testing import CliRunner from markidocx.cli import app store, run_id = store_with_run runner = CliRunner() result = runner.invoke(app, ["evidence", "list"]) assert result.exit_code == 0 assert run_id in result.output def test_list_json(self, store_with_run) -> None: from typer.testing import CliRunner from markidocx.cli import app store, run_id = store_with_run runner = CliRunner() result = runner.invoke(app, ["evidence", "list", "--json"]) assert result.exit_code == 0 data = json.loads(result.output.strip()) assert "runs" in data assert run_id in data["runs"] def test_list_empty_store(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: from markidocx.evidence import EvidenceStore empty_dir = tmp_path / "empty_evidence" _orig_init = EvidenceStore.__init__ def _patched_init(self, base_dir=None): _orig_init(self, base_dir=empty_dir) monkeypatch.setattr(EvidenceStore, "__init__", _patched_init) from typer.testing import CliRunner from markidocx.cli import app runner = CliRunner() result = runner.invoke(app, ["evidence", "list"]) assert result.exit_code == 0 class TestEvidenceGetCommand: def test_get_existing_run(self, store_with_run) -> None: from typer.testing import CliRunner from markidocx.cli import app store, run_id = store_with_run runner = CliRunner() result = runner.invoke(app, ["evidence", "get", run_id]) assert result.exit_code == 0 assert run_id in result.output def test_get_json(self, store_with_run) -> None: from typer.testing import CliRunner from markidocx.cli import app store, run_id = store_with_run runner = CliRunner() result = runner.invoke(app, ["evidence", "get", run_id, "--json"]) assert result.exit_code == 0 data = json.loads(result.output.strip()) assert data["run_id"] == run_id assert "classification" in data def test_get_not_found(self, store_with_run) -> None: from typer.testing import CliRunner from markidocx.cli import app runner = CliRunner() result = runner.invoke(app, ["evidence", "get", "no-such-run-id"]) assert result.exit_code == 1 def test_get_output_file(self, store_with_run, tmp_path: Path) -> None: from typer.testing import CliRunner from markidocx.cli import app store, run_id = store_with_run out_file = tmp_path / "ev.json" runner = CliRunner() result = runner.invoke(app, ["evidence", "get", run_id, "--output", str(out_file)]) assert result.exit_code == 0 assert out_file.exists() data = json.loads(out_file.read_text()) assert data["run_id"] == run_id