forked from multisynq/synchronizer-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-websocket-direct.js
More file actions
executable file
Β·234 lines (199 loc) Β· 7.84 KB
/
test-websocket-direct.js
File metadata and controls
executable file
Β·234 lines (199 loc) Β· 7.84 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
#!/usr/bin/env node
/**
* @fileoverview Direct WebSocket connection test utility
* @description Standalone test script for validating WebSocket connectivity to the synchronizer container.
* Tests connection to localhost:3333, sends various request types, and analyzes response quality.
*
* @module test-websocket-direct
* @requires ws
* @requires chalk
* @requires child_process
*
* Usage: node test-websocket-direct.js
*
* Tests performed:
* - Container availability check
* - WebSocket connection establishment
* - Multiple request types (stats, debug, queryWalletStats, pingFromMain)
* - Periodic stats polling every 5 seconds
* - Data quality analysis
* - Response time measurement
*/
const WebSocket = require('ws');
const chalk = require('chalk');
console.log(chalk.blue('π§ͺ Direct WebSocket Test Script'));
console.log(chalk.yellow('Testing connection to synchronizer container on localhost:3333\n'));
// Check if container is running first
const { execSync } = require('child_process');
try {
const psOutput = execSync('docker ps --filter name=synchronizer --format "{{.Names}}"', {
encoding: 'utf8',
stdio: 'pipe'
});
if (!psOutput.trim()) {
console.log(chalk.red('β No synchronizer container found running'));
console.log(chalk.yellow('Start a container first with: synchronize start'));
process.exit(1);
}
console.log(chalk.green(`β
Found running container: ${psOutput.trim()}`));
} catch (error) {
console.log(chalk.red('β Error checking containers:', error.message));
process.exit(1);
}
// Connect to WebSocket
const wsUrl = 'ws://localhost:3333';
console.log(chalk.cyan(`π Connecting to ${wsUrl}...`));
const ws = new WebSocket(wsUrl, {
handshakeTimeout: 10000,
timeout: 10000
});
let messageCount = 0;
let connectionStartTime = Date.now();
ws.on('open', () => {
console.log(chalk.green('β
WebSocket connected successfully!'));
console.log(chalk.gray(`Connection established in ${Date.now() - connectionStartTime}ms\n`));
// Send different types of requests to see what the container responds with
const requests = [
{ what: 'stats' },
{ what: 'debug' },
{ what: 'queryWalletStats' },
{ what: 'pingFromMain' }
];
console.log(chalk.blue('π‘ Sending test requests...\n'));
requests.forEach((request, index) => {
setTimeout(() => {
console.log(chalk.yellow(`β Sending request ${index + 1}: ${JSON.stringify(request)}`));
ws.send(JSON.stringify(request));
}, index * 1000);
});
// Send periodic stats requests
const statsInterval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
console.log(chalk.gray(`β Requesting stats (periodic)...`));
ws.send(JSON.stringify({ what: 'stats' }));
} else {
clearInterval(statsInterval);
}
}, 5000);
});
ws.on('message', (data) => {
messageCount++;
const timestamp = new Date().toLocaleTimeString();
console.log(chalk.blue(`\nπ¨ Message ${messageCount} received at ${timestamp}:`));
console.log(chalk.white('Raw data:'), data.toString());
try {
const parsed = JSON.parse(data.toString());
console.log(chalk.green('β
Parsed JSON:'));
console.log(JSON.stringify(parsed, null, 2));
// Analyze the content
if (parsed.what) {
console.log(chalk.cyan(`π Message type: "${parsed.what}"`));
}
if (parsed.value || parsed.data) {
const stats = parsed.value || parsed.data;
console.log(chalk.magenta('π Stats found:'));
// Show key stats
const keyStats = {
sessions: stats.sessions,
users: stats.users,
syncLifePoints: stats.syncLifePoints,
walletLifePoints: stats.walletLifePoints,
syncLifeTraffic: stats.syncLifeTraffic,
bytesIn: stats.bytesIn,
bytesOut: stats.bytesOut,
proxyConnectionState: stats.proxyConnectionState,
availability: stats.availability,
reliability: stats.reliability,
efficiency: stats.efficiency,
isEarning: stats.isEarning
};
Object.entries(keyStats).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
console.log(chalk.white(` ${key}: ${value}`));
}
});
}
// Check for real vs fake data indicators
const dataQuality = analyzeDataQuality(parsed);
console.log(chalk.yellow(`π Data quality assessment: ${dataQuality}`));
} catch (error) {
console.log(chalk.red('β Failed to parse JSON:'), error.message);
}
console.log(chalk.gray('β'.repeat(60)));
});
ws.on('error', (error) => {
console.log(chalk.red('β WebSocket error:'), error.message);
if (error.code === 'ECONNREFUSED') {
console.log(chalk.yellow('\nπ‘ Troubleshooting:'));
console.log(chalk.gray('1. Make sure synchronizer container is running: docker ps'));
console.log(chalk.gray('2. Check if port 3333 is exposed: docker port <container-name>'));
console.log(chalk.gray('3. Verify container started with: -p 3333:3333'));
}
});
ws.on('close', (code, reason) => {
console.log(chalk.yellow(`\nπ WebSocket closed. Code: ${code}, Reason: ${reason || 'No reason given'}`));
console.log(chalk.blue(`π Total messages received: ${messageCount}`));
process.exit(0);
});
// Handle Ctrl+C
process.on('SIGINT', () => {
console.log(chalk.yellow('\nπ Stopping WebSocket test...'));
if (ws.readyState === WebSocket.OPEN) {
ws.close();
} else {
process.exit(0);
}
});
// Timeout after 30 seconds if no messages
setTimeout(() => {
if (messageCount === 0) {
console.log(chalk.red('\nβ° No messages received after 30 seconds'));
console.log(chalk.yellow('The container might not be responding to WebSocket requests'));
ws.close();
}
}, 30000);
/**
* Analyzes WebSocket data quality to determine if real activity is present
* @function analyzeDataQuality
* @param {Object} data - WebSocket response data to analyze
* @returns {string} Quality assessment message with emoji indicators
* @complexity O(1) - Fixed number of field checks
* @description Checks for:
* - Non-zero sessions and users (real activity)
* - Lifetime points (earning history)
* - Network traffic (bytes in/out)
* - Connection state validity
* Returns categorized assessment:
* - β ALL ZEROS - No activity
* - β οΈ CONNECTED BUT NO ACTIVITY - Waiting state
* - β
REAL ACTIVITY DETECTED - Active sessions
* - β
LIFETIME POINTS DETECTED - Has earnings
* - β UNCLEAR - Ambiguous data
*/
function analyzeDataQuality(data) {
const stats = data.value || data.data || data;
if (!stats || typeof stats !== 'object') {
return 'No stats data found';
}
// Check for signs of real data
const hasNonZeroSessions = (stats.sessions && stats.sessions > 0);
const hasNonZeroUsers = (stats.users && stats.users > 0);
const hasLifePoints = (stats.syncLifePoints && stats.syncLifePoints > 0) || (stats.walletLifePoints && stats.walletLifePoints > 0);
const hasTraffic = (stats.bytesIn && stats.bytesIn > 0) || (stats.bytesOut && stats.bytesOut > 0);
const hasConnectionState = stats.proxyConnectionState && stats.proxyConnectionState !== 'UNKNOWN';
// Check for signs of fake data
const allZeros = !hasNonZeroSessions && !hasNonZeroUsers && !hasLifePoints && !hasTraffic;
const onlyConnectedState = hasConnectionState && !hasNonZeroSessions && !hasNonZeroUsers;
if (allZeros) {
return 'β ALL ZEROS - No real activity detected';
} else if (onlyConnectedState) {
return 'β οΈ CONNECTED BUT NO ACTIVITY - May be waiting for traffic';
} else if (hasNonZeroSessions || hasNonZeroUsers) {
return 'β
REAL ACTIVITY DETECTED - Has active sessions/users';
} else if (hasLifePoints) {
return 'β
LIFETIME POINTS DETECTED - Has earning history';
} else {
return 'β UNCLEAR - Data present but quality uncertain';
}
}
console.log(chalk.gray('Press Ctrl+C to stop the test\n'));