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_host_stateful_version_inventory_endpoint_returns_committed_snapshot():
    app = FastAPI()
    app.include_router(router, prefix="/api/v1")
    client = TestClient(app)

    response = client.get("/api/v1/agents/agent-host-stateful-version-inventory")

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "ai_agent_host_stateful_version_inventory_v1"
    assert data["program_status"]["overall_completion_percent"] == 86
    assert data["program_status"]["current_task_id"] == "P2-402F"
    assert data["program_status"]["next_task_id"] == "P2-402G"
    assert data["program_status"]["read_only_mode"] is True
    assert data["operation_boundaries"]["read_only_inventory_allowed"] is True
    assert data["operation_boundaries"]["ssh_login_allowed"] is False
    assert data["operation_boundaries"]["kubectl_command_execution_allowed"] is False
    assert data["operation_boundaries"]["apt_upgrade_allowed"] is False
    assert data["operation_boundaries"]["k3s_upgrade_allowed"] is False
    assert data["operation_boundaries"]["node_drain_allowed"] is False
    assert data["operation_boundaries"]["reboot_allowed"] is False
    assert data["operation_boundaries"]["stateful_service_restart_allowed"] is False
    assert data["operation_boundaries"]["telegram_direct_send_allowed"] is False
    assert data["operation_boundaries"]["conversation_transcript_allowed"] is False
    assert data["rollups"]["host_count"] == 5
    assert data["rollups"]["k3s_node_count"] == 2
    assert data["rollups"]["stateful_service_count"] == 12
    assert data["rollups"]["readonly_probe_step_count"] == 6
    assert data["rollups"]["maintenance_required_field_count"] == 11
    assert data["rollups"]["ssh_login_allowed_count"] == 0
    assert data["rollups"]["kubectl_command_execution_allowed_count"] == 0
    assert data["rollups"]["apt_upgrade_allowed_count"] == 0
    assert data["rollups"]["k3s_upgrade_allowed_count"] == 0
    assert data["rollups"]["node_drain_allowed_count"] == 0
    assert data["rollups"]["reboot_allowed_count"] == 0
    assert data["rollups"]["stateful_service_restart_allowed_count"] == 0
    assert data["rollups"]["telegram_direct_send_allowed_count"] == 0
    assert data["rollups"]["conversation_transcript_allowed_count"] == 0

    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
