Files
pipelines/app/test/application/chat_quick_switch_shortcut_test.dart
T
alan 12c7642e35 Chat quick switcher MRU, Closed section and markers; Alt+N, Alt+J and run tab keys
Ctrl+K lists open chats most recently used first (tracked on open and pane
focus, persisted per server) and preselects the previous chat, so Ctrl+K,
Enter returns to it. Rows mark needs you / working / unread; a query also
lists matching closed chats under Closed, and choosing one reopens it.
Alt+N (Option+N) starts a new chat and Alt+J (Option+J) opens the next chat
waiting for you, unread or failed, cycling; the terminal keeps both. In run
detail, bare 1-4 switch tabs when not typing. Cheatsheet entries added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 00:49:36 +00:00

273 lines
9.5 KiB
Dart

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<void> _replySessions(
WidgetTester tester,
FakeChannel channel, {
bool awaiting = false,
}) 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',
if (awaiting) 'activity': {'awaiting_operator': 1, 'seq': 1},
},
],
}),
);
await tester.pump();
}
Future<FakeChannel> _mountOnChat(
WidgetTester tester,
PlatformRouteInformationProvider provider, {
bool awaiting = false,
}) 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, awaiting: awaiting);
await replySubscribe(tester, channel);
await replyHistory(tester, channel);
// A chat waiting for the operator pulses its badge, so it never settles.
if (awaiting) {
await tester.pump(const Duration(seconds: 1));
} else {
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<void> _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<EditableText>(
find.descendant(of: _field, matching: find.byType(EditableText)),
)
.focusNode
.hasPrimaryFocus;
bool _composerFocused(WidgetTester tester, String id) => tester
.widget<EditableText>(
find.descendant(
of: find.byKey(ValueKey('chat-composer-$id')),
matching: find.byType(EditableText),
),
)
.focusNode
.hasPrimaryFocus;
Future<void> _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('the shortcut then Enter returns to the previous chat', (
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(provider.value.uri.path, '/chat/s2');
await _chord(tester, LogicalKeyboardKey.controlLeft);
// Most recent first: the open chat, then the one before it, selected.
final rows = tester
.widgetList<ListTile>(
find.byWidgetPredicate(
(w) =>
w is ListTile && '${w.key}'.contains('chat-quick-switch-row'),
),
)
.toList();
expect(
rows.map((tile) => '${tile.key}'.split('row-').last.split("'").first),
['s2', 's1', 's3'],
);
expect(rows[1].selected, isTrue);
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(provider.value.uri.path, '/chat/s1');
await _teardown(tester);
});
testWidgets('Alt+J opens the next chat waiting for you', (tester) async {
final provider = _provider();
final channel = await _mountOnChat(tester, provider, awaiting: true);
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyJ);
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
await tester.pump();
await tester.pump();
await replySubscribe(tester, channel, index: 1);
await replyHistory(tester, channel, index: 1);
await tester.pump(const Duration(seconds: 1));
expect(provider.value.uri.path, '/chat/s3');
// Only the open chat needs you now: nothing else to go to.
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyJ);
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
await tester.pump();
await tester.pump();
expect(provider.value.uri.path, '/chat/s3');
expect(find.text('No other chat needs you.'), findsOneWidget);
await _teardown(tester);
});
testWidgets('Alt+N from Runs opens New chat', (tester) async {
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 tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyN);
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
await tester.pump();
await tester.pump(const Duration(milliseconds: 600));
expect(find.byType(Dialog), findsOneWidget);
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),
);
}