|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +import scripts.ai_workflow_snapshot as ai_workflow_snapshot |
| 8 | +from scripts.safe_pr_gate import GateState |
| 9 | + |
| 10 | + |
| 11 | +def test_build_ai_workflow_snapshot_is_deterministic_and_includes_requested_evidence() -> None: |
| 12 | + state = GateState( |
| 13 | + branch="feat/ai-workflow-snapshot", |
| 14 | + status_short=(), |
| 15 | + changed_paths=("scripts/ai_workflow_snapshot.py",), |
| 16 | + ) |
| 17 | + |
| 18 | + snapshot = ai_workflow_snapshot.build_ai_workflow_snapshot( |
| 19 | + state, |
| 20 | + validation_commands=["python -m compileall -q scripts/ai_workflow_snapshot.py"], |
| 21 | + validation_results=["pass"], |
| 22 | + mcp_context_output_ref="artifacts/mcp_context_layer_example.json", |
| 23 | + ) |
| 24 | + |
| 25 | + assert snapshot["ok"] is True |
| 26 | + assert snapshot["result"] == "PASS" |
| 27 | + assert snapshot["safe_pr_gate"] == snapshot["agent_artifact_bundle"]["safe_pr_gate"] |
| 28 | + assert snapshot["validation_evidence"] == snapshot["agent_artifact_bundle"]["validation_evidence"] |
| 29 | + assert snapshot["mcp_context_output_ref"] == "artifacts/mcp_context_layer_example.json" |
| 30 | + assert snapshot["agent_artifact_bundle"]["mcp_context_output_ref"] == "artifacts/mcp_context_layer_example.json" |
| 31 | + |
| 32 | + first = json.dumps(snapshot, separators=(",", ":"), sort_keys=True) |
| 33 | + second = json.dumps(snapshot, separators=(",", ":"), sort_keys=True) |
| 34 | + assert first == second |
| 35 | + |
| 36 | + |
| 37 | +def test_build_ai_workflow_snapshot_reflects_safe_gate_failure_without_main_error() -> None: |
| 38 | + state = GateState(branch="main", status_short=(), changed_paths=()) |
| 39 | + |
| 40 | + snapshot = ai_workflow_snapshot.build_ai_workflow_snapshot( |
| 41 | + state, |
| 42 | + validation_commands=[], |
| 43 | + validation_results=[], |
| 44 | + ) |
| 45 | + |
| 46 | + assert snapshot["ok"] is False |
| 47 | + assert snapshot["result"] == "FAIL" |
| 48 | + assert snapshot["safe_pr_gate"]["ok"] is False |
| 49 | + assert snapshot["safe_pr_gate"]["result"] == "FAIL" |
| 50 | + assert snapshot["safe_pr_gate"]["problems"] == ["on_main_branch"] |
| 51 | + |
| 52 | + |
| 53 | +def test_build_ai_workflow_snapshot_omits_optional_mcp_reference() -> None: |
| 54 | + state = GateState(branch="feat/ai-workflow-snapshot", status_short=(), changed_paths=()) |
| 55 | + |
| 56 | + snapshot = ai_workflow_snapshot.build_ai_workflow_snapshot( |
| 57 | + state, |
| 58 | + validation_commands=[], |
| 59 | + validation_results=[], |
| 60 | + ) |
| 61 | + |
| 62 | + assert "mcp_context_output_ref" not in snapshot |
| 63 | + assert "mcp_context_output_ref" not in snapshot["agent_artifact_bundle"] |
| 64 | + |
| 65 | + |
| 66 | +def test_main_emits_compact_deterministic_json( |
| 67 | + monkeypatch: pytest.MonkeyPatch, |
| 68 | + capsys: pytest.CaptureFixture[str], |
| 69 | +) -> None: |
| 70 | + state = GateState( |
| 71 | + branch="feat/ai-workflow-snapshot", |
| 72 | + status_short=(), |
| 73 | + changed_paths=("scripts/ai_workflow_snapshot.py",), |
| 74 | + ) |
| 75 | + monkeypatch.setattr(ai_workflow_snapshot, "collect_gate_state", lambda: state) |
| 76 | + |
| 77 | + exit_code = ai_workflow_snapshot.main( |
| 78 | + [ |
| 79 | + "--validation-command", |
| 80 | + "python -m compileall -q scripts/ai_workflow_snapshot.py", |
| 81 | + "--validation-result", |
| 82 | + "pass", |
| 83 | + ] |
| 84 | + ) |
| 85 | + captured = capsys.readouterr() |
| 86 | + output = json.loads(captured.out) |
| 87 | + |
| 88 | + assert exit_code == 0 |
| 89 | + assert captured.err == "" |
| 90 | + assert captured.out == json.dumps(output, separators=(",", ":"), sort_keys=True) + "\n" |
| 91 | + assert output["ok"] is True |
| 92 | + assert output["result"] == "PASS" |
| 93 | + assert "mcp_context_output_ref" not in output |
| 94 | + |
| 95 | + |
| 96 | +def test_main_returns_failure_exit_code_when_safe_gate_fails( |
| 97 | + monkeypatch: pytest.MonkeyPatch, |
| 98 | + capsys: pytest.CaptureFixture[str], |
| 99 | +) -> None: |
| 100 | + state = GateState( |
| 101 | + branch="feat/ai-workflow-snapshot", |
| 102 | + status_short=(" M docs/example.md",), |
| 103 | + changed_paths=("docs/example.md",), |
| 104 | + ) |
| 105 | + monkeypatch.setattr(ai_workflow_snapshot, "collect_gate_state", lambda: state) |
| 106 | + |
| 107 | + exit_code = ai_workflow_snapshot.main([]) |
| 108 | + output = json.loads(capsys.readouterr().out) |
| 109 | + |
| 110 | + assert exit_code == 1 |
| 111 | + assert output["ok"] is False |
| 112 | + assert output["result"] == "FAIL" |
| 113 | + assert output["safe_pr_gate"]["problems"] == ["dirty_working_tree"] |
| 114 | + |
| 115 | + |
| 116 | +def test_main_reports_validation_mismatch_as_deterministic_error_json( |
| 117 | + monkeypatch: pytest.MonkeyPatch, |
| 118 | + capsys: pytest.CaptureFixture[str], |
| 119 | +) -> None: |
| 120 | + state = GateState(branch="feat/ai-workflow-snapshot", status_short=(), changed_paths=()) |
| 121 | + monkeypatch.setattr(ai_workflow_snapshot, "collect_gate_state", lambda: state) |
| 122 | + |
| 123 | + exit_code = ai_workflow_snapshot.main(["--validation-command", "pytest tests/test_ai_workflow_snapshot.py -q"]) |
| 124 | + output = json.loads(capsys.readouterr().out) |
| 125 | + |
| 126 | + assert exit_code == 1 |
| 127 | + assert output == { |
| 128 | + "error": { |
| 129 | + "message": "validation command/result count mismatch: 1 command(s), 0 result(s)", |
| 130 | + "type": "RuntimeError", |
| 131 | + }, |
| 132 | + "ok": False, |
| 133 | + "result": "ERROR", |
| 134 | + } |
0 commit comments