diff --git a/reporter.py b/reporter.py index c8bd6db..4f863c2 100644 --- a/reporter.py +++ b/reporter.py @@ -137,8 +137,11 @@ class LocalStateReporter: ) if heal_result is not None: effective_heal_result = heal_result - if effective_last_heal_timestamp is None: - effective_last_heal_timestamp = datetime.now().isoformat() + effective_last_heal_timestamp = ( + last_heal_timestamp + if last_heal_timestamp is not None + else datetime.now().isoformat() + ) now_epoch = time.time() checks_total = max(0, self._get_int(previous, "checks_total", 0)) diff --git a/tests/test_reporter.py b/tests/test_reporter.py index c191ab7..ce5b353 100644 --- a/tests/test_reporter.py +++ b/tests/test_reporter.py @@ -147,6 +147,26 @@ class ReporterStateTests(unittest.TestCase): 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()