"""Standalone checks for stasis_redo.py and its adjacent published JSON.""" from dataclasses import FrozenInstanceError from itertools import product import json from pathlib import Path import unittest from stasis_redo import ( EXPECTED, INITIAL, LOG, Write, copy_first_to_second, replay, result, start_states, state_reading_example, ) class RedoTests(unittest.TestCase): def test_exact_forward_result(self): self.assertEqual(replay(INITIAL, LOG), EXPECTED) def test_enumerated_states_are_exact_not_random(self): self.assertEqual(set(start_states()), {bytes(x) for x in product((0, 1, 3), (0, 2), (7,))}) def test_individual_writes_are_idempotent(self): for state in start_states(): for entry in LOG: once = replay(state, (entry,)) self.assertEqual(replay(once, (entry,)), once) def test_complete_log_restores_every_start_and_is_repeatable(self): for state in start_states(): self.assertEqual(replay(state, LOG), EXPECTED) self.assertEqual(replay(state, LOG + LOG), EXPECTED) def test_reversal_is_not_harmless_deduplication(self): for state in start_states(): self.assertEqual(replay(state, tuple(reversed(LOG))), bytes((1, 2, 7))) self.assertNotEqual(replay(state, tuple(reversed(LOG))), EXPECTED) def test_stale_retry_after_newer_write_breaks_result(self): self.assertEqual(replay(EXPECTED, LOG[:1]), bytes((1, 2, 7))) def test_missing_entry_can_leave_an_older_byte(self): self.assertEqual(replay(INITIAL, LOG[1:]), bytes((3, 0, 7))) self.assertEqual(replay(bytes((0, 2, 7)), LOG[1:]), EXPECTED) def test_every_interrupted_replay_path_recovers(self): counts = result()["interruptedReplay"] self.assertEqual(counts["pathsChecked"], 6 * 3 * 8) self.assertEqual(counts["pathsRestored"], counts["pathsChecked"]) self.assertFalse(counts["countsAreDistinctStates"]) def test_unlogged_corruption_is_not_repaired(self): self.assertEqual(replay(bytes((0, 0, 8)), LOG), bytes((3, 2, 8))) self.assertNotEqual(replay(bytes((0, 0, 8)), LOG), EXPECTED) def test_state_reading_copy_is_idempotent_for_all_byte_pairs(self): for source, destination in product(range(256), repeat=2): state = bytes((source, destination, 7)) copied = copy_first_to_second(state) self.assertEqual(copy_first_to_second(copied), copied) self.assertEqual(copied, bytes((source, source, 7))) def test_ordered_state_reading_replay_can_corrupt_a_settled_result(self): overwrite = Write(20, 0, bytes((3,))) initial = bytes((2, 0, 7)) forward = replay(copy_first_to_second(initial), (overwrite,)) repeated = replay(copy_first_to_second(forward), (overwrite,)) self.assertEqual(forward, EXPECTED) self.assertEqual(repeated, bytes((3, 3, 7))) self.assertNotEqual(repeated, forward) def test_logged_copy_value_is_not_recomputed_during_redo(self): blind_log = (Write(10, 1, bytes((2,))), Write(20, 0, bytes((3,)))) for state in product((2, 3), (0, 2), (7,)): self.assertEqual(replay(bytes(state), blind_log), EXPECTED) self.assertEqual(replay(bytes(state), blind_log + blind_log), EXPECTED) def test_state_reading_example_has_separate_denominators_and_trace(self): example = state_reading_example() self.assertEqual(example["initial"], [2, 0, 7]) self.assertEqual(example["afterForwardCopy"], [2, 2, 7]) self.assertEqual(example["afterForwardSequence"], [3, 2, 7]) self.assertEqual(example["afterRepeatedCopy"], [3, 3, 7]) self.assertEqual(example["afterRepeatedSequence"], [3, 3, 7]) self.assertEqual(example["totalStates"], 4) self.assertEqual(example["stateReadingMatchingStates"], 2) self.assertEqual(example["blindMatchingStates"], 4) self.assertEqual({tuple(row["start"]) for row in example["rows"]}, set(product((2, 3), (0, 2), (7,)))) for row in example["rows"]: source = row["start"][0] self.assertEqual(row["stateReadingEnd"], [3, source, 7]) self.assertEqual(row["blindEnd"], [3, 2, 7]) def test_state_reading_copy_rejects_invalid_shape_and_type(self): for state in (b"", b"ab", b"abcd", bytearray(b"abc"), [2, 0, 7], None): with self.subTest(state=state), self.assertRaises(ValueError): copy_first_to_second(state) def test_inputs_are_immutable(self): before = bytes(INITIAL) replay(before, LOG) self.assertEqual(before, INITIAL) with self.assertRaises(FrozenInstanceError): LOG[0].offset = 1 def test_invalid_geometry_and_values_are_rejected(self): for args in ((0, 0, b"x"), (True, 0, b"x"), (1, -1, b"x"), (1, True, b"x"), (1, 0, b""), (1, 0, "x")): with self.subTest(args=args), self.assertRaises(ValueError): Write(*args) with self.assertRaises(ValueError): replay(INITIAL, (Write(1, 3, b"x"),)) with self.assertRaises(ValueError): replay(INITIAL, (object(),)) with self.assertRaises(ValueError): replay([0, 0, 7], LOG) def test_summary_denominators_and_controls(self): rows = result()["scenarios"] self.assertEqual([r["matchingStates"] for r in rows], [6, 6, 0, 0, 3]) self.assertTrue(all(r["totalStates"] == 6 for r in rows)) def test_published_result_is_exact_and_not_stale(self): path = Path(__file__).with_name("stasis-redo.json") expected = json.dumps(result(), indent=2) + "\n" self.assertEqual(path.read_text(encoding="utf-8"), expected) if __name__ == "__main__": unittest.main(verbosity=2)