Chat: Ctrl+K / Alt+K on Chat opens a quick switcher for the open chats
On Chat the chord now toggles a centred dialog (bottom sheet on a phone) with an autofocused filter over the open chats in list order; Ctrl+N/Down and Ctrl+P/Up move the wrapping selection, Enter opens the chat as Ctrl+Tab does (pane reused, composer focused), Esc or the chord closes it. From other destinations the chord still jumps to Chat. Ctrl+N/Ctrl+P are handled so the web page prevents their defaults (Ctrl+P no longer prints); a browser tab keeps Ctrl+N reserved, documented in the help and notes/chat-quick-switch.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,226 @@
|
||||
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/conversation/chat_quick_switch_dialog.dart';
|
||||
import 'package:pipelines_app/features/chat/data/chat_repository.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_controller.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/sessions/session_row_content.dart';
|
||||
|
||||
import '../ui/sessions/chat_test_harness.dart' show MemoryChatSessionLocalStore;
|
||||
import '../ui/sessions/fake_chat_gateway.dart';
|
||||
|
||||
final rows = <JsonMap>[
|
||||
{'id': 'one', 'title': 'Build app', 'directory': '/home/a/app'},
|
||||
{
|
||||
'id': 'two',
|
||||
'title': 'Deploy server',
|
||||
'directory': '/home/a/server',
|
||||
'backend': 'codex',
|
||||
'running': true,
|
||||
},
|
||||
{'id': 'three', 'title': 'Notes', 'directory': '/home/a/app'},
|
||||
{'id': 'gone', 'title': 'Closed build', 'closed_at': 3},
|
||||
];
|
||||
|
||||
class _Switcher {
|
||||
ModalRoute<JsonMap>? route;
|
||||
final chosen = <String>[];
|
||||
}
|
||||
|
||||
Future<_Switcher> _open(
|
||||
WidgetTester tester, {
|
||||
Size size = const Size(900, 800),
|
||||
}) async {
|
||||
tester.view.physicalSize = size;
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
final gateway = FakeChatGateway()..sessionsReply = {'chat_sessions': rows};
|
||||
final controller = ChatSessionsController(
|
||||
ChatRepository(gateway),
|
||||
localStore: MemoryChatSessionLocalStore(),
|
||||
);
|
||||
addTearDown(controller.dispose);
|
||||
addTearDown(gateway.pushed.close);
|
||||
await controller.refresh();
|
||||
final switcher = _Switcher();
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Builder(
|
||||
builder: (context) => Center(
|
||||
child: TextButton(
|
||||
onPressed: () => switcher.route = showChatQuickSwitch(
|
||||
context,
|
||||
sessions: controller,
|
||||
onSelected: (row) => switcher.chosen.add('${row['id']}'),
|
||||
),
|
||||
child: const Text('Open'),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.tap(find.text('Open'));
|
||||
await tester.pump(const Duration(milliseconds: 400));
|
||||
return switcher;
|
||||
}
|
||||
|
||||
List<String> _shown(WidgetTester tester) => [
|
||||
for (final tile in tester.widgetList<ListTile>(
|
||||
find.byWidgetPredicate(
|
||||
(widget) =>
|
||||
widget is ListTile &&
|
||||
'${widget.key}'.contains('chat-quick-switch-row-'),
|
||||
),
|
||||
))
|
||||
(tile.key! as ValueKey<String>).value.replaceFirst(
|
||||
'chat-quick-switch-row-',
|
||||
'',
|
||||
),
|
||||
];
|
||||
|
||||
String? _selected(WidgetTester tester) {
|
||||
for (final id in _shown(tester)) {
|
||||
final tile = tester.widget<ListTile>(
|
||||
find.byKey(Key('chat-quick-switch-row-$id')),
|
||||
);
|
||||
if (tile.selected) return id;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<bool> _chord(WidgetTester tester, LogicalKeyboardKey key) async {
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
final handled = await tester.sendKeyDownEvent(key);
|
||||
await tester.sendKeyUpEvent(key);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
return handled;
|
||||
}
|
||||
|
||||
Future<void> _key(WidgetTester tester, LogicalKeyboardKey key) async {
|
||||
await tester.sendKeyEvent(key);
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'opens focused on its field with the open chats, first selected',
|
||||
(tester) async {
|
||||
await _open(tester);
|
||||
expect(find.byType(Dialog), findsOneWidget);
|
||||
final field = tester.widget<EditableText>(
|
||||
find.descendant(
|
||||
of: find.byKey(const Key('chat-quick-switch-field')),
|
||||
matching: find.byType(EditableText),
|
||||
),
|
||||
);
|
||||
expect(field.focusNode.hasPrimaryFocus, isTrue);
|
||||
expect(_shown(tester), ['one', 'three', 'two']);
|
||||
expect(_selected(tester), 'one');
|
||||
// The list's avatar carries the harness, colour and activity badge.
|
||||
expect(find.byType(ChatSessionAvatar), findsNWidgets(3));
|
||||
expect(find.text('~/app'), findsNWidgets(2));
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'Ctrl+N, Ctrl+P, Down and Up move the selection and wrap',
|
||||
(tester) async {
|
||||
await _open(tester);
|
||||
expect(await _chord(tester, LogicalKeyboardKey.keyN), isTrue);
|
||||
expect(_selected(tester), 'three');
|
||||
await _key(tester, LogicalKeyboardKey.arrowDown);
|
||||
expect(_selected(tester), 'two');
|
||||
await _key(tester, LogicalKeyboardKey.arrowDown);
|
||||
expect(_selected(tester), 'one');
|
||||
expect(await _chord(tester, LogicalKeyboardKey.keyP), isTrue);
|
||||
expect(_selected(tester), 'two');
|
||||
await _key(tester, LogicalKeyboardKey.arrowUp);
|
||||
expect(_selected(tester), 'three');
|
||||
},
|
||||
variant: TargetPlatformVariant.desktop(),
|
||||
);
|
||||
|
||||
testWidgets('typing filters, ranks and selects the first match', (
|
||||
tester,
|
||||
) async {
|
||||
final switcher = await _open(tester);
|
||||
await _key(tester, LogicalKeyboardKey.arrowDown);
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('chat-quick-switch-field')),
|
||||
'app',
|
||||
);
|
||||
await tester.pump();
|
||||
expect(_shown(tester), ['one', 'three']);
|
||||
expect(_selected(tester), 'one');
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('chat-quick-switch-field')),
|
||||
'build',
|
||||
);
|
||||
await tester.pump();
|
||||
// The closed chat titled "Closed build" is not offered.
|
||||
expect(_shown(tester), ['one']);
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('chat-quick-switch-field')),
|
||||
'zzz',
|
||||
);
|
||||
await tester.pump();
|
||||
expect(find.byKey(const Key('chat-quick-switch-empty')), findsOneWidget);
|
||||
// Ctrl+P still belongs to the switcher with nothing to select, so the
|
||||
// browser does not print.
|
||||
expect(await _chord(tester, LogicalKeyboardKey.keyP), isTrue);
|
||||
await _key(tester, LogicalKeyboardKey.enter);
|
||||
expect(switcher.chosen, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('Enter chooses the selected chat and closes', (tester) async {
|
||||
final switcher = await _open(tester);
|
||||
await _key(tester, LogicalKeyboardKey.arrowDown);
|
||||
await _key(tester, LogicalKeyboardKey.enter);
|
||||
await tester.pumpAndSettle();
|
||||
expect(switcher.chosen, ['three']);
|
||||
expect(find.byType(Dialog), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('a click chooses a row; Esc closes without choosing', (
|
||||
tester,
|
||||
) async {
|
||||
final switcher = await _open(tester);
|
||||
await _key(tester, LogicalKeyboardKey.escape);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(Dialog), findsNothing);
|
||||
expect(switcher.chosen, isEmpty);
|
||||
|
||||
await tester.tap(find.text('Open'));
|
||||
await tester.pump(const Duration(milliseconds: 400));
|
||||
await tester.tap(find.text('Deploy server'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(switcher.chosen, ['two']);
|
||||
});
|
||||
|
||||
testWidgets('closeChatQuickSwitch closes it from outside', (tester) async {
|
||||
final switcher = await _open(tester);
|
||||
closeChatQuickSwitch(switcher.route!);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(Dialog), findsNothing);
|
||||
expect(switcher.chosen, isEmpty);
|
||||
// A second close of a closed switcher does nothing.
|
||||
closeChatQuickSwitch(switcher.route!);
|
||||
await tester.pump();
|
||||
});
|
||||
|
||||
testWidgets('a phone shows it as a full-width bottom sheet', (tester) async {
|
||||
await _open(tester, size: const Size(400, 800));
|
||||
expect(find.byType(Dialog), findsNothing);
|
||||
expect(find.byType(BottomSheet), findsOneWidget);
|
||||
expect(
|
||||
tester.getSize(find.byKey(const Key('chat-quick-switch-row-one'))).width,
|
||||
400,
|
||||
);
|
||||
expect(_selected(tester), 'one');
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user