Files
pipelines/app/test/features/settings/settings_device_delivery_section_test.dart
T
alan fc7dbb0ac0 Remove ntfy: notifications go only through the app notification stream
Server: drop ntfy publishing (stage hooks, run outbox, chat attention, chat
tool notify, auto-switch and schedule notices), the chat push outbox (its
table is dropped at startup), ntfy status/endpoints and PIPELINES_NTFY_*.
The app_notifications stream, subscribe/clear and kinds are unchanged.
Notification settings keep run_finished/chat_needs_you/chat_reply_finished;
ntfy_enabled is accepted and ignored and its stored row is never read.

Stage type=ntfy is rejected: "ntfy stages were removed; notifications go to
the Pipelines apps — use the stage notify hook". The notify hook remains.

Installer: no ntfy build, accounts, config or supervisor child; manifest
gets top-level "mode" (legacy ntfy.mode still read). Reinstall retires an
old ntfy (safe PID-file stop, deletes binary, config, data dir, token, log,
with a note in logs/install.log); uninstall cleans leftovers.

App: ntfy settings section replaced by "Notification events"; texts updated.
Docs: docs/ntfy.md folded into docs/notifications.md; install, docker, cli,
author skill references updated; security review M1 marked resolved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 23:24:24 +00:00

291 lines
10 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/notification_service/notification_service_bridge.dart';
import 'package:pipelines_app/features/settings/settings_device_delivery_section.dart';
import '../notification_service/fake_notification_service.dart';
Widget _host(
NotificationServiceBridge bridge, {
Future<void> Function()? requestPermission,
}) {
final body = SingleChildScrollView(
child: SettingsDeviceDeliverySection(
bridge: bridge,
requestNotificationPermission: requestPermission,
),
);
return MaterialApp(home: Scaffold(body: body));
}
int _requests(FakeNotificationService service) =>
service.calls.where((call) => call.$1 == 'requestBatteryExemption').length;
bool _switchValue(WidgetTester tester) {
final tile = find.byKey(DeviceDeliveryKeys.toggle);
return tester.widget<SwitchListTile>(tile).value;
}
void main() {
testWidgets('shows the switch, status and delivery note', (tester) async {
final service = FakeNotificationService();
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
expect(find.text(deviceDeliverySwitchLabel), findsOneWidget);
expect(_switchValue(tester), isTrue);
expect(find.text('Connected'), findsOneWidget);
expect(find.text(deviceDeliveryNote), findsOneWidget);
expect(find.text(deviceDeliveryBatteryLabel), findsOneWidget);
expect(find.byKey(DeviceDeliveryKeys.permission), findsNothing);
});
testWidgets('switching off stops delivery', (tester) async {
final service = FakeNotificationService();
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
await tester.tap(find.byKey(DeviceDeliveryKeys.toggle));
await tester.pumpAndSettle();
expect(service.calls.last.$1, 'setEnabled');
expect(service.calls.last.$2, {'enabled': false});
expect(_switchValue(tester), isFalse);
expect(find.text('Off'), findsOneWidget);
// The grants only matter while delivery is on.
expect(find.text(deviceDeliveryBatteryLabel), findsNothing);
});
testWidgets('live status pushed by the service is shown', (tester) async {
final service = FakeNotificationService();
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
service.push(service.copy(state: DeliveryState.reconnecting));
await tester.pumpAndSettle();
expect(find.text('Reconnecting…'), findsOneWidget);
});
testWidgets('allow background delivery asks for the exemption', (
tester,
) async {
final service = FakeNotificationService()..batteryGranted = false;
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
// The open-time prompt was declined; the row is the user's second try.
service.batteryGranted = true;
await tester.tap(find.text(deviceDeliveryBatteryLabel));
await tester.pumpAndSettle();
expect(
service.calls.map((call) => call.$1),
contains('requestBatteryExemption'),
);
expect(
find.text('Allowed: battery optimisation will not pause delivery'),
findsOneWidget,
);
});
testWidgets('blocked notifications offer the permission request', (
tester,
) async {
var asked = 0;
final service = FakeNotificationService();
service.current = service.copy(notificationsAllowed: false);
await tester.pumpWidget(
_host(
service,
requestPermission: () async {
asked++;
service.current = service.copy(notificationsAllowed: true);
},
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(DeviceDeliveryKeys.permission));
await tester.pumpAndSettle();
expect(asked, 1);
expect(find.byKey(DeviceDeliveryKeys.permission), findsNothing);
});
testWidgets('switching on asks for notifications when blocked', (
tester,
) async {
var asked = 0;
final service = FakeNotificationService();
service.current = service.copy(
enabled: false,
state: DeliveryState.off,
notificationsAllowed: false,
);
await tester.pumpWidget(
_host(service, requestPermission: () async => asked++),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(DeviceDeliveryKeys.toggle));
await tester.pumpAndSettle();
expect(asked, 1);
expect(service.calls.last.$1, 'setEnabled');
expect(service.calls.last.$2, {'enabled': true});
});
testWidgets('a failing platform is reported, not hidden', (tester) async {
await tester.pumpWidget(_host(_FailingService()));
await tester.pumpAndSettle();
expect(find.byKey(DeviceDeliveryKeys.error), findsOneWidget);
});
testWidgets(
'a missing battery exemption keeps a warning row with the Samsung note',
(tester) async {
// The open-time prompt is declined, so the exemption stays missing.
final service = FakeNotificationService()..batteryGranted = false;
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
expect(find.byKey(DeviceDeliveryKeys.batteryWarning), findsOneWidget);
expect(find.text(deviceDeliveryWarningTitle), findsOneWidget);
expect(find.textContaining(deviceDeliverySamsungNote), findsOneWidget);
},
);
testWidgets('the warning goes away once the exemption is granted', (
tester,
) async {
final service = FakeNotificationService()..batteryGranted = false;
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
expect(find.byKey(DeviceDeliveryKeys.batteryWarning), findsOneWidget);
service.batteryGranted = true;
await tester.tap(find.text(deviceDeliveryBatteryLabel));
await tester.pumpAndSettle();
expect(find.byKey(DeviceDeliveryKeys.batteryWarning), findsNothing);
});
testWidgets('switching on asks for the battery exemption before starting', (
tester,
) async {
final service = FakeNotificationService();
service.current = service.copy(enabled: false, state: DeliveryState.off);
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
await tester.tap(find.byKey(DeviceDeliveryKeys.toggle));
await tester.pumpAndSettle();
final names = service.calls.map((call) => call.$1).toList();
expect(names, contains('requestBatteryExemption'));
expect(
names.indexOf('requestBatteryExemption'),
lessThan(names.indexOf('setEnabled')),
);
});
testWidgets(
'opening Settings with delivery on and no exemption asks right away',
(tester) async {
final service = FakeNotificationService();
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
expect(_requests(service), 1);
expect(
find.text('Allowed: battery optimisation will not pause delivery'),
findsOneWidget,
);
expect(find.byKey(DeviceDeliveryKeys.batteryWarning), findsNothing);
},
);
testWidgets('a declined open-time prompt is not repeated on resume', (
tester,
) async {
final service = FakeNotificationService()..batteryGranted = false;
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
expect(_requests(service), 1);
expect(find.byKey(DeviceDeliveryKeys.batteryWarning), findsOneWidget);
// Returning from the system screen re-reads the status only.
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
await tester.pumpAndSettle();
expect(_requests(service), 1);
expect(find.byKey(DeviceDeliveryKeys.batteryWarning), findsOneWidget);
});
testWidgets('no open-time prompt when the exemption is already granted', (
tester,
) async {
final service = FakeNotificationService();
service.current = service.copy(batteryExempt: true);
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
expect(_requests(service), 0);
});
testWidgets('no open-time prompt while delivery is off', (tester) async {
final service = FakeNotificationService();
service.current = service.copy(enabled: false, state: DeliveryState.off);
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
expect(_requests(service), 0);
});
testWidgets('no warning while delivery is off', (tester) async {
final service = FakeNotificationService();
service.current = service.copy(enabled: false, state: DeliveryState.off);
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
expect(find.byKey(DeviceDeliveryKeys.batteryWarning), findsNothing);
});
testWidgets('a refused session says the user must sign in again', (
tester,
) async {
final service = FakeNotificationService();
service.current = service.copy(state: DeliveryState.signInRequired);
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
expect(
find.text('Sign in to Pipelines again to keep notifications'),
findsOneWidget,
);
});
testWidgets(
'the connection log lists the service events, failures included',
(tester) async {
final service = FakeNotificationService();
service.events = [
NotificationConnectionEvent(
time: DateTime(2026, 11, 12, 9, 30, 5),
event: 'failed',
error: 'Expected HTTP 101',
code: 403,
),
NotificationConnectionEvent(
time: DateTime(2026, 11, 12, 9, 29),
event: 'connected',
),
];
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
await tester.tap(find.text(deviceDeliveryLogTitle));
await tester.pumpAndSettle();
expect(find.text('Socket failed'), findsOneWidget);
expect(
find.textContaining('HTTP 403 · Expected HTTP 101'),
findsOneWidget,
);
expect(find.text('Socket opened'), findsOneWidget);
},
);
testWidgets('an empty connection log says so', (tester) async {
final service = FakeNotificationService();
await tester.pumpWidget(_host(service));
await tester.pumpAndSettle();
await tester.tap(find.text(deviceDeliveryLogTitle));
await tester.pumpAndSettle();
expect(find.text(deviceDeliveryLogEmpty), findsOneWidget);
});
}
class _FailingService extends FakeNotificationService {
@override
Future<NotificationServiceStatus> status() async =>
throw StateError('no service');
}