#!/usr/bin/env python3
"""
Minimal asyncio test to isolate Railway import issue.
Tests if asyncio can be imported and run in isolation.
"""

import sys

print("=" * 80)
print("MINIMAL ASYNCIO TEST")
print("=" * 80)
print(f"Python version: {sys.version}")
print(f"Python executable: {sys.executable}")
print("=" * 80)

# Test 1: Import asyncio
print("\n[TEST 1] Importing asyncio...")
sys.stdout.flush()

try:
    import asyncio
    sys.stdout.flush()
    print("✅ asyncio imported successfully")
    sys.stdout.flush()
except Exception as e:
    print(f"❌ Failed to import asyncio: {e}")
    sys.stdout.flush()
    sys.exit(1)

# Test 2: Get event loop
print("\n[TEST 2] Getting event loop...")
sys.stdout.flush()

try:
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    sys.stdout.flush()
    print(f"✅ Event loop created: {type(loop)}")
    sys.stdout.flush()
except Exception as e:
    print(f"❌ Failed to create event loop: {e}")
    sys.stdout.flush()
    sys.exit(1)

# Test 3: Run simple async function
print("\n[TEST 3] Running async function...")
sys.stdout.flush()

async def test_async():
    """Simple async function"""
    await asyncio.sleep(0.1)
    return "success"

try:
    result = asyncio.run(test_async())
    sys.stdout.flush()
    print(f"✅ Async function completed: {result}")
    sys.stdout.flush()
except Exception as e:
    print(f"❌ Failed to run async function: {e}")
    sys.stdout.flush()
    sys.exit(1)

# Test 4: Test with multiple coroutines
print("\n[TEST 4] Running multiple coroutines...")
sys.stdout.flush()

async def test_gather():
    """Test gather with multiple coroutines"""
    async def task(n):
        await asyncio.sleep(0.01)
        return n * 2

    results = await asyncio.gather(
        task(1), task(2), task(3)
    )
    return results

try:
    results = asyncio.run(test_gather())
    sys.stdout.flush()
    print(f"✅ Multiple coroutines completed: {results}")
    sys.stdout.flush()
except Exception as e:
    print(f"❌ Failed to run multiple coroutines: {e}")
    sys.stdout.flush()
    sys.exit(1)

# All tests passed
print("\n" + "=" * 80)
print("✅ ALL ASYNCIO TESTS PASSED")
print("=" * 80)
sys.stdout.flush()
