Files
gandalf/tests/test_attention.py
jaredandClaude Opus 5.5 c535bfd9fb Dashboard: one Needs attention list from every page; say what GANDALF stands for
attention.build gathers alerts, power faults, cutsheet mismatches, UniFi port names,
undocumented links, labels to print and rack overlaps into Broken now and Tidy up,
each linking to where it is fixed. /api/attention serves it; the dashboard renders it
and refreshes it with everything else. The header, help dialog, footer and page
description spell out Global Advanced Network Detection And Link Facilitator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 14:56:13 -05:00

87 lines
4.3 KiB
Python

"""The dashboard's "Needs attention" list (attention.py)."""
import attention
def _alert(target, severity='critical', **kw):
return dict({'target_name': target, 'severity': severity, 'is_suppressed': False}, **kw)
def _outlet(index, on=True, target=None, power_w=10.0, match=None):
return {'index': index, 'on': on, 'target': target, 'power_w': power_w,
'match': match or ('name' if target else 'none')}
class TestAlerts:
def test_one_line_for_all_alerts_with_the_targets(self):
out = attention.build([_alert('pbs'), _alert('24RJ45-POE', 'warning')], [], None)
[line] = out['broken']
assert line['text'] == '2 active alerts (1 critical)'
assert line['severity'] == 'bad'
assert line['detail'] == '24RJ45-POE, pbs'
assert line['href'] == '#events-table-wrap'
def test_suppressed_and_info_alerts_are_left_out(self):
events = [_alert('pbs', is_suppressed=True), _alert('large1', 'info')]
assert attention.build(events, [], None)['broken'] == []
def test_warnings_only_is_amber(self):
[line] = attention.build([_alert('x', 'warning')], [], None)['broken']
assert line['severity'] == 'warn' and line['text'] == '1 active alert'
class TestPower:
def test_frozen_readings_and_an_off_outlet_feeding_a_device(self):
power = [{'node': 'RPDU', 'stale_since': '2026-11-15 05:26:54 UTC',
'outlets': [_outlet(19, on=False, target='pbs'), _outlet(3, on=False)]}]
broken = attention.build([], power, None)['broken']
assert [b['text'] for b in broken] == ['RPDU outlet 19 is off, and it feeds pbs',
'RPDU: outlet watts are frozen']
def test_powered_outlets_with_nothing_assigned(self):
power = [{'node': 'RPDU', 'outlets': [
_outlet(1, target='large1'), _outlet(2, power_w=40), _outlet(3, power_w=1),
_outlet(4, power_w=40, match='cleared'), _outlet(5, on=False, power_w=0)]}]
[line] = attention.build([], power, None)['tidy']
assert line['text'] == '1 powered outlet with no device assigned'
assert line['detail'] == 'RPDU 2'
class TestCutsheet:
VIEW = {
'cables': [{'id': 1, 'label': 'C01', 'status': 'installed', 'problem': True},
{'id': 2, 'label': 'C02', 'status': 'retired', 'problem': True},
{'id': 3, 'label': 'C03', 'status': 'installed', 'problem': False}],
'suggestions': [{}, {}, {}],
'port_names': [{'kind': 'wrong', 'switch': 'POE', 'port_idx': 11, 'port_name': 'Large1 Management'},
{'kind': 'empty', 'switch': 'POE', 'port_idx': 20, 'port_name': 'WAP'},
{'kind': 'unnamed', 'switch': 'POE', 'port_idx': 4, 'port_name': 'Port 4'}],
'labels': {'cables': [{}, {}], 'ports': [{}]},
}
def test_every_cutsheet_todo(self):
out = attention.build([], [], self.VIEW)
assert [b['text'] for b in out['broken']] == ['1 cable not where the cutsheet says']
assert out['broken'][0]['detail'] == 'C01'
assert out['broken'][0]['href'].startswith('/cutsheet?problems=1')
tidy = {t['text']: t for t in out['tidy']}
assert set(tidy) == {'1 UniFi port name pointing at the wrong device', '1 named port with nothing plugged in',
'3 live links not on the cutsheet', '3 labels to print and stick on'}
assert tidy['1 UniFi port name pointing at the wrong device']['detail'] == 'POE port 11 "Large1 Management"'
# Worst first
assert out['tidy'][0]['severity'] == 'warn'
def test_unnamed_ports_alone_are_not_worth_a_line(self):
view = {'port_names': [{'kind': 'unnamed', 'switch': 'POE', 'port_idx': 4, 'port_name': 'Port 4'}]}
assert attention.build([], [], view) == {'broken': [], 'tidy': []}
class TestRack:
def test_overlaps_and_items_outside_the_rack(self):
racks = {'Rack 1': {'overlaps': [{'name': 'NAS', 'units': 'U5-6', 'with': ['storage-01']}],
'outside': [{'name': 'Shelf', 'units': 'U43'}]}}
texts = [t['text'] for t in attention.build([], [], None, racks)['tidy']]
assert texts == ['NAS overlaps storage-01 at U5-6', 'Shelf at U43 is outside Rack 1']
def test_nothing_to_report(self):
assert attention.build([], [], {}, {}) == {'broken': [], 'tidy': []}