#!/usr/bin/env python3
"""
/api/state, /api/export 등 다른 엔드포인트 테스트
"""

import requests
import json
import time

BASE_URL = "http://localhost:5000"

print("="*80)
print("API 엔드포인트 테스트")
print("="*80)

# 1. /api/state 테스트
print("\n[1] GET /api/state")
try:
    start = time.time()
    response = requests.get(f"{BASE_URL}/api/state", timeout=15)
    elapsed = time.time() - start

    print(f"  응답 시간: {elapsed:.2f}s")
    print(f"  상태 코드: {response.status_code}")

    if response.status_code == 200:
        result = response.json()
        print(f"  [OK] 키: {list(result.keys())[:5]}")
    else:
        print(f"  [FAIL] {response.text[:100]}")

except Exception as e:
    print(f"  [ERROR] {e}")

# 2. /api/export 테스트
print("\n[2] GET /api/export")
try:
    start = time.time()
    response = requests.get(f"{BASE_URL}/api/export", timeout=15)
    elapsed = time.time() - start

    print(f"  응답 시간: {elapsed:.2f}s")
    print(f"  상태 코드: {response.status_code}")
    print(f"  응답 크기: {len(response.text)} bytes")

    if response.status_code == 200:
        try:
            result = response.json()
            print(f"  [OK] 키: {list(result.keys())[:3]}")
        except:
            print(f"  [OK] (JSON 파싱 불가, 바이너리일 수 있음)")
    else:
        print(f"  [FAIL] {response.text[:100]}")

except Exception as e:
    print(f"  [ERROR] {e}")

# 3. /api/tables 테스트
print("\n[3] GET /api/tables")
try:
    start = time.time()
    response = requests.get(f"{BASE_URL}/api/tables", timeout=15)
    elapsed = time.time() - start

    print(f"  응답 시간: {elapsed:.2f}s")
    print(f"  상태 코드: {response.status_code}")

    if response.status_code == 200:
        result = response.json()
        print(f"  [OK] {len(result)} 테이블")
        for table in result[:3]:
            print(f"    - {table['table']}: {table['row_count']} rows")
    else:
        print(f"  [FAIL] {response.text[:100]}")

except Exception as e:
    print(f"  [ERROR] {e}")

print("\n[완료]")
