from __future__ import annotations
import json
from pathlib import Path
from typing import Any
import pytest
from document_schema import assert_valid
cli_main = pytest.importorskip("matra._core", reason="matra wheel not built").cli_main
ROOT = Path(__file__).resolve().parents[2]
SPEC = ROOT / "spec" / "tests" / "cli" / "envelope.json"
SKILL = ROOT / "skills" / "matra" / "SKILL.md"
def test_version_names_the_version_then_the_compiled_features(
capsys: pytest.CaptureFixture[str],
) -> None:
assert cli_main(["--version"]) == 0
lines = capsys.readouterr().out.splitlines()
assert lines[0].startswith("matra ")
assert lines[1].startswith("features:")
assert "udpipe" in lines[1]
assert "python" in lines[1]
def test_the_skill_is_the_file_verbatim(capsys: pytest.CaptureFixture[str]) -> None:
assert cli_main(["--skill"]) == 0
assert capsys.readouterr().out == SKILL.read_text()
def test_a_reference_is_the_file_verbatim(capsys: pytest.CaptureFixture[str]) -> None:
reference = SKILL.parent / "references" / "json.md"
assert cli_main(["--skill", "-r", "json"]) == 0
assert capsys.readouterr().out == reference.read_text()
def test_an_unknown_reference_exits_two_and_names_the_known_ones(
capsys: pytest.CaptureFixture[str],
) -> None:
assert cli_main(["--skill", "-r", "jsn"]) == 2
captured = capsys.readouterr()
assert captured.out == ""
for name in sorted(path.stem for path in (SKILL.parent / "references").glob("*.md")):
assert name in captured.err
def test_a_missing_file_exits_two_on_stderr(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
missing = str(tmp_path / "absent.txt")
assert cli_main(["analyze", missing]) == 2
captured = capsys.readouterr()
assert captured.out == ""
assert "cannot read" in captured.err
assert missing in captured.err
def test_a_failure_under_json_is_the_envelope(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
missing = str(tmp_path / "absent.txt")
assert cli_main(["analyze", missing, "--json"]) == 2
captured = capsys.readouterr()
got = json.loads(captured.out)
assert sorted(got) == ["command", "error", "format_version", "input", "result"]
assert got["result"] is None
assert got["input"] == missing
assert got["error"]["kind"] == "io"
assert got["error"]["details"]["path"] == missing
assert_valid("error", got["error"], "the failure envelope")
def test_a_directory_is_refused_by_name(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
assert cli_main(["summarize", str(tmp_path)]) == 2
captured = capsys.readouterr()
assert captured.out == ""
assert "is a directory" in captured.err
assert "matra analyze" in captured.err
def test_a_non_utf8_argument_survives_the_crossing(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
path = str(tmp_path / "caf\udcff.txt")
assert cli_main(["analyze", path]) == 2
captured = capsys.readouterr()
assert captured.out == ""
assert "cannot read" in captured.err
def test_an_argument_that_is_not_a_path_is_a_type_error() -> None:
with pytest.raises(TypeError):
cli_main(["analyze", 7])
@pytest.mark.model
def test_envelope_matches_the_conformance_fixture(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
spec: dict[str, Any] = json.loads(SPEC.read_text())
source = tmp_path / spec["filename"]
source.write_text(spec["input"])
subcommand, flag = spec["args"]
assert cli_main([subcommand, str(source), flag]) == 0
got = json.loads(capsys.readouterr().out)
expect = spec["expect"]
assert sorted(got) == expect["envelope_keys"]
assert got["format_version"] == expect["format_version"]
assert got["command"] == expect["command"]
assert got["input"] == str(source)
assert sorted(got["result"]) == expect["result_keys"]
assert got["result"]["provenance"]["schema"] == expect["schema"]
assert got["result"]["provenance"]["format"] == expect["format"]
assert got["result"]["metrics"]["total_sentences"] == expect["result_sentences"]
assert_valid("document", got["result"], "the envelope fixture")
sections = got["result"]["sections"]
assert len(sections) == expect["result_sections"]
sentences = sum(
len(paragraph["sentences"]) for section in sections for paragraph in section["paragraphs"]
)
assert sentences == expect["result_sentences"]