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 calls; Map 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); expect(await bridge.connectionEvents(), isEmpty); }); test('a refused session reports sign_in_required', () { expect( NotificationServiceStatus.fromMap({'status': 'sign_in_required'}).state, DeliveryState.signInRequired, ); }); test('connectionEvents reads the service log, newest first', () async { messenger.setMockMethodCallHandler(channel, (call) async { if (call.method == 'connectionEvents') { return [ { 'time': 2000, 'event': 'failed', 'error': 'Expected HTTP 101', 'code': 403, }, {'time': 1000, 'event': 'connected'}, ]; } return reply(); }); final bridge = MethodChannelNotificationServiceBridge(channel); final events = await bridge.connectionEvents(); expect(events.map((event) => event.event), ['failed', 'connected']); expect(events.first.code, 403); expect(events.first.error, 'Expected HTTP 101'); expect(events.first.detail, 'HTTP 403 ยท Expected HTTP 101'); expect(events.last.code, isNull); expect(events.last.error, isNull); expect(events.last.time, DateTime.fromMillisecondsSinceEpoch(1000)); }); }