import json import os import tempfile import unittest from datetime import datetime from unittest.mock import patch from reporter import LocalStateReporter class ReporterStateTests(unittest.TestCase): def test_update_status_writes_drift_and_fingerprint_fields(self): with tempfile.TemporaryDirectory() as tmp_dir: state_file = os.path.join(tmp_dir, "state.json") reporter = LocalStateReporter(state_file=state_file, node_name="n1") reporter.update_status( False, 10, last_error="boom", missing_count=2, extra_count=3, metadata_drift_count=1, last_drift_fingerprint="abc123", last_heal_result="failed", ) with open(state_file, "r") as f: payload = json.load(f) self.assertEqual("n1", payload["node_name"]) self.assertEqual("DESYNC_DETECTED", payload["status"]) self.assertEqual(10, payload["kernel_ban_count"]) self.assertEqual(2, payload["missing_count"]) self.assertEqual(3, payload["extra_count"]) self.assertEqual(1, payload["metadata_drift_count"]) self.assertEqual("abc123", payload["last_drift_fingerprint"]) self.assertEqual("failed", payload["last_heal_result"]) def test_update_status_reports_degraded_when_synced_with_unenforceable_ranges(self): with tempfile.TemporaryDirectory() as tmp_dir: state_file = os.path.join(tmp_dir, "state.json") reporter = LocalStateReporter(state_file=state_file, node_name="n1") reporter.update_status(True, 10, unenforceable_range_count=1) with open(state_file, "r") as f: degraded = json.load(f) reporter.update_status(True, 10) with open(state_file, "r") as f: healthy = json.load(f) self.assertEqual("DEGRADED", degraded["status"]) self.assertTrue(degraded["is_synced"]) self.assertEqual(1, degraded["unenforceable_range_count"]) self.assertEqual("HEALTHY", healthy["status"]) self.assertEqual(0, healthy["unenforceable_range_count"]) def test_update_status_preserves_last_heal_result_when_omitted(self): with tempfile.TemporaryDirectory() as tmp_dir: state_file = os.path.join(tmp_dir, "state.json") reporter = LocalStateReporter(state_file=state_file, node_name="n1") reporter.update_status( False, 7, last_heal_result="success", last_drift_fingerprint="oldfp", ) reporter.update_status( True, 7, missing_count=0, extra_count=0, metadata_drift_count=0, last_drift_fingerprint="", ) with open(state_file, "r") as f: payload = json.load(f) self.assertEqual("success", payload["last_heal_result"]) self.assertEqual("", payload["last_drift_fingerprint"]) def test_update_status_tracks_checks_and_heal_counters(self): with tempfile.TemporaryDirectory() as tmp_dir: state_file = os.path.join(tmp_dir, "state.json") reporter = LocalStateReporter(state_file=state_file, node_name="n1") reporter.update_status(True, 1, checks_increment=1) reporter.update_status(True, 1, checks_increment=1) reporter.update_status(False, 1, drift_events_increment=1) reporter.update_status(False, 1, heal_result="failed") reporter.update_status(True, 1, heal_result="success") with open(state_file, "r") as f: payload = json.load(f) self.assertEqual(2, payload["checks_total"]) self.assertEqual(1, payload["drift_events_total"]) self.assertEqual(2, payload["drift_heal_attempts_total"]) self.assertEqual(1, payload["drift_events_24h"]) self.assertEqual(2, payload["drift_heal_attempts_24h"]) self.assertEqual(2, payload["heals_24h"]) self.assertEqual(1, payload["heal_failures_24h"]) self.assertEqual(2, payload["heals_total"]) self.assertEqual(1, payload["heal_failures_total"]) self.assertEqual("success", payload["last_heal_result"]) self.assertIn("last_heal_timestamp", payload) datetime.fromisoformat(payload["last_heal_timestamp"]) self.assertGreaterEqual(payload["uptime_seconds"], 0) def test_update_status_can_show_suppressed_heal_gap(self): with tempfile.TemporaryDirectory() as tmp_dir: state_file = os.path.join(tmp_dir, "state.json") reporter = LocalStateReporter(state_file=state_file, node_name="n1") reporter.update_status(False, 5, checks_increment=1, drift_events_increment=1) reporter.update_status(False, 5, heal_result="success") reporter.update_status( False, 5, checks_increment=1, drift_events_increment=1, heal_suppressed=True ) with open(state_file, "r") as f: payload = json.load(f) self.assertEqual(2, payload["drift_events_total"]) self.assertEqual(1, payload["drift_heal_attempts_total"]) self.assertEqual(1, payload["heals_suppressed_total"]) self.assertEqual(2, payload["drift_events_24h"]) self.assertEqual(1, payload["drift_heal_attempts_24h"]) self.assertEqual(1, payload["heals_suppressed_24h"]) # Every drift is accounted for: a heal attempt or a suppressed heal. self.assertEqual( payload["drift_events_24h"], payload["drift_heal_attempts_24h"] + payload["heals_suppressed_24h"], ) @patch("reporter.time.time") def test_update_status_rolls_24h_window(self, time_mock): with tempfile.TemporaryDirectory() as tmp_dir: state_file = os.path.join(tmp_dir, "state.json") reporter = LocalStateReporter(state_file=state_file, node_name="n1") reporter.started_at_epoch = 0 # Initial write at hour 1000. time_mock.return_value = 1000 * 3600 reporter.update_status( False, 1, drift_events_increment=1, heal_result="failed", heal_suppressed=True ) # New write 25 hours later; previous hour should be pruned from 24h view. time_mock.return_value = (1000 + 25) * 3600 reporter.update_status(True, 1, checks_increment=1) with open(state_file, "r") as f: payload = json.load(f) self.assertEqual(0, payload["drift_events_24h"]) self.assertEqual(0, payload["drift_heal_attempts_24h"]) self.assertEqual(0, payload["heals_24h"]) self.assertEqual(0, payload["heal_failures_24h"]) self.assertEqual(0, payload["heals_suppressed_24h"]) def test_update_status_preserves_last_heal_timestamp_when_no_new_heal(self): with tempfile.TemporaryDirectory() as tmp_dir: state_file = os.path.join(tmp_dir, "state.json") reporter = LocalStateReporter(state_file=state_file, node_name="n1") reporter.update_status(False, 1, heal_result="failed") with open(state_file, "r") as f: first_payload = json.load(f) first_ts = first_payload["last_heal_timestamp"] reporter.update_status(True, 1, checks_increment=1) with open(state_file, "r") as f: second_payload = json.load(f) self.assertEqual(first_ts, second_payload["last_heal_timestamp"]) @patch("reporter.datetime") def test_update_status_refreshes_last_heal_timestamp_on_each_heal(self, datetime_mock): with tempfile.TemporaryDirectory() as tmp_dir: state_file = os.path.join(tmp_dir, "state.json") reporter = LocalStateReporter(state_file=state_file, node_name="n1") datetime_mock.now.side_effect = [ datetime.fromisoformat("2026-03-06T10:00:00"), datetime.fromisoformat("2026-03-06T10:00:00"), datetime.fromisoformat("2026-03-06T10:05:00"), datetime.fromisoformat("2026-03-06T10:05:00"), ] reporter.update_status(False, 1, heal_result="failed") reporter.update_status(False, 1, heal_result="success") with open(state_file, "r") as f: payload = json.load(f) self.assertEqual("2026-03-06T10:05:00", payload["last_heal_timestamp"]) if __name__ == "__main__": unittest.main()