Files
document-haness/scripts/tests/test_pipeline_run_supersession.py

100 lines
3.0 KiB
Python

"""Whole-pipeline run aggregation keeps old receipts historical.
A remediation creates a new receipt for the same Record. The older receipt must still be
validated as a receipt, but only the newest receipt may be compared with the current
publication bytes.
"""
import importlib.util
import json
from pathlib import Path
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
SCRIPT = ROOT / "scripts" / "verify-pipeline.py"
_spec = importlib.util.spec_from_file_location("verify_pipeline", SCRIPT)
vp = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(vp)
class _FakeVerifier:
def __init__(self):
self.calls = []
def verify(self, path, *, check_current_publication=True):
p = Path(path)
self.calls.append((p.parent.name, check_current_publication))
return (p.parent.name, check_current_publication)
class SupersededRunAggregation(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.root = Path(self.tmp.name)
self.fake = _FakeVerifier()
original = vp._load
vp._load = lambda *_args, **_kwargs: self.fake
self.addCleanup(setattr, vp, "_load", original)
def ledger(self, run_id, *, record, finished_at=None, updated_at=None):
path = self.root / "runs" / "demo" / run_id / "run.json"
path.parent.mkdir(parents=True, exist_ok=True)
data = {
"project": "demo",
"record": record,
"runId": run_id,
"stages": [],
}
if finished_at is not None:
data["finishedAt"] = finished_at
if updated_at is not None:
data["updatedAt"] = updated_at
path.write_text(json.dumps(data), encoding="utf-8")
return path
def test_only_latest_run_for_same_record_checks_current_publication(self):
self.ledger(
"old-run",
record="docs/demo/record.md",
finished_at="2026-09-21T01:00:00+00:00",
)
self.ledger(
"new-run",
record="docs/demo/record.md",
finished_at="2026-09-21T02:00:00+00:00",
)
vp.verify_runs(self.root)
self.assertEqual(
{"old-run": False, "new-run": True},
dict(self.fake.calls),
)
def test_single_run_still_checks_current_publication(self):
self.ledger(
"only-run",
record="docs/demo/only.md",
finished_at="2026-09-21T01:00:00+00:00",
)
vp.verify_runs(self.root)
self.assertEqual([("only-run", True)], self.fake.calls)
def test_run_id_is_deterministic_tie_breaker_when_timestamps_are_missing(self):
self.ledger("run-a", record="docs/demo/tie.md")
self.ledger("run-b", record="docs/demo/tie.md")
vp.verify_runs(self.root)
self.assertEqual(
{"run-a": False, "run-b": True},
dict(self.fake.calls),
)
if __name__ == "__main__":
unittest.main()