import 'package:flutter/material.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 '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), }) 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); await tester.pumpWidget( MaterialApp( home: Scaffold( body: ChatScreen(gateway: gateway, persistWorkspace: true), ), ), ); 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) async { final layout = ChatWorkspaceController() ..open(a) ..splitActive(b, 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.floatingPane?.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 can still float, swapping with the floating one. await tester.tap( find .descendant( of: _thread('a'), matching: find.byTooltip('More actions'), ) .hitTestable(), ); await tester.pumpAndSettle(); expect(find.text('Float (Ctrl+Y)'), findsOneWidget); await tester.tapAt(const Offset(5, 995)); await tester.pumpAndSettle(); await _menu(tester, 'b', 'Dock'); expect(controller.floatingPane, isNull); 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 chat (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'), )!, )!; expect(record.pane?.sessionId, 'b'); expect(record.shown, isFalse); expect(record.rect.left, chatFloatDefaultRect.left + 200); await tester.pumpWidget(const SizedBox.shrink()); await _mount(tester); final controller = _workspace(tester); expect(controller.floatingPane?.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()); }); 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()); }); }