"""
Kuaishou Kling AI Tool Test Script
Test core functionality: API request building, parameter validation, etc.
"""
import asyncio
import sys
import os

# Add parent directory to path for module imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from kling_client import KlingClient, BizType, TaskStatus


async def test_client_initialization():
    """Test client initialization"""
    print("Test 1: Client initialization")
    client = KlingClient(cookie="test_cookie", timeout=30)
    assert client.cookie == "test_cookie"
    assert client.timeout == 30
    assert client.base_url == "https://kling.kuaishou.com/api"
    print("[PASS] Client initialization successful")


async def test_headers():
    """Test request header generation"""
    print("\nTest 2: Request header generation")
    client = KlingClient(cookie="test_cookie")
    headers = client._get_headers()
    assert "Content-Type" in headers
    assert headers["Content-Type"] == "application/json"
    assert "Cookie" in headers
    assert headers["Cookie"] == "test_cookie"
    print("[PASS] Request headers correct")


async def test_biz_type_enum():
    """Test business type enum"""
    print("\nTest 3: Business type enum")
    assert BizType.AI_IMAGE.value == "aiImage"
    assert BizType.AI_VIDEO.value == "aiVideo"
    assert BizType.AI_LIP_SYNC.value == "aiLipSync"
    print("[PASS] Business type enum correct")


async def test_task_status_enum():
    """Test task status enum"""
    print("\nTest 4: Task status enum")
    assert TaskStatus.PROCESS.value == "process"
    assert TaskStatus.FINISHED.value == "finished"
    assert TaskStatus.FAILED.value == "failed"
    print("[PASS] Task status enum correct")


async def test_image_task_payload():
    """Test image generation task parameter building"""
    print("\nTest 5: Image generation task parameters")
    client = KlingClient()
    
    # Verify parameters are correctly passed (by checking method signature)
    import inspect
    sig = inspect.signature(client.create_image_task)
    params = sig.parameters
    
    assert "prompt" in params
    assert "negative_prompt" in params
    assert "cfg" in params
    assert "aspect_ratio" in params
    assert "image_count" in params
    assert "version" in params
    
    print("[PASS] Image generation task parameters correct")


async def test_video_task_payload():
    """Test video generation task parameter building"""
    print("\nTest 6: Video generation task parameters")
    client = KlingClient()
    
    # Verify parameters are correctly passed
    import inspect
    sig = inspect.signature(client.create_video_task)
    params = sig.parameters
    
    assert "prompt" in params
    assert "mode" in params
    assert "image_url" in params
    assert "aspect_ratio" in params
    assert "add_audio" in params
    assert "start_frame_image" in params
    assert "end_frame_image" in params
    
    print("[PASS] Video generation task parameters correct")


async def test_lipsync_task_payload():
    """Test lip sync task parameter building"""
    print("\nTest 7: Lip sync task parameters")
    client = KlingClient()
    
    # Verify parameters are correctly passed
    import inspect
    sig = inspect.signature(client.create_lipsync_task)
    params = sig.parameters
    
    assert "video_id" in params
    assert "video_url" in params
    assert "mode" in params
    assert "text" in params
    assert "voice_id" in params
    assert "voice_language" in params
    assert "voice_speed" in params
    assert "audio_type" in params
    
    print("[PASS] Lip sync task parameters correct")


async def main():
    """Run all tests"""
    print("=" * 50)
    print("Kuaishou Kling AI Tool - Core Function Tests")
    print("=" * 50)
    
    try:
        await test_client_initialization()
        await test_headers()
        await test_biz_type_enum()
        await test_task_status_enum()
        await test_image_task_payload()
        await test_video_task_payload()
        await test_lipsync_task_payload()
        
        print("\n" + "=" * 50)
        print("[SUCCESS] All tests passed!")
        print("=" * 50)
        
    except AssertionError as e:
        print(f"\n[FAIL] Test failed: {e}")
        sys.exit(1)
    except Exception as e:
        print(f"\n[ERROR] Test error: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)


if __name__ == "__main__":
    asyncio.run(main())
