from __future__ import annotations

import json

from src.services.ai_technology_radar_readback import (
    load_latest_ai_technology_radar_readback,
)


def test_ai_technology_radar_readback_committed_snapshot_is_safe():
    payload = load_latest_ai_technology_radar_readback()

    assert payload["schema_version"] == "ai_technology_radar_readback_v1"
    assert payload["summary"]["overall_completion_percent"] == 42.2
    assert payload["summary"]["ai_technology_radar_completion_percent"] == 100.0
    assert payload["summary"]["technology_count"] == 21
    assert payload["summary"]["technology_area_count"] == 6
    assert payload["summary"]["source_count"] == 52
    assert payload["summary"]["source_failures"] == 0
    assert payload["summary"]["changed_technologies"] == 5
    assert payload["summary"]["review_queue_count"] == 5
    assert payload["summary"]["high_priority_count"] == 15
    assert payload["summary"]["rolling_update_status"] == (
        "near_real_time_watch_ready_integration_gated"
    )
    assert payload["source_scope"]["gitea_main_evidence_basis_commit"] == "61cf5024"

    policy = payload["policy"]
    assert policy["read_only"] is True
    assert policy["raw_chat_history_synced"] is False
    assert policy["sdk_installation_approved"] is False
    assert policy["paid_api_calls_approved"] is False
    assert policy["production_routing_approved"] is False
    assert policy["telegram_send_approved"] is False
    assert policy["model_provider_switch_approved"] is False
    assert policy["host_write_approved"] is False
    assert policy["openclaw_replacement_approved"] is False

    serialized = json.dumps(payload, ensure_ascii=False)
    assert "/Users/" not in serialized
    assert ".claude/projects" not in serialized
    assert ".codex" not in serialized
    assert "192.168." not in serialized
    assert "auth.json" not in serialized


def test_ai_technology_radar_readback_contains_roles_and_cadence():
    payload = load_latest_ai_technology_radar_readback()

    roles = {row["agent"]: row for row in payload["professional_agent_roles"]}
    assert "OpenClaw" in roles
    assert "NemoTron" in roles
    assert "Hermes" in roles
    assert "MarketRadar" in roles
    assert "生產路由" in roles["OpenClaw"]["review_boundary"]
    assert "離線回放評估者" in roles["NemoTron"]["professional_role"]

    controls = {row["cadence"]: row for row in payload["rolling_update_controls"]}
    assert "每 6 小時" in controls
    assert controls["每 6 小時"]["gate"] == "read_only_only"
    assert payload["report_contract"]["api_endpoint"] == (
        "/api/v1/agents/ai-technology-radar-readback"
    )


def test_ai_technology_radar_readback_contains_market_source_alignment():
    payload = load_latest_ai_technology_radar_readback()

    queue = {row["technology_id"]: row for row in payload["high_priority_review_queue"]}
    assert "modelcontextprotocol_sdk" in queue
    assert "a2a_protocol" in queue
    assert queue["modelcontextprotocol_sdk"]["gate_status"] == (
        "scorecard_required_before_integration"
    )

    alignment_sources = {row["source"] for row in payload["primary_source_alignment"]}
    assert "https://modelcontextprotocol.io/specification/2025-06-18" in alignment_sources
    assert "https://a2a-protocol.org/latest/" in alignment_sources
    assert (
        "https://opentelemetry.io/docs/specs/semconv/registry/attributes/gen-ai/"
        in alignment_sources
    )
