from __future__ import annotations

import json

from fastapi import FastAPI
from fastapi.testclient import TestClient

from src.api.v1.agents import router


def test_agent_telegram_action_required_digest_policy_endpoint_returns_committed_snapshot():
    app = FastAPI()
    app.include_router(router, prefix="/api/v1")
    client = TestClient(app)

    response = client.get("/api/v1/agents/agent-telegram-action-required-digest-policy")

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "ai_agent_telegram_action_required_digest_policy_v1"
    assert data["program_status"]["overall_completion_percent"] == 68
    assert data["program_status"]["current_task_id"] == "P2-402D"
    assert data["program_status"]["next_task_id"] == "P2-402E"
    assert data["program_status"]["read_only_mode"] is True
    assert data["operation_boundaries"]["telegram_direct_send_allowed"] is False
    assert data["operation_boundaries"]["telegram_test_message_allowed"] is False
    assert data["operation_boundaries"]["telegram_gateway_queue_write_allowed"] is False
    assert data["operation_boundaries"]["alertmanager_route_change_allowed"] is False
    assert data["operation_boundaries"]["telegram_receiver_change_allowed"] is False
    assert data["operation_boundaries"]["conversation_transcript_allowed"] is False
    assert data["approval_boundaries"]["telegram_digest_send_approved"] is False
    assert data["rollups"]["rule_count"] == 8
    assert data["rollups"]["channel_count"] == 3
    assert data["rollups"]["trigger_category_count"] == 5
    assert data["rollups"]["telegram_direct_send_allowed_count"] == 0
    assert data["rollups"]["telegram_gateway_queue_write_allowed_count"] == 0
    assert data["rollups"]["route_change_allowed_count"] == 0
    assert data["rollups"]["conversation_transcript_allowed_count"] == 0
    assert any(
        rule["rule_id"] == "telegram_delivery_silence_detected"
        for rule in data["digest_rules"]
    )

    serialized = json.dumps(data, ensure_ascii=False)
    for marker in [
        "批准！繼續",
        "My request for Codex",
        "Current URL:",
        "# In app browser",
        "AGENTS.md instructions",
    ]:
        assert marker not in serialized
