Files
pipelines/app/test/features/terminal/terminal_shortcuts_test.dart
T
alan 7d1ffe8ad3 Terminal: xterm key encoding, no live reflow on resize, Alt+T/Ctrl+T reach the shell
- Live terminals no longer reflow (xterm.dart 4.0.0 left the cursor on the
  wrong row); narrowing cuts and erases cells past the width, rows scroll
  into scrollback keeping the cursor row; finished output still reflows.
- App-side key encoding before xterm.dart's defaults: Alt as Meta (ESC +
  the typed character), Ctrl symbol table, xterm modifier parameter for
  arrows/Home/End/PgUp/PgDn/Ins/Del/F-keys, DECCKM and DECKPAM, Alt+BS.
- Settings > Appearance > Terminal: Use Option as Meta key (Macs, on).
- Inside a focused terminal only Cmd+T and Ctrl+` toggle it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 23:32:29 +00:00

401 lines
14 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';
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));
});
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 tester.sendKeyEvent(LogicalKeyboardKey.keyX);
await tester.pump();
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 tester.sendKeyEvent(LogicalKeyboardKey.slash, character: '/');
await tester.pump();
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),
);