|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +import scripts.agent_artifact_bundle as agent_artifact_bundle |
| 8 | +from scripts.safe_pr_gate import GateState, evaluate_gate |
| 9 | + |
| 10 | + |
| 11 | +def test_build_agent_artifact_bundle_is_deterministic_and_includes_optional_metadata() -> None: |
| 12 | + state = GateState( |
| 13 | + branch="feat/agent-artifact-bundle", |
| 14 | + status_short=(), |
| 15 | + changed_paths=(), |
| 16 | + ) |
| 17 | + |
| 18 | + bundle = agent_artifact_bundle.build_agent_artifact_bundle( |
| 19 | + state, |
| 20 | + allow_main=False, |
| 21 | + validation_commands=["python -m compileall -q scripts/agent_artifact_bundle.py", "pytest tests/test_agent_artifact_bundle.py -q"], |
| 22 | + validation_results=["pass", "pass"], |
| 23 | + mcp_context_output_ref="artifacts/mcp_context_layer_example.json", |
| 24 | + ) |
| 25 | + |
| 26 | + assert bundle == { |
| 27 | + "branch": "feat/agent-artifact-bundle", |
| 28 | + "changed_files": [], |
| 29 | + "mcp_context_output_ref": "artifacts/mcp_context_layer_example.json", |
| 30 | + "ok": True, |
| 31 | + "result": "PASS", |
| 32 | + "safe_pr_gate": evaluate_gate(state).to_dict(), |
| 33 | + "validation_evidence": [ |
| 34 | + { |
| 35 | + "command": "python -m compileall -q scripts/agent_artifact_bundle.py", |
| 36 | + "result": "pass", |
| 37 | + }, |
| 38 | + { |
| 39 | + "command": "pytest tests/test_agent_artifact_bundle.py -q", |
| 40 | + "result": "pass", |
| 41 | + }, |
| 42 | + ], |
| 43 | + } |
| 44 | + |
| 45 | + first = json.dumps(bundle, indent=2, sort_keys=True) |
| 46 | + second = json.dumps(bundle, indent=2, sort_keys=True) |
| 47 | + assert first == second |
| 48 | + |
| 49 | + |
| 50 | +def test_build_agent_artifact_bundle_rejects_main_without_allow_main() -> None: |
| 51 | + state = GateState(branch="main", status_short=(), changed_paths=()) |
| 52 | + |
| 53 | + with pytest.raises(RuntimeError, match="main branch is not allowed for agent artifact bundling"): |
| 54 | + agent_artifact_bundle.build_agent_artifact_bundle( |
| 55 | + state, |
| 56 | + allow_main=False, |
| 57 | + validation_commands=[], |
| 58 | + validation_results=[], |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def test_build_agent_artifact_bundle_reflects_safe_gate_failure() -> None: |
| 63 | + state = GateState( |
| 64 | + branch="feat/agent-artifact-bundle", |
| 65 | + status_short=(" M docs/example.md",), |
| 66 | + changed_paths=("docs/example.md",), |
| 67 | + ) |
| 68 | + |
| 69 | + bundle = agent_artifact_bundle.build_agent_artifact_bundle( |
| 70 | + state, |
| 71 | + allow_main=False, |
| 72 | + validation_commands=["python -m compileall -q scripts/agent_artifact_bundle.py"], |
| 73 | + validation_results=["pass"], |
| 74 | + ) |
| 75 | + |
| 76 | + assert bundle["ok"] is False |
| 77 | + assert bundle["result"] == "FAIL" |
| 78 | + assert bundle["safe_pr_gate"]["ok"] is False |
| 79 | + assert bundle["safe_pr_gate"]["result"] == "FAIL" |
| 80 | + |
| 81 | + |
| 82 | +def test_main_emits_deterministic_json_and_omits_optional_reference(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]) -> None: |
| 83 | + state = GateState( |
| 84 | + branch="feat/agent-artifact-bundle", |
| 85 | + status_short=(), |
| 86 | + changed_paths=("scripts/agent_artifact_bundle.py",), |
| 87 | + ) |
| 88 | + monkeypatch.setattr(agent_artifact_bundle, "collect_gate_state", lambda: state) |
| 89 | + |
| 90 | + exit_code = agent_artifact_bundle.main( |
| 91 | + [ |
| 92 | + "--validation-command", |
| 93 | + "python -m compileall -q scripts/agent_artifact_bundle.py", |
| 94 | + "--validation-result", |
| 95 | + "pass", |
| 96 | + ] |
| 97 | + ) |
| 98 | + output = json.loads(capsys.readouterr().out) |
| 99 | + |
| 100 | + assert exit_code == 0 |
| 101 | + assert output == { |
| 102 | + "branch": "feat/agent-artifact-bundle", |
| 103 | + "changed_files": ["scripts/agent_artifact_bundle.py"], |
| 104 | + "ok": True, |
| 105 | + "result": "PASS", |
| 106 | + "safe_pr_gate": evaluate_gate(state).to_dict(), |
| 107 | + "validation_evidence": [ |
| 108 | + { |
| 109 | + "command": "python -m compileall -q scripts/agent_artifact_bundle.py", |
| 110 | + "result": "pass", |
| 111 | + } |
| 112 | + ], |
| 113 | + } |
| 114 | + assert "mcp_context_output_ref" not in output |
| 115 | + |
| 116 | + |
| 117 | +def test_main_returns_failure_exit_code_when_safe_gate_fails( |
| 118 | + monkeypatch: pytest.MonkeyPatch, |
| 119 | + capsys: pytest.CaptureFixture[str], |
| 120 | +) -> None: |
| 121 | + state = GateState( |
| 122 | + branch="feat/agent-artifact-bundle", |
| 123 | + status_short=(" M docs/example.md",), |
| 124 | + changed_paths=("docs/example.md",), |
| 125 | + ) |
| 126 | + monkeypatch.setattr(agent_artifact_bundle, "collect_gate_state", lambda: state) |
| 127 | + |
| 128 | + exit_code = agent_artifact_bundle.main( |
| 129 | + [ |
| 130 | + "--validation-command", |
| 131 | + "python -m compileall -q scripts/agent_artifact_bundle.py", |
| 132 | + "--validation-result", |
| 133 | + "pass", |
| 134 | + ] |
| 135 | + ) |
| 136 | + output = json.loads(capsys.readouterr().out) |
| 137 | + |
| 138 | + assert exit_code == 1 |
| 139 | + assert output["ok"] is False |
| 140 | + assert output["result"] == "FAIL" |
| 141 | + assert output["safe_pr_gate"]["ok"] is False |
| 142 | + assert output["safe_pr_gate"]["result"] == "FAIL" |
| 143 | + |
| 144 | + |
| 145 | +def test_main_reports_main_branch_as_deterministic_error_json( |
| 146 | + monkeypatch: pytest.MonkeyPatch, |
| 147 | + capsys: pytest.CaptureFixture[str], |
| 148 | +) -> None: |
| 149 | + monkeypatch.setattr(agent_artifact_bundle, "collect_gate_state", lambda: GateState(branch="main", status_short=(), changed_paths=())) |
| 150 | + |
| 151 | + exit_code = agent_artifact_bundle.main([]) |
| 152 | + output = json.loads(capsys.readouterr().out) |
| 153 | + |
| 154 | + assert exit_code == 1 |
| 155 | + assert output == { |
| 156 | + "error": { |
| 157 | + "message": "main branch is not allowed for agent artifact bundling", |
| 158 | + "type": "RuntimeError", |
| 159 | + }, |
| 160 | + "ok": False, |
| 161 | + "result": "ERROR", |
| 162 | + } |
0 commit comments