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/ui/sessions/session_actions.dart'; import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart'; import 'package:pipelines_app/shell/adaptive_shell.dart'; import 'package:pipelines_app/shell/destination.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 _input(String id) => find.descendant( of: find.byKey(ValueKey('chat-composer-$id')), matching: find.byType(EditableText), ); EditableText _editable(WidgetTester tester, String id) => tester.widget(_input(id)); Finder _actions(String id) => find.byWidgetPredicate( (widget) => widget is SessionActionsButton && widget.session['id'] == id, ); ChatWorkspaceController _workspace(WidgetTester tester) => tester.widget(find.byType(ChatWorkspace)).controller; Future _chord( WidgetTester tester, { bool reverse = false, bool alt = false, bool meta = false, }) async { await tester.sendKeyDownEvent( meta ? LogicalKeyboardKey.metaLeft : LogicalKeyboardKey.controlLeft, ); if (reverse) await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft); if (alt) await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft); await tester.sendKeyEvent(LogicalKeyboardKey.tab); if (alt) await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft); if (reverse) await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft); await tester.sendKeyUpEvent( meta ? LogicalKeyboardKey.metaLeft : LogicalKeyboardKey.controlLeft, ); await tester.pumpAndSettle(); } Future _setup(WidgetTester tester, {bool open = true}) async { SharedPreferences.setMockInitialValues({}); tester.view.physicalSize = 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: AdaptiveShell( destination: AppDestination.chat, destinationBodies: { AppDestination.chat: (_) => ChatScreen(gateway: gateway), AppDestination.search: (_) => const Text('Search destination'), }, ), ), ); await tester.pumpAndSettle(); if (open) { await tester.tap(find.byKey(const Key('chat-row-a'))); await tester.pumpAndSettle(); } } Future _split(WidgetTester tester, String id, String action) async { await tester.tap(_actions(id)); await tester.pumpAndSettle(); await tester.tap(find.text(action)); await tester.pumpAndSettle(); } void _focusSidebar(WidgetTester tester) { final icon = find .descendant(of: _actions('a'), matching: find.byType(Icon)) .first; Focus.of(tester.element(icon)).requestFocus(); } void main() { testWidgets('Control Tab wraps visual pane order and focuses each composer', ( tester, ) async { await _setup(tester); await tester.enterText(_input('a'), 'Draft A'); await _split(tester, 'b', 'Open to the right'); await tester.enterText(_input('b'), 'Draft B'); await _split(tester, 'c', 'Open below'); await tester.enterText(_input('c'), 'Draft C'); final workspace = _workspace(tester); for (final id in ['a', 'b', 'c', 'a']) { await _chord(tester); expect(workspace.activePane?.sessionId, id); expect(_editable(tester, id).focusNode.hasPrimaryFocus, isTrue); } await _chord(tester, reverse: true); expect(workspace.activePane?.sessionId, 'c'); expect(_editable(tester, 'c').focusNode.hasPrimaryFocus, isTrue); _focusSidebar(tester); await tester.pumpAndSettle(); expect(_editable(tester, 'c').focusNode.hasPrimaryFocus, isFalse); await _chord(tester); expect(workspace.activePane?.sessionId, 'a'); expect(_editable(tester, 'a').focusNode.hasPrimaryFocus, isTrue); for (final id in ['a', 'b', 'c']) { expect( _editable(tester, id).controller.text, 'Draft ${id.toUpperCase()}', ); } await _chord(tester, alt: true); await _chord(tester, meta: true); expect(workspace.activePane?.sessionId, 'a'); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('single pane focuses input; empty workspace does not consume', ( tester, ) async { await _setup(tester, open: false); await _chord(tester); expect(find.byType(ChatWorkspace), findsNothing); await tester.tap(find.byKey(const Key('chat-row-a'))); await tester.pumpAndSettle(); _focusSidebar(tester); await tester.pumpAndSettle(); await _chord(tester); expect(_editable(tester, 'a').focusNode.hasPrimaryFocus, isTrue); expect(_editable(tester, 'a').controller.text, isEmpty); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('popup menus and other destinations retain their own keyboard', ( tester, ) async { await _setup(tester); await _split(tester, 'b', 'Open to the right'); final workspace = _workspace(tester); await tester.tap(_actions('a')); await tester.pumpAndSettle(); await _chord(tester); expect(workspace.activePane?.sessionId, 'b'); expect(find.text('Open to the right'), findsOneWidget); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); await tester.tap(find.byKey(const ValueKey('shell.destination.search'))); await tester.pumpAndSettle(); await _chord(tester); expect(find.text('Search destination'), findsOneWidget); expect(workspace.activePane?.sessionId, 'b'); await tester.pumpWidget(const SizedBox.shrink()); }); }