#!/usr/bin/env python3 """ Simple test script to verify WebSocket transport functionality. """ import asyncio import logging from pathlib import Path import sys # Add the libp2p directory to the path so we can import it sys.path.insert(0, str(Path(__file__).parent)) import multiaddr from libp2p.transport import create_transport, create_transport_for_multiaddr from libp2p.transport.upgrader import TransportUpgrader # Set up logging logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) async def test_websocket_transport(): """Test basic WebSocket transport functionality.""" print("๐Ÿงช Testing WebSocket Transport Functionality") print("=" * 50) # Create a dummy upgrader upgrader = TransportUpgrader({}, {}) # Test creating WebSocket transport try: ws_transport = create_transport("ws", upgrader) print(f"โœ… WebSocket transport created: {type(ws_transport).__name__}") # Test creating transport from multiaddr ws_maddr = multiaddr.Multiaddr("/ip4/127.0.0.1/tcp/8080/ws") ws_transport_from_maddr = create_transport_for_multiaddr(ws_maddr, upgrader) print( f"โœ… WebSocket transport from multiaddr: " f"{type(ws_transport_from_maddr).__name__}" ) # Test creating listener handler_called = False async def test_handler(conn): nonlocal handler_called handler_called = True print(f"โœ… Connection handler called with: {type(conn).__name__}") await conn.close() listener = ws_transport.create_listener(test_handler) print(f"โœ… WebSocket listener created: {type(listener).__name__}") # Test that the transport can be used print( f"โœ… WebSocket transport supports dialing: {hasattr(ws_transport, 'dial')}" ) print( f"โœ… WebSocket transport supports listening: " f"{hasattr(ws_transport, 'create_listener')}" ) print("\n๐ŸŽฏ WebSocket Transport Test Results:") print("โœ… Transport creation: PASS") print("โœ… Multiaddr parsing: PASS") print("โœ… Listener creation: PASS") print("โœ… Interface compliance: PASS") except Exception as e: print(f"โŒ WebSocket transport test failed: {e}") import traceback traceback.print_exc() return False return True async def test_transport_registry(): """Test the transport registry functionality.""" print("\n๐Ÿ”ง Testing Transport Registry") print("=" * 30) from libp2p.transport import ( get_supported_transport_protocols, get_transport_registry, ) registry = get_transport_registry() supported = get_supported_transport_protocols() print(f"Supported protocols: {supported}") # Test getting transports for protocol in supported: transport_class = registry.get_transport(protocol) class_name = transport_class.__name__ if transport_class else "None" print(f" {protocol}: {class_name}") # Test creating transports through registry upgrader = TransportUpgrader({}, {}) for protocol in supported: try: transport = registry.create_transport(protocol, upgrader) if transport: print(f"โœ… {protocol}: Created successfully") else: print(f"โŒ {protocol}: Failed to create") except Exception as e: print(f"โŒ {protocol}: Error - {e}") async def main(): """Run all tests.""" print("๐Ÿš€ WebSocket Transport Integration Test Suite") print("=" * 60) print() # Run tests success = await test_websocket_transport() await test_transport_registry() print("\n" + "=" * 60) if success: print("๐ŸŽ‰ All tests passed! WebSocket transport is working correctly.") else: print("โŒ Some tests failed. Check the output above for details.") print("\n๐Ÿš€ WebSocket transport is ready for use in py-libp2p!") if __name__ == "__main__": try: asyncio.run(main()) except KeyboardInterrupt: print("\n๐Ÿ‘‹ Test interrupted by user") except Exception as e: print(f"\nโŒ Test failed with error: {e}") import traceback traceback.print_exc()