-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-websocket.js
More file actions
147 lines (122 loc) Β· 4.06 KB
/
test-websocket.js
File metadata and controls
147 lines (122 loc) Β· 4.06 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
const WebSocket = require('ws');
// Test configuration
const TEST_CONFIG = {
serverUrl: 'ws://localhost:3000/ws/notifications',
guestId: 123,
token: 'your-test-token-here', // Replace with actual JWT token
testMessage: 'This is a test notification from WebSocket'
};
// Test WebSocket connection
function testWebSocketConnection() {
console.log('π§ͺ Testing WebSocket Connection...');
const wsUrl = `${TEST_CONFIG.serverUrl}?token=${TEST_CONFIG.token}&guestId=${TEST_CONFIG.guestId}`;
const ws = new WebSocket(wsUrl);
ws.on('open', () => {
console.log('β
WebSocket connected successfully');
console.log('π‘ Listening for notifications...');
});
ws.on('message', (data) => {
try {
const message = JSON.parse(data);
console.log('π¨ Received message:', message);
if (message.type === 'connection') {
console.log('β
Connection confirmed by server');
} else if (message.type === 'notification') {
console.log('π Notification received:', message.data.title);
console.log(' Message:', message.data.message);
}
} catch (error) {
console.error('β Error parsing message:', error);
}
});
ws.on('close', (code, reason) => {
console.log(`π WebSocket closed: ${code} - ${reason}`);
});
ws.on('error', (error) => {
console.error('β WebSocket error:', error);
});
return ws;
}
// Test HTTP endpoints
async function testHttpEndpoints() {
console.log('\nπ Testing HTTP Endpoints...');
const baseUrl = 'http://localhost:3000/api/communications';
try {
// Test WebSocket stats
console.log('π Getting WebSocket statistics...');
const statsResponse = await fetch(`${baseUrl}/websocket/stats`);
const stats = await statsResponse.json();
console.log('β
WebSocket stats:', stats);
// Test sending notification via HTTP
console.log('π€ Sending test notification via HTTP...');
const notificationData = {
hotel_id: 1,
type: 'notification',
title: 'Test Notification from HTTP',
message: 'This notification was sent via HTTP API',
category: 'general',
recipient_type: 'guest',
recipient_id: TEST_CONFIG.guestId,
priority: 'normal',
sender_type: 'hotel'
};
const createResponse = await fetch(baseUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${TEST_CONFIG.token}`
},
body: JSON.stringify(notificationData)
});
const createResult = await createResponse.json();
console.log('β
Notification created:', createResult);
// Test WebSocket test endpoint
console.log('π§ͺ Testing WebSocket test endpoint...');
const testResponse = await fetch(`${baseUrl}/websocket/test`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
guestId: TEST_CONFIG.guestId,
message: TEST_CONFIG.testMessage
})
});
const testResult = await testResponse.json();
console.log('β
Test notification sent:', testResult);
} catch (error) {
console.error('β HTTP test error:', error);
}
}
// Main test function
async function runTests() {
console.log('π Starting WebSocket and HTTP Tests...\n');
// Start WebSocket connection
const ws = testWebSocketConnection();
// Wait a bit for connection to establish
setTimeout(async () => {
// Test HTTP endpoints
await testHttpEndpoints();
// Keep WebSocket open for a while to receive notifications
console.log('\nβ³ Waiting 10 seconds for notifications...');
setTimeout(() => {
console.log('π Tests completed');
ws.close();
process.exit(0);
}, 10000);
}, 2000);
}
// Handle process termination
process.on('SIGINT', () => {
console.log('\nπ Tests interrupted');
process.exit(0);
});
// Run tests if this file is executed directly
if (require.main === module) {
runTests();
}
module.exports = {
testWebSocketConnection,
testHttpEndpoints,
runTests
};