test_review_stamp.py
file-level
1
files
1
commits
0
hotspots
0
🧊 dead
0
💥 blast risk
| 1 | """Unit tests for review stamp behavior (§K5.7).""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from pathlib import Path |
| 6 | from unittest.mock import patch |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | from cli.atomic import WriteFailure |
| 11 | from tests.support import FIXTURES |
| 12 | from tools.freeze_reviewer.artifact import parse_artifact |
| 13 | from tools.freeze_reviewer.stamp import build_stamp, reference_digest, render_stamped_text, write_stamp |
| 14 | from tools.freeze_reviewer.types import ReviewerSettings |
| 15 | |
| 16 | |
| 17 | def test_markdown_fence_gets_review_stamp(tmp_path: Path) -> None: |
| 18 | path = tmp_path / "docs" / "freeze.md" |
| 19 | path.parent.mkdir(parents=True) |
| 20 | path.write_text((FIXTURES / "freeze-artifact.md").read_text(encoding="utf-8"), encoding="utf-8") |
| 21 | parsed = parse_artifact(path, rel_path="docs/freeze.md") |
| 22 | stamp = build_stamp(parsed, reviewer=ReviewerSettings("agent", "thinking-high", "local", "human"), kit_version="0.1.0") |
| 23 | text = render_stamped_text(parsed, stamp) |
| 24 | assert "review_stamp:" in text |
| 25 | assert "| Round |" not in text or "review_stamp:" in text |
| 26 | |
| 27 | |
| 28 | def test_yaml_whole_file_gets_top_level_stamp(tmp_path: Path) -> None: |
| 29 | path = tmp_path / "freeze.yaml" |
| 30 | path.write_text((FIXTURES / "freeze-artifact.yaml").read_text(encoding="utf-8"), encoding="utf-8") |
| 31 | parsed = parse_artifact(path, rel_path="freeze.yaml") |
| 32 | stamp = build_stamp(parsed, reviewer=ReviewerSettings("agent", "thinking-high", "local", "human"), kit_version="0.1.0") |
| 33 | text = render_stamped_text(parsed, stamp) |
| 34 | assert text.startswith("phase:") |
| 35 | assert "review_stamp:" in text |
| 36 | |
| 37 | |
| 38 | def test_operator_forced_markdown_appends_marker(tmp_path: Path) -> None: |
| 39 | path = tmp_path / "notes.md" |
| 40 | path.write_text("# Notes only\n", encoding="utf-8") |
| 41 | parsed = parse_artifact(path, rel_path="notes.md") |
| 42 | stamp = build_stamp(parsed, reviewer=ReviewerSettings("agent", "thinking-high", "local", "human"), kit_version="0.1.0") |
| 43 | text = render_stamped_text(parsed, stamp) |
| 44 | assert "<!-- overseer:review-stamp -->" in text |
| 45 | |
| 46 | |
| 47 | def test_idempotent_same_digest_noop(tmp_path: Path) -> None: |
| 48 | path = tmp_path / "freeze.yaml" |
| 49 | path.write_text((FIXTURES / "freeze-artifact.yaml").read_text(encoding="utf-8"), encoding="utf-8") |
| 50 | parsed = parse_artifact(path, rel_path="freeze.yaml") |
| 51 | reviewer = ReviewerSettings("agent", "thinking-high", "local", "human") |
| 52 | stamp = build_stamp(parsed, reviewer=reviewer, kit_version="0.1.0") |
| 53 | write_stamp(path, parsed, stamp) |
| 54 | first = path.read_bytes() |
| 55 | reparsed = parse_artifact(path, rel_path="freeze.yaml") |
| 56 | stamp2 = build_stamp(reparsed, reviewer=reviewer, kit_version="0.1.0") |
| 57 | assert write_stamp(path, reparsed, stamp2) is False |
| 58 | assert path.read_bytes() == first |
| 59 | |
| 60 | |
| 61 | def test_atomic_failure_preserves_bytes(tmp_path: Path) -> None: |
| 62 | path = tmp_path / "freeze.yaml" |
| 63 | original = (FIXTURES / "freeze-artifact.yaml").read_text(encoding="utf-8") |
| 64 | path.write_text(original, encoding="utf-8") |
| 65 | parsed = parse_artifact(path, rel_path="freeze.yaml") |
| 66 | stamp = build_stamp(parsed, reviewer=ReviewerSettings("agent", "thinking-high", "local", "human"), kit_version="0.1.0") |
| 67 | with patch("tools.freeze_reviewer.stamp.atomic_write_text", side_effect=WriteFailure(path, OSError("disk"))): |
| 68 | with pytest.raises(WriteFailure): |
| 69 | write_stamp(path, parsed, stamp) |
| 70 | assert path.read_text(encoding="utf-8") == original |
| 71 | |
| 72 | |
| 73 | def test_digest_uses_bom_strip_and_lf(tmp_path: Path) -> None: |
| 74 | path = tmp_path / "freeze.yaml" |
| 75 | path.write_bytes(b"\xef\xbb\xbfphase: K\r\noutputs:\r\n - id: a\r\n path: docs/a.md\r\n frozen: true\r\n") |
| 76 | parsed = parse_artifact(path, rel_path="freeze.yaml") |
| 77 | digest = reference_digest(parsed) |
| 78 | assert digest.startswith("sha256:") |