import tempfile import unittest from datetime import datetime from unittest.mock import patch from healing import should_heal, execute_heal_and_verify class FakeReporter: def __init__(self): self.update_calls = [] def update_status(self, is_synced, ban_count, last_error=None, **kwargs): self.update_calls.append( { "is_synced": is_synced, "ban_count": ban_count, "last_error": last_error, **kwargs, } ) class FakeAuditor: def __init__(self, heal_ok=True, reconcile_result=None, reconcile_exception=None): self.heal_ok = heal_ok self.reconcile_result = reconcile_result self.reconcile_exception = reconcile_exception self.last_heal_drift = None def heal(self): return self.heal_ok def heal_with_drift(self, drift): self.last_heal_drift = drift return self.heal_ok def reconcile(self): if self.reconcile_exception: raise self.reconcile_exception return self.reconcile_result class HealGateTests(unittest.TestCase): def test_should_heal_suppresses_identical_recent_fingerprint(self): now = datetime.now().isoformat() with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp: tmp.write( f"{now} - HEAL: drift_fingerprint=abc123 heal_result=success post_drift_fingerprint=\n" ) path = tmp.name self.assertFalse(should_heal(path, "abc123", min_interval=900)) def test_should_heal_allows_different_fingerprint(self): now = datetime.now().isoformat() with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp: tmp.write( f"{now} - HEAL: drift_fingerprint=abc123 heal_result=success post_drift_fingerprint=\n" ) path = tmp.name self.assertTrue(should_heal(path, "xyz999", min_interval=900)) def test_should_heal_reads_recent_heal_from_tail(self): now = datetime.now().isoformat() with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp: for idx in range(4000): tmp.write(f"noise line {idx}\n") tmp.write( f"{now} - HEAL: drift_fingerprint=tail123 heal_result=success post_drift_fingerprint=\n" ) path = tmp.name self.assertFalse(should_heal(path, "tail123", min_interval=900)) class PostHealVerificationTests(unittest.TestCase): @patch("healing.time.sleep", return_value=None) def test_execute_heal_and_verify_success(self, _sleep): reporter = FakeReporter() post_drift = { "reason": "in_sync", "missing_count": 0, "extra_count": 0, "metadata_drift_count": 0, "fingerprint": "", } auditor = FakeAuditor(heal_ok=True, reconcile_result=(True, 12, post_drift)) heal_result, returned_post_drift = execute_heal_and_verify( auditor, reporter, count=12, drift={"fingerprint": "pre123"}, heal_verify_delay=0, ) self.assertEqual("success", heal_result) self.assertEqual(post_drift, returned_post_drift) self.assertEqual(1, len(reporter.update_calls)) self.assertTrue(reporter.update_calls[0]["is_synced"]) self.assertEqual(12, reporter.update_calls[0]["ban_count"]) self.assertEqual("success", reporter.update_calls[0]["heal_result"]) self.assertEqual("", reporter.update_calls[0]["last_drift_fingerprint"]) self.assertEqual({"fingerprint": "pre123"}, auditor.last_heal_drift) @patch("healing.time.sleep", return_value=None) def test_execute_heal_and_verify_restart_failed(self, _sleep): reporter = FakeReporter() auditor = FakeAuditor(heal_ok=False, reconcile_result=(True, 0, {})) heal_result, returned_post_drift = execute_heal_and_verify( auditor, reporter, count=7, drift={"fingerprint": "pre123"}, heal_verify_delay=0, ) self.assertEqual("restart_failed", heal_result) self.assertEqual("not_verified", returned_post_drift["reason"]) self.assertEqual(1, len(reporter.update_calls)) self.assertFalse(reporter.update_calls[0]["is_synced"]) self.assertEqual(7, reporter.update_calls[0]["ban_count"]) self.assertEqual("Bouncer restart failed", reporter.update_calls[0]["last_error"]) self.assertEqual("restart_failed", reporter.update_calls[0]["heal_result"]) @patch("healing.time.sleep", return_value=None) def test_execute_heal_and_verify_verify_exception(self, _sleep): reporter = FakeReporter() auditor = FakeAuditor(heal_ok=True, reconcile_exception=RuntimeError("boom")) heal_result, returned_post_drift = execute_heal_and_verify( auditor, reporter, count=7, drift={"fingerprint": "pre123"}, heal_verify_delay=0, ) self.assertEqual("verify_error", heal_result) self.assertEqual("verify_error", returned_post_drift["reason"]) self.assertEqual(1, len(reporter.update_calls)) self.assertFalse(reporter.update_calls[0]["is_synced"]) self.assertEqual(7, reporter.update_calls[0]["ban_count"]) self.assertIn("Post-heal verify error: boom", reporter.update_calls[0]["last_error"]) self.assertEqual("verify_error", reporter.update_calls[0]["heal_result"]) if __name__ == "__main__": unittest.main()