fix(telegram): preserve incident history html output
All checks were successful
Code Review / ai-code-review (push) Successful in 11s
CD Pipeline / tests (push) Successful in 1m1s
CD Pipeline / build-and-deploy (push) Successful in 3m27s
CD Pipeline / post-deploy-checks (push) Successful in 1m29s

This commit is contained in:
Your Name
2026-05-14 23:33:43 +08:00
parent f4a8390dc0
commit 65001da0d8
6 changed files with 218 additions and 5 deletions

View File

@@ -53,6 +53,51 @@ def test_auto_repair_status_line_distinguishes_auto_resolved() -> None:
assert "CPU 92%->30%" in result
def test_telegram_html_chunks_preserve_complete_lines() -> None:
"""歷史/詳情長訊息不得用 text[:500] 切壞 HTML tag。"""
lines = [
"📊 <b>事件歷史統計</b>",
"",
"<code>INC-20260513-79ED5E</code>",
"🧭 <b>DB Truth-chain</b>",
"階段: <code>blocked</code> / <code>manual_required</code>",
] * 90
chunks = telegram_gateway_module._telegram_html_chunks(lines, limit=900)
assert len(chunks) > 1
assert all(len(chunk) <= 900 for chunk in chunks)
assert all(chunk.count("<code>") == chunk.count("</code>") for chunk in chunks)
assert all(chunk.count("<b>") == chunk.count("</b>") for chunk in chunks)
@pytest.mark.asyncio
async def test_send_html_line_message_falls_back_to_plain_text_on_parse_error(monkeypatch):
"""Telegram HTML parse 400 時要送純文字 fallback不可回報成歷史查詢失敗。"""
sent_requests = []
gateway = TelegramGateway()
async def fake_send_request(method, payload):
sent_requests.append((method, payload))
if payload.get("parse_mode") == "HTML":
raise telegram_gateway_module.TelegramGatewayError("HTTP error: 400")
return {"ok": True}
monkeypatch.setattr(gateway, "_send_request", fake_send_request)
await gateway._send_html_line_message(
["📊 <b>事件歷史統計</b>", "階段: <code>blocked</code>"],
chat_id="chat",
failure_context="test_history",
)
assert len(sent_requests) == 2
assert sent_requests[0][1]["parse_mode"] == "HTML"
assert "parse_mode" not in sent_requests[1][1]
assert "<code>" not in sent_requests[1][1]["text"]
assert "blocked" in sent_requests[1][1]["text"]
class TestTelegramMessageFormat:
"""測試現有 TelegramMessage 格式化"""