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_float_store.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 'package:pipelines_app/shell/windows/window_store.dart'; import 'package:pipelines_app/ui/motion/fade_through_stack.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'}, ], }); } if (type == 'chat_shells') return Future.value({'chat_shells': []}); return super.call(type, fields); } } class _Store implements WindowStore { String? value; @override Future read() async => value; @override Future write(String record) async { value = record; return true; } } Finder _thread(String id) => find.byWidgetPredicate( (widget) => widget is ChatThreadView && widget.sessionId == id, skipOffstage: false, ); Finder _input(String id) => find.descendant( of: find.byKey(ValueKey('chat-composer-$id'), skipOffstage: false), matching: find.byType(EditableText, skipOffstage: false), skipOffstage: false, ); ChatWorkspaceController _workspace(WidgetTester tester) => tester .widget(find.byType(ChatWorkspace, skipOffstage: false)) .controller; /// The window whose title is [title]. Finder _window(String title) => find.ancestor(of: find.text(title), matching: find.byType(WindowFrame)); /// That window's title bar, where it shows and takes input. Finder _shown(String title) => find .descendant( of: _window(title), matching: find.byKey(const ValueKey('window-title')), ) .hitTestable(); /// The app in miniature: the Chat destination beside another one, and the /// window host above both, with app-lifetime chat state. class _App { _App(this.store); final _Store store; final gateway = _Gateway()..history = (_) async => chatPage([]); final destination = ValueNotifier(0); final chatBuilt = ValueNotifier(true); final opened = []; late final ChatRuntime runtime; late final WindowManager manager; late final ChatWindows windows; Future start( WidgetTester tester, { Size size = const Size(1800, 1000), }) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); bindServerIdentity(gateway, 'server-a'); runtime = ChatRuntime(gateway: gateway, serverKey: 'server-a'); manager = WindowManager( kinds: WindowKinds(), store: store, server: 'server-a', ); windows = ChatWindows( manager: manager, runtime: runtime, openInChat: opened.add, )..start(); await manager.restore(); await windows.migrate( const SharedPreferencesChatFloatStore(serverScope: 'server-a'), ); await tester.pumpWidget( MaterialApp( home: Scaffold( body: Stack( fit: StackFit.expand, children: [ ListenableBuilder( listenable: Listenable.merge([destination, chatBuilt]), builder: (context, _) => FadeThroughStack( index: destination.value, children: [ if (chatBuilt.value) ChatScreen( gateway: gateway, persistWorkspace: true, runtime: runtime, windows: windows, ) else const Center(child: Text('Not connected')), const Center(child: Text('Runs page')), ], ), ), Positioned.fill(child: WindowHost(manager: manager)), ], ), ), ), ); await tester.pumpAndSettle(); } Future stop(WidgetTester tester) async { await tester.pumpWidget(const SizedBox.shrink()); windows.dispose(); runtime.dispose(); manager.dispose(); destination.dispose(); chatBuilt.dispose(); gateway.close(); } List get floating => [for (final w in windows.windows) w.contentId]; } /// Opens [session]'s pane menu (wherever it is) and chooses [label]. Future _menu(WidgetTester tester, String session, String label) async { await tester.tap( find .descendant( of: _thread(session), matching: find.byTooltip('More actions'), ) .hitTestable(), ); await tester.pumpAndSettle(); await tester.tap(find.text(label).last); await tester.pumpAndSettle(); } Future _saved(String a, String b, [String? c]) async { final layout = ChatWorkspaceController() ..open(a) ..splitActive(b, ChatSplitAxis.horizontal); if (c != null) layout.splitActive(c, ChatSplitAxis.horizontal); SharedPreferences.setMockInitialValues({ scopedServerKey(chatWorkspacePreferenceKey, 'server-a'): layout .snapshot()! .encode(), }); } Future _chord(WidgetTester tester) async { await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft); await tester.sendKeyEvent(LogicalKeyboardKey.keyY); await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft); await tester.pumpAndSettle(); } void main() { testWidgets('Float and Dock move the live chat with its draft', ( tester, ) async { await _saved('a', 'b'); final app = _App(_Store()); await app.start(tester); final b = tester.state(_thread('b')); await tester.enterText(_input('b'), 'Draft B'); await _menu(tester, 'b', 'Float (Alt+Y)'); final controller = _workspace(tester); expect(app.floating, ['b']); expect(controller.panes.map((p) => p.sessionId), ['a']); expect(tester.state(_thread('b')), same(b)); expect(tester.widget(_input('b')).controller.text, 'Draft B'); // A window over the app, titled by its chat, not a pane. expect(tester.getSize(_window('Chat b')).width, lessThan(1800 / 2)); expect( find.descendant(of: _window('Chat b'), matching: find.byTooltip('Dock')), findsOneWidget, ); // The lone tiled pane cannot float: the tiling would be empty. await tester.tap( find .descendant( of: _thread('a'), matching: find.byTooltip('More actions'), ) .hitTestable(), ); await tester.pumpAndSettle(); expect(find.textContaining('Float'), findsNothing); await tester.tapAt(const Offset(5, 995)); await tester.pumpAndSettle(); await tester.tap(find.byTooltip('Dock')); await tester.pumpAndSettle(); expect(app.floating, isEmpty); expect(controller.panes.map((p) => p.sessionId), ['a', 'b']); expect(tester.state(_thread('b')), same(b)); expect(tester.widget(_input('b')).controller.text, 'Draft B'); expect( app.gateway.calls.where( (call) => ['chat_close', 'chat_interrupt'].contains(call.type), ), isEmpty, ); expect(tester.takeException(), isNull); await app.stop(tester); }); testWidgets('X closes the window and returns nothing to the tiling', ( tester, ) async { await _saved('a', 'b'); final app = _App(_Store()); await app.start(tester); await _menu(tester, 'b', 'Float (Alt+Y)'); await tester.tap(find.byTooltip('Close')); await tester.pumpAndSettle(); expect(app.floating, isEmpty); expect(_workspace(tester).panes.map((p) => p.sessionId), ['a']); expect(_thread('b'), findsNothing); expect(app.gateway.calls.where((c) => c.type == 'chat_close'), isEmpty); await app.stop(tester); }); testWidgets('popups from a floating chat open above its window', ( tester, ) async { await _saved('a', 'b'); final app = _App(_Store()); await app.start(tester); await _menu(tester, 'b', 'Float (Alt+Y)'); // The More actions menu, opened from inside the window. await tester.tap( find .descendant( of: _thread('b'), matching: find.byTooltip('More actions'), ) .hitTestable(), ); await tester.pumpAndSettle(); expect(find.text('Chat details').hitTestable(), findsOneWidget); // Chat details, the sheet that used to open behind the window. await tester.tap(find.text('Chat details')); await tester.pumpAndSettle(); final sheet = find.byType(BottomSheet); expect(sheet.hitTestable(), findsOneWidget); // On the root navigator: wider than the window, and over it. expect( tester.getSize(sheet).width, greaterThan(tester.getSize(_window('Chat b')).width), ); expect(_shown('Chat b'), findsNothing); expect(tester.takeException(), isNull); await app.stop(tester); }); testWidgets('windows and their frames survive a reload, without the Chat ' 'destination being built', (tester) async { await _saved('a', 'b'); final store = _Store(); var app = _App(store); await app.start(tester); await _menu(tester, 'b', 'Float (Alt+Y)'); await tester.drag( find.descendant( of: _window('Chat b'), matching: find.byKey(const ValueKey('window-title')), ), const Offset(200, 40), ); await tester.pumpAndSettle(); await tester.tap(find.byTooltip('Minimise')); await tester.pumpAndSettle(); expect(_shown('Chat b'), findsNothing); await app.stop(tester); app = _App(store)..chatBuilt.value = false; await app.start(tester); expect(find.text('Not connected'), findsOneWidget); final entry = app.windows.windows.single; expect(entry.contentId, 'b'); expect(entry.minimized, isTrue); expect(entry.frame.left, chatFloatDefaultRect.left + 200); await _chord(tester); expect(_shown('Chat b'), findsOneWidget); expect(tester.getTopLeft(_window('Chat b')).dx, entry.frame.left); expect(_thread('b').hitTestable(), findsOneWidget); // Dock with no tiling opens the chat in the Chat destination. await tester.tap(find.byTooltip('Dock')); await tester.pumpAndSettle(); expect(app.opened, ['b']); expect(app.floating, isEmpty); await app.stop(tester); }); testWidgets('several chats float; the chord hides and shows them all ' 'where they were', (tester) async { await _saved('a', 'b', 'c'); final app = _App(_Store()); await app.start(tester); await _menu(tester, 'b', 'Float (Alt+Y)'); await _menu(tester, 'c', 'Float (Alt+Y)'); final controller = _workspace(tester); expect(app.floating, ['b', 'c']); expect(controller.panes.map((p) => p.sessionId), ['a']); // The second window opens stepped from the first, so both show. final first = tester.getTopLeft(_window('Chat b')); final second = tester.getTopLeft(_window('Chat c')); expect(second.dx, greaterThan(first.dx)); await _chord(tester); expect( find.byKey(const ValueKey('window-title')).hitTestable(), findsNothing, ); await _chord(tester); expect(tester.getTopLeft(_window('Chat b')), first); expect(tester.getTopLeft(_window('Chat c')), second); // Docking one leaves the other floating. await tester.tap( find.descendant(of: _window('Chat b'), matching: find.byTooltip('Dock')), ); await tester.pumpAndSettle(); expect(app.floating, ['c']); expect(controller.panes.map((p) => p.sessionId), ['a', 'b']); expect(tester.takeException(), isNull); await app.stop(tester); }); testWidgets('a floating chat types, runs its tickers, hides and shows over ' 'another destination', (tester) async { await _saved('a', 'b'); final app = _App(_Store()); await app.start(tester); await _menu(tester, 'b', 'Float (Alt+Y)'); app.destination.value = 1; await tester.pumpAndSettle(); expect(find.text('Runs page'), findsOneWidget); expect(_shown('Chat b'), findsOneWidget); // Not muted by the hidden Chat destination. expect(TickerMode.valuesOf(tester.element(_thread('b'))).enabled, isTrue); // The composer takes the keyboard there. final input = _input('b'); await tester.tapAt(tester.getCenter(input)); await tester.pump(); final field = tester.widget(input); expect(field.focusNode.hasFocus, isTrue); tester.testTextInput.enterText('From runs'); await tester.pump(); expect(field.controller.text, 'From runs'); await _chord(tester); expect(_shown('Chat b'), findsNothing); await _chord(tester); expect(_shown('Chat b'), findsOneWidget); // Shown again by the chord, the composer has the keyboard at once. expect(tester.widget(_input('b')).focusNode.hasFocus, isTrue); expect(tester.takeException(), isNull); await app.stop(tester); }); testWidgets('floating chats saved by an older build come back as windows', ( tester, ) async { await _saved('a', 'c'); final preferences = await SharedPreferences.getInstance(); await preferences.setString( scopedServerKey(chatFloatPreferenceKey, 'server-a'), const ChatFloatRecord( panes: [ChatWorkspacePane('chat-pane-9', 'b')], shown: true, frames: {'chat-pane-9': Rect.fromLTWH(400, 300, 500, 420)}, ).encode(), ); final app = _App(_Store()); await app.start(tester); expect(app.floating, ['b']); expect(tester.getTopLeft(_window('Chat b')), const Offset(400, 300)); expect(_workspace(tester).panes.map((p) => p.sessionId), ['a', 'c']); await app.stop(tester); }); testWidgets('a chat is drawn once: opening a floating chat from the list ' 'shows its window', (tester) async { await _saved('a', 'b'); final app = _App(_Store()); await app.start(tester); await _menu(tester, 'b', 'Float (Alt+Y)'); app.manager.setGroupShown(chatWindowKind, false); await tester.pumpAndSettle(); _workspace(tester).open('b'); await tester.pumpAndSettle(); expect(_shown('Chat b'), findsOneWidget); expect(_workspace(tester).panes.map((p) => p.sessionId), ['a']); expect(_thread('b'), findsOneWidget); expect(tester.takeException(), isNull); await app.stop(tester); }); testWidgets('narrow layouts offer no Float and show no window', ( tester, ) async { await _saved('a', 'b'); final app = _App(_Store()); await app.start(tester, size: const Size(430, 900)); final finder = find.byTooltip('More actions').hitTestable(); await tester.tap(finder.first); await tester.pumpAndSettle(); expect(find.textContaining('Float'), findsNothing); expect(find.text('Open chats (2)'), findsOneWidget); await app.stop(tester); }); }