Files
gaokao-volunteer-system/tests/test_ops_alerts.py
Hermes Agent bf7cce90f9
Some checks failed
CI / pytest (Python 3.10) (push) Has been cancelled
CI / pytest (Python 3.11) (push) Has been cancelled
CI / pytest (Python 3.12) (push) Has been cancelled
feat(ops): add admin ops alert audit and watchdog alert sink
2026-06-15 18:17:30 +08:00

57 lines
1.7 KiB
Python

from __future__ import annotations
import json
from pathlib import Path
from data.notifications.ops_alerts import OpsAlertSink
def test_ops_alert_sink_appends_jsonl(tmp_path: Path):
log_path = tmp_path / "alerts.jsonl"
sink = OpsAlertSink(log_path=str(log_path), recipients=[])
result = sink.emit(
alert_type="test_alert",
title="Test Alert",
body="body",
details={"order_id": "O-1", "failed": 2},
)
assert result.log_written is True
assert result.emails_sent == 0
content = log_path.read_text(encoding="utf-8").strip().splitlines()
assert len(content) == 1
payload = json.loads(content[0])
assert payload["alert_type"] == "test_alert"
assert payload["details"]["order_id"] == "O-1"
def test_ops_alert_sink_can_use_fake_email_sender(tmp_path: Path):
log_path = tmp_path / "alerts.jsonl"
class FakeSender:
def __init__(self) -> None:
self.sent: list[dict[str, str]] = []
def send_report_ready(self, *, recipient: str, order_id: str, subject: str, body: str) -> dict[str, str]:
payload = {
"recipient": recipient,
"order_id": order_id,
"subject": subject,
"body": body,
}
self.sent.append(payload)
return payload
sender = FakeSender()
sink = OpsAlertSink(log_path=str(log_path), recipients=["ops@example.com"], email_sender=sender)
result = sink.emit(
alert_type="watchdog_failed",
title="watchdog failed",
body="something failed",
details={"order_id": "O-2"},
)
assert result.emails_sent == 1
assert len(sender.sent) == 1
assert sender.sent[0]["recipient"] == "ops@example.com"