import copy import json from pathlib import Path import csv import io import unittest from run import FIELDS, expected_rows, parse_output, validate_capture, load_capture def encode(rows): stream = io.StringIO() writer = csv.DictWriter(stream, fieldnames=FIELDS) writer.writeheader() writer.writerows(rows) return stream.getvalue() class ObservationTests(unittest.TestCase): def test_complete_controls(self): self.assertEqual(parse_output(encode(expected_rows())), expected_rows()) def test_omitted_control_rejected(self): with self.assertRaises(ValueError): parse_output(encode(expected_rows()[:-1])) def test_wrong_observations_rejected(self): for index, field, value in [(3, "result", "B"), (1, "result", "B"), (4, "position_before_retry", 0), (3, "error_after_retry", 1), (4, "eof_before_retry", 1)]: with self.subTest(index=index, field=field): rows = expected_rows() rows[index][field] = value with self.assertRaises(ValueError): parse_output(encode(rows)) def test_wrong_schema_rejected(self): with self.assertRaises(ValueError): parse_output("result\nEOF\n") class CaptureTests(unittest.TestCase): def setUp(self): self.capture = json.loads(Path(__file__).with_name("observations.json").read_text()) def test_published_capture_and_different_replay_host(self): observed = copy.deepcopy(self.capture) observed["environment"]["release"] = "different-replay-kernel" observed["capturedAt"] = "2026-11-16T00:00:00+00:00" validate_capture(self.capture, observed) def test_numeric_type_mutations_rejected(self): for value in (False, 0.0): for field in ("rows", "runs"): with self.subTest(value=value, field=field): changed = copy.deepcopy(self.capture) rows = changed[field] if field == "rows" else changed[field][0]["rows"] rows[0]["grow"] = value with self.assertRaises(ValueError): validate_capture(changed, self.capture) def test_missing_provenance_rejected(self): for field in self.capture: with self.subTest(field=field): changed = copy.deepcopy(self.capture) del changed[field] with self.assertRaises(ValueError): validate_capture(changed, self.capture) def test_malformed_provenance_rejected(self): mutations = [("schemaVersion", True), ("schemaVersion", 1.0), ("capturedAt", "2026-11-15T21:00:00"), ("capturedAt", "not-a-date"), ("environment", {}), ("sourceSha256", "0" * 64), ("rows", []), ("runs", [])] for field, value in mutations: with self.subTest(field=field, value=value): changed = copy.deepcopy(self.capture) changed[field] = value with self.assertRaises(ValueError): validate_capture(changed, self.capture) for field, value in [("compiler", ""), ("libc", ["glibc"]), ("libc", ["glibc", ""]), ("system", False)]: with self.subTest(environment=field): changed = copy.deepcopy(self.capture) changed["environment"][field] = value with self.assertRaises(ValueError): validate_capture(changed, self.capture) def test_duplicate_json_keys_rejected(self): for text in ('{"schemaVersion": 2, "schemaVersion": 1}', '{"environment":{"compiler":"a","compiler":"b"}}'): with self.assertRaises(ValueError): load_capture(text) self.assertEqual(load_capture(json.dumps(self.capture)), self.capture) if __name__ == "__main__": unittest.main()