Report delivered device count from the test-notification endpoint

POST /api/notifications/test previously returned a blanket 200 even when there
were no registered devices, so a test that reached nobody looked like a success.
push::send now returns the number of devices the relay confirmed delivery to, and
the endpoint returns it as {"delivered": N}. Settings shows "sent to N device(s)"
on success and an explicit "No devices are registered..." warning when N is 0,
which is the case that previously masqueraded as success.

Backend, API and widget tests updated; clippy and dart analyze clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
rzuasti
2026-06-09 14:06:56 -04:00
co-authored by Claude Opus 4.8
parent 0075072196
commit 475edcb2cf
8 changed files with 82 additions and 33 deletions
+15 -5
View File
@@ -143,12 +143,22 @@ class _SettingsState extends State<Settings> {
Future<void> _sendTestNotification() async {
setState(() => _testBusy = true);
try {
await BackendAPI.instance.sendTestNotification();
final delivered = await BackendAPI.instance.sendTestNotification();
if (!mounted) return;
UISnackbars.showSuccess(
context,
'Test notification sent. It should arrive shortly.',
);
if (delivered == 0) {
// The request succeeded but no device received it — usually the backend
// lost this device's token (e.g. after a restart); re-toggle to re-register.
UISnackbars.showError(
context,
'No devices are registered to receive push notifications.',
);
} else {
final devices = delivered == 1 ? 'device' : 'devices';
UISnackbars.showSuccess(
context,
'Test notification sent to $delivered $devices.',
);
}
} catch (e) {
debugPrint('Failed to send test notification: $e');
if (!mounted) return;
@@ -15,9 +15,11 @@ extension NotificationApi on BackendAPI {
}
/// Asks the backend to deliver a one-off test push to every registered device,
/// used from Settings to verify end-to-end push delivery.
Future<void> sendTestNotification() async {
await _dio.post('/notifications/test');
/// used from Settings to verify end-to-end push delivery. Returns the number of
/// devices the relay confirmed delivery to (0 when none are registered).
Future<int> sendTestNotification() async {
final response = await _dio.post('/notifications/test');
return (response.data as Map<String, dynamic>)['delivered'] as int;
}
Future<({List<Notification> items, int totalCount})> listNotifications(
@@ -36,13 +36,13 @@ void main() {
await BackendAPI.instance.markAllNotificationsAsRead();
});
test('sendTestNotification POSTs to /notifications/test', () async {
test('sendTestNotification POSTs and returns the delivered count', () async {
adapter.onPost(
'/notifications/test',
(server) => server.reply(200, null),
(server) => server.reply(200, {'delivered': 2}),
);
await BackendAPI.instance.sendTestNotification();
expect(await BackendAPI.instance.sendTestNotification(), 2);
});
test(
+24 -3
View File
@@ -173,11 +173,13 @@ void main() {
expect(find.text('Send test notification'), findsNothing);
});
testWidgets('sends a test notification when push is on', (tester) async {
testWidgets('reports the device count when a test notification is sent', (
tester,
) async {
stubNotificationMethod('push');
adapter.onPost(
'/notifications/test',
(server) => server.reply(200, null),
(server) => server.reply(200, {'delivered': 2}),
);
await PrefUtil.setValue('push_enabled', true);
await pumpScreen(tester, Settings(pushService: _FakePushService()));
@@ -186,7 +188,26 @@ void main() {
await tester.tap(find.text('Send test notification'));
await pumpUntilFound(
tester,
find.text('Test notification sent. It should arrive shortly.'),
find.text('Test notification sent to 2 devices.'),
);
});
testWidgets('warns when a test notification reaches no devices', (
tester,
) async {
stubNotificationMethod('push');
adapter.onPost(
'/notifications/test',
(server) => server.reply(200, {'delivered': 0}),
);
await PrefUtil.setValue('push_enabled', true);
await pumpScreen(tester, Settings(pushService: _FakePushService()));
await pumpUntilFound(tester, find.text('Send test notification'));
await tester.tap(find.text('Send test notification'));
await pumpUntilFound(
tester,
find.text('No devices are registered to receive push notifications.'),
);
});
}