"""Tests for ticket_text.py: the Markdown Gandalf puts in tickets.""" import os import re import sys from datetime import datetime, timedelta import pytest sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) import ticket_text as tt # noqa: E402 NOW = datetime(2026, 11, 10, 18, 0) def test_duration(): assert tt.duration(42) == '42 s' assert tt.duration(600) == '10 min' assert tt.duration(3600) == '1 h' assert tt.duration(3 * 3600 + 120) == '3 h 2 min' assert tt.duration(3 * 86400 + 7200) == '3 d 2 h' def test_speed(): assert tt.speed(100) == '100 Mbps' assert tt.speed(2500) == '2.5 Gbps' assert tt.speed(10000) == '10 Gbps' assert tt.speed(0) == '' def test_device_url_escapes_names(): assert tt.device_url('https://g', 'USW Flex Mini', 3) == 'https://g/device/USW%20Flex%20Mini#port-3' assert tt.device_url('', 'x') == '' def test_port_text(): assert tt.port_text('sw', 'Port 4', {'port_idx': 4, 'up': True, 'speed_mbps': 1000, 'poe_power': 4.21}) == \ 'sw port 4: up at 1 Gbps, PoE 4.2 W' assert tt.port_text('sw', 'Cam', {'port_idx': 9, 'up': False, 'sfp': {'found': True, 'kind': 'optic', 'part': 'SR', 'rx_fault': True}}) == \ 'sw port 9 "Cam": down, module: optic SR, **module reports a fault**' def test_description_skips_empty_facts_and_sections(): text = tt.description('Alert', [('Host', 'a'), ('Blank', '')], 'Summary.', ['One', 'Two'], [('Empty', []), ('Links', ['- x'])]) assert text.startswith('## Alert\n\n- **Host:** a\n\nSummary.') assert 'Blank' not in text and '### Empty' not in text assert '1. One\n2. Two' in text and '### Links\n- x' in text def test_history_lines(): assert tt.history_lines({'last_7d': 0, 'last_30d': 0}, NOW) == ['- First time in the last 30 days.'] lines = tt.history_lines({'last_7d': 1, 'last_30d': 2, 'last_first_seen': NOW - timedelta(hours=26), 'last_resolved_at': NOW - timedelta(hours=25, minutes=55)}, NOW) assert lines[0].startswith('- Happened 1 time in the last 7 days and 2') assert lines[1] == '- Last cleared 2026-11-09 16:05 UTC (25 h 55 min ago), after 5 min.' def test_recovery_comment(): text = tt.recovery_comment({'event_type': 'interface_down', 'target_name': 'large1', 'target_detail': 'eno1', 'first_seen': NOW - timedelta(minutes=7), 'resolved_at': NOW, 'consecutive_failures': 3}, NOW, 'https://g/device/large1') assert text.startswith('**✅ Cleared**: interface down on large1 eno1.') assert '- **Lasted:** 7 min (first seen 2026-11-10 17:53 UTC)' in text assert '- **Gandalf:** https://g/device/large1' in text assert 'leaves this ticket open' in text class TestTitle: """The shared title standard (thor#1, gandalf#66): [source][auto][app][area] Summary [scope][env][type].""" @staticmethod def _hash_parts(title): # What Tinker's TicketHash.php reads from an automated network title: the first tag, the # environment and scope tags, and the text outside the tags. Same title parts, same ticket. tags = re.findall(r'\[([^\]]+)\]', title) env = sorted(t for t in tags if t in ('production', 'development', 'staging', 'single-node', 'cluster-wide')) heading = re.sub(r'\[[^\]]*\]', '', title).strip(' -\t') return tags[0], '[auto]' in title, 'network' in title.lower(), env, heading def test_format(self): assert tt.title('monitor-01', 'Interface enp3s0 link-down') == \ '[monitor-01][auto][gandalf][network] Interface enp3s0 link-down [single-node][production][issue]' assert tt.title('att-modem', 'WAN down', 'cluster-wide').endswith('WAN down [cluster-wide][production][issue]') def test_hashes_like_the_old_format(self): for source, summary, scope in (('monitor-01', 'Interface enp3s0 link-down', 'single-node'), ('att-modem', 'Internet degraded (high latency or packet loss)', 'cluster-wide'), ('24RJ45-POE', 'Cable C07 does not match the cutsheet', 'single-node')): old = f'[{source}][auto][production][issue][network][{scope}] {summary}' assert self._hash_parts(tt.title(source, summary, scope)) == self._hash_parts(old) def test_unknown_scope_is_refused(self): with pytest.raises(ValueError): tt.title('x', 'y', 'rack')