#!/usr/bin/env python3
"""
API 핸들러의 각 컴포넌트 테스트
"""

import sys
sys.path.insert(0, 'src/quant_engine')

from snapshot_admin_store_v1 import (
    is_locked,
    lock_conflicts_for_rows,
    summarize_workspace,
    open_connection,
)
from pathlib import Path

db_path = Path('src/quant_engine/snapshot_admin.db')

print("="*80)
print("API 컴포넌트 테스트")
print("="*80)

# 1. is_locked 테스트
print("\n[1] is_locked 테스트")
try:
    locked = is_locked(db_path, "settings")
    print(f"  [OK] is_locked result: {locked}")
except Exception as e:
    print(f"  [ERROR] {e}")

# 2. lock_conflicts_for_rows 테스트
print("\n[2] lock_conflicts_for_rows 테스트")
try:
    test_rows = [
        {
            "ordinal": 5,
            "key": "total_asset_krw",
            "value": "450000000",
            "note": "test"
        }
    ]

    conflicts = lock_conflicts_for_rows(db_path, "settings", test_rows)
    print(f"  [OK] conflicts: {conflicts}")
except Exception as e:
    print(f"  [ERROR] {e}")

# 3. summarize_workspace 테스트
print("\n[3] summarize_workspace 테스트")
try:
    import time
    start = time.time()
    summary = summarize_workspace(db_path)
    elapsed = time.time() - start
    print(f"  [OK] summarize_workspace completed in {elapsed:.2f}s")
    print(f"  Keys: {list(summary.keys())[:5]}")
except Exception as e:
    print(f"  [ERROR] {e}")
    import traceback
    traceback.print_exc()

print("\n[완료]")
