7891984cd1
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
"""AlertHit[] → 飞书消息文本:分组 / 截断 / 含关键词。"""
|
||
from __future__ import annotations
|
||
|
||
from app.services.compare_alert import AlertHit
|
||
from app.services.compare_alert_format import ALERT_KEYWORD, format_alert_message
|
||
|
||
|
||
def _hits(n, alert_type="T1"):
|
||
return [
|
||
AlertHit(trace_id=f"t{i}", alert_type=alert_type, reason="比价过程出错", app_version="v0.3.4")
|
||
for i in range(n)
|
||
]
|
||
|
||
|
||
def test_contains_keyword_and_count():
|
||
msg = format_alert_message(
|
||
_hits(2), window_label="2026-08-04 08:00–08:30",
|
||
max_detail_per_type=20, max_total=50,
|
||
)
|
||
assert ALERT_KEYWORD in msg
|
||
assert "本期触发 2 条" in msg
|
||
assert "系统技术失败 2 条" in msg
|
||
assert "t0" in msg and "v0.3.4" in msg
|
||
|
||
|
||
def test_group_by_type():
|
||
hits = _hits(1, "T1") + _hits(1, "T6") + _hits(1, "T5")
|
||
msg = format_alert_message(hits, window_label="w", max_detail_per_type=20, max_total=50)
|
||
assert "系统技术失败 1 条" in msg
|
||
assert "商品识别失败 1 条" in msg
|
||
assert "深度放弃(cancelled) 1 条" in msg
|
||
|
||
|
||
def test_per_type_truncation():
|
||
msg = format_alert_message(_hits(25), window_label="w", max_detail_per_type=20, max_total=50)
|
||
assert msg.count("t0") == 1
|
||
assert "另有 5 条" in msg
|
||
|
||
|
||
def test_total_truncation_counts_only():
|
||
msg = format_alert_message(_hits(60), window_label="w", max_detail_per_type=20, max_total=50)
|
||
assert "系统技术失败 60 条" in msg
|
||
assert "t0" not in msg
|
||
assert "分析库" in msg
|