Files
planet/backend/tests/test_situational_alert_ai_brief.py
rayd1o 81970a1d05
Some checks failed
ci / backend (push) Has been cancelled
ci / frontend (push) Has been cancelled
ci / delivery (push) Has been cancelled
release / images (push) Has been cancelled
release: bump version to 0.60.0
2026-05-17 02:50:42 +08:00

72 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pytest
from app.services.situational_alert_ai_brief import build_situational_alert_brief_request
class _SingleUseScalarResult:
def __init__(self, value=0, rows=None):
self.value = value
self.rows = rows or []
self.scalar_calls = 0
def scalar(self):
self.scalar_calls += 1
if self.scalar_calls > 1:
raise AssertionError("scalar result was consumed more than once")
return self.value
def fetchall(self):
return self.rows
def scalar_one_or_none(self):
return None
def scalars(self):
rows = self.rows
class _Scalars:
def all(self):
return rows
return _Scalars()
class _FakeBriefSession:
def __init__(self):
self._results = [
_SingleUseScalarResult(3),
_SingleUseScalarResult(2),
_SingleUseScalarResult(rows=[]),
_SingleUseScalarResult(rows=[]),
_SingleUseScalarResult(rows=[]),
_SingleUseScalarResult(4),
_SingleUseScalarResult(1),
_SingleUseScalarResult(rows=[]),
_SingleUseScalarResult(rows=[]),
_SingleUseScalarResult(5),
_SingleUseScalarResult(2),
_SingleUseScalarResult(rows=[]),
_SingleUseScalarResult(),
]
async def execute(self, _query):
return self._results.pop(0)
@pytest.mark.asyncio
async def test_situational_alert_brief_builder_reuses_counts_without_reconsuming_results(monkeypatch):
monkeypatch.setattr(
"app.services.situational_alert_ai_brief.get_latest_bgp_brief_record",
lambda: None,
)
request, facts, context = await build_situational_alert_brief_request(_FakeBriefSession())
assert request.title == "态势告警 AI 简报"
assert "总告警 3 条active 2 条" in facts[0]
assert "累计 incidents 4 条active incidents 1 条" in facts[1]
assert "累计 anomalies 5 条active anomalies 2 条" in facts[2]
assert context["active_system_alerts"] == 2
assert context["active_bgp_incidents"] == 1
assert context["active_bgp_anomalies"] == 2