Files
pipelines/app/test/features/notification_service/notification_service_bridge_test.dart
T
alan 826e420423 Deliver server notifications from the Android app itself
A native foreground service keeps an authenticated websocket to the
connected server (handed over by the app on every connect), subscribes with
a replay cursor, shows each notification with kind-based channels and
grouping, opens pipelines:// links on tap, and clears taps, dismissals and
cross-device clears. It starts at boot and app launch, backs off and follows
network changes. Settings gains the delivery switch, status, notification
and battery-exemption actions, and the server's optional ntfy_enabled
toggle.
2026-11-01 02:21:22 +00:00

97 lines
3.1 KiB
Dart

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/notification_service/notification_service_bridge.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
const channel = MethodChannel('test/notification_service');
final messenger =
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
late List<MethodCall> calls;
Map<String, Object?> reply({String status = 'connected'}) => {
'enabled': true,
'configured': true,
'status': status,
'notificationsAllowed': true,
'batteryExempt': false,
};
setUp(() {
calls = [];
messenger.setMockMethodCallHandler(channel, (call) async {
calls.add(call);
return call.method == 'requestBatteryExemption' ? true : reply();
});
});
tearDown(() => messenger.setMockMethodCallHandler(channel, null));
test('the production channel name matches the Kotlin side', () {
expect(
MethodChannelNotificationServiceBridge.channelName,
'pipelines/notification_service',
);
});
test('configure hands over the server, addresses and cookie', () async {
final bridge = MethodChannelNotificationServiceBridge(channel);
final status = await bridge.configure(
serverId: 'home',
endpoints: ['wss://a:8443/connect', 'ws://b:8081/connect'],
cookie: 'pipelines_session=abc',
);
expect(calls.single.method, 'configure');
expect(calls.single.arguments, {
'serverId': 'home',
'endpoints': ['wss://a:8443/connect', 'ws://b:8081/connect'],
'cookie': 'pipelines_session=abc',
});
expect(status.state, DeliveryState.connected);
expect(status.enabled, isTrue);
expect(status.batteryExempt, isFalse);
});
test('setEnabled, start, status and the battery prompt', () async {
final bridge = MethodChannelNotificationServiceBridge(channel);
await bridge.setEnabled(false);
await bridge.start();
await bridge.status();
expect(await bridge.requestBatteryExemption(), isTrue);
expect(calls.map((call) => call.method), [
'setEnabled',
'start',
'status',
'requestBatteryExemption',
]);
expect(calls.first.arguments, {'enabled': false});
});
test('statusChanged from the service reaches listeners', () async {
final bridge = MethodChannelNotificationServiceBridge(channel);
final next = bridge.changes.first;
await messenger.handlePlatformMessage(
channel.name,
channel.codec.encodeMethodCall(
MethodCall('statusChanged', reply(status: 'waiting_for_network')),
),
(_) {},
);
expect((await next).state, DeliveryState.waitingForNetwork);
});
test('unknown or malformed status falls back to off', () {
expect(
NotificationServiceStatus.fromMap({'status': 'weird'}).state,
DeliveryState.off,
);
expect(NotificationServiceStatus.fromMap(null).enabled, isFalse);
});
test('the host test platform has no service', () async {
final bridge = createNotificationServiceBridge();
expect(bridge.supported, isFalse);
expect((await bridge.status()).state, DeliveryState.off);
});
}