"""How a host's downtime reads on its page (#5756).""" from datetime import datetime, timedelta, timezone from types import SimpleNamespace from plugins.host_agent.downtime import describe, restart_grace_seconds NOW = datetime(2026, 10, 14, 12, 0, tzinfo=timezone.utc) def _row(kind, minutes_ago, lasted=None): start = NOW - timedelta(minutes=minutes_ago) return SimpleNamespace(kind=kind, started_at=start, ended_at=start + timedelta(minutes=lasted) if lasted is not None else None) def test_each_kind_reads_as_what_it_was(): assert describe(_row("reboot", 30, 2), NOW)["label"] == "Rebooted" assert describe(_row("unclean", 30, 9), NOW)["label"] == "Restarted without shutting down" assert describe(_row("lost_contact", 30, 20), NOW)["label"] == "Lost contact" assert describe(_row("agent_stop", 30, 1), NOW)["label"] == "Agent restarted" assert describe(_row("agent_stop", 3), NOW)["label"] == "Agent stopped" def test_one_still_going_counts_to_now(): got = describe(_row("reboot", 4), NOW) assert got["ongoing"] and got["seconds"] == 240.0 done = describe(_row("reboot", 30, 2), NOW) assert not done["ongoing"] and done["seconds"] == 120.0 def test_the_grace_comes_from_the_plugin_settings(): assert restart_grace_seconds({}) == 600 assert restart_grace_seconds({"PLUGINS": {"host_agent": {"reboot_grace_seconds": 120}}}) == 120