import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/transport/pipelines_client.dart'; import 'package:pipelines_app/features/notification_service/notification_service_bridge.dart'; import 'package:pipelines_app/features/notification_service/notification_service_handoff.dart'; import '../settings/fake_channel.dart'; import 'fake_notification_service.dart'; void main() { test('each connection hands the proven server to the service', () async { final channels = []; final client = PipelinesClient( channelFactory: (uri) { final channel = FakeChannel(uri)..markReady(); channels.add(channel); return channel; }, ); final service = FakeNotificationService(); final handoff = NotificationServiceHandoff( bridge: service, client: client, serverId: 'home', headersFor: (uri) => uri.host == 'b' ? {'Cookie': 'pipelines_session=abc'} : const {}, ); // The app starts the service at launch when it is enabled. expect(service.calls.single.$1, 'start'); await client.connectAny([ Uri.parse('wss://b:8443/connect'), Uri.parse('ws://a:8081/connect'), ]); await pumpEventQueue(); final configure = service.calls.last; expect(configure.$1, 'configure'); expect(configure.$2, { 'serverId': 'home', 'endpoints': ['wss://b:8443/connect', 'ws://a:8081/connect'], 'cookie': 'pipelines_session=abc', }); handoff.dispose(); await client.dispose(); }); test('without a server id the connected address names the server', () async { final client = PipelinesClient( channelFactory: (uri) => FakeChannel(uri)..markReady(), ); final service = FakeNotificationService(); final handoff = NotificationServiceHandoff( bridge: service, client: client, serverId: null, headersFor: (_) => const {}, ); await client.connect(Uri.parse('ws://a:8081/connect')); await pumpEventQueue(); expect(service.calls.last.$2['serverId'], 'ws://a:8081/connect'); expect(service.calls.last.$2['cookie'], ''); handoff.dispose(); await client.dispose(); }); test('an unsupported platform is never called', () async { final client = PipelinesClient( channelFactory: (uri) => FakeChannel(uri)..markReady(), ); final handoff = NotificationServiceHandoff( bridge: const UnsupportedNotificationServiceBridge(), client: client, serverId: 'home', headersFor: (_) => throw StateError('must not read credentials'), ); await client.connect(Uri.parse('ws://a:8081/connect')); await pumpEventQueue(); handoff.dispose(); await client.dispose(); }); }