-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_nova_ws.py
More file actions
311 lines (266 loc) · 9.6 KB
/
Copy pathtest_nova_ws.py
File metadata and controls
311 lines (266 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python3
"""
E2E test for the Nova WebSocket channel.
Connects as a user via WebSocket, sends a message to a bot via the
API Gateway Management API, and waits for the bot's response on the
WebSocket. Works with any bot — no SSH or EC2 access required.
Usage:
pip install websockets boto3
python scripts/test_nova_ws.py [--message "your test message"]
python scripts/test_nova_ws.py --device-id <uuid>
python scripts/test_nova_ws.py --connection-id <id>
python scripts/test_nova_ws.py --list
Requires:
- AWS credentials with access to the DynamoDB table and APIGW Management API
"""
import argparse
import asyncio
import json
import os
import time
import uuid
import boto3
import websockets
# --- Configuration ---
WS_ENDPOINT = "wss://ws.nova-claw.agi.amazon.dev"
APIGW_MANAGEMENT_URL = "https://3x0jx6dr52.execute-api.us-east-1.amazonaws.com/prod"
DDB_TABLE = "nova-personal-connections-prod"
REGION = "us-east-1"
# Auth – set NOVA_API_KEY in your environment or .env file
API_KEY = os.environ.get("NOVA_API_KEY", "")
# Test user identity
TEST_USER_ID = "test-user"
TEST_DEVICE_ID = str(uuid.uuid4())
def list_connections() -> list[dict]:
"""List all active connections from DynamoDB."""
ddb = boto3.client("dynamodb", region_name=REGION)
resp = ddb.scan(TableName=DDB_TABLE)
connections = []
for item in resp.get("Items", []):
conn = {
"connectionId": item.get("connectionId", {}).get("S", ""),
"deviceId": item.get("deviceId", {}).get("S", ""),
"userId": item.get("userId", {}).get("S", ""),
"sourceIp": (
item.get("metadata", {}).get("M", {}).get("sourceIp", {}).get("S", "")
),
"connectedAt": item.get("connectedAt", {}).get("S", ""),
}
if conn["connectionId"]:
connections.append(conn)
# Sort by connectedAt descending (most recent first)
connections.sort(key=lambda c: c["connectedAt"], reverse=True)
return connections
def find_connection(
connection_id: str | None = None,
device_id: str | None = None,
user_id: str | None = None,
source_ip: str | None = None,
) -> dict:
"""Find a bot connection by connectionId, deviceId, userId, or sourceIp."""
connections = list_connections()
if not connections:
raise RuntimeError("No connections found in DynamoDB")
if connection_id:
for c in connections:
if c["connectionId"] == connection_id:
return c
raise RuntimeError(f"No connection found with connectionId={connection_id}")
if user_id:
for c in connections:
if c["userId"] == user_id:
return c
raise RuntimeError(f"No connection found with userId={user_id}")
if device_id:
for c in connections:
if c["deviceId"] == device_id:
return c
raise RuntimeError(f"No connection found with deviceId={device_id}")
if source_ip:
for c in connections:
if c["sourceIp"] == source_ip:
return c
raise RuntimeError(f"No connection found with sourceIp={source_ip}")
# Default: return the most recent connection
return connections[0]
def print_connections(connections: list[dict]):
"""Pretty-print a list of connections."""
if not connections:
print(" (no connections)")
return
for i, c in enumerate(connections):
print(f" [{i}] connectionId: {c['connectionId']}")
print(f" userId: {c['userId'] or '(not set)'}")
print(f" deviceId: {c['deviceId']}")
print(f" sourceIp: {c['sourceIp']}")
print(f" connectedAt: {c['connectedAt']}")
if i < len(connections) - 1:
print()
def send_to_bot(connection_id: str, message: dict):
"""Post a message to the bot's connection via the Management API."""
client = boto3.client(
"apigatewaymanagementapi",
endpoint_url=APIGW_MANAGEMENT_URL,
region_name=REGION,
)
client.post_to_connection(
ConnectionId=connection_id,
Data=json.dumps(message).encode("utf-8"),
)
async def run_test(
test_message: str,
timeout_secs: int,
connection_id: str | None,
device_id: str | None,
user_id: str | None,
source_ip: str | None,
):
"""Run the E2E test."""
print("=" * 60)
print("Nova WebSocket E2E Test")
print("=" * 60)
# Step 1: Find the bot's connection
print("\n[1] Looking up bot connection from DynamoDB...")
conn = find_connection(connection_id, device_id, user_id, source_ip)
bot_conn_id = conn["connectionId"]
print(f" connectionId: {conn['connectionId']}")
print(f" userId: {conn['userId'] or '(not set)'}")
print(f" deviceId: {conn['deviceId']}")
print(f" sourceIp: {conn['sourceIp']}")
print(f" connectedAt: {conn['connectedAt']}")
# Step 2: Connect as a user via WebSocket
print("\n[2] Connecting as test user via WebSocket...")
ws_url = f"{WS_ENDPOINT}?userId={TEST_USER_ID}&deviceId={TEST_DEVICE_ID}"
ws = await websockets.connect(
ws_url,
additional_headers={"Authorization": f"Bearer {API_KEY}"},
)
print(f" Connected (userId={TEST_USER_ID}, deviceId={TEST_DEVICE_ID})")
try:
# Step 3: Send message to bot
message_id = str(uuid.uuid4())
inbound = {
"action": "message",
"userId": TEST_USER_ID,
"text": test_message,
"messageId": message_id,
"timestamp": int(time.time() * 1000),
}
print(f"\n[3] Sending message to bot...")
print(f" messageId: {message_id}")
print(f" text: {test_message}")
try:
send_to_bot(bot_conn_id, inbound)
print(" Sent successfully!")
except Exception as e:
print(f" ERROR: {e}")
return
# Step 4: Wait for response on WebSocket
print(f"\n[4] Waiting for bot response on WebSocket (timeout {timeout_secs}s)...")
print("-" * 60)
chunks: list[str] = []
start = time.time()
try:
while time.time() - start < timeout_secs:
remaining = timeout_secs - (time.time() - start)
if remaining <= 0:
break
try:
raw = await asyncio.wait_for(ws.recv(), timeout=min(remaining, 5.0))
except asyncio.TimeoutError:
elapsed = int(time.time() - start)
if elapsed > 0 and elapsed % 10 == 0:
print(f" ... waiting ({elapsed}s)")
continue
try:
frame = json.loads(raw)
except json.JSONDecodeError:
continue
action = frame.get("action", "")
frame_type = frame.get("type", "")
reply_to = frame.get("replyTo", "")
text = frame.get("text", "")
# Only process response frames for our message
if action == "response" and reply_to == message_id:
if text:
chunks.append(text)
if frame_type == "done":
break
elif action == "ping":
# Ignore heartbeats
continue
else:
# Log unexpected frames for debugging
print(f" [debug] frame: action={action} type={frame_type} replyTo={reply_to}")
except websockets.ConnectionClosed:
print(" WebSocket connection closed unexpectedly")
print("-" * 60)
if chunks:
response = "".join(chunks)
print(f"\nBot response:")
print(response)
else:
print("\n[Result] No response within timeout.")
print(" The bot may not be running or may not have processed the message.")
finally:
await ws.close()
print("\n" + "=" * 60)
print("Test complete.")
async def run_list():
"""List all active connections."""
print("=" * 60)
print("Active Nova WebSocket Connections")
print("=" * 60)
print()
connections = list_connections()
print_connections(connections)
print()
print(f"Total: {len(connections)} connection(s)")
def main():
parser = argparse.ArgumentParser(description="Test Nova WebSocket E2E")
parser.add_argument(
"--message", "-m",
default="Hello! What is 2 + 2?",
help="Message to send to the bot",
)
parser.add_argument(
"--timeout", "-t",
type=int,
default=60,
help="Seconds to wait for response (default: 60)",
)
parser.add_argument(
"--connection-id",
help="Target bot's connectionId (from DynamoDB)",
)
parser.add_argument(
"--device-id",
help="Target bot's deviceId (from DynamoDB)",
)
parser.add_argument(
"--user-id",
help="Target bot's userId (from DynamoDB) — stable across reconnects",
)
parser.add_argument(
"--source-ip",
help="Target bot's source IP (from DynamoDB)",
)
parser.add_argument(
"--list", "-l",
action="store_true",
help="List all active connections and exit",
)
args = parser.parse_args()
if args.list:
asyncio.run(run_list())
else:
asyncio.run(run_test(
args.message,
args.timeout,
args.connection_id,
args.device_id,
args.user_id,
args.source_ip,
))
if __name__ == "__main__":
main()