"""
测试检查点安全修复功能
"""
import sys
sys.path.insert(0, '.')

from collections import deque

def test_topological_sort():
    """测试拓扑排序功能"""
    # 模拟依赖关系: regions -> region_themes -> region_theme_permits
    dependencies = {
        'regions': ['region_themes', 'region_scopes'],
        'themes': ['region_themes'],
        'business_scopes': [],
        'permits': [],
        'risks': [],
        'region_themes': ['region_theme_permits'],
        'region_scopes': ['region_permit_scopes'],
        'region_theme_permits': ['region_permit_risks']
    }
    
    all_tables = list(dependencies.keys())
    
    # 计算入度
    in_degree = {table: 0 for table in all_tables}
    for parent_table, children in dependencies.items():
        for child in children:
            in_degree[child] += 1
    
    # 找到父表（入度为0）
    queue = deque([table for table in all_tables if in_degree[table] == 0])
    result = []
    
    while queue:
        table = queue.popleft()
        result.append(table)
        
        # 减少依赖该表的子表的入度
        for parent, children in dependencies.items():
            if parent == table:
                for child in children:
                    if in_degree[child] > 0:
                        in_degree[child] -= 1
                        if in_degree[child] == 0:
                            queue.append(child)
    
    print("Test 1: Topological Sort")
    print(f"Result order: {result}")
    
    # 验证顺序是否正确
    # business_scopes, permits, risks 应该最先出现（没有依赖）
    expected_first = {'business_scopes', 'permits', 'risks'}
    actual_first = set(result[:3])
    
    assert expected_first == actual_first, \
        f"Expected first tables to be {expected_first}, got: {actual_first}"
    
    # region_permit_risks, region_theme_permits 应该最后出现（依赖最多）
    expected_last = {'region_permit_risks'}
    actual_last = set(result[-1:])
    
    assert expected_last == actual_last, \
        f"Expected last table to be {expected_last}, got: {actual_last}"
    
    print("[OK] Topological sort preserves dependency order")
    return True


def test_create_checkpoint_api_signature():
    """测试 create_checkpoint 函数签名"""
    from lawrisk.services.licensing_repo import create_checkpoint
    import inspect
    
    sig = inspect.signature(create_checkpoint)
    params = list(sig.parameters.keys())
    
    print("\nTest 2: Create Checkpoint Signature")
    print(f"Parameters: {params}")
    
    assert 'description' in params, "Missing 'description' parameter"
    print("[OK] create_checkpoint has required parameters")
    return True


def test_restore_checkpoint_api_signature():
    """测试 restore_checkpoint 函数签名"""
    from lawrisk.services.licensing_repo import restore_checkpoint
    import inspect
    
    sig = inspect.signature(restore_checkpoint)
    params = list(sig.parameters.keys())
    
    print("\nTest 3: Restore Checkpoint Signature")
    print(f"Parameters: {params}")
    
    assert 'checkpoint_id' in params, "Missing 'checkpoint_id' parameter"
    assert 'create_auto_backup' in params, "Missing 'create_auto_backup' parameter"
    print("[OK] restore_checkpoint has required parameters")
    return True


def main():
    print("=" * 60)
    print("Checkpoint Security Fix Validation Tests")
    print("=" * 60)
    
    tests = [
        test_topological_sort,
        test_create_checkpoint_api_signature,
        test_restore_checkpoint_api_signature
    ]
    
    passed = 0
    failed = 0
    
    for test in tests:
        try:
            if test():
                passed += 1
        except Exception as e:
            print(f"[FAILED] {test.__name__}: {e}")
            import traceback
            traceback.print_exc()
            failed += 1
    
    print("\n" + "=" * 60)
    print(f"Results: {passed} passed, {failed} failed")
    print("=" * 60)
    
    if failed > 0:
        sys.exit(1)
    else:
        print("\n[SUCCESS] All validation tests passed!")
        print("\nKey improvements:")
        print("1. Added table dependency tracking")
        print("2. Topological sort ensures correct restore order")
        print("3. restore_checkpoint now has auto-backup feature")
        print("4. Transaction protection for all operations")
        print("5. Table-level locks during restore")


if __name__ == '__main__':
    main()
