import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/application/detached/detached_route.dart'; import 'package:pipelines_app/application/detached/detached_windows.dart'; import 'package:pipelines_app/application/pipelines_app.dart'; import 'package:pipelines_app/core/transport/pipelines_client.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import 'package:pipelines_app/shell/detached/detached_window_scope.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../../core/transport/pipelines_client_test.dart' show FakeChannel; import '../chat_registration_test.dart' show webServer; import 'detached_fixtures.dart'; Future _run(WidgetTester tester, ChatServer server, Duration d) async { for (var t = 0; t < d.inMilliseconds; t += 16) { server.answer(); await tester.pump(const Duration(milliseconds: 16)); } server.answer(); } void main() { Future<(ChatServer, FakeDetachedPlatform)> pump( WidgetTester tester, { String route = '/chat/a', }) async { SharedPreferences.setMockInitialValues({}); tester.view.physicalSize = const Size(1600, 1000); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final platform = FakeDetachedPlatform(); final channel = FakeChannel()..readyCompleter.complete(); final server = ChatServer( channel, openChats: [ {'session_id': 'a'}, ], ); final provider = PlatformRouteInformationProvider( initialRouteInformation: RouteInformation(uri: Uri.parse(route)), ); addTearDown(provider.dispose); await tester.pumpWidget( PipelinesApp( androidSupported: false, server: webServer(), client: PipelinesClient(channelFactory: (_) => channel), routeInformationProvider: provider, detachedPlatform: platform, ), ); await _run(tester, server, const Duration(milliseconds: 1200)); return (server, platform); } testWidgets('the main window offers new windows and sends the chat out of ' 'its workspace', (tester) async { final (server, platform) = await pump(tester); final thread = find.byType(ChatThreadView); expect(thread, findsOneWidget); expect(DetachedWindowScope.maybeOf(tester.element(thread)), isNotNull); await tester.tap( find .descendant(of: thread, matching: find.byTooltip('More actions')) .hitTestable(), ); await tester.pumpAndSettle(); await tester.tap(find.text('Open in new window (Alt+Shift+Y)')); await _run(tester, server, const Duration(milliseconds: 600)); expect(platform.launches.single.route, DetachedRoute.chat('a')); expect(platform.launches.single.title, 'Chat a'); expect(find.byType(ChatThreadView), findsNothing); // The main window's own workspace is written without the chat. final write = server.workspaceWrites.last['chat_open_workspace_request']; expect((write as Map)['entries'], isEmpty); }); testWidgets('a chat brought back opens in the main window', (tester) async { final (server, platform) = await pump(tester, route: '/runs'); expect(find.byType(ChatThreadView), findsNothing); platform.incoming.add( const DetachedReturn(DetachedKind.chat, 'b').toMessage(), ); await _run(tester, server, const Duration(milliseconds: 900)); final thread = tester.widget(find.byType(ChatThreadView)); expect(thread.sessionId, 'b'); }); }