-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestNotificationsLive.ts
More file actions
252 lines (225 loc) · 8.71 KB
/
testNotificationsLive.ts
File metadata and controls
252 lines (225 loc) · 8.71 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
/**
* LIVE NOTIFICATION TESTING SCRIPT
*
* Run this directly from Expo console to test the notification system
*
* Usage in Expo dev console:
* import('./testNotificationsLive').then(module => module.runLiveTests())
*/
import * as Notifications from 'expo-notifications';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {
scheduleRoutineNotifications,
requestNotificationPermissions,
getCompletionStatus,
cancelAllNotifications
} from './utils/notificationManager';
import {
loadRoutines,
loadSettings,
createRoutine,
confirmRoutine,
undoRoutineToday,
DEFAULT_SETTINGS
} from './utils/settingsStorage';
/**
* Live notification testing
*/
export const runLiveTests = async () => {
console.log('\n\n');
console.log('════════════════════════════════════════════════════════');
console.log('🧪 LIVE NOTIFICATION SYSTEM TESTING');
console.log('════════════════════════════════════════════════════════\n');
let passed = 0;
let failed = 0;
let skipped = 0;
try {
// TEST 1: Permission Handling
console.log('📌 TEST 1: Permission Handling');
console.log('─'.repeat(50));
try {
const permissions = await Notifications.getPermissionsAsync();
console.log(` ✓ Current permissions: ${permissions.status}`);
if (permissions.status !== 'granted') {
console.log(' ⓘ Requesting permissions...');
const result = await requestNotificationPermissions();
if (result) {
console.log(' ✅ PASS: Permissions granted');
passed++;
} else {
console.log(' ❌ FAIL: Permission request failed');
failed++;
}
} else {
console.log(' ✅ PASS: Permissions already granted');
passed++;
}
} catch (error) {
console.log(` ❌ ERROR: ${error}`);
failed++;
}
// TEST 2: Settings Persistence
console.log('\n📌 TEST 2: Settings Persistence');
console.log('─'.repeat(50));
try {
const settingsData = await loadSettings();
console.log(` ✓ Loaded settings:`, {
multipleReminders: settingsData.multipleReminders,
customTimes: settingsData.customTimes,
maxNotifications: settingsData.maxEscalationLevel
});
console.log(' ✅ PASS: Settings loaded correctly');
passed++;
} catch (error) {
console.log(` ❌ ERROR: ${error}`);
failed++;
}
// TEST 3: Routine Loading
console.log('\n📌 TEST 3: Routine Loading');
console.log('─'.repeat(50));
try {
const routines = await loadRoutines();
console.log(` ✓ Loaded ${routines.length} routines:`);
routines.forEach(r => {
console.log(` - ${r.name} (${r.isActive ? 'active' : 'inactive'})`);
});
if (routines.length > 0) {
console.log(' ✅ PASS: Routines loaded successfully');
passed++;
} else {
console.log(' ⚠️ SKIP: No routines configured');
skipped++;
}
} catch (error) {
console.log(` ❌ ERROR: ${error}`);
failed++;
}
// TEST 4: Status Calculation (Real-Time)
console.log('\n📌 TEST 4: Real-Time Status Calculation');
console.log('─'.repeat(50));
try {
const routines = await loadRoutines();
if (routines.length > 0) {
const status = await getCompletionStatus(routines);
console.log(` ✓ Status:`, {
total: status.total,
completed: status.completed,
skipped: status.skipped,
remaining: status.remaining
});
// Check if status shows false positives
const today = new Date().toISOString().split('T')[0];
const canCalculateToday = routines.some(r => {
const lastConfirmed = r.lastConfirmed || '';
return lastConfirmed !== today;
});
if (canCalculateToday || status.completed === 0) {
console.log(' ✅ PASS: Real-time status calculation working');
passed++;
} else {
console.log(' ⚠️ SKIP: All routines completed today');
skipped++;
}
} else {
console.log(' ⚠️ SKIP: No routines to check');
skipped++;
}
} catch (error) {
console.log(` ❌ ERROR: ${error}`);
failed++;
}
// TEST 5: Notification Scheduling
console.log('\n📌 TEST 5: Notification Scheduling');
console.log('─'.repeat(50));
try {
console.log(' ℹ Scheduling notifications...');
await scheduleRoutineNotifications();
// Check how many notifications were scheduled
const scheduled = await Notifications.getAllScheduledNotificationsAsync();
console.log(` ✓ Scheduled ${scheduled.length} notifications`);
// Log scheduled times
const times = scheduled
.filter(n => n.trigger && 'hour' in n.trigger)
.map(n => {
const trigger = n.trigger as any;
return `${String(trigger.hour).padStart(2, '0')}:${String(trigger.minute || 0).padStart(2, '0')}`;
})
.sort();
if (times.length > 0) {
console.log(` ✓ Notification times: ${times.join(', ')}`);
}
// Check if cap is enforced (max 6)
if (scheduled.length <= 6) {
console.log(` ✅ PASS: Notification cap enforced (${scheduled.length} ≤ 6)`);
passed++;
} else {
console.log(` ❌ FAIL: Too many notifications (${scheduled.length} > 6)`);
failed++;
}
} catch (error) {
console.log(` ❌ ERROR: ${error}`);
failed++;
}
// TEST 6: Settings Respect
console.log('\n📌 TEST 6: Settings Respect Check');
console.log('─'.repeat(50));
try {
const settings = await loadSettings();
// Check if custom times are respected
if (settings.customTimes && settings.reminderTimes && settings.reminderTimes.length > 0) {
const scheduled = await Notifications.getAllScheduledNotificationsAsync();
const times = scheduled
.filter(n => n.trigger && 'hour' in n.trigger)
.map(n => {
const trigger = n.trigger as any;
return `${String(trigger.hour).padStart(2, '0')}:${String(trigger.minute || 0).padStart(2, '0')}`;
});
console.log(` ✓ Custom times configured: ${settings.reminderTimes.join(', ')}`);
console.log(` ✓ Scheduled times: ${times.join(', ')}`);
// Check if scheduled times match custom times
const customTimes = settings.reminderTimes || [];
const allMatch = times.every(t => customTimes.includes(t));
if (allMatch || times.length === customTimes.length) {
console.log(' ✅ PASS: Custom times are respected');
passed++;
} else {
console.log(' ⚠️ PARTIAL: Times may differ from custom settings');
skipped++;
}
} else {
console.log(' ✓ Using escalation times (no custom times set)');
const scheduled = await Notifications.getAllScheduledNotificationsAsync();
if (scheduled.length > 0 && scheduled.length <= 6) {
console.log(` ✅ PASS: Escalation respects settings (${scheduled.length} notifications)`);
passed++;
} else {
console.log(` ⚠️ PARTIAL: Check escalation logic`);
skipped++;
}
}
} catch (error) {
console.log(` ❌ ERROR: ${error}`);
failed++;
}
// SUMMARY
console.log('\n' + '═'.repeat(50));
console.log('📊 TEST SUMMARY');
console.log('═'.repeat(50));
console.log(`✅ Passed: ${passed}`);
console.log(`❌ Failed: ${failed}`);
console.log(`⚠️ Skipped: ${skipped}`);
console.log(`📈 Total: ${passed + failed + skipped}`);
const successRate = ((passed / (passed + failed)) * 100).toFixed(1);
console.log(`\n🎯 Success Rate: ${successRate}%`);
if (failed === 0) {
console.log('\n🎉 ALL CRITICAL TESTS PASSED!');
console.log('✅ Notification system is production-ready\n');
} else {
console.log('\n⚠️ Some tests failed. Check logs above for details.\n');
}
} catch (error) {
console.error('FATAL ERROR:', error);
}
};
// Export for use in other files
export default { runLiveTests };