from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]
def read(p): return (ROOT/p).read_text(encoding='utf-8')
def test_v014_router_and_ui():
    main=read('app/main.py'); api=read('app/api/v014.py')
    assert 'v014_router' in main and '/high-availability' in main
    for route in ['/summary','/nodes/heartbeat','/leases/acquire','/backup-policies','/dr-plans','/drills']: assert route in api
    assert (ROOT/'app/static/high-availability.html').exists()
def test_v014_models_and_migration():
    entities=read('app/models/entities.py'); migration=read('migrations/014_high_availability.sql')
    for name in ['ClusterNode','ClusterLease','FailoverEvent','BackupPolicy','BackupRun','DisasterRecoveryPlan','DisasterRecoveryDrill']: assert f'class {name}' in entities
    for table in ['cluster_nodes_v014','cluster_leases_v014','failover_events_v014','backup_policies_v014','backup_runs_v014','disaster_recovery_plans_v014','disaster_recovery_drills_v014']: assert table in migration
def test_cluster_leases_and_backup_worker():
    service=read('app/services/cluster.py'); tasks=read('app/workers/tasks.py'); celery=read('app/workers/celery_app.py')
    assert 'fencing_token' in service and 'acquire_lease' in service and 'pg_dump' in service and 'PGPASSWORD' in service
    assert 'cluster-heartbeat' in tasks and 'run-backup-schedules' in tasks
    assert 'cluster-heartbeat-every-15-seconds' in celery
def test_ha_infrastructure_assets():
    compose=read('docker-compose.ha.yml'); haproxy=read('infrastructure/ha/haproxy/haproxy.cfg'); k8s=read('infrastructure/kubernetes/cedp-api.yaml')
    assert 'postgresql-repmgr' in compose and 'pgpool' in compose and 'api-1' in compose and 'api-2' in compose
    assert 'balance leastconn' in haproxy and 'GET /health' in haproxy
    assert 'replicas: 3' in k8s and 'PodDisruptionBudget' in k8s and 'HorizontalPodAutoscaler' in k8s
