Files
pipelines/app/test/features/terminal/terminal_shortcuts_test.dart
alan c5af000453 Terminal: text input for IMEs, Capture browser keys, touch key row
- Desktop and web terminals use the text-input path too, so IMEs, dead
  keys, the emoji picker and dictation work; the key handler still
  encodes special keys and chords first (handled keys are never typed
  again), composing text is drawn at the cursor and sent on commit, the
  platform editable sits at the cursor, and a closed input reopens on
  the next key. The editable terminal is a semantics text field so web
  typing also works with semantics on.
- Web: "Capture browser keys" (terminal menu and right-click menu) goes
  full screen and calls navigator.keyboard.lock(); hold Esc to leave.
  No-op where unsupported; cheatsheet and menu note give alternatives.
- Touch: scrollable key row with sticky Ctrl/Alt (tap once, double-tap
  lock), Esc, Tab, arrows, Home/End, PgUp/PgDn and symbols through the
  hardware encoder; soft keyboard types per key (delete detection,
  Return as Enter); the line field moves behind the row's Line button.
- Capture kit: compose=, --semantics false, --user-agent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 02:43:57 +00:00

418 lines
15 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/terminal/terminal_hub.dart';
import 'package:pipelines_app/features/terminal/terminal_layout.dart';
import 'package:pipelines_app/features/terminal/terminal_shortcuts.dart';
import 'package:pipelines_app/features/terminal/terminal_tab.dart';
import 'package:pipelines_app/ui/input/keyboard_owner.dart';
import 'package:xterm/xterm.dart';
import 'terminal_app.dart';
import 'terminal_fixtures.dart';
import '../chat/shell/terminal_typing.dart';
void main() {
late TerminalGateway mac;
late TerminalHub hub;
late TerminalLayout layout;
setUp(() {
mac = TerminalGateway()
..add('term_a', cwd: '/Users/op/src')
..add('term_b', cwd: '/Users/op/docs')
..add('term_c', cwd: '/Users/op/notes');
hub = TerminalHub(TestMachines([machine('mac', mac, name: 'Mac')]));
layout = TerminalLayout(store: MemoryLayoutStore());
});
tearDown(() async {
hub.dispose();
await mac.close();
});
String key(String id) => terminalKey('mac', id);
/// Each toggle chord, and what it sends to the shell from inside the
/// terminal (null: it hides the terminal there too).
const toggles = {
'Alt+T': (LogicalKeyboardKey.keyT, LogicalKeyboardKey.altLeft, '\x1bt'),
'Ctrl+T': (LogicalKeyboardKey.keyT, LogicalKeyboardKey.controlLeft, '\x14'),
'Cmd+T': (LogicalKeyboardKey.keyT, LogicalKeyboardKey.metaLeft, null),
'Ctrl+`': (
LogicalKeyboardKey.backquote,
LogicalKeyboardKey.controlLeft,
null,
),
};
for (final mode in TerminalMode.values) {
for (final MapEntry(key: name, value: (letter, modifier, sent))
in toggles.entries) {
if (sent != null) {
desktopTestWidgets(
'$name shows the ${mode.label.toLowerCase()} from a text field; '
'inside the terminal it reaches the shell',
(tester) async {
layout.setMode(mode);
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.tap(find.byKey(appField));
await tester.pump();
await chord(tester, letter, [modifier]);
expect(layout.visible, isTrue);
expect(find.text('t'), findsNothing);
expect(terminalFocused(key('term_a')), isTrue);
await chord(tester, letter, [modifier]);
expect(layout.visible, isTrue);
expect(mac.bodies('terminal_input').last['data'], sent);
// Cmd+T and Ctrl+` still hide it from inside.
await chord(tester, LogicalKeyboardKey.backquote, [
LogicalKeyboardKey.controlLeft,
]);
expect(layout.visible, isFalse);
await tester.pump(const Duration(milliseconds: 300));
},
);
continue;
}
desktopTestWidgets(
'$name toggles the ${mode.label.toLowerCase()} from a text '
'field and from inside the terminal',
(tester) async {
layout.setMode(mode);
final surface = mode == TerminalMode.panel
? panelFinder
: floatFinder;
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.tap(find.byKey(appField));
await tester.pump();
await chord(tester, letter, [modifier]);
expect(surface, findsOneWidget);
expect(layout.visible, isTrue);
// The shortcut never types into the field it was pressed in.
expect(find.text('t'), findsNothing);
expect(terminalFocused(key('term_a')), isTrue);
// Inside the terminal the same keys hide it.
final typed = mac.bodies('terminal_input').length;
await chord(tester, letter, [modifier]);
expect(surface, findsNothing);
expect(layout.visible, isFalse);
expect(mac.bodies('terminal_input'), hasLength(typed));
expect(hub.tabs, hasLength(3));
},
);
}
}
test('Option+T on a Mac, which types †, toggles by key position', () {
const event = KeyDownEvent(
physicalKey: PhysicalKeyboardKey.keyT,
logicalKey: LogicalKeyboardKey(0x2020),
character: '†',
timeStamp: Duration.zero,
);
expect(terminalToggleShortcut(event, _AltHeld()), isTrue);
// Inside the terminal Option+T is the shell's (ESC t, or † when Option
// is not Meta).
expect(
terminalToggleShortcut(event, _AltHeld(), inTerminal: true),
isFalse,
);
});
test('inside the terminal only Cmd+T and Ctrl+` toggle', () {
KeyDownEvent down(LogicalKeyboardKey key, PhysicalKeyboardKey physical) =>
KeyDownEvent(
physicalKey: physical,
logicalKey: key,
timeStamp: Duration.zero,
);
final t = down(LogicalKeyboardKey.keyT, PhysicalKeyboardKey.keyT);
final tick = down(
LogicalKeyboardKey.backquote,
PhysicalKeyboardKey.backquote,
);
bool inside(KeyDownEvent event, HardwareKeyboard keyboard) =>
terminalToggleShortcut(event, keyboard, inTerminal: true);
expect(inside(t, _AltHeld()), isFalse);
expect(inside(t, _Held(control: true)), isFalse);
expect(inside(t, _Held(meta: true)), isTrue);
expect(inside(tick, _Held(control: true)), isTrue);
expect(terminalToggleShortcut(t, _Held(control: true)), isTrue);
});
test('the menu and cheatsheet say which keys reach the shell', () {
expect(
const TerminalShortcutLabels(
web: false,
platform: TargetPlatform.macOS,
).inTerminalNote,
'Inside the terminal, Option+T and Ctrl+T reach the shell; Cmd+T or '
'Ctrl+` hides it.',
);
const linuxWeb = TerminalShortcutLabels(
web: true,
platform: TargetPlatform.linux,
);
expect(
linuxWeb.inTerminalNote,
'Inside the terminal, Alt+T and Ctrl+T reach the shell; Ctrl+` hides '
'it.',
);
expect(linuxWeb.summary, contains(linuxWeb.inTerminalNote));
});
test('a browser outside macOS keeps Ctrl+W: the menu says how to send '
'it anyway and what to press instead', () {
const linuxWeb = TerminalShortcutLabels(
web: true,
platform: TargetPlatform.linux,
);
expect(linuxWeb.browserKeysNote, contains('Capture browser keys'));
expect(linuxWeb.browserKeysNote, contains('Alt+Backspace'));
expect(linuxWeb.summary, contains(linuxWeb.browserKeysNote));
for (final labels in const [
TerminalShortcutLabels(web: true, platform: TargetPlatform.macOS),
TerminalShortcutLabels(web: false, platform: TargetPlatform.linux),
]) {
expect(labels.browserKeysNote, isNull);
expect(labels.summary, isNot(contains('Ctrl+W')));
}
});
desktopTestWidgets('holding the chord toggles once and types nothing', (
tester,
) async {
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
await tester.sendKeyDownEvent(LogicalKeyboardKey.keyT);
await tester.pumpAndSettle();
expect(terminalFocused(key('term_a')), isTrue);
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.keyT);
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.keyT);
await tester.sendKeyUpEvent(LogicalKeyboardKey.keyT);
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
await tester.pumpAndSettle();
expect(layout.visible, isTrue);
expect(mac.bodies('terminal_input'), isEmpty);
});
desktopTestWidgets('Shift or a second modifier leaves T alone', (
tester,
) async {
await pumpTerminalApp(tester, hub: hub, layout: layout);
for (final modifiers in [
[LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.shiftLeft],
[LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.altLeft],
[LogicalKeyboardKey.metaLeft, LogicalKeyboardKey.shiftLeft],
]) {
await chord(tester, LogicalKeyboardKey.keyT, modifiers);
expect(layout.visible, isFalse, reason: '$modifiers');
}
});
desktopTestWidgets(
'showing focuses the terminal; hiding gives the keyboard back '
'to the field that had it',
(tester) async {
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.tap(find.byKey(appField));
await tester.pump();
expect(appFieldFocused(tester), isTrue);
await altT(tester);
expect(terminalFocused(key('term_a')), isTrue);
expect(appFieldFocused(tester), isFalse);
// Typing goes straight to the shell.
await typeKey(tester, LogicalKeyboardKey.keyX);
expect(mac.bodies('terminal_input').last['data'], 'x');
await toggleTerminal(tester);
expect(appFieldFocused(tester), isTrue);
// The bar button focuses it as well.
await tester.tap(find.byIcon(Icons.terminal));
await tester.pumpAndSettle();
expect(terminalFocused(key('term_a')), isTrue);
},
);
desktopTestWidgets('a terminal left shown before a reload is focused once it '
'lists', (tester) async {
layout.show();
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.pumpAndSettle();
expect(panelFinder, findsOneWidget);
expect(terminalFocused(key('term_a')), isTrue);
// The emulator's size reaches the server after its debounce.
await tester.pump(const Duration(milliseconds: 300));
});
desktopTestWidgets('Ctrl+Tab and Ctrl+Shift+Tab move between terminal tabs, '
'wrapping, and focus each; hidden, they do nothing here', (tester) async {
await pumpTerminalApp(tester, hub: hub, layout: layout);
await ctrlTab(tester);
expect(hub.active, isFalse);
await tester.tap(find.byKey(appField));
await altT(tester);
for (final id in ['term_b', 'term_c', 'term_a', 'term_b']) {
await ctrlTab(tester);
expect(hub.selected!.info.id, id);
expect(terminalFocused(key(id)), isTrue, reason: id);
}
for (final id in ['term_a', 'term_c']) {
await ctrlTab(tester, reverse: true);
expect(hub.selected!.info.id, id);
expect(terminalFocused(key(id)), isTrue, reason: id);
}
// From the field too: the terminal claims the keys while it is shown.
await tester.tap(find.byKey(appField));
await tester.pump();
await ctrlTab(tester);
expect(hub.selected!.info.id, 'term_a');
expect(terminalFocused(key('term_a')), isTrue);
await toggleTerminal(tester);
await ctrlTab(tester);
expect(hub.selected!.info.id, 'term_a');
});
desktopTestWidgets('a tab picked by pointer takes the keyboard', (
tester,
) async {
layout.show();
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.pumpAndSettle();
await tester.tap(find.text('notes'));
await tester.pumpAndSettle();
expect(terminalFocused(key('term_c')), isTrue);
});
desktopTestWidgets('opening with no terminal starts one', (tester) async {
final empty = TerminalGateway();
final bare = TerminalHub(TestMachines([machine('mac', empty)]));
addTearDown(bare.dispose);
await pumpTerminalApp(tester, hub: bare, layout: layout);
await altT(tester);
expect(empty.bodies('terminal_open'), hasLength(1));
expect(bare.tabs, hasLength(1));
expect(terminalFocused(bare.tabs.single.key), isTrue);
await toggleTerminal(tester);
await toggleTerminal(tester);
expect(empty.bodies('terminal_open'), hasLength(1));
});
desktopTestWidgets(
'on a narrow window the keys open and close the full-screen '
'page, focused, and Ctrl+Tab moves between its tabs',
(tester) async {
await pumpTerminalApp(
tester,
hub: hub,
layout: layout,
size: const Size(390, 800),
);
await tester.tap(find.byKey(appField));
await tester.pump();
await chord(tester, LogicalKeyboardKey.keyT, [
LogicalKeyboardKey.controlLeft,
]);
expect(find.widgetWithText(AppBar, 'Terminal'), findsOneWidget);
expect(terminalFocused(key('term_a')), isTrue);
await ctrlTab(tester, reverse: true);
expect(hub.selected!.info.id, 'term_c');
expect(terminalFocused(key('term_c')), isTrue);
await chord(tester, LogicalKeyboardKey.backquote, [
LogicalKeyboardKey.controlLeft,
]);
expect(find.widgetWithText(AppBar, 'Terminal'), findsNothing);
expect(find.byType(TerminalView), findsNothing);
expect(appFieldFocused(tester), isTrue);
},
);
test('the listed shortcuts are the ones the platform delivers', () {
const mac = TerminalShortcutLabels(
web: false,
platform: TargetPlatform.macOS,
);
expect(mac.toggle, ['Option+T', 'Cmd+T', 'Ctrl+T', 'Ctrl+`']);
expect(mac.tabs, ['Ctrl+Tab', 'Ctrl+Shift+Tab']);
const linux = TerminalShortcutLabels(
web: false,
platform: TargetPlatform.linux,
);
expect(linux.toggle, ['Alt+T', 'Ctrl+T', 'Ctrl+`']);
const macWeb = TerminalShortcutLabels(
web: true,
platform: TargetPlatform.macOS,
);
expect(macWeb.toggle, ['Option+T', 'Ctrl+T', 'Ctrl+`']);
expect(macWeb.button, 'Terminal (Option+T)');
expect(macWeb.tabs, isEmpty);
expect(macWeb.summary, isNot(contains('Tab')));
const windowsWeb = TerminalShortcutLabels(
web: true,
platform: TargetPlatform.windows,
);
expect(windowsWeb.toggle, ['Alt+T', 'Ctrl+`']);
expect(windowsWeb.button, 'Terminal (Alt+T)');
});
desktopTestWidgets('typing / in the terminal stays in the terminal', (
tester,
) async {
layout.setMode(TerminalMode.panel);
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.tap(find.byKey(appField));
await tester.pump();
await chord(tester, LogicalKeyboardKey.keyT, [LogicalKeyboardKey.altLeft]);
expect(terminalFocused(key('term_a')), isTrue);
// App-wide shortcuts see a typing surface and leave the key alone.
expect(primaryFocusIsTypingSurface(), isTrue);
expect(primaryFocusTakesTyping(), isTrue);
await typeKey(tester, LogicalKeyboardKey.slash, character: '/');
expect(terminalFocused(key('term_a')), isTrue);
expect(
mac.bodies('terminal_input').map((b) => b['data']).join(),
contains('/'),
);
expect(
find.text('/'),
findsNothing,
reason: 'nothing typed into the field',
);
});
}
/// A keyboard with the given modifiers held.
class _Held extends HardwareKeyboard {
_Held({this.control = false, this.meta = false});
final bool control;
final bool meta;
@override
bool get isAltPressed => false;
@override
bool get isControlPressed => control;
@override
bool get isMetaPressed => meta;
@override
bool get isShiftPressed => false;
}
/// A keyboard on which only Alt (Option) is held.
class _AltHeld extends HardwareKeyboard {
@override
bool get isAltPressed => true;
@override
bool get isControlPressed => false;
@override
bool get isMetaPressed => false;
@override
bool get isShiftPressed => false;
}
/// Hardware keys reach the terminal on desktop; phones type through the
/// on-screen keyboard instead. These tests drive desktop key events.
void desktopTestWidgets(String description, WidgetTesterCallback callback) =>
testWidgets(
description,
callback,
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);