import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/servers/server_catalog.dart'; import 'package:pipelines_app/core/servers/server_catalog_store.dart'; import 'package:pipelines_app/features/settings/server_connections.dart'; SavedServer profile(String id) => SavedServer.validated( id: id, name: id, endpoints: ['wss://$id.test/connect'], ); void main() { test('a native host hands the primary role to the next machine', () async { final store = MemoryServerCatalogStore( catalog: ServerCatalog( servers: [profile('mac'), profile('tank')], selectedId: 'mac', ), ); final activated = []; final connections = ServerConnections( store: store, onSelected: (server) async => activated.add(server?.id), replaceRemovedPrimary: true, ); addTearDown(connections.dispose); await connections.load(); expect(await connections.remove('mac'), isTrue); expect(connections.catalog.active?.id, 'tank'); expect(store.catalog?.selectedId, 'tank'); expect(activated, ['tank']); // Removing the last machine activates "nothing" so the root can recover. expect(await connections.remove('tank'), isTrue); expect(connections.catalog.servers, isEmpty); expect(activated, ['tank', null]); }); test('removing a non-primary machine keeps the primary', () async { final store = MemoryServerCatalogStore( catalog: ServerCatalog( servers: [profile('mac'), profile('tank')], selectedId: 'mac', ), ); final activated = []; final connections = ServerConnections( store: store, onSelected: (server) async => activated.add(server?.id), replaceRemovedPrimary: true, ); addTearDown(connections.dispose); await connections.load(); expect(await connections.remove('tank'), isTrue); expect(connections.catalog.active?.id, 'mac'); expect(activated, isEmpty); }); test('browsers never switch machines when the primary is removed', () async { final store = MemoryServerCatalogStore( catalog: ServerCatalog( servers: [profile('mac'), profile('tank')], selectedId: 'mac', ), ); final activated = []; final connections = ServerConnections( store: store, onSelected: (server) async => activated.add(server?.id), ); addTearDown(connections.dispose); await connections.load(); expect(await connections.remove('mac'), isTrue); expect(connections.catalog.active, isNull); expect(connections.catalog.servers.map((s) => s.id), ['tank']); expect(activated, [null]); }); }