-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-turn-taking.js
More file actions
executable file
Β·314 lines (259 loc) Β· 9.37 KB
/
test-turn-taking.js
File metadata and controls
executable file
Β·314 lines (259 loc) Β· 9.37 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
312
313
314
#!/usr/bin/env node
/**
* Turn-Taking Lock Tests
* Tests the turn-taking mechanism with two simulated clients
*/
const axios = require('axios');
const API_BASE = 'http://localhost:3001';
// Colors for console output
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
};
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
function logTest(testName, status, details = '') {
const statusColor = status === 'PASS' ? 'green' : status === 'FAIL' ? 'red' : 'yellow';
const statusIcon = status === 'PASS' ? 'β
' : status === 'FAIL' ? 'β' : 'β οΈ';
log(`${statusIcon} ${testName}: ${status}`, statusColor);
if (details) {
log(` ${details}`, 'cyan');
}
}
async function callApi(method, endpoint, data = null, headers = {}) {
try {
const config = {
method,
url: `${API_BASE}${endpoint}`,
headers: {
'Content-Type': 'application/json',
...headers,
},
};
if (data) {
config.data = data;
}
const response = await axios(config);
return { success: true, data: response.data, status: response.status, headers: response.headers };
} catch (error) {
return {
success: false,
error: error.response?.data || error.message,
status: error.response?.status,
headers: error.response?.headers
};
}
}
async function setupTestUsers() {
log('\nπ§ Setting up test users...', 'blue');
// Create Alice
const aliceAuthResult = await callApi('POST', '/auth/verify-code', {
email: 'alice@test.com',
code: '123456'
});
if (!aliceAuthResult.success) {
logTest('Alice Authentication', 'FAIL', aliceAuthResult.error);
return null;
}
// Create Bob
const bobAuthResult = await callApi('POST', '/auth/verify-code', {
email: 'bob@test.com',
code: '123456'
});
if (!bobAuthResult.success) {
logTest('Bob Authentication', 'FAIL', bobAuthResult.error);
return null;
}
logTest('Test Users Setup', 'PASS');
return {
alice: {
token: aliceAuthResult.data.accessToken,
headers: { Authorization: `Bearer ${aliceAuthResult.data.accessToken}` }
},
bob: {
token: bobAuthResult.data.accessToken,
headers: { Authorization: `Bearer ${bobAuthResult.data.accessToken}` }
}
};
}
async function setupTestSession(users) {
log('\nπ§ Setting up test session...', 'blue');
// Alice creates a couple
const coupleResult = await callApi('POST', '/couples', null, users.alice.headers);
if (!coupleResult.success) {
logTest('Couple Creation', 'FAIL', coupleResult.error);
return null;
}
// Bob joins the couple (simulate invite acceptance)
const inviteResult = await callApi('POST', '/invites', null, users.alice.headers);
if (!inviteResult.success) {
logTest('Invite Creation', 'FAIL', inviteResult.error);
return null;
}
// Bob accepts invite
const acceptResult = await callApi('POST', `/invites/${inviteResult.data.code}/accept`, null, users.bob.headers);
if (!acceptResult.success) {
logTest('Invite Acceptance', 'FAIL', acceptResult.error);
return null;
}
// Alice creates a session
const sessionResult = await callApi('POST', '/sessions', null, users.alice.headers);
if (!sessionResult.success) {
logTest('Session Creation', 'FAIL', sessionResult.error);
return null;
}
logTest('Test Session Setup', 'PASS');
return sessionResult.data.sessionId;
}
async function testTurnTaking(users, sessionId) {
log('\nπ― Testing Turn-Taking Mechanism...', 'blue');
// Test 1: Alice sends first message (should succeed)
log(' Test 1: Alice sends first message...', 'yellow');
const aliceMessage1 = await callApi('POST', `/sessions/${sessionId}/messages`, {
content: 'Hello Bob, I want to talk about our plans for the weekend.',
client_message_id: 'alice_msg_1'
}, users.alice.headers);
if (aliceMessage1.success) {
logTest('Alice First Message', 'PASS');
} else {
logTest('Alice First Message', 'FAIL', aliceMessage1.error);
return false;
}
// Test 2: Alice tries to send another message immediately (should fail)
log(' Test 2: Alice tries to send another message...', 'yellow');
const aliceMessage2 = await callApi('POST', `/sessions/${sessionId}/messages`, {
content: 'Actually, let me add something else...',
client_message_id: 'alice_msg_2'
}, users.alice.headers);
if (aliceMessage2.status === 409) {
logTest('Alice Turn Lock', 'PASS', 'Alice correctly blocked from sending consecutive messages');
} else {
logTest('Alice Turn Lock', 'FAIL', 'Alice should have been blocked');
return false;
}
// Test 3: Bob sends message (should succeed)
log(' Test 3: Bob responds to Alice...', 'yellow');
const bobMessage1 = await callApi('POST', `/sessions/${sessionId}/messages`, {
content: 'Hi Alice! I\'d love to hear your ideas for the weekend.',
client_message_id: 'bob_msg_1'
}, users.bob.headers);
if (bobMessage1.success) {
logTest('Bob Response', 'PASS');
} else {
logTest('Bob Response', 'FAIL', bobMessage1.error);
return false;
}
// Test 4: Bob tries to send another message (should fail)
log(' Test 4: Bob tries to send another message...', 'yellow');
const bobMessage2 = await callApi('POST', `/sessions/${sessionId}/messages`, {
content: 'Let me add one more thing...',
client_message_id: 'bob_msg_2'
}, users.bob.headers);
if (bobMessage2.status === 409) {
logTest('Bob Turn Lock', 'PASS', 'Bob correctly blocked from sending consecutive messages');
} else {
logTest('Bob Turn Lock', 'FAIL', 'Bob should have been blocked');
return false;
}
// Test 5: Alice responds (should succeed)
log(' Test 5: Alice responds to Bob...', 'yellow');
const aliceMessage3 = await callApi('POST', `/sessions/${sessionId}/messages`, {
content: 'Great! I was thinking we could go hiking on Saturday.',
client_message_id: 'alice_msg_3'
}, users.alice.headers);
if (aliceMessage3.success) {
logTest('Alice Response', 'PASS');
} else {
logTest('Alice Response', 'FAIL', aliceMessage3.error);
return false;
}
return true;
}
async function testConcurrentAccess(users, sessionId) {
log('\nβ‘ Testing Concurrent Access...', 'blue');
// Simulate both users trying to send messages simultaneously
log(' Simulating concurrent message attempts...', 'yellow');
const promises = [
callApi('POST', `/sessions/${sessionId}/messages`, {
content: 'Concurrent message from Alice',
client_message_id: 'alice_concurrent'
}, users.alice.headers),
callApi('POST', `/sessions/${sessionId}/messages`, {
content: 'Concurrent message from Bob',
client_message_id: 'bob_concurrent'
}, users.bob.headers)
];
const results = await Promise.all(promises);
// One should succeed, one should fail
const successCount = results.filter(r => r.success).length;
const failCount = results.filter(r => r.status === 409).length;
if (successCount === 1 && failCount === 1) {
logTest('Concurrent Access Control', 'PASS', 'Only one message succeeded, one was blocked');
} else {
logTest('Concurrent Access Control', 'FAIL', `Expected 1 success, 1 fail. Got ${successCount} success, ${failCount} fails`);
return false;
}
return true;
}
async function testTurnLockExpiration(users, sessionId) {
log('\nβ° Testing Turn Lock Expiration...', 'blue');
// This test would require modifying the lock duration for testing
// For now, we'll just verify the lock mechanism is working
log(' Turn lock expiration test would require shorter lock duration for testing', 'yellow');
logTest('Turn Lock Expiration', 'SKIP', 'Requires test configuration');
return true;
}
async function runTurnTakingTests() {
log('π Starting Turn-Taking Tests...', 'bright');
log('Testing turn-taking mechanism with two simulated clients', 'cyan');
try {
// Setup
const users = await setupTestUsers();
if (!users) {
log('β Failed to setup test users', 'red');
return;
}
const sessionId = await setupTestSession(users);
if (!sessionId) {
log('β Failed to setup test session', 'red');
return;
}
// Run tests
const turnTakingSuccess = await testTurnTaking(users, sessionId);
if (!turnTakingSuccess) {
log('β Turn-taking tests failed', 'red');
return;
}
const concurrentSuccess = await testConcurrentAccess(users, sessionId);
if (!concurrentSuccess) {
log('β Concurrent access tests failed', 'red');
return;
}
await testTurnLockExpiration(users, sessionId);
log('\nπ Turn-Taking Tests Completed Successfully!', 'green');
log('=====================================', 'green');
log('β
Turn-taking mechanism is working correctly', 'cyan');
log('β
Concurrent access is properly controlled', 'cyan');
log('β
Users cannot send consecutive messages', 'cyan');
} catch (error) {
log(`\nβ Test suite failed: ${error.message}`, 'red');
process.exit(1);
}
}
// Run tests if this script is executed directly
if (require.main === module) {
runTurnTakingTests();
}
module.exports = {
runTurnTakingTests,
testTurnTaking,
testConcurrentAccess,
testTurnLockExpiration,
};