import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/application/detached/detached_app.dart'; import 'package:pipelines_app/application/detached/detached_route.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/features/terminal/terminal_layout.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() { late FakeDetachedPlatform platform; late ChatServer server; Future pumpChat(WidgetTester tester) async { SharedPreferences.setMockInitialValues({}); tester.view.physicalSize = const Size(720, 860); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); addTearDown(() => TerminalLayout.instance.persist = true); platform = FakeDetachedPlatform(); final channel = FakeChannel()..readyCompleter.complete(); server = ChatServer(channel); await tester.pumpWidget( DetachedWindowApp( route: DetachedRoute.chat('a')!, server: webServer(), client: PipelinesClient(channelFactory: (_) => channel), platform: platform, ), ); await _run(tester, server, const Duration(milliseconds: 900)); } testWidgets('a detached chat is the chat pane alone, named after it', ( tester, ) async { await pumpChat(tester); expect(find.byType(ChatThreadView), findsOneWidget); expect(find.text('Hello from chat'), findsOneWidget); // No shell: no navigation, no chat list, no other chat. expect(find.byType(NavigationRail), findsNothing); expect(find.byType(NavigationBar), findsNothing); expect(find.text('Chat b'), findsNothing); final app = tester.widget(find.byType(MaterialApp)); expect(app.title, 'Chat a ยท Pipelines'); // Inside a detached window there is nothing to send to another one. final context = tester.element(find.byType(ChatThreadView)); expect(DetachedWindowScope.maybeOf(context), isNull); }); testWidgets('a detached window writes no shared layout', (tester) async { await pumpChat(tester); expect(server.workspaceWrites, isEmpty); expect( server.requests.where((r) => r['type'] == 'chat_open_workspace'), isEmpty, reason: 'the open-chat workspace is the main window\'s', ); expect(TerminalLayout.instance.persist, isFalse); final prefs = await SharedPreferences.getInstance(); expect(prefs.getKeys().where((k) => k.contains('window')), isEmpty); expect(prefs.getString(terminalLayoutKey), isNull); }); testWidgets('Bring back hands the chat to the main window and closes', ( tester, ) async { await pumpChat(tester); await tester.tap(find.byTooltip('More actions').hitTestable().first); await tester.pumpAndSettle(); await tester.tap(find.text('Bring back to main window')); await tester.pumpAndSettle(); expect(platform.sent.single, { 'type': 'bring-back', 'kind': 'chat', 'id': 'a', }); expect(platform.closed, isTrue); }); testWidgets('with no main window, Bring back opens the full app here', ( tester, ) async { await pumpChat(tester); platform.mainOpen = false; await tester.tap(find.byTooltip('More actions').hitTestable().first); await tester.pumpAndSettle(); await tester.tap(find.text('Bring back to main window')); await tester.pumpAndSettle(); expect(platform.sent, isEmpty); expect(platform.becameMain, '/chat/a'); }); }