-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_complete.js
More file actions
297 lines (255 loc) · 7.93 KB
/
Copy pathtest_complete.js
File metadata and controls
297 lines (255 loc) · 7.93 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
const WebSocket = require('ws');
const http = require('http');
const BASE_URL = 'http://103.40.13.96:8081';
const WS_URL = 'ws://103.40.13.96:8081';
const results = { passed: 0, failed: 0, tests: [] };
function log(message) {
console.log(`[${new Date().toISOString()}] ${message}`);
}
function assert(condition, testName) {
if (condition) {
results.passed++;
results.tests.push({ name: testName, status: 'PASS' });
log(`✓ ${testName}`);
} else {
results.failed++;
results.tests.push({ name: testName, status: 'FAIL' });
log(`✗ ${testName}`);
}
}
async function httpRequest(method, path, data, token) {
return new Promise((resolve, reject) => {
const postData = data ? JSON.stringify(data) : null;
const options = {
hostname: '103.40.13.96',
port: 8081,
path,
method,
headers: {
'Content-Type': 'application/json',
...(token && { 'Authorization': `Bearer ${token}` }),
...(postData && { 'Content-Length': Buffer.byteLength(postData) })
}
};
const req = http.request(options, (res) => {
let responseData = '';
res.on('data', chunk => responseData += chunk);
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
try {
resolve(responseData ? JSON.parse(responseData) : {});
} catch (e) {
// Response is not JSON (like "ok" from healthz)
resolve({ raw: responseData });
}
} else {
reject(new Error(`HTTP ${res.statusCode}: ${responseData}`));
}
});
});
req.on('error', reject);
if (postData) req.write(postData);
req.end();
});
}
async function testHealthCheck() {
try {
await httpRequest('GET', '/healthz');
assert(true, 'Health check endpoint');
} catch (e) {
assert(false, 'Health check endpoint');
}
}
async function testReadyCheck() {
try {
await httpRequest('GET', '/readyz');
assert(true, 'Ready check endpoint');
} catch (e) {
assert(false, 'Ready check endpoint');
}
}
async function testCompleteCallFlow() {
const timestamp = Date.now().toString().slice(-8);
const password = 'TestP@ss123!';
try {
// Register two users
const user1 = await httpRequest('POST', '/v1/auth/register', {
username: `caller_${timestamp}`,
password,
displayName: `Caller ${timestamp}`
});
log(`Caller created: ${user1.user.id}`);
const user2 = await httpRequest('POST', '/v1/auth/register', {
username: `callee_${timestamp}`,
password,
displayName: `Callee ${timestamp}`
});
log(`Callee created: ${user2.user.id}`);
// Create session between users
const session = await httpRequest('POST', '/v1/sessions', {
peerUserId: user2.user.id
}, user1.token);
log(`Session created: ${session.session.id}`);
// Create a call
const call = await httpRequest('POST', '/v1/calls', {
calleeUserId: user2.user.id,
mediaType: 'voice'
}, user1.token);
log(`Call created: ${call.call.id}`);
// Accept the call
await httpRequest('POST', `/v1/calls/${call.call.id}/accept`, {}, user2.token);
log(`Call accepted`);
// Now test frame relay with valid call
await testFrameRelayWithCall(user1.token, user2.token, call.call.id);
assert(true, 'Complete call flow');
} catch (e) {
log(`Call flow error: ${e.message}`);
assert(false, 'Complete call flow');
}
}
async function testFrameRelayWithCall(token1, token2, callId) {
return new Promise((resolve) => {
const ws1 = new WebSocket(`${WS_URL}/v1/ws?token=${token1}`);
const ws2 = new WebSocket(`${WS_URL}/v1/ws?token=${token2}`);
let ws1Ready = false;
let ws2Ready = false;
let audioReceived = false;
let videoReceived = false;
ws1.on('open', () => {
ws1Ready = true;
checkReady();
});
ws2.on('open', () => {
ws2Ready = true;
checkReady();
});
function checkReady() {
if (ws1Ready && ws2Ready) {
// Send audio frame
ws1.send(JSON.stringify({
type: 'audio.frame',
callId,
data: Buffer.from('test audio').toString('base64')
}));
// Send video frame
setTimeout(() => {
ws1.send(JSON.stringify({
type: 'video.frame',
callId,
data: Buffer.from('test video').toString('base64')
}));
}, 100);
}
}
ws2.on('message', (data) => {
try {
const msg = JSON.parse(data.toString());
if (msg.type === 'audio.frame' && msg.payload?.callId === callId) {
audioReceived = true;
log('Audio frame received');
}
if (msg.type === 'video.frame' && msg.payload?.callId === callId) {
videoReceived = true;
log('Video frame received');
}
} catch (e) {}
});
setTimeout(() => {
assert(audioReceived, 'Audio frame relay with valid call');
assert(videoReceived, 'Video frame relay with valid call');
ws1.close();
ws2.close();
resolve();
}, 2000);
ws1.on('error', () => {});
ws2.on('error', () => {});
});
}
async function testHighFrequencyWithCall() {
const timestamp = Date.now().toString().slice(-8);
const password = 'TestP@ss123!';
try {
// Create users and call
const user1 = await httpRequest('POST', '/v1/auth/register', {
username: `hf1_${timestamp}`,
password,
displayName: `HF1`
});
const user2 = await httpRequest('POST', '/v1/auth/register', {
username: `hf2_${timestamp}`,
password,
displayName: `HF2`
});
await httpRequest('POST', '/v1/sessions', {
peerUserId: user2.user.id
}, user1.token);
const call = await httpRequest('POST', '/v1/calls', {
calleeUserId: user2.user.id,
mediaType: 'voice'
}, user1.token);
await httpRequest('POST', `/v1/calls/${call.call.id}/accept`, {}, user2.token);
// Test high frequency
await new Promise((resolve) => {
const ws1 = new WebSocket(`${WS_URL}/v1/ws?token=${user1.token}`);
const ws2 = new WebSocket(`${WS_URL}/v1/ws?token=${user2.token}`);
let sentCount = 0;
let receivedCount = 0;
const totalFrames = 100;
ws1.on('open', () => {
ws2.on('open', () => {
const interval = setInterval(() => {
if (sentCount >= totalFrames) {
clearInterval(interval);
return;
}
ws1.send(JSON.stringify({
type: 'audio.frame',
callId: call.call.id,
data: Buffer.from(`frame${sentCount}`).toString('base64')
}));
sentCount++;
}, 10);
});
});
ws2.on('message', (data) => {
try {
const msg = JSON.parse(data.toString());
if (msg.type === 'audio.frame') receivedCount++;
} catch (e) {}
});
setTimeout(() => {
const rate = (receivedCount / totalFrames) * 100;
assert(rate >= 90, `High frequency (${receivedCount}/${totalFrames} = ${rate.toFixed(1)}%)`);
ws1.close();
ws2.close();
resolve();
}, 3000);
});
} catch (e) {
log(`High frequency test error: ${e.message}`);
assert(false, 'High frequency frames');
}
}
async function runTests() {
log('Starting comprehensive external tests...\n');
await testHealthCheck();
await testReadyCheck();
await testCompleteCallFlow();
await testHighFrequencyWithCall();
log('\n' + '='.repeat(50));
log('TEST RESULTS');
log('='.repeat(50));
results.tests.forEach(test => {
log(`${test.status === 'PASS' ? '✓' : '✗'} ${test.name}`);
});
log('='.repeat(50));
log(`Total: ${results.passed + results.failed} tests`);
log(`Passed: ${results.passed}`);
log(`Failed: ${results.failed}`);
log('='.repeat(50));
process.exit(results.failed > 0 ? 1 : 0);
}
runTests().catch(err => {
log(`Fatal error: ${err.message}`);
process.exit(1);
});