# SPDX-License-Identifier: MIT import json from pathlib import Path import unittest from observe import BASE_NS, FIXTURES, POLICIES, fixture, parse_count # Explicit expectations in POLICY insertion order, separate from observation code. EXPECTED = { "identical-same-time": [0, 0, 0, 0, 1, 0], "identical-destination-plus-2s": [0, 0, 0, 0, 0, 0], "changed-same-time": [1, 1, 1, 0, 1, 0], "changed-destination-plus-400ms": [1, 1, 1, 0, 1, 0], "changed-destination-plus-1s": [1, 0, 1, 0, 0, 0], "changed-destination-plus-2s": [1, 0, 0, 0, 0, 0], "changed-destination-minus-400ms": [1, 1, 1, 0, 1, 0], "changed-destination-minus-1s": [1, 1, 1, 1, 1, 0], "changed-destination-minus-2s": [1, 1, 1, 1, 1, 0], "shorter-destination-plus-2s": [1, 0, 0, 0, 0, 0], "missing-destination": [1, 1, 1, 1, 1, 1], } class FixtureTests(unittest.TestCase): def test_matrix_identity(self): self.assertEqual(set(EXPECTED), set(FIXTURES)) self.assertTrue(all(len(row) == len(POLICIES) for row in EXPECTED.values())) def test_fixture_controls(self): src, dst, ns = fixture("changed-destination-plus-400ms") self.assertEqual(sum(a != b for a, b in zip(src, dst)), 1) self.assertEqual(len(src), 8192) self.assertEqual(ns, BASE_NS + 400_000_000) self.assertEqual(ns // 10**9, BASE_NS // 10**9) self.assertEqual(len(fixture("shorter-destination-plus-2s")[1]), 4096) self.assertIsNone(fixture("missing-destination")[1]) def test_invalid_fixture(self): with self.assertRaises(ValueError): fixture("typo") def test_statistics_parser(self): self.assertEqual(parse_count("Literal data: 8,192 bytes\n", "Literal data"), 8192) for text in ("", "Literal data: unknown", "Literal data: 1\nLiteral data: 2"): with self.assertRaises(ValueError): parse_count(text, "Literal data") def test_recorded_matrix(self): paths = sorted(Path(__file__).parent.glob("results-*.json")) self.assertTrue(paths, "Generate observations before validating them") for path in paths: data = json.loads(path.read_text()) self.assertEqual(len(data["cases"]), 66, path.name) seen = set() for row in data["cases"]: key = row["fixture"], row["policy"] self.assertNotIn(key, seen) seen.add(key) selected = EXPECTED[row["fixture"]][list(POLICIES).index(row["policy"])] with self.subTest(path=path.name, fixture=key): self.assertEqual(row["transferredFiles"], selected) self.assertEqual(row["exitCode"], 0) self.assertTrue(row["sourceUnchanged"]) self.assertEqual(row["byteEqualAfter"], bool(selected) or row["fixture"].startswith("identical")) before = row["before"] after = row["after"]["destination"] expected_hash = before["source"]["sha256"] if selected else before["destination"]["sha256"] self.assertEqual(after["sha256"], expected_hash) self.assertEqual(row["literalBytes"] + row["matchedBytes"], 8192 * selected) if not selected: expected = dict(before["destination"]) # A checksum equality skip can still apply --times metadata. if row["fixture"] == "identical-destination-plus-2s" and row["policy"] == "checksum": expected["mtimeNs"] = BASE_NS self.assertEqual(after, expected) self.assertEqual(seen, {(f, p) for f in FIXTURES for p in POLICIES}) if __name__ == "__main__": unittest.main()