import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.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/features/search/search_screen.dart'; import 'package:pipelines_app/features/search/ui/search_bar_controls.dart'; import 'package:pipelines_app/shell/adaptive_shell.dart'; import 'package:pipelines_app/shell/destination.dart'; import 'package:shared_preferences/shared_preferences.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 _setup(WidgetTester tester) 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: (_) => SearchScreen(gateway: gateway, onOpen: (_) {}, embedded: true), }, ), ), ); await tester.pumpAndSettle(); } Future _openRow(WidgetTester tester, String id) async { await tester.tap(find.byKey(Key('chat-row-$id'))); await tester.pumpAndSettle(); } /// Puts focus on a sidebar control, outside every text field. Future _focusSidebar(WidgetTester tester) async { final icon = find .descendant(of: _actions('a'), matching: find.byType(Icon)) .first; Focus.of(tester.element(icon)).requestFocus(); await tester.pumpAndSettle(); } Future _slash(WidgetTester tester) async { final handled = await tester.sendKeyEvent(LogicalKeyboardKey.slash); await tester.pumpAndSettle(); return handled; } Future _controlTab(WidgetTester tester, {bool shift = false}) async { await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); if (shift) await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft); await tester.sendKeyEvent(LogicalKeyboardKey.tab); if (shift) await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft); await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); await tester.pumpAndSettle(); } void main() { testWidgets('slash outside a text field focuses the composer, untyped', ( tester, ) async { await _setup(tester); await _openRow(tester, 'a'); await _focusSidebar(tester); expect(_editable(tester, 'a').focusNode.hasPrimaryFocus, isFalse); expect(await _slash(tester), isTrue); expect(_editable(tester, 'a').focusNode.hasPrimaryFocus, isTrue); expect(_editable(tester, 'a').controller.text, isEmpty); // Inside the composer a slash is an ordinary character for the field. expect(await _slash(tester), isFalse); expect(_editable(tester, 'a').focusNode.hasPrimaryFocus, isTrue); // Shift+/ (a question mark) is not the shortcut. Alt+/ opens the Search // destination, while Ctrl+/ remains the shell's own Search chord. await _focusSidebar(tester); await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft); await tester.sendKeyEvent(LogicalKeyboardKey.slash, character: '?'); await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft); await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft); await tester.sendKeyEvent(LogicalKeyboardKey.slash); await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft); await tester.pumpAndSettle(); expect(find.byKey(searchFieldKey), findsOneWidget); expect( tester.widget(find.byKey(searchFieldKey)).focusNode!.hasFocus, isTrue, ); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('slash leaves a dialog text field and a popup alone', ( tester, ) async { await _setup(tester); await _openRow(tester, 'a'); await tester.tap(_actions('a')); await tester.pumpAndSettle(); expect(await _slash(tester), isFalse); expect(find.text('Rename…'), findsOneWidget); await tester.tap(find.text('Rename…')); await tester.pumpAndSettle(); final dialogField = find.descendant( of: find.byType(Dialog), matching: find.byType(EditableText), ); expect(dialogField, findsOneWidget); await tester.tap(dialogField); await tester.pumpAndSettle(); expect(await _slash(tester), isFalse); expect(find.byType(Dialog), findsOneWidget); expect(_editable(tester, 'a').focusNode.hasPrimaryFocus, isFalse); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('Control Tab over one pane cycles the sidebar chats in both ' 'directions', (tester) async { await _setup(tester); await _openRow(tester, 'a'); final workspace = _workspace(tester); await _focusSidebar(tester); for (final id in ['b', 'c', 'a']) { await _controlTab(tester); expect(workspace.activePane?.sessionId, id); expect(_editable(tester, id).focusNode.hasPrimaryFocus, isTrue); } await _focusSidebar(tester); for (final id in ['c', 'b', 'a']) { await _controlTab(tester, shift: true); expect(workspace.activePane?.sessionId, id); expect(_editable(tester, id).focusNode.hasPrimaryFocus, isTrue); } // Two panes still cycle panes, not the chat list. await _openRow(tester, 'c'); await tester.tap(_actions('a')); await tester.pumpAndSettle(); await tester.tap(find.text('Open to the right')); await tester.pumpAndSettle(); expect(workspace.paneCount, 2); expect(workspace.activePane?.sessionId, 'a'); await _controlTab(tester); expect(workspace.activePane?.sessionId, 'c'); await _controlTab(tester); expect(workspace.activePane?.sessionId, 'a'); expect(workspace.paneCount, 2); await tester.pumpWidget(const SizedBox.shrink()); }); }