"""Envelope tests for apply, apply_patch, archive.""" from __future__ import annotations from collections.abc import Mapping import json, pathlib import pytest from tests.cli_test_helper import CliRunner runner = CliRunner() _FIELDS = ("muse_version", "schema", "timestamp", "warnings") def _has_envelope(d: Mapping[str, object]) -> None: for f in _FIELDS: assert f in d, f"missing {f}" assert "schema_version" not in d class TestApplyEnvelope: def test_apply_json(self, tmp_path: pathlib.Path) -> None: fake = tmp_path / "fake.patch" fake.write_text("# fake patch\n") r = runner.invoke(None, ["apply", str(fake), "--json"]) # may fail with user error but must emit JSON try: d = json.loads(r.output) _has_envelope(d) except (json.JSONDecodeError, AssertionError): pytest.skip("apply errored before JSON output") class TestApplyPatchEnvelope: def test_check_has_envelope(self) -> None: r = runner.invoke(None, ["apply-patch", "nonexistent.patch", "--check", "--json"]) try: d = json.loads(r.output) _has_envelope(d) except (json.JSONDecodeError, AssertionError): pytest.skip("apply-patch errored before JSON output") class TestArchiveEnvelope: def test_list_has_envelope(self) -> None: r = runner.invoke(None, ["archive", "--list", "--json"]) assert r.exit_code == 0, r.output d = json.loads(r.output) _has_envelope(d)