"""The dashboard SNMP widget (#5255, milestone 491): a verdict, devices not answering first with since when, status in words, two readings in units.""" from datetime import datetime, timedelta, timezone from tests._templates import render_plugin_template def _dev(name, reach, ok_minutes=None, key=()): now = datetime.now(timezone.utc) return {"id": f"id-{name}", "name": name, "reach": reach, "says": "No reply.", "polled_at": now - timedelta(seconds=30), "ok_at": now - timedelta(minutes=ok_minutes) if ok_minutes is not None else None, "key": [{"name": n, "text": t} for n, t in key]} async def test_a_silent_device_leads_with_since_when(app): devices = [_dev("ups", "down", 14, [("Charge", "100%")]), _dev("printer", "pending"), _dev("switch", "up", 1, [("Uptime", "12d 4h")])] async with app.test_request_context("/plugins/snmp/widget"): page = await render_plugin_template(app, "snmp", "snmp/widget.html", devices=devices, more=0, counts={"down": 1, "stale": 0, "pending": 1, "up": 1}, total=3) page = " ".join(page.split()) assert "ups isn't answering." in page and "no reply since" in page and ">14m ago<" in page assert page.count("Device") == 1 assert page.index('title="ups"') < page.index('title="printer"') < page.index('title="switch"') assert "not polled yet" in page and "Uptime 12d 4h" in page async def test_all_answering_says_so(app): async with app.test_request_context("/plugins/snmp/widget"): page = await render_plugin_template(app, "snmp", "snmp/widget.html", devices=[_dev("switch", "up", 1)], more=0, counts={"down": 0, "stale": 0, "pending": 0, "up": 1}, total=1) empty = await render_plugin_template(app, "snmp", "snmp/widget.html", devices=[], more=0, counts={}, total=0) assert "All 1 device answering." in page and "—" in page assert "No SNMP devices yet." in empty