Chat: slash focuses the composer; Ctrl+Tab over one pane toggles recent chats

A bare / typed outside any text field puts the keyboard in the active
chat's composer through the existing focus request, after the frame, so
the slash is not typed into it. Ctrl+Tab with a single pane now swaps in
the chat shown before the current one and back; two or more panes keep
cycling panes. The visit remembers its shown chats most-recent-first,
dropping closed, /new-moved and other-server ids.
This commit is contained in:
2026-11-07 00:46:02 +00:00
parent f4ba2ab25e
commit de0c06cec1
5 changed files with 335 additions and 13 deletions
@@ -0,0 +1,190 @@
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/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<JsonMap> 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<EditableText>(_input(id));
Finder _actions(String id) => find.byWidgetPredicate(
(widget) => widget is SessionActionsButton && widget.session['id'] == id,
);
ChatWorkspaceController _workspace(WidgetTester tester) =>
tester.widget<ChatWorkspace>(find.byType(ChatWorkspace)).controller;
Future<void> _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),
},
),
),
);
await tester.pumpAndSettle();
}
Future<void> _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<void> _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<bool> _slash(WidgetTester tester) async {
final handled = await tester.sendKeyEvent(LogicalKeyboardKey.slash);
await tester.pumpAndSettle();
return handled;
}
Future<void> _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) and Alt+/ are not the shortcut; Ctrl+/ is
// 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(_editable(tester, 'a').focusNode.hasPrimaryFocus, isFalse);
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 returns to the previous chat', (
tester,
) async {
await _setup(tester);
await _openRow(tester, 'a');
final workspace = _workspace(tester);
// Nothing shown before: the chord only focuses the composer.
await _controlTab(tester);
expect(workspace.activePane?.sessionId, 'a');
expect(_editable(tester, 'a').focusNode.hasPrimaryFocus, isTrue);
await _openRow(tester, 'b');
await tester.enterText(_input('b'), 'Draft B');
await _focusSidebar(tester);
for (final id in ['a', 'b', 'a']) {
await _controlTab(tester);
expect(workspace.paneCount, 1);
expect(workspace.activePane?.sessionId, id);
expect(_editable(tester, id).focusNode.hasPrimaryFocus, isTrue);
}
await _controlTab(tester, shift: true);
expect(workspace.activePane?.sessionId, 'b');
expect(_editable(tester, 'b').controller.text, 'Draft B');
// A third chat makes the pair c and b; a is no longer one step away.
await _openRow(tester, 'c');
await _controlTab(tester);
expect(workspace.activePane?.sessionId, 'b');
await _controlTab(tester);
expect(workspace.activePane?.sessionId, 'c');
// Two panes cycle the panes as before, not the history.
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());
});
}