|
| 1 | +"""Oracle bridge tests — all subprocess calls are mocked; no real CLI/podman invoked.""" |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | +from types import SimpleNamespace |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from prd_taskmaster.oracle_bridge import grade_card, grade_task, OracleCardError |
| 10 | + |
| 11 | + |
| 12 | +# --------------------------------------------------------------------------- |
| 13 | +# Helpers |
| 14 | +# --------------------------------------------------------------------------- |
| 15 | + |
| 16 | +def _write_graded_card(path: Path, extra: dict | None = None) -> Path: |
| 17 | + """Write a minimal valid Graded Card (has the required 'grading' key).""" |
| 18 | + card = {"id": "C-001", "grading": {"criteria": "all tests pass"}} |
| 19 | + if extra: |
| 20 | + card.update(extra) |
| 21 | + path.write_text(json.dumps(card)) |
| 22 | + return path |
| 23 | + |
| 24 | + |
| 25 | +def _fake_proc(stdout: str = "", stderr: str = "", returncode: int = 0): |
| 26 | + """Return a fake subprocess.CompletedProcess-like object.""" |
| 27 | + return SimpleNamespace(stdout=stdout, stderr=stderr, returncode=returncode) |
| 28 | + |
| 29 | + |
| 30 | +def _common_args(tmp_path: Path): |
| 31 | + """Return common keyword args for grade_card, pointing at tmp_path sub-dirs.""" |
| 32 | + return dict( |
| 33 | + repo_path=tmp_path / "repo", |
| 34 | + commit_sha="abc123", |
| 35 | + held_root=tmp_path / "held", |
| 36 | + evidence_dir=tmp_path / "evidence", |
| 37 | + ledger_dir=tmp_path / "ledger", |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +# --------------------------------------------------------------------------- |
| 42 | +# Test 1: PASS round-trip + argv mapping |
| 43 | +# --------------------------------------------------------------------------- |
| 44 | + |
| 45 | +def test_pass_roundtrip_and_argv_mapping(tmp_path, monkeypatch): |
| 46 | + """grade_card returns ("PASS", full_dict) and passes correct CLI args.""" |
| 47 | + card_path = _write_graded_card(tmp_path / "card.json") |
| 48 | + args = _common_args(tmp_path) |
| 49 | + |
| 50 | + captured_cmd = [] |
| 51 | + |
| 52 | + def fake_run(cmd, **kwargs): |
| 53 | + captured_cmd.extend(cmd) |
| 54 | + return _fake_proc(stdout='{"verdict":"PASS","exitCode":0,"ledgerEventId":"x"}') |
| 55 | + |
| 56 | + monkeypatch.setattr("prd_taskmaster.oracle_bridge.subprocess.run", fake_run) |
| 57 | + |
| 58 | + oracle_cmd = ["atlas"] |
| 59 | + verdict, detail = grade_card(card_path=card_path, oracle_cmd=oracle_cmd, **args) |
| 60 | + |
| 61 | + assert verdict == "PASS" |
| 62 | + assert detail == {"verdict": "PASS", "exitCode": 0, "ledgerEventId": "x"} |
| 63 | + |
| 64 | + # argv mapping checks |
| 65 | + assert "oracle" in captured_cmd |
| 66 | + assert "grade" in captured_cmd |
| 67 | + assert "--repo" in captured_cmd |
| 68 | + assert str(args["repo_path"]) in captured_cmd |
| 69 | + assert "--commit" in captured_cmd |
| 70 | + assert "abc123" in captured_cmd |
| 71 | + assert "--card" in captured_cmd |
| 72 | + assert str(card_path) in captured_cmd |
| 73 | + assert "--held" in captured_cmd |
| 74 | + assert str(args["held_root"]) in captured_cmd |
| 75 | + assert "--evidence" in captured_cmd |
| 76 | + assert str(args["evidence_dir"]) in captured_cmd |
| 77 | + assert "--ledger" in captured_cmd |
| 78 | + assert str(args["ledger_dir"]) in captured_cmd |
| 79 | + |
| 80 | + |
| 81 | +# --------------------------------------------------------------------------- |
| 82 | +# Test 2: Missing 'grading' block → OracleCardError |
| 83 | +# --------------------------------------------------------------------------- |
| 84 | + |
| 85 | +def test_missing_grading_block_raises(tmp_path, monkeypatch): |
| 86 | + """A card without a 'grading' key must raise OracleCardError (before shelling out).""" |
| 87 | + card_path = tmp_path / "card_no_grading.json" |
| 88 | + card_path.write_text(json.dumps({"id": "C-002", "title": "Some task"})) |
| 89 | + |
| 90 | + # subprocess.run should NOT be called — we assert by not patching it; |
| 91 | + # if the bridge reaches it the test will fail for a different reason. |
| 92 | + # But to be clean and not accidentally run a real 'atlas' binary, patch it too. |
| 93 | + def fake_run(cmd, **kwargs): |
| 94 | + raise AssertionError("subprocess.run should not be called when grading block absent") |
| 95 | + |
| 96 | + monkeypatch.setattr("prd_taskmaster.oracle_bridge.subprocess.run", fake_run) |
| 97 | + |
| 98 | + with pytest.raises(OracleCardError, match="no 'grading' block"): |
| 99 | + grade_card(card_path=card_path, **_common_args(tmp_path)) |
| 100 | + |
| 101 | + |
| 102 | +# --------------------------------------------------------------------------- |
| 103 | +# Test 3: Fail-closed on unparseable output (non-JSON stdout) |
| 104 | +# --------------------------------------------------------------------------- |
| 105 | + |
| 106 | +def test_fail_closed_on_unparseable_output(tmp_path, monkeypatch): |
| 107 | + """Non-JSON stdout (e.g. 'podman: command not found') yields ("FAIL", {...}), never raises.""" |
| 108 | + card_path = _write_graded_card(tmp_path / "card.json") |
| 109 | + |
| 110 | + def fake_run(cmd, **kwargs): |
| 111 | + return _fake_proc(stdout="podman: command not found", returncode=127) |
| 112 | + |
| 113 | + monkeypatch.setattr("prd_taskmaster.oracle_bridge.subprocess.run", fake_run) |
| 114 | + |
| 115 | + verdict, detail = grade_card(card_path=card_path, oracle_cmd=["atlas"], **_common_args(tmp_path)) |
| 116 | + |
| 117 | + assert verdict == "FAIL" |
| 118 | + assert "error" in detail |
| 119 | + assert detail["returncode"] == 127 |
| 120 | + |
| 121 | + |
| 122 | +# --------------------------------------------------------------------------- |
| 123 | +# Test 4: Fail-closed on CLI invocation error (FileNotFoundError) |
| 124 | +# --------------------------------------------------------------------------- |
| 125 | + |
| 126 | +def test_fail_closed_on_invocation_error(tmp_path, monkeypatch): |
| 127 | + """FileNotFoundError from subprocess.run yields ("FAIL", {...}), never raises.""" |
| 128 | + card_path = _write_graded_card(tmp_path / "card.json") |
| 129 | + |
| 130 | + def fake_run(cmd, **kwargs): |
| 131 | + raise FileNotFoundError("atlas: No such file or directory") |
| 132 | + |
| 133 | + monkeypatch.setattr("prd_taskmaster.oracle_bridge.subprocess.run", fake_run) |
| 134 | + |
| 135 | + verdict, detail = grade_card(card_path=card_path, oracle_cmd=["atlas"], **_common_args(tmp_path)) |
| 136 | + |
| 137 | + assert verdict == "FAIL" |
| 138 | + assert "error" in detail |
| 139 | + assert "oracle CLI invocation failed" in detail["error"] |
| 140 | + |
| 141 | + |
| 142 | +# --------------------------------------------------------------------------- |
| 143 | +# Test 5: FAIL verdict passthrough |
| 144 | +# --------------------------------------------------------------------------- |
| 145 | + |
| 146 | +def test_fail_verdict_passthrough(tmp_path, monkeypatch): |
| 147 | + """A FAIL verdict from the oracle is passed through as ("FAIL", detail).""" |
| 148 | + card_path = _write_graded_card(tmp_path / "card.json") |
| 149 | + |
| 150 | + def fake_run(cmd, **kwargs): |
| 151 | + return _fake_proc(stdout='{"verdict":"FAIL"}', returncode=1) |
| 152 | + |
| 153 | + monkeypatch.setattr("prd_taskmaster.oracle_bridge.subprocess.run", fake_run) |
| 154 | + |
| 155 | + verdict, detail = grade_card(card_path=card_path, oracle_cmd=["atlas"], **_common_args(tmp_path)) |
| 156 | + |
| 157 | + assert verdict == "FAIL" |
| 158 | + assert detail == {"verdict": "FAIL"} |
| 159 | + |
| 160 | + |
| 161 | +# --------------------------------------------------------------------------- |
| 162 | +# Test 6: grade_task missing card → OracleCardError |
| 163 | +# --------------------------------------------------------------------------- |
| 164 | + |
| 165 | +def test_grade_task_missing_card_raises(tmp_path): |
| 166 | + """grade_task raises OracleCardError when no CDD card exists for the given task.""" |
| 167 | + # tmp_path has no .atlas-ai/cdd/task-1.json |
| 168 | + with pytest.raises(OracleCardError, match="no CDD card at"): |
| 169 | + grade_task( |
| 170 | + repo_root=tmp_path, |
| 171 | + task_id=1, |
| 172 | + commit_sha="deadbeef", |
| 173 | + held_root=tmp_path / "held", |
| 174 | + evidence_dir=tmp_path / "evidence", |
| 175 | + ledger_dir=tmp_path / "ledger", |
| 176 | + ) |
0 commit comments