import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:pipelines_app/application/pipelines_root.dart'; import 'package:pipelines_app/application/server_endpoint.dart'; import 'package:pipelines_app/core/servers/server_catalog.dart'; import 'package:pipelines_app/core/servers/server_catalog_store.dart'; import 'package:pipelines_app/core/session/server_session.dart'; import 'package:pipelines_app/core/transport/pipelines_client.dart'; import '../core/transport/pipelines_client_test.dart' show FakeChannel; /// Answers the app's `get_status` with [hostname] and refuses every other /// request: the client sends one request at a time. Future answerStatus( WidgetTester tester, FakeChannel channel, String hostname, ) async { final answered = {}; for (var frame = 0; frame < 24; frame++) { await tester.pump(const Duration(milliseconds: 20)); for (final raw in List.of(channel.outgoing)) { final message = jsonDecode('$raw') as Map; final id = '${message['id']}'; if (message['type'] == 'subscribe' || !answered.add(id)) continue; channel.incoming.add( jsonEncode( message['type'] == 'get_status' ? { 'type': 'status', 'id': id, 'status': {'hostname': hostname}, } : {'type': 'error', 'id': id, 'error': 'not in this fixture'}, ), ); } } } void main() { testWidgets('connecting renames the legacy "My server" to its hostname', ( tester, ) async { SharedPreferences.setMockInitialValues({ 'pipelines.session.v2.legacy-server.cookie': 'pipelines_session=x', }); await ServerSession.load(); final legacy = SavedServer.validated( id: 'legacy-server', name: 'My server', endpoints: ['ws://192.168.1.2:8083/connect'], ); final store = MemoryServerCatalogStore( catalog: ServerCatalog(servers: [legacy], selectedId: legacy.id), ); final channel = FakeChannel()..readyCompleter.complete(); final client = PipelinesClient(channelFactory: (_) => channel); await tester.pumpWidget( PipelinesRoot( federatedChats: false, nativeUpdatesSupported: false, server: ServerEndpoint.resolve( base: Uri.parse('file:///'), web: false, configured: legacy.endpoints.first, ), catalogStore: store, check: (_) async => SessionState.signedIn, clientFactory: () => client, ), ); await answerStatus(tester, channel, 'tank'); expect(store.catalog!.active!.name, 'tank'); expect(store.catalog!.active!.id, 'legacy-server'); await tester.pumpWidget(const SizedBox.shrink()); await tester.pump(); }); testWidgets('a name the operator typed is kept after connecting', ( tester, ) async { SharedPreferences.setMockInitialValues({ 'pipelines.session.v2.home.cookie': 'pipelines_session=x', }); await ServerSession.load(); final home = SavedServer.validated( id: 'home', name: 'Home', endpoints: ['ws://192.168.1.2:8083/connect'], ); final store = MemoryServerCatalogStore( catalog: ServerCatalog(servers: [home], selectedId: home.id), ); final channel = FakeChannel()..readyCompleter.complete(); final client = PipelinesClient(channelFactory: (_) => channel); await tester.pumpWidget( PipelinesRoot( federatedChats: false, nativeUpdatesSupported: false, server: ServerEndpoint.resolve( base: Uri.parse('file:///'), web: false, configured: home.endpoints.first, ), catalogStore: store, check: (_) async => SessionState.signedIn, clientFactory: () => client, ), ); await answerStatus(tester, channel, 'tank'); expect( channel.outgoing.where((raw) => '$raw'.contains('"get_status"')), hasLength(1), ); expect(store.catalog!.active!.name, 'Home'); await tester.pumpWidget(const SizedBox.shrink()); await tester.pump(); }); }