import 'dart:convert'; 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/features/chat/screen.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import 'package:pipelines_app/features/chat/ui/sessions/session_actions.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, ); Finder _input(String id) => find.descendant( of: find.byKey(ValueKey('chat-composer-$id')), matching: find.byType(EditableText), ); Future<_Gateway> _setup(WidgetTester tester, {Size? size}) async { SharedPreferences.setMockInitialValues({}); tester.view.physicalSize = size ?? const Size(1800, 1000); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final gateway = _Gateway()..history = (_) async => chatPage([]); addTearDown(gateway.close); await tester.pumpWidget( MaterialApp( home: Scaffold( body: ChatScreen(gateway: gateway, persistWorkspace: true), ), ), ); await tester.pumpAndSettle(); await tester.tap(find.text('Chat a').first); await tester.pumpAndSettle(); return gateway; } Future _split(WidgetTester tester, String id) async { await tester.tap( find.byWidgetPredicate( (widget) => widget is SessionActionsButton && widget.session['id'] == id, ), ); await tester.pumpAndSettle(); await tester.tap(find.text('Open to the right')); await tester.pumpAndSettle(); } Future _openMenu(WidgetTester tester, String session) async { await tester.tap( find.descendant( of: find.descendant( of: _thread(session), matching: find.byKey(const Key('chat-actions-island')), ), matching: find.byTooltip('More actions'), ), ); await tester.pumpAndSettle(); } MenuItemButton _item(WidgetTester tester, String label) => tester.widget( find.ancestor( of: find.text(label), matching: find.byType(MenuItemButton), ), ); ChatWorkspaceController _controller(WidgetTester tester) => tester.widget(find.byType(ChatWorkspace)).controller; void main() { testWidgets( 'pane menu moves a chat beside its neighbour and keeps its state', (tester) async { final gateway = await _setup(tester); await _split(tester, 'b'); final state = tester.state(_thread('a')); await tester.enterText(_input('a'), 'Keep this draft'); gateway.emitEvent( chatTextFrame('Streamed to a', session: 'a', messageId: 'live-a'), ); await tester.pumpAndSettle(); expect( tester.getCenter(_thread('a')).dx, lessThan(tester.getCenter(_thread('b')).dx), ); await _openMenu(tester, 'a'); expect(_item(tester, 'Move right').onPressed, isNotNull); for (final label in ['Move up', 'Move down', 'Move left']) { expect(_item(tester, label).onPressed, isNull, reason: label); } expect( (_item(tester, 'Move right').shortcut! as SingleActivator) .debugDescribeKeys(), 'Control + Alt + Shift + Arrow Right', ); await tester.tap(find.text('Move right')); await tester.pumpAndSettle(); expect( tester.getCenter(_thread('a')).dx, greaterThan(tester.getCenter(_thread('b')).dx), ); expect(tester.state(_thread('a')), same(state)); expect( tester.widget(_input('a')).controller.text, 'Keep this draft', ); expect( find.descendant(of: _thread('a'), matching: find.text('Streamed to a')), findsOneWidget, ); expect(_controller(tester).activePane?.sessionId, 'a'); expect( tester.widget(_input('a')).focusNode.hasFocus, isTrue, ); // The new order is what a reload restores. await tester.pump(const Duration(milliseconds: 300)); await tester.pumpAndSettle(); final preferences = await SharedPreferences.getInstance(); final saved = preferences .getKeys() .where((key) => key.startsWith(chatWorkspacePreferenceKey)) .map(preferences.getString) .single!; final root = jsonDecode(saved)['root'] as Map; expect((root['first'] as Map)['session_id'], 'b'); expect((root['second'] as Map)['session_id'], 'a'); await _openMenu(tester, 'a'); expect(_item(tester, 'Move left').onPressed, isNotNull); expect(_item(tester, 'Move right').onPressed, isNull); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); }, variant: TargetPlatformVariant.only(TargetPlatform.linux), ); testWidgets( 'Ctrl+Alt+Shift+Arrow moves the focused chat with its draft and focus', (tester) async { await _setup(tester); await _split(tester, 'b'); final state = tester.state(_thread('b')); await tester.enterText(_input('b'), 'Typed in b'); await tester.pumpAndSettle(); final text = tester.widget(_input('b')).controller; final selection = text.selection; await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft); await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft); await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft); await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft); await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft); await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); await tester.pumpAndSettle(); expect( tester.getCenter(_thread('b')).dx, lessThan(tester.getCenter(_thread('a')).dx), ); expect(tester.state(_thread('b')), same(state)); expect(text.text, 'Typed in b'); expect(text.selection, selection); expect( tester.widget(_input('b')).focusNode.hasPrimaryFocus, isTrue, ); expect(_controller(tester).activePane?.sessionId, 'b'); expect(tester.takeException(), isNull); }, variant: TargetPlatformVariant.only(TargetPlatform.linux), ); testWidgets('a phone shows no move items for its one-at-a-time panes', ( tester, ) async { SharedPreferences.setMockInitialValues({}); tester.view.physicalSize = const Size(560, 1000); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final gateway = _Gateway()..history = (_) async => chatPage([]); addTearDown(gateway.close); await tester.pumpWidget( MaterialApp( home: Scaffold(body: ChatScreen(gateway: gateway)), ), ); await tester.pumpAndSettle(); await tester.tap(find.text('Chat a').first); await tester.pumpAndSettle(); _controller(tester).splitActive('b', ChatSplitAxis.horizontal); await tester.pumpAndSettle(); await tester.tap(find.byTooltip('More actions').hitTestable().first); await tester.pumpAndSettle(); expect(find.text('Close pane'), findsOneWidget); for (final label in ['Move up', 'Move down', 'Move left', 'Move right']) { expect(find.text(label), findsNothing, reason: label); } }); }