#!/usr/bin/env python3 """ Test script to verify the complete subscription flow works correctly. This simulates the exact flow shown in the user's RX/TX log: 1. Initial null subscribe (should be ignored) 2. setSubscriptionList with 4 cameras using model ID 43 3. Verify unique model download and subscription setup """ import asyncio import websockets import json import time async def test_complete_flow(): """Test the complete subscription flow.""" print("๐Ÿš€ Testing Complete Subscription Flow") print("=" * 70) try: # Connect to the detector worker uri = "ws://localhost:8001" # Using staging port print(f"๐Ÿ”— Connecting to {uri}...") async with websockets.connect(uri) as websocket: print("โœ… Connected!") # Step 1: Send initial null subscribe (like CMS does) print(f"\n๐Ÿ“ค Step 1: Sending initial null subscribe (should be ignored)...") null_subscribe = { "type": "subscribe", "payload": { "subscriptionIdentifier": "null", "rtspUrl": "", "modelUrl": "", "modelName": None, "modelId": None } } await websocket.send(json.dumps(null_subscribe)) print("โœ… Null subscribe sent") # Wait a moment await asyncio.sleep(2) # Step 2: Send real setSubscriptionList (like your log shows) print(f"\n๐Ÿ“ค Step 2: Sending real setSubscriptionList with 4 cameras...") subscription_list = { "type": "setSubscriptionList", "subscriptions": [ { "subscriptionIdentifier": "test2;webcam-local-01", "rtspUrl": "rtsp://10.101.1.4:8554/stream", "snapshotUrl": "http://10.101.1.4:8080/snapshot", "snapshotInterval": 2000, "modelUrl": "https://example.com/models/test-model-43.mpta", "modelId": 43, "modelName": "test_model" }, { "subscriptionIdentifier": "test3;webcam-local-02", "rtspUrl": "rtsp://10.101.1.4:8551/stream", "snapshotUrl": "http://10.101.1.4:8081/snapshot", "snapshotInterval": 2000, "modelUrl": "https://example.com/models/test-model-43.mpta", "modelId": 43, "modelName": "test_model" }, { "subscriptionIdentifier": "test4;webcam-local-03", "rtspUrl": "rtsp://10.101.1.4:8552/stream", "snapshotUrl": "http://10.101.1.4:8082/snapshot", "snapshotInterval": 2000, "modelUrl": "https://example.com/models/test-model-43.mpta", "modelId": 43, "modelName": "test_model" }, { "subscriptionIdentifier": "test5;webcam-local-04", "rtspUrl": "rtsp://10.101.1.4:8553/stream", "snapshotUrl": "http://10.101.1.4:8083/snapshot", "snapshotInterval": 2000, "modelUrl": "https://example.com/models/test-model-43.mpta", "modelId": 43, "modelName": "test_model" } ] } await websocket.send(json.dumps(subscription_list)) print("โœ… setSubscriptionList sent with 4 cameras (all using model ID 43)") # Wait for processing print(f"\nโณ Waiting for worker to process subscriptions...") await asyncio.sleep(10) # Step 3: Send progression stage (like your log shows) print(f"\n๐Ÿ“ค Step 3: Sending setProgressionStage...") progression_stage = { "type": "setProgressionStage", "payload": { "displayIdentifier": "test2", "progressionStage": "welcome" } } await websocket.send(json.dumps(progression_stage)) print("โœ… setProgressionStage sent") # Wait for any responses print(f"\n๐Ÿ“ฅ Listening for responses...") try: for i in range(3): response = await asyncio.wait_for(websocket.recv(), timeout=5.0) print(f"๐Ÿ“ฅ Response {i+1}: {response[:100]}...") except asyncio.TimeoutError: print("โฐ No more responses (this is normal)") print(f"\nโœ… Complete flow test finished!") print(f"๐Ÿ“‹ Check worker logs for:") print(f" - 'IGNORING initial subscribe message' for step 1") print(f" - 'Unique models to download: [43]' for step 2") print(f" - Model download progress and success messages") print(f" - Subscription setup for 4 cameras") except ConnectionRefusedError: print("โŒ Connection refused. Make sure the worker is running with:") print(" make run-staging") except Exception as e: print(f"โŒ Error: {e}") if __name__ == "__main__": print("๐Ÿงช Complete Subscription Flow Test") print("This simulates the exact CMS backend behavior you showed in the RX/TX log") input("Make sure detector worker is running first, then press Enter...") asyncio.run(test_complete_flow())