import json import shutil import subprocess import pytest from adf_codec import convert from failure import Failure DOC = {"type": "doc", "version": 1, "content": [{"type": "paragraph", "content": [{"type": "text", "text": "Hi"}]}]} def fake(status: int, out: str = "", err: str = ""): calls: list[tuple[str, str]] = [] def run(mode: str, text: str) -> convert.Ran: calls.append((mode, text)) return convert.Ran(status, out, err) run.calls = calls # type: ignore[attr-defined] return run def test_markdown_to_adf_returns_the_document_the_shim_printed(): run = fake(0, json.dumps(DOC)) assert convert.markdown_to_adf("Hi\n", run) == DOC assert run.calls == [("to-adf", "Hi\n")] def test_adf_to_markdown_sends_the_document_as_json_and_returns_the_text(): run = fake(0, "Hi\n") assert convert.adf_to_markdown(DOC, run) == "Hi\n" assert run.calls == [("to-markdown", json.dumps(DOC))] def test_a_refusal_becomes_a_failure_with_adf_codecs_code_and_message(): refusal = {"error": {"code": "malformed-pipe-table", "message": "row has too many cells", "path": [], "position": {"line": 3, "offset": 9}}} failure = convert.markdown_to_adf("| a |\n", fake(2, json.dumps(refusal))) assert failure == Failure("adf-codec refused the input with malformed-pipe-table: row has too many cells (at line 3)") def test_a_refusal_without_a_position_names_the_path(): refusal = {"error": {"code": "unsupported-node-shape", "message": "bad node", "path": ["content", 0]}} failure = convert.adf_to_markdown(DOC, fake(2, json.dumps(refusal))) assert failure == Failure("adf-codec refused the input with unsupported-node-shape: bad node (at content.0)") def test_a_shim_that_could_not_run_becomes_a_failure_with_its_stderr(): failure = convert.adf_to_markdown(DOC, fake(1, "", "Unexpected token")) assert failure == Failure("the adf-codec shim failed with exit 1: Unexpected token") def test_a_container_that_was_killed_for_memory_says_so(): failure = convert.markdown_to_adf("x", fake(137)) assert isinstance(failure, Failure) and "137" in failure.reason def test_output_that_is_not_json_becomes_a_failure(): failure = convert.markdown_to_adf("x", fake(0, "not json")) assert failure == Failure("the adf-codec shim printed output that is not JSON: not json") def test_a_refusal_that_is_not_the_expected_json_becomes_a_failure(): failure = convert.markdown_to_adf("x", fake(2, "oops")) assert failure == Failure("the adf-codec shim refused with output that is not an error: oops") def test_a_failure_from_the_runner_passes_through(): def run(mode: str, text: str) -> Failure: return Failure("docker is not installed") assert convert.markdown_to_adf("x", run) == Failure("docker is not installed") def test_the_container_has_a_memory_and_swap_limit_and_no_network(): args = convert.docker_args("image:1", "to-adf") assert args[:3] == ["docker", "run", "--rm"] assert args[args.index("--memory") + 1] == args[args.index("--memory-swap") + 1] assert args[args.index("--network") + 1] == "none" assert args[-2:] == ["image:1", "to-adf"] def test_the_image_tag_changes_with_the_shim_files(tmp_path): for name in convert.SHIM_FILES: (tmp_path / name).write_text("a") before = convert.image_tag(tmp_path) (tmp_path / "shim.mjs").write_text("b") assert convert.image_tag(tmp_path) != before @pytest.mark.skipif(shutil.which("docker") is None or subprocess.run(["docker", "info"], capture_output=True).returncode != 0, reason="needs a docker daemon") def test_round_trip_through_the_real_container(): doc = convert.markdown_to_adf("# Title\n\nSome **bold** text.\n") assert isinstance(doc, dict) and doc["type"] == "doc" and doc["content"][0]["type"] == "heading" markdown = convert.adf_to_markdown(doc) assert isinstance(markdown, str) and markdown.startswith("# Title") and "**bold**" in markdown refused = convert.adf_to_markdown({"type": "nope"}) assert isinstance(refused, Failure) and "not-an-adf-document" in refused.reason