import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/application/pipelines_app.dart'; import 'package:pipelines_app/core/transport/pipelines_client.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import '../core/transport/pipelines_client_test.dart' show FakeChannel; import 'chat_registration_test.dart' show replyHistory, replySubscribe, waitForRequest, webServer; final _field = find.byKey(const Key('chat-quick-switch-field')); Future _replySessions(WidgetTester tester, FakeChannel channel) async { final request = await waitForRequest(tester, channel, 'chat_sessions'); channel.incoming.add( jsonEncode({ 'id': request['id'], 'type': 'chat_sessions', 'chat_sessions': [ {'id': 's1', 'title': 'Wire chat', 'directory': '/workspace/demo'}, {'id': 's2', 'title': 'Second chat', 'directory': '/workspace/other'}, {'id': 's3', 'title': 'Third chat', 'directory': '/workspace/demo'}, ], }), ); await tester.pump(); } Future _mountOnChat( WidgetTester tester, PlatformRouteInformationProvider provider, ) async { tester.view.physicalSize = const Size(1200, 900); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final channel = FakeChannel()..readyCompleter.complete(); final client = PipelinesClient(channelFactory: (_) => channel); await tester.pumpWidget( PipelinesApp( androidSupported: false, server: webServer(), client: client, routeInformationProvider: provider, ), ); await tester.pump(); await _replySessions(tester, channel); await replySubscribe(tester, channel); await replyHistory(tester, channel); await tester.pumpAndSettle(); expect(find.byType(ChatThreadView), findsOneWidget); return channel; } PlatformRouteInformationProvider _provider() { final provider = PlatformRouteInformationProvider( initialRouteInformation: RouteInformation(uri: Uri.parse('/chat/s1')), ); addTearDown(provider.dispose); return provider; } Future _chord(WidgetTester tester, LogicalKeyboardKey modifier) async { await tester.sendKeyDownEvent(modifier); await tester.sendKeyEvent(LogicalKeyboardKey.keyK); await tester.sendKeyUpEvent(modifier); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); } bool _fieldFocused(WidgetTester tester) => tester .widget( find.descendant(of: _field, matching: find.byType(EditableText)), ) .focusNode .hasPrimaryFocus; bool _composerFocused(WidgetTester tester, String id) => tester .widget( find.descendant( of: find.byKey(ValueKey('chat-composer-$id')), matching: find.byType(EditableText), ), ) .focusNode .hasPrimaryFocus; Future _teardown(WidgetTester tester) async { await tester.pumpWidget(const SizedBox.shrink()); await tester.pump(); } void main() { testWidgets('the chat shortcut toggles the switcher while on Chat', ( tester, ) async { await _mountOnChat(tester, _provider()); await _chord(tester, LogicalKeyboardKey.altLeft); expect(_field, findsOneWidget); expect(_fieldFocused(tester), isTrue); await _chord(tester, LogicalKeyboardKey.altLeft); await tester.pumpAndSettle(); expect(_field, findsNothing); await _chord(tester, LogicalKeyboardKey.controlLeft); expect(_field, findsOneWidget); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(_field, findsNothing); expect(tester.takeException(), isNull); await _teardown(tester); }); testWidgets('Enter opens the chosen chat in place, focusing its composer', ( tester, ) async { final provider = _provider(); final channel = await _mountOnChat(tester, provider); await _chord(tester, LogicalKeyboardKey.controlLeft); await tester.enterText(_field, 'second'); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pump(); await replySubscribe(tester, channel, index: 1); await replyHistory(tester, channel, index: 1); await tester.pumpAndSettle(); expect(_field, findsNothing); expect(provider.value.uri.path, '/chat/s2'); // The chat replaced the open one rather than adding a pane. expect(find.byType(ChatThreadView), findsOneWidget); expect(_composerFocused(tester, 's2'), isTrue); // Choosing the chat already open keeps the one pane. await _chord(tester, LogicalKeyboardKey.controlLeft); await tester.enterText(_field, 'second'); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(provider.value.uri.path, '/chat/s2'); expect(find.byType(ChatThreadView), findsOneWidget); expect(_composerFocused(tester, 's2'), isTrue); expect(tester.takeException(), isNull); await _teardown(tester); }); testWidgets('elsewhere the shortcut still jumps to Chat', (tester) async { final provider = _provider(); await _mountOnChat(tester, provider); await tester.tap(find.byKey(const ValueKey('shell.destination.runs'))); await tester.pump(const Duration(milliseconds: 600)); FocusManager.instance.primaryFocus?.unfocus(); await tester.pump(); await _chord(tester, LogicalKeyboardKey.controlLeft); await tester.pumpAndSettle(); expect(provider.value.uri.path, '/chat/s1'); expect(_field, findsNothing); expect(_composerFocused(tester, 's1'), isTrue); await _teardown(tester); }); testWidgets( 'Cmd+K toggles the switcher on Chat on a Mac', (tester) async { await _mountOnChat(tester, _provider()); await _chord(tester, LogicalKeyboardKey.metaLeft); expect(_field, findsOneWidget); expect(_fieldFocused(tester), isTrue); await _chord(tester, LogicalKeyboardKey.metaLeft); await tester.pumpAndSettle(); expect(_field, findsNothing); await _teardown(tester); }, variant: TargetPlatformVariant.only(TargetPlatform.macOS), ); }