import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/conversation/composer_selection_focus.dart'; import 'package:pipelines_app/features/chat/screen.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'; /// The list shows Starred (apple), then Chats (Recent, Second), then Closed. const _rows = [ {'id': 'r', 'title': 'Recent', 'directory': '/w'}, {'id': 'c', 'title': 'Done', 'directory': '/w', 'closed_at': 1}, {'id': 's', 'title': 'Second', 'directory': '/w'}, {'id': 'a', 'title': 'apple', 'directory': '/w', 'starred': true}, ]; class _Gateway extends FakeChatGateway { @override Future call(String type, [JsonMap fields = const {}]) { if (type == 'chat_sessions') { return Future.value({ 'chat_sessions': [ for (final row in _rows) {...row}, ], }); } if (type == 'chat_shells') return Future.value({'chat_shells': []}); return super.call(type, fields); } } Future _pump(WidgetTester tester, Size size) async { SharedPreferences.setMockInitialValues({}); tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final gateway = _Gateway()..history = (_) async => chatPage(const []); addTearDown(gateway.close); final route = ValueNotifier(null); addTearDown(route.dispose); await tester.pumpWidget( MaterialApp( home: AdaptiveShell( destination: AppDestination.chat, destinationBodies: { AppDestination.chat: (_) => ValueListenableBuilder( valueListenable: route, builder: (context, id, _) => ChatScreen( gateway: gateway, initialSessionId: id, onSessionSelected: (next) => route.value = next, onBack: () => route.value = null, ), ), }, ), ), ); await tester.pumpAndSettle(); } Finder _thread(String id) => find.byKey(ValueKey('chat-thread-$id')); bool _composerFocused(WidgetTester tester, String id) => tester .widget( find.descendant( of: find.byKey(ValueKey('chat-composer-$id')), matching: find.byType(EditableText), ), ) .focusNode .hasPrimaryFocus; Future _tap( WidgetTester tester, Finder finder, { PointerDeviceKind kind = PointerDeviceKind.touch, }) async { await tester.tap(finder, kind: kind); await tester.pumpAndSettle(); } Finder _row(String id) => find.byKey(Key('chat-row-$id')); final _desktop = TargetPlatformVariant.only(TargetPlatform.macOS); final _mobile = TargetPlatformVariant.only(TargetPlatform.android); void main() { testWidgets('desktop, wide: clicked rows focus the composer', (tester) async { await _pump(tester, const Size(1280, 800)); await _tap(tester, _row('r'), kind: PointerDeviceKind.mouse); expect(_thread('r'), findsOneWidget); expect(_composerFocused(tester, 'r'), isTrue); // Focus elsewhere, then the chat already open: clicking returns it. FocusManager.instance.primaryFocus?.unfocus(); await tester.pump(); await _tap(tester, _row('r'), kind: PointerDeviceKind.mouse); expect(_composerFocused(tester, 'r'), isTrue); await _tap(tester, _row('s'), kind: PointerDeviceKind.mouse); expect(_thread('s'), findsOneWidget); expect(_composerFocused(tester, 's'), isTrue); await tester.pumpWidget(const SizedBox.shrink()); }, variant: _desktop); testWidgets( 'desktop, narrow window: a tapped row focuses the chat page composer', (tester) async { await _pump(tester, const Size(390, 844)); await _tap(tester, find.text('Second')); expect(_thread('s'), findsOneWidget); expect(_composerFocused(tester, 's'), isTrue); await tester.pumpWidget(const SizedBox.shrink()); }, variant: _desktop, ); testWidgets('desktop: a closed chat leaves its composer unfocused', ( tester, ) async { await _pump(tester, const Size(1280, 800)); await _tap(tester, find.text('Closed')); await _tap(tester, _row('c'), kind: PointerDeviceKind.mouse); expect(_thread('c'), findsOneWidget); expect(_composerFocused(tester, 'c'), isFalse); await tester.pumpWidget(const SizedBox.shrink()); }, variant: _desktop); testWidgets('mobile: finger taps do not raise the keyboard', (tester) async { await _pump(tester, const Size(390, 844)); await _tap(tester, find.text('Recent')); expect(_thread('r'), findsOneWidget); expect(_composerFocused(tester, 'r'), isFalse); await tester.pageBack(); await tester.pumpAndSettle(); await _tap(tester, find.text('Second')); expect(_thread('s'), findsOneWidget); expect(_composerFocused(tester, 's'), isFalse); await tester.pumpWidget(const SizedBox.shrink()); }, variant: _mobile); testWidgets( 'mobile, wide: a touch tap leaves focus, a mouse click focuses', (tester) async { await _pump(tester, const Size(1280, 800)); await _tap(tester, _row('r')); expect(_composerFocused(tester, 'r'), isFalse); await _tap(tester, _row('s'), kind: PointerDeviceKind.mouse); expect(_composerFocused(tester, 's'), isTrue); await tester.pumpWidget(const SizedBox.shrink()); }, variant: _mobile, ); test('the rule: desktop or a mouse, never a closed chat', () { const open = {'id': 'x'}; const closed = {'id': 'x', 'closed_at': 1}; for (final platform in TargetPlatform.values) { final desktop = { TargetPlatform.macOS, TargetPlatform.windows, TargetPlatform.linux, }.contains(platform); for (final kind in PointerDeviceKind.values) { final mouse = kind == PointerDeviceKind.mouse || kind == PointerDeviceKind.trackpad; expect( chatSelectionFocusesComposer(open, platform: platform, pointer: kind), desktop || mouse, reason: '$platform $kind', ); expect( chatSelectionFocusesComposer( closed, platform: platform, pointer: kind, ), isFalse, ); } } }); }