from __future__ import annotations

from fastapi import FastAPI
from fastapi.testclient import TestClient

from src.api.v1.agents import router


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

    response = client.get("/api/v1/agents/gitea-workflow-runner-health")

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "gitea_workflow_runner_health_v1"
    assert data["program_status"]["overall_completion_percent"] == 100
    assert data["program_status"]["current_task_id"] == "P1-002"
    assert data["program_status"]["next_task_id"] == "P1-003"
    assert data["program_status"]["read_only_mode"] is True
    assert data["rollups"]["total_workflows"] == len(data["workflow_records"]) == 9
    assert data["rollups"]["workflows_with_schedule"] == 2
    assert data["rollups"]["workflows_with_workflow_dispatch"] == 7
    assert data["rollups"]["workflow_ids_requiring_runner_attestation"] == [
        "agent_market_watch",
        "ansible_lint",
        "cd_dev",
        "code_review",
        "deploy_alerts",
        "e2e_health",
        "run_migration",
        "type_sync_check",
    ]
    assert data["rollups"]["runner_contracts_requiring_action"] == [
        "ubuntu_latest_gitea_runner_label"
    ]
    assert data["rollups"]["notification_contracts_quiet_success_count"] == 2
    assert data["operation_boundaries"]["read_only_api_allowed"] is True
    assert data["operation_boundaries"]["workflow_modification_allowed"] is False
    assert data["operation_boundaries"]["runner_restart_allowed"] is False
    assert data["operation_boundaries"]["secret_plaintext_allowed"] is False
    assert data["operation_boundaries"]["deploy_trigger_allowed"] is False
    assert data["approval_boundaries"]["workflow_modification_authorized"] is False
    assert data["approval_boundaries"]["runner_mutation_authorized"] is False
    assert data["approval_boundaries"]["migration_trigger_authorized"] is False
    cd_pipeline = next(row for row in data["workflow_records"] if row["workflow_id"] == "cd_pipeline")
    assert cd_pipeline["runner_evidence_status"] == "host_runner_mapped"
    assert cd_pipeline["job_count"] == 3
    e2e_health = next(row for row in data["workflow_records"] if row["workflow_id"] == "e2e_health")
    assert e2e_health["notification_policy"] == "failure_only"
    assert "workflow 修改批准" in data["operator_contract"]["must_not_interpret_as"]
    assert "Secret 已讀取或可輸出" in data["operator_contract"]["must_not_interpret_as"]
