forked from shreyan001/backend-socials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-live-server.js
More file actions
104 lines (86 loc) · 3.2 KB
/
test-live-server.js
File metadata and controls
104 lines (86 loc) · 3.2 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
const https = require('https');
const http = require('http');
async function testServerRequest(input, description) {
console.log(`\n=== Testing: ${description} ===`);
console.log(`Input: "${input}"`);
return new Promise((resolve, reject) => {
const url = `http://localhost:3001/api/agent?input=${encodeURIComponent(input)}&chat_history=[]&wallet_address=0x1234567890123456789012345678901234567890`;
const req = http.get(url, (res) => {
console.log(`Status: ${res.statusCode}`);
console.log(`Headers:`, res.headers);
let data = '';
const messages = [];
res.on('data', (chunk) => {
data += chunk.toString();
// Parse Server-Sent Events
const lines = data.split('\n');
data = lines.pop() || ''; // Keep the last incomplete line
for (const line of lines) {
if (line.startsWith('data: ')) {
try {
const jsonStr = line.substring(6); // Remove 'data: '
if (jsonStr.trim()) {
const messageData = JSON.parse(jsonStr);
messages.push(messageData);
console.log(`📨 [${messageData.type}] ${messageData.content || 'N/A'}`);
if (messageData.wager) {
console.log(` Wager: ${JSON.stringify(messageData.wager, null, 2)}`);
}
// If we get an "end" event, close the connection
if (messageData.type === "end") {
console.log('🏁 Stream ended');
resolve(messages);
return;
}
}
} catch (error) {
console.log(`⚠️ Failed to parse: ${line}`);
}
}
}
});
res.on('end', () => {
console.log('🔚 Connection ended');
resolve(messages);
});
res.on('error', (error) => {
console.error('❌ Response error:', error);
reject(error);
});
});
req.on('error', (error) => {
console.error('❌ Request error:', error);
reject(error);
});
// Set a timeout
req.setTimeout(10000, () => {
console.log('⏰ Request timeout');
req.destroy();
resolve(messages);
});
});
}
async function runServerTests() {
console.log('Testing live server...\n');
try {
// Test 1: Incomplete wager
const messages1 = await testServerRequest(
"I want to bet on a cricket match",
"Incomplete wager request"
);
console.log(`✅ Received ${messages1.length} messages`);
// Test 2: More complete wager
const messages2 = await testServerRequest(
"I want to bet on India vs England cricket match, my prediction is India will win",
"Complete wager request"
);
console.log(`✅ Received ${messages2.length} messages`);
console.log('\n=== Server Test Results ===');
console.log('✅ Server is responding to requests');
console.log('✅ EventSource streaming is working');
console.log('✅ Messages are being sent in the correct format');
} catch (error) {
console.error('❌ Server test failed:', error.message);
}
}
runServerTests();