diff --git a/.woodpecker/main.yml b/.woodpecker/main.yml index 6ed90ac..e8cccdf 100644 --- a/.woodpecker/main.yml +++ b/.woodpecker/main.yml @@ -28,15 +28,7 @@ steps: python3 tests/consumers/analysis_consumer.py /tmp/control-analysis.json --require-transfers python3 tests/consumers/observation_consumer.py --snapshot --analysis /tmp/control-analysis.json /tmp/control-observation.json python3 tests/consumers/observation_consumer.py --validate-schema --analysis /tmp/control-analysis.json /tmp/control-events.jsonl - - | - set -eu - python3 - <<'PY' - import json, pathlib - events = [json.loads(line) for line in pathlib.Path("/tmp/control-events.jsonl").read_bytes().splitlines()] - snapshot = json.loads(pathlib.Path("/tmp/control-observation.json").read_text()) - assert snapshot["events"] == events, "stream and snapshot must be one event sequence" - print("stream and snapshot share one event sequence") - PY + - python3 tools/ci/verify_observation_stream.py /tmp/control-events.jsonl /tmp/control-observation.json - | set -eu cp /tmp/control-analysis.json /tmp/analysis-stale.json diff --git a/docs/ci.md b/docs/ci.md index 3c0307f..18d80d4 100644 --- a/docs/ci.md +++ b/docs/ci.md @@ -36,7 +36,7 @@ test: check + run the nested-loop control-flow conformance fixture validate_protocols.py / analysis_consumer.py --require-transfers observation_consumer.py (stream, snapshot, stale-Analysis rejection) - stream JSONL == snapshot events assertion + stream JSONL == snapshot events (tools/ci/verify_observation_stream.py) cargo test -p marqdo-cli --test docs_consistency cargo test -p marqdo-cli --test architecture_boundaries cargo test -p marqdo-cli --test stdlib_product diff --git a/tools/ci/test_verify_observation_stream.py b/tools/ci/test_verify_observation_stream.py new file mode 100644 index 0000000..692769b --- /dev/null +++ b/tools/ci/test_verify_observation_stream.py @@ -0,0 +1,81 @@ +"""Tests for the one-run Observation stream/snapshot verifier.""" + +import json +import pathlib +import subprocess +import sys +import tempfile +import unittest + +ROOT = pathlib.Path(__file__).resolve().parents[2] +SCRIPT = ROOT / "tools" / "ci" / "verify_observation_stream.py" + + +def event(seq: int, kind: str) -> dict: + return { + "schema": "marqdo.observation", + "schema_version": 1, + "run_id": "run-1", + "seq": seq, + "kind": kind, + "payload": {}, + } + + +def write(path: pathlib.Path, value) -> pathlib.Path: + if isinstance(value, list): + path.write_text( + "".join(json.dumps(item) + "\n" for item in value), encoding="utf-8" + ) + else: + path.write_text(json.dumps(value), encoding="utf-8") + return path + + +class VerifyObservationStreamTest(unittest.TestCase): + def run_script(self, stream, snapshot): + with tempfile.TemporaryDirectory() as name: + directory = pathlib.Path(name) + stream_path = write(directory / "stream.jsonl", stream) + snapshot_path = write(directory / "snapshot.json", snapshot) + return subprocess.run( + [sys.executable, str(SCRIPT), str(stream_path), str(snapshot_path)], + capture_output=True, + text=True, + ) + + def test_identical_stream_and_snapshot_pass(self): + events = [event(0, "stream.opened"), event(1, "run.completed")] + snapshot = { + "schema": "marqdo.observation", + "schema_version": 1, + "run": {"id": "run-1", "status": "ok", "source": None}, + "capture": {}, + "events": events, + } + result = self.run_script(events, snapshot) + self.assertEqual(result.returncode, 0, result.stderr) + + def test_divergent_event_fails(self): + stream = [event(0, "stream.opened"), event(1, "run.completed")] + snapshot = { + "schema": "marqdo.observation", + "schema_version": 1, + "run": {"id": "run-1", "status": "ok", "source": None}, + "capture": {}, + "events": [event(0, "stream.opened"), event(1, "run.failed")], + } + result = self.run_script(stream, snapshot) + self.assertEqual(result.returncode, 1, result.stderr) + self.assertIn("differ", result.stderr) + + def test_retired_format_fails(self): + events = [event(0, "stream.opened")] + retired = dict(events[0], schema="marqdo.execution-events") + result = self.run_script([retired], {"events": [events[0]]}) + self.assertEqual(result.returncode, 1) + self.assertIn("current Observation", result.stderr) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/ci/verify_observation_stream.py b/tools/ci/verify_observation_stream.py new file mode 100644 index 0000000..4113f79 --- /dev/null +++ b/tools/ci/verify_observation_stream.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +"""Assert that one Observation run's JSONL stream and JSON snapshot agree. + +Usage: + python3 tools/ci/verify_observation_stream.py STREAM.jsonl SNAPSHOT.json + +Exit 0 when the snapshot's events are exactly the stream's events, 1 when they +differ or a document is not a current Observation artifact. Both destinations +must come from the same execution; this check never re-runs the program. +""" +from __future__ import annotations + +import json +import pathlib +import sys + + +def load_events(path: pathlib.Path, *, snapshot: bool) -> list: + raw = path.read_bytes() + if snapshot: + document = json.loads(raw.decode("utf-8")) + if document.get("schema") != "marqdo.observation": + raise SystemExit(f"{path}: not a current Observation snapshot") + events = document.get("events") + else: + if raw and not raw.endswith(b"\n"): + raise SystemExit(f"{path}: interrupted JSONL record") + events = [json.loads(line) for line in raw.splitlines()] + if not isinstance(events, list) or not events: + raise SystemExit(f"{path}: no Observation events") + for event in events: + if event.get("schema") != "marqdo.observation": + raise SystemExit(f"{path}: event is not current Observation") + return events + + +def main(argv: list[str]) -> int: + if len(argv) != 3: + print( + "usage: verify_observation_stream.py STREAM.jsonl SNAPSHOT.json", + file=sys.stderr, + ) + return 2 + stream = load_events(pathlib.Path(argv[1]), snapshot=False) + snapshot = load_events(pathlib.Path(argv[2]), snapshot=True) + if stream != snapshot: + for index, (left, right) in enumerate(zip(stream, snapshot)): + if left != right: + print( + f"stream and snapshot differ at seq {left.get('seq', index)}", + file=sys.stderr, + ) + break + else: + print( + f"stream has {len(stream)} events, snapshot has {len(snapshot)}", + file=sys.stderr, + ) + return 1 + print(f"stream and snapshot share {len(stream)} events from one run") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main(sys.argv))