-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-sse-client.js
More file actions
152 lines (127 loc) Β· 4.32 KB
/
Copy pathtest-sse-client.js
File metadata and controls
152 lines (127 loc) Β· 4.32 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
/**
* Test SSE Client
* Simulates frontend SSE connection for testing
*/
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const EventSourceLib = require('eventsource');
const EventSource = EventSourceLib.default || EventSourceLib;
const BASE_URL = 'http://localhost:8051/api/create';
const TEST_SESSION_ID = 'test-session-' + Date.now();
console.log('π§ͺ Testing SSE connectivity...');
console.log(`π‘ Session ID: ${TEST_SESSION_ID}`);
console.log(`π SSE URL: ${BASE_URL}/streaming/test-sse/${TEST_SESSION_ID}`);
// First, let's test if the server responds to regular requests
async function testServerConnection() {
try {
const response = await fetch(`${BASE_URL}/health`);
if (response.ok) {
console.log('β
Server is reachable');
return true;
} else {
console.log('β Server responded with error:', response.status);
return false;
}
} catch (error) {
console.log('β Cannot reach server:', error.message);
return false;
}
}
// Test SSE connection (without auth for now)
function testSSEConnection() {
console.log('\nπ‘ Testing SSE connection...');
const eventSource = new EventSource(`${BASE_URL}/streaming/test-sse/${TEST_SESSION_ID}`);
eventSource.onopen = function(event) {
console.log('β
SSE connection opened');
};
eventSource.onmessage = function(event) {
console.log('π¨ Received message:', event.data);
};
// Listen for specific events
eventSource.addEventListener('connected', function(event) {
console.log('π Connected event:', JSON.parse(event.data));
});
eventSource.addEventListener('batch-started', function(event) {
console.log('π Batch started:', JSON.parse(event.data));
});
eventSource.addEventListener('text-chunk', function(event) {
const data = JSON.parse(event.data);
console.log(`π¬ Text chunk for ${data.questionId}: "${data.chunk}"`);
});
eventSource.addEventListener('question-complete', function(event) {
const data = JSON.parse(event.data);
console.log(`β
Question complete: ${data.questionId}`);
});
eventSource.addEventListener('batch-complete', function(event) {
console.log('π Batch complete:', JSON.parse(event.data));
eventSource.close();
console.log('π Test completed - SSE connection closed');
process.exit(0);
});
eventSource.addEventListener('error', function(event) {
console.log('π¨ SSE error event:', JSON.parse(event.data));
});
eventSource.onerror = function(event) {
console.log('β SSE connection error:', event);
if (event.type === 'error') {
console.log('π Will retry connection...');
}
};
// Cleanup after 30 seconds
setTimeout(() => {
console.log('β° Test timeout - closing connection');
eventSource.close();
process.exit(0);
}, 30000);
return eventSource;
}
// Test mock generation endpoint
async function testMockGeneration() {
console.log('\nπ§ͺ Testing mock generation...');
try {
const response = await fetch(`${BASE_URL}/streaming/test-mock`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
sessionId: TEST_SESSION_ID,
testType: 'mock-generation'
})
});
if (response.ok) {
const result = await response.json();
console.log('β
Mock generation started:', result);
return true;
} else {
console.log('β Mock generation failed:', response.status);
const error = await response.text();
console.log('Error details:', error);
return false;
}
} catch (error) {
console.log('β Mock generation request failed:', error.message);
return false;
}
}
// Run the test
async function runTest() {
console.log('π― Starting SSE Test Suite\n');
// Test 1: Check server connectivity
const serverOk = await testServerConnection();
if (!serverOk) {
console.log('β Cannot proceed - server not reachable');
process.exit(1);
}
// Test 2: Setup SSE connection
const eventSource = testSSEConnection();
// Test 3: Wait a moment for connection to establish
setTimeout(async () => {
// Test 4: Trigger mock generation
await testMockGeneration();
}, 2000);
}
runTest().catch(error => {
console.error('β Test failed:', error);
process.exit(1);
});