|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +def _run( |
| 12 | + tmp_path: Path, *args: str, output_format: str | None = "json", |
| 13 | +) -> subprocess.CompletedProcess[str]: |
| 14 | + format_args = [] if output_format is None else ["--format", output_format] |
| 15 | + return subprocess.run( |
| 16 | + [sys.executable, "-m", "loopx.entrypoint", "--runtime-root", |
| 17 | + str(tmp_path / "runtime"), "--registry", str(tmp_path / "registry.json"), |
| 18 | + *format_args, "external-evidence", *args], |
| 19 | + cwd=Path(__file__).resolve().parents[2], capture_output=True, |
| 20 | + text=True, check=False, timeout=30, |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +def _plan(tmp_path: Path, source: str) -> subprocess.CompletedProcess[str]: |
| 25 | + return _run( |
| 26 | + tmp_path, "plan", "--objective", "Inspect synthetic evidence", |
| 27 | + "--user-activity", "Choose a provider", "--decision", "Whether to adopt", |
| 28 | + "--evidence-kind", "current_behavior", "--provider-inventory-json", source, |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def _inventory() -> dict: |
| 33 | + return {"providers": [{ |
| 34 | + "provider_id": "host:synthetic-research", "provider_kind": "method", |
| 35 | + "protocol": "external_evidence_research_v0", "declared": True, |
| 36 | + "installed": True, "enabled": True, "ready": True, |
| 37 | + "unavailable_reason": None, |
| 38 | + }]} |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize("action", [ |
| 42 | + "discover", "plan", "execute", "readback", "receipt", "admit", "retire", |
| 43 | +]) |
| 44 | +def test_source_cli_help_exposes_json_file_contract( |
| 45 | + tmp_path: Path, monkeypatch, action: str, |
| 46 | +) -> None: |
| 47 | + monkeypatch.setenv("LOOPX_USAGE_PING", "0") |
| 48 | + result = _run(tmp_path, action, "--help") |
| 49 | + assert result.returncode == 0 |
| 50 | + help_text = " ".join(result.stdout.split()) |
| 51 | + assert "FILE" in help_text and "Path to" in help_text |
| 52 | + if action in {"discover", "plan"}: |
| 53 | + assert "--provider-inventory-json FILE" in help_text |
| 54 | + assert "providers array" in help_text and "not inline JSON" in help_text |
| 55 | + |
| 56 | + |
| 57 | +def test_source_cli_file_read_error_is_actionable_without_echoing_input( |
| 58 | + tmp_path: Path, monkeypatch, |
| 59 | +) -> None: |
| 60 | + monkeypatch.setenv("LOOPX_USAGE_PING", "0") |
| 61 | + for source in ( |
| 62 | + '{"providers": [], "note": "DO_NOT_ECHO_INLINE_INPUT"}', |
| 63 | + str(tmp_path / "missing inventory.json"), |
| 64 | + ): |
| 65 | + result = _plan(tmp_path, source) |
| 66 | + payload = json.loads(result.stdout) |
| 67 | + assert result.returncode == 1 and payload["status"] == "invalid_request" |
| 68 | + assert "readable JSON file path" in payload["error"] |
| 69 | + assert "not inline JSON" in payload["error"] |
| 70 | + assert source not in result.stdout + result.stderr |
| 71 | + assert "Traceback" not in result.stderr |
| 72 | + |
| 73 | + |
| 74 | +def test_source_cli_default_markdown_keeps_the_json_error_reason( |
| 75 | + tmp_path: Path, monkeypatch, |
| 76 | +) -> None: |
| 77 | + monkeypatch.setenv("LOOPX_USAGE_PING", "0") |
| 78 | + args = ("discover", "--provider-inventory-json", str(tmp_path / "missing.json")) |
| 79 | + json_result = _run(tmp_path, *args) |
| 80 | + markdown_result = _run(tmp_path, *args, output_format=None) |
| 81 | + assert json_result.returncode == markdown_result.returncode == 1 |
| 82 | + error = json.loads(json_result.stdout) |
| 83 | + assert error["status"] == "invalid_request" |
| 84 | + assert error["error"] in markdown_result.stdout |
| 85 | + assert "invalid_request" in markdown_result.stdout |
| 86 | + assert "Traceback" not in markdown_result.stderr |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.parametrize("content,diagnostic", [ |
| 90 | + (b'{"providers":', "valid JSON"), |
| 91 | + (b'\xff\xfeinvalid', "valid JSON"), |
| 92 | + (b"[]", "JSON object"), |
| 93 | + (b'{"providers": {}}', "providers array"), |
| 94 | +]) |
| 95 | +def test_source_cli_distinguishes_file_content_errors_and_recovers( |
| 96 | + tmp_path: Path, monkeypatch, content: bytes, diagnostic: str, |
| 97 | +) -> None: |
| 98 | + monkeypatch.setenv("LOOPX_USAGE_PING", "0") |
| 99 | + path = tmp_path / "provider inventory.json" |
| 100 | + path.write_bytes(content) |
| 101 | + rejected = _plan(tmp_path, str(path)) |
| 102 | + error = json.loads(rejected.stdout) |
| 103 | + assert rejected.returncode == 1 and error["status"] == "invalid_request" |
| 104 | + assert diagnostic in error["error"] |
| 105 | + assert "readable JSON file path" not in error["error"] |
| 106 | + assert "Traceback" not in rejected.stderr |
| 107 | + |
| 108 | + path.write_text(json.dumps(_inventory()), encoding="utf-8") |
| 109 | + recovered = _plan(tmp_path, str(path)) |
| 110 | + plan = json.loads(recovered.stdout) |
| 111 | + assert recovered.returncode == 0 and plan["status"] == "ready" |
| 112 | + assert plan["selected_provider"]["provider_id"] == "host:synthetic-research" |
| 113 | + |
| 114 | + |
| 115 | +def test_source_cli_preserves_valid_file_encodings_and_plan_identity( |
| 116 | + tmp_path: Path, monkeypatch, |
| 117 | +) -> None: |
| 118 | + monkeypatch.setenv("LOOPX_USAGE_PING", "0") |
| 119 | + plans = [] |
| 120 | + for name, encoding in (("providers with spaces.json", "utf-8"), ("{}.json", "utf-16")): |
| 121 | + path = tmp_path / name |
| 122 | + path.write_text(json.dumps(_inventory()), encoding=encoding) |
| 123 | + result = _plan(tmp_path, str(path)) |
| 124 | + assert result.returncode == 0 |
| 125 | + plans.append(json.loads(result.stdout)) |
| 126 | + assert plans[0]["status"] == plans[1]["status"] == "ready" |
| 127 | + assert plans[0]["plan_id"] == plans[1]["plan_id"] |
0 commit comments