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/conversation/chat_thread_view.dart'; import 'package:pipelines_app/features/chat/screen.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/ui/floating/floating_layer.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); } } 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)).controller; Future<_Gateway> _mount( WidgetTester tester, { Size size = const Size(1800, 1000), ValueNotifier? destination, }) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final gateway = _Gateway()..history = (_) async => chatPage([]); bindServerIdentity(gateway, 'server-a'); addTearDown(gateway.close); final chat = ChatScreen(gateway: gateway, persistWorkspace: true); await tester.pumpWidget( MaterialApp( home: Scaffold( body: destination == null ? chat // As the shell keeps destinations: the hidden one is muted, // unfocusable and takes no input. : FloatingFocusRoot( child: ValueListenableBuilder( valueListenable: destination, builder: (context, index, _) => FadeThroughStack( index: index, children: [ chat, const Center(child: Text('Runs page')), ], ), ), ), ), ), ); await tester.pumpAndSettle(); return gateway; } /// 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(), }); } void main() { testWidgets('Float and Dock move the live chat with its draft', ( tester, ) async { await _saved('a', 'b'); final gateway = await _mount(tester); final b = tester.state(_thread('b')); await tester.enterText(_input('b'), 'Draft B'); await _menu(tester, 'b', 'Float (Ctrl+Y)'); final controller = _workspace(tester); expect(controller.floatingPanes.single.sessionId, 'b'); expect(controller.panes.map((p) => p.sessionId), ['a']); expect(find.byKey(const ValueKey('chat-float-title')), findsOneWidget); expect(tester.state(_thread('b')), same(b)); expect(tester.widget(_input('b')).controller.text, 'Draft B'); // The window floats over the app, not inside a pane. final window = tester.getRect( find.byKey(const ValueKey('chat-float-title')), ); expect(window.width, lessThan(1800 / 2)); // 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.text('Float (Ctrl+Y)'), findsNothing); await tester.tapAt(const Offset(5, 995)); await tester.pumpAndSettle(); await _menu(tester, 'b', 'Dock'); expect(controller.floatingPanes, 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( gateway.calls.where( (call) => ['chat_close', 'chat_interrupt'].contains(call.type), ), isEmpty, ); expect(tester.takeException(), isNull); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('the floating chat and its frame survive a reload', ( tester, ) async { await _saved('a', 'b'); await _mount(tester); await _menu(tester, 'b', 'Float (Ctrl+Y)'); await tester.drag( find.byKey(const ValueKey('chat-float-title')), const Offset(200, 40), ); await tester.pumpAndSettle(); await _menu(tester, 'b', 'Hide floating chats (Ctrl+Y)'); expect(find.byKey(const ValueKey('chat-float-title')), findsNothing); await tester.pump(const Duration(seconds: 1)); final preferences = await SharedPreferences.getInstance(); final record = ChatFloatRecord.decode( preferences.getString( scopedServerKey(chatFloatPreferenceKey, 'server-a'), )!, )!; final floated = record.panes.single; expect(floated.sessionId, 'b'); expect(record.shown, isFalse); expect(record.frames[floated.id]?.left, chatFloatDefaultRect.left + 200); await tester.pumpWidget(const SizedBox.shrink()); await _mount(tester); final controller = _workspace(tester); expect(controller.floatingPanes.single.sessionId, 'b'); expect(controller.floatShown, isFalse); expect(controller.panes.map((p) => p.sessionId), ['a']); controller.setFloatShown(true); await tester.pumpAndSettle(); expect( tester.getTopLeft(find.byKey(const ValueKey('chat-float-title'))).dx, greaterThanOrEqualTo(chatFloatDefaultRect.left + 200), ); await tester.pumpWidget(const SizedBox.shrink()); }); Future chord(WidgetTester tester) async { await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); await tester.sendKeyEvent(LogicalKeyboardKey.keyY); await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); await tester.pumpAndSettle(); } final titles = find.byKey(const ValueKey('chat-float-title')); testWidgets('several chats float; the chord hides and shows them all ' 'where they were', (tester) async { await _saved('a', 'b', 'c'); await _mount(tester); await _menu(tester, 'b', 'Float (Ctrl+Y)'); await _menu(tester, 'c', 'Float (Ctrl+Y)'); final controller = _workspace(tester); expect(controller.floatingPanes.map((p) => p.sessionId), ['b', 'c']); expect(controller.panes.map((p) => p.sessionId), ['a']); expect(titles, findsNWidgets(2)); // The second window opens stepped from the first, so both show. final first = tester.getTopLeft(titles.first); final second = tester.getTopLeft(titles.last); expect(second.dx, greaterThan(first.dx)); await tester.drag(titles.first, const Offset(300, 100)); await tester.pumpAndSettle(); final moved = tester.getTopLeft(titles.first); expect(moved.dx, greaterThan(first.dx + 200)); await chord(tester); expect(controller.floatShown, isFalse); expect(titles, findsNothing); await chord(tester); expect(controller.floatShown, isTrue); expect(titles, findsNWidgets(2)); expect(tester.getTopLeft(titles.first), moved); expect(tester.getTopLeft(titles.last), second); // Docking one leaves the other floating. await _menu(tester, 'b', 'Dock'); expect(controller.floatingPanes.map((p) => p.sessionId), ['c']); expect(titles, findsOneWidget); await tester.pump(const Duration(seconds: 1)); final preferences = await SharedPreferences.getInstance(); final record = ChatFloatRecord.decode( preferences.getString( scopedServerKey(chatFloatPreferenceKey, 'server-a'), )!, )!; expect(record.panes.map((p) => p.sessionId), ['c']); // The docked chat keeps its frame for its next float. expect(record.frames, hasLength(2)); expect(tester.takeException(), isNull); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('a floating chat moves, types, hides and shows over another ' 'destination', (tester) async { await _saved('a', 'b'); final destination = ValueNotifier(0); addTearDown(destination.dispose); await _mount(tester, destination: destination); await _menu(tester, 'b', 'Float (Ctrl+Y)'); final controller = _workspace(tester); destination.value = 1; await tester.pumpAndSettle(); expect(find.text('Runs page'), findsOneWidget); // The chat page is offstage, so finders look past it; the window itself // paints and takes input in the root overlay. final title = find.byKey( const ValueKey('chat-float-title'), skipOffstage: false, ); bool hits(Finder finder) { final box = tester.renderObject(finder); return tester .hitTestOnBinding(tester.getCenter(finder)) .path .any((entry) => identical(entry.target, box)); } expect(hits(title), isTrue); final before = tester.getTopLeft(title); await tester.drag(title, const Offset(150, 60)); await tester.pumpAndSettle(); expect(tester.getTopLeft(title).dx, greaterThan(before.dx + 100)); expect(tester.getTopLeft(title).dy, greaterThan(before.dy + 20)); // The composer takes the keyboard there. final input = _input('b'); expect(hits(input), isTrue); 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'); // The chord and the window's own hide button both work there. // The window itself animates away and back, not only the state. await chord(tester); expect(controller.floatShown, isFalse); expect(hits(title), isFalse); await chord(tester); expect(controller.floatShown, isTrue); expect(hits(title), isTrue); final hide = find.byTooltip( 'Hide floating chats (Ctrl+Y)', skipOffstage: false, ); expect(hits(hide), isTrue); await tester.tapAt(tester.getCenter(hide)); await tester.pumpAndSettle(); expect(controller.floatShown, isFalse); expect(hits(title), isFalse); expect(tester.takeException(), isNull); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('narrow layouts offer no Float', (tester) async { await _saved('a', 'b'); await _mount(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 tester.pumpWidget(const SizedBox.shrink()); }); }