-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_alert_functionality.html
More file actions
200 lines (173 loc) · 8.41 KB
/
test_alert_functionality.html
File metadata and controls
200 lines (173 loc) · 8.41 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alert Functionality Test</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
.test-section { margin: 20px 0; padding: 15px; border: 1px solid #ccc; border-radius: 5px; }
.success { background: #d4edda; border-color: #c3e6cb; }
.error { background: #f8d7da; border-color: #f5c6cb; }
button { padding: 10px 20px; margin: 5px; cursor: pointer; }
.log { background: #f8f9fa; padding: 10px; border-radius: 5px; margin: 10px 0; font-family: monospace; }
</style>
</head>
<body>
<h1>🚨 CrowdShield Alert Functionality Test</h1>
<div class="test-section">
<h2>1. Backend Health Check</h2>
<button onclick="testBackendHealth()">Test Backend Health</button>
<div id="health-result" class="log"></div>
</div>
<div class="test-section">
<h2>2. Alert Endpoint Test</h2>
<button onclick="testAlertEndpoint()">Test Alert Endpoint</button>
<div id="alert-result" class="log"></div>
</div>
<div class="test-section">
<h2>3. Frontend Alert Page Test</h2>
<button onclick="testFrontendAlert()">Test Frontend Alert Page</button>
<div id="frontend-result" class="log"></div>
</div>
<div class="test-section">
<h2>4. Complete Alert Flow Test</h2>
<button onclick="testCompleteFlow()">Test Complete Alert Flow</button>
<div id="complete-result" class="log"></div>
</div>
<script>
function log(elementId, message, isSuccess = true) {
const element = document.getElementById(elementId);
const timestamp = new Date().toLocaleTimeString();
const status = isSuccess ? '✅' : '❌';
element.innerHTML += `<div>[${timestamp}] ${status} ${message}</div>`;
element.scrollTop = element.scrollHeight;
}
async function testBackendHealth() {
const resultDiv = document.getElementById('health-result');
resultDiv.innerHTML = '';
try {
log('health-result', 'Testing backend health...');
const response = await fetch('http://localhost:5003/health');
const data = await response.json();
if (response.ok) {
log('health-result', 'Backend is healthy!');
log('health-result', `Services: ${Object.keys(data.services).join(', ')}`);
} else {
log('health-result', `Backend error: ${response.status}`, false);
}
} catch (error) {
log('health-result', `Backend connection failed: ${error.message}`, false);
}
}
async function testAlertEndpoint() {
const resultDiv = document.getElementById('alert-result');
resultDiv.innerHTML = '';
try {
log('alert-result', 'Testing alert endpoint...');
const alertData = {
level: 'CRITICAL',
message: 'Test emergency alert from CrowdShield',
location: 'Nashik Kumbh Mela - Central Festival Area',
density: '2.8 people/m²',
threshold_exceeded: '+180%',
affected_areas: '3 Gates',
coordinates: {
latitude: 19.9975,
longitude: 73.7898
},
recommendations: [
'Deploy additional security personnel',
'Activate crowd control barriers'
]
};
const response = await fetch('http://localhost:5003/api/alerts/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(alertData)
});
const result = await response.json();
if (response.ok) {
log('alert-result', 'Alert sent successfully!');
log('alert-result', `MQTT sent: ${result.mqtt_sent}`);
log('alert-result', `Twilio sent: ${result.twilio_sent}`);
log('alert-result', `Alert ID: ${result.alert_data.alert_id}`);
} else {
log('alert-result', `Alert endpoint error: ${result.error}`, false);
}
} catch (error) {
log('alert-result', `Alert endpoint failed: ${error.message}`, false);
}
}
async function testFrontendAlert() {
const resultDiv = document.getElementById('frontend-result');
resultDiv.innerHTML = '';
try {
log('frontend-result', 'Testing frontend alert page...');
const response = await fetch('http://localhost:3000/alert.html');
if (response.ok) {
log('frontend-result', 'Frontend alert page is accessible!');
log('frontend-result', 'Alert page loaded successfully');
} else {
log('frontend-result', `Frontend error: ${response.status}`, false);
}
} catch (error) {
log('frontend-result', `Frontend connection failed: ${error.message}`, false);
}
}
async function testCompleteFlow() {
const resultDiv = document.getElementById('complete-result');
resultDiv.innerHTML = '';
try {
log('complete-result', 'Testing complete alert flow...');
// Step 1: Check backend health
log('complete-result', 'Step 1: Checking backend health...');
const healthResponse = await fetch('http://localhost:5003/health');
if (!healthResponse.ok) {
throw new Error('Backend health check failed');
}
log('complete-result', '✅ Backend is healthy');
// Step 2: Send test alert
log('complete-result', 'Step 2: Sending test alert...');
const alertData = {
level: 'CRITICAL',
message: 'Complete flow test alert',
location: 'Nashik Kumbh Mela - Test Area',
density: '3.0 people/m²',
threshold_exceeded: '+200%',
affected_areas: 'Test Gates',
coordinates: {
latitude: 19.9975,
longitude: 73.7898
},
recommendations: ['Test recommendation']
};
const alertResponse = await fetch('http://localhost:5003/api/alerts/send', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(alertData)
});
if (!alertResponse.ok) {
throw new Error('Alert sending failed');
}
const alertResult = await alertResponse.json();
log('complete-result', '✅ Alert sent successfully');
log('complete-result', `MQTT: ${alertResult.mqtt_sent}, Twilio: ${alertResult.twilio_sent}`);
// Step 3: Check frontend
log('complete-result', 'Step 3: Checking frontend...');
const frontendResponse = await fetch('http://localhost:3000/alert.html');
if (!frontendResponse.ok) {
throw new Error('Frontend check failed');
}
log('complete-result', '✅ Frontend is accessible');
log('complete-result', '🎉 Complete alert flow test PASSED!');
log('complete-result', 'Your alert system is working correctly!');
} catch (error) {
log('complete-result', `❌ Complete flow test failed: ${error.message}`, false);
}
}
</script>
</body>
</html>