Files
awoooi/apps/api/tests/test_agent99_dispatch_receipt_projection.py

109 lines
3.7 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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.
from __future__ import annotations
import pytest
from src.services import telegram_gateway as telegram_gateway_module
@pytest.mark.asyncio
async def test_telegram_projects_correlated_receipt_without_dispatch(
monkeypatch,
) -> None:
calls: list[tuple[str, str]] = []
correlated_receipt = {
"schema_version": "agent99_controlled_dispatch_receipt_v1",
"status": "dispatch_accepted_verifier_pending",
"identity": {
"incident_id": "INC-20260711-11C751",
"run_id": "7cf8fdf7-0966-5ac6-95b2-09e32808b248",
"trace_id": "00-11111111111111111111111111111111-2222222222222222-01",
"work_item_id": "agent99-dispatch:awoooi:INC-20260711-11C751:recovery",
"idempotency_key": "agent99-controlled:stable-key",
},
"dispatch_receipt": {
"status": "accepted_queue_persisted",
"transport": "relay",
"accepted": True,
"inbox_triggered": True,
"queue_accepted": True,
"dispatch_identity_matched": True,
},
"receipt_persisted": True,
"runtime_execution_authorized": False,
"runtime_closure_verified": False,
"verifier": {"status": "pending"},
"learning_writeback": {"status": "pending_verifier"},
}
async def fake_read(*, project_id: str, incident_id: str):
calls.append((project_id, incident_id))
return correlated_receipt
monkeypatch.setattr(
telegram_gateway_module,
"read_agent99_dispatch_receipt",
fake_read,
)
projection = (
await telegram_gateway_module._mirror_ai_automation_alert_card_to_agent99(
"\n".join(
[
" ACTION REQUIRED | <b>低風險</b>",
"📋 <code>INC-20260711-11C751</code>",
"🎯 資源:<code>cold-start-gate</code>",
"🏷️ 分類:<b>general</b>",
"🤖 <b>AI 自動化鏈路</b>",
]
),
source="unit-test",
)
)
assert calls == [("awoooi", "INC-20260711-11C751")]
assert projection is not None
assert projection["schema_version"] == (
"telegram_agent99_dispatch_receipt_projection_v1"
)
assert projection["dispatch_performed"] is False
assert projection["accepted"] is True
assert projection["incident_id"] == "INC-20260711-11C751"
assert projection["run_id"] == correlated_receipt["identity"]["run_id"]
assert projection["trace_id"] == correlated_receipt["identity"]["trace_id"]
assert projection["idempotency_key"] == (
correlated_receipt["identity"]["idempotency_key"]
)
assert projection["runtime_closure_verified"] is False
@pytest.mark.asyncio
async def test_telegram_unbound_card_fails_closed_without_receipt_read(
monkeypatch,
) -> None:
reads: list[str] = []
async def unexpected_read(*, project_id: str, incident_id: str):
reads.append(f"{project_id}:{incident_id}")
return None
monkeypatch.setattr(
telegram_gateway_module,
"read_agent99_dispatch_receipt",
unexpected_read,
)
card = telegram_gateway_module.format_aiops_signal_alert_card(
"SourceProviderIngestionStale provider_freshness_signal freshness "
"window timeout Sentry SigNoz provider freshness last seen missing "
"direct ref verifier readback"
)
projection = await telegram_gateway_module._mirror_ai_automation_alert_card_to_agent99(
card,
source="unit-test",
)
assert projection is not None
assert projection["dispatch_performed"] is False
assert projection["status"] == "receipt_unbound_fail_closed"
assert reads == []