import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/core/servers/server_scope.dart'; import 'package:pipelines_app/features/chat/chat_runtime.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import 'package:pipelines_app/features/chat/screen.dart'; import 'package:pipelines_app/features/chat/windows/chat_windows.dart'; import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart'; import 'package:pipelines_app/features/chat/workspace/chat_workspace_store.dart'; import 'package:pipelines_app/shell/windows/window_frame.dart'; import 'package:pipelines_app/shell/windows/window_host.dart'; import 'package:pipelines_app/shell/windows/window_kind.dart'; import 'package:pipelines_app/shell/windows/window_manager.dart'; import '../../application/detached/detached_fixtures.dart'; import 'conversation/chat_thread_fixtures.dart'; class _Gateway extends FakeChatGateway { @override Future call(String type, [JsonMap fields = const {}]) { if (type == 'chat_sessions') { return Future.value({ 'chat_sessions': [ for (final id in ['a', 'b', 'c']) {'id': id, 'title': 'Chat $id', 'directory': '/home/$id'}, ], }); } if (type == 'chat_shells') return Future.value({'chat_shells': []}); return super.call(type, fields); } } const _label = 'Open in new window (Alt+Shift+Y)'; Finder _thread(String id) => find.byWidgetPredicate( (widget) => widget is ChatThreadView && widget.sessionId == id, skipOffstage: false, ); List _tiled(WidgetTester tester) => [ for (final pane in tester .widget( find.byType(ChatWorkspace, skipOffstage: false), ) .controller .panes) pane.sessionId, ]; /// The Chat destination with its floating windows, as the app hosts them, /// with [opener] as the platform's new windows (none when null). class _App { final gateway = _Gateway()..history = (_) async => chatPage([]); final opener = FakeOpener(); late final ChatRuntime runtime; late final WindowManager manager; late final ChatWindows windows; Future start(WidgetTester tester, {bool detached = true}) async { tester.view.physicalSize = const Size(1800, 1000); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final layout = ChatWorkspaceController() ..open('a') ..splitActive('b', ChatSplitAxis.horizontal); SharedPreferences.setMockInitialValues({ scopedServerKey(chatWorkspacePreferenceKey, 'server-a'): layout .snapshot()! .encode(), }); bindServerIdentity(gateway, 'server-a'); runtime = ChatRuntime(gateway: gateway, serverKey: 'server-a'); manager = WindowManager(kinds: WindowKinds(), server: 'server-a'); windows = ChatWindows( manager: manager, runtime: runtime, openInChat: (_) {}, )..start(); if (detached) windows.detached = opener; await tester.pumpWidget( MaterialApp( home: Scaffold( body: Stack( fit: StackFit.expand, children: [ ChatScreen( gateway: gateway, persistWorkspace: true, runtime: runtime, windows: windows, ), Positioned.fill(child: WindowHost(manager: manager)), ], ), ), ), ); await tester.pumpAndSettle(); addTearDown(() async { await tester.pumpWidget(const SizedBox.shrink()); windows.dispose(); runtime.dispose(); manager.dispose(); gateway.close(); }); } List get floating => [for (final w in windows.windows) w.contentId]; } Future _paneMenu(WidgetTester tester, String session) async { await tester.tap( find .descendant( of: _thread(session), matching: find.byTooltip('More actions'), ) .hitTestable(), ); await tester.pumpAndSettle(); } Future _rowMenu(WidgetTester tester, String id) async { await tester.tap(find.byTooltip('Chat actions for Chat $id').first); await tester.pumpAndSettle(); } void main() { testWidgets('the pane menu sends a tiled chat to a new window', ( tester, ) async { final app = _App(); await app.start(tester); expect(_tiled(tester), ['a', 'b']); await _paneMenu(tester, 'b'); expect(find.text('Float (Alt+Y)'), findsOneWidget); await tester.tap(find.text(_label)); await tester.pumpAndSettle(); expect(app.opener.chats, ['b']); // It left the tiling, as Float takes it out, and no floating window. expect(_tiled(tester), ['a']); expect(app.floating, isEmpty); }); testWidgets('a chat whose window did not open stays where it was', ( tester, ) async { final app = _App(); await app.start(tester); app.opener.opens = false; await _paneMenu(tester, 'b'); await tester.tap(find.text(_label)); await tester.pumpAndSettle(); expect(app.opener.chats, ['b']); expect(_tiled(tester), ['a', 'b']); }); testWidgets('without platform windows the action is not offered', ( tester, ) async { final app = _App(); await app.start(tester, detached: false); await _paneMenu(tester, 'b'); expect(find.text('Float (Alt+Y)'), findsOneWidget); expect(find.textContaining('new window'), findsNothing); await tester.tapAt(const Offset(5, 995)); await tester.pumpAndSettle(); await _rowMenu(tester, 'c'); expect(find.textContaining('new window'), findsNothing); }); testWidgets('the row menu offers it after Float, for any chat', ( tester, ) async { final app = _App(); await app.start(tester); await _rowMenu(tester, 'c'); final float = tester.getTopLeft(find.text('Float (Alt+Y)').last); final open = tester.getTopLeft(find.text(_label).last); expect(open.dy, greaterThan(float.dy)); await tester.tap(find.text(_label).last); await tester.pumpAndSettle(); expect(app.opener.chats, ['c']); expect(_tiled(tester), ['a', 'b']); }); testWidgets('a floating chat goes to a new window from its title bar', ( tester, ) async { final app = _App(); await app.start(tester); await _rowMenu(tester, 'c'); await tester.tap(find.text('Float (Alt+Y)').last); await tester.pumpAndSettle(); expect(app.floating, ['c']); final window = find.ancestor( of: find.text('Chat c'), matching: find.byType(WindowFrame), ); await tester.tap( find.descendant(of: window, matching: find.byTooltip(_label)).first, ); await tester.pumpAndSettle(); expect(app.opener.chats, ['c']); expect(app.floating, isEmpty); }); testWidgets('Alt+Shift+Y sends the active pane; Alt+Y still floats', ( tester, ) async { final app = _App(); await app.start(tester); // The keyboard is in chat b's composer, the active pane. await tester.tap( find .descendant( of: find.byKey(const ValueKey('chat-composer-b')), matching: find.byType(EditableText), ) .first, ); await tester.pumpAndSettle(); await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft); await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft); await tester.sendKeyEvent(LogicalKeyboardKey.keyY); await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft); await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft); await tester.pumpAndSettle(); expect(app.opener.chats, ['b']); expect(_tiled(tester), ['a']); expect(app.floating, isEmpty); }); testWidgets('a chat brought back from its window joins the tiling', ( tester, ) async { final app = _App(); await app.start(tester); app.windows.bringBack('c'); await tester.pumpAndSettle(); expect(_tiled(tester), ['a', 'b', 'c']); }); }