Merge main into the terminal flow-control branch

Conflicts resolved keeping both sides: main's active-client rules, input
lane, reply marking and key input, and this branch's flow control,
acknowledgements, subscriptions and sliced emulator feed. terminal_ack
joins the terminal lane; the feed marks each live slice it writes as
drawing, so the emulator's answers still go to onReply.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 00:03:20 +00:00
148 changed files with 18443 additions and 153 deletions
@@ -0,0 +1,101 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/shell/chat_shell_terminals.dart';
import 'package:pipelines_app/features/terminal/terminal_info.dart';
import 'package:pipelines_app/features/terminal/terminal_tab.dart';
import 'terminal_fixtures.dart';
void main() {
testWidgets('answers are marked, typing carries the size, focus activates', (
tester,
) async {
final gateway = TerminalGateway();
final tab = TerminalTab(
machineId: 'm',
machineName: 'm',
gateway: gateway,
info: TerminalInfo.fromJson(gateway.add('t1')),
);
// Before a view lays it out, focus has no size to claim.
tab.session.activate();
await tester.pump();
expect(gateway.bodies('terminal_resize'), isEmpty);
tab.session.terminal.resize(100, 30);
await tester.pump(chatShellResizeDelay);
expect(gateway.bodies('terminal_resize'), [
{'terminal_id': 't1', 'cols': 100, 'rows': 30},
]);
// A cursor position query in the output: the answer is marked.
tab.frame({'output': '\x1b[6n', 'offset': 0, 'end': 4});
await tester.pump();
// Typing carries the size this client draws at.
tab.session.terminal.textInput('ls');
await tester.pump();
expect(gateway.bodies('terminal_input'), [
{'terminal_id': 't1', 'data': '\x1b[1;1R', 'reply': true},
{'terminal_id': 't1', 'data': 'ls', 'cols': 100, 'rows': 30},
]);
// A view gaining focus claims the terminal at once, at its size.
tab.session.activate();
await tester.pump();
expect(gateway.bodies('terminal_resize').last, {
'terminal_id': 't1',
'cols': 100,
'rows': 30,
'activate': true,
});
// A new connection reports its size again.
tab.attach(gateway, TerminalInfo.fromJson(gateway.terminals['t1']!));
await tester.pump(chatShellResizeDelay);
expect(gateway.bodies('terminal_resize'), hasLength(3));
expect(gateway.bodies('terminal_resize').last, {
'terminal_id': 't1',
'cols': 100,
'rows': 30,
});
tab.dispose();
});
test('the input pipe keeps answers and typing apart, in order', () async {
final sent = <(String, bool)>[];
final pipe = ChatShellInputPipe((data, reply) async {
sent.add((data, reply));
await Future<void>.delayed(Duration.zero);
}, (_) {});
pipe.add('a');
// While the first is in flight, typing, an answer and typing wait.
pipe.add('b');
pipe.add('c');
pipe.add('\x1b[1;1R', reply: true);
pipe.add('d');
pipe.add('${'x' * chatShellInputChunk}y');
await pumpEventQueue();
expect(sent, [
('a', false),
('bc', false),
('\x1b[1;1R', true),
('d${'x' * (chatShellInputChunk - 1)}', false),
('xy', false),
]);
});
test('a refusal drops what was waiting and reports once', () async {
final failures = <Object>[];
final sent = <String>[];
final pipe = ChatShellInputPipe((data, reply) async {
sent.add(data);
await Future<void>.delayed(Duration.zero);
throw StateError('not running');
}, failures.add);
pipe.add('a');
pipe.add('b');
pipe.add('?', reply: true);
await pumpEventQueue();
expect(sent, ['a']);
expect(failures, hasLength(1));
});
}
+11 -3
View File
@@ -90,9 +90,7 @@ class TerminalTestAppState extends State<TerminalTestApp> {
child: Scaffold(
appBar: AppBar(
title: const Text('App'),
actions: [
TerminalToggleButton(layout: widget.layout),
],
actions: [TerminalToggleButton(layout: widget.layout)],
),
body: const TextField(key: appField),
),
@@ -125,6 +123,8 @@ Future<void> chord(
}
}
/// Alt+T: shows or hides the terminal from outside it (inside, it is the
/// shell's ESC t).
Future<void> altT(WidgetTester tester, {bool settle = true}) => chord(
tester,
LogicalKeyboardKey.keyT,
@@ -132,6 +132,14 @@ Future<void> altT(WidgetTester tester, {bool settle = true}) => chord(
settle: settle,
);
/// Ctrl+`: shows or hides the terminal wherever the keyboard is.
Future<void> toggleTerminal(WidgetTester tester, {bool settle = true}) => chord(
tester,
LogicalKeyboardKey.backquote,
[LogicalKeyboardKey.controlLeft],
settle: settle,
);
Future<void> ctrlTab(WidgetTester tester, {bool reverse = false}) => chord(
tester,
LogicalKeyboardKey.tab,
@@ -57,9 +57,7 @@ Future<Widget?> _pumpBar(
await tester.pumpWidget(
MaterialApp(
theme: theme,
home: actions == null
? bar
: TerminalScope(actions: actions, child: bar),
home: actions == null ? bar : TerminalScope(actions: actions, child: bar),
),
);
await tester.pump();
@@ -106,7 +106,7 @@ void main() {
// Hidden again: the composer that had the keyboard gets it back, and
// Ctrl+Tab is the chat's once more.
await altT(tester);
await toggleTerminal(tester);
expect(_composer(tester, 'a').focusNode.hasPrimaryFocus, isTrue);
await ctrlTab(tester);
expect(workspace.activePane?.sessionId, 'b');
@@ -48,7 +48,7 @@ void main() {
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.tap(find.byKey(appField));
await tester.pump();
await altT(tester, settle: false);
await toggleTerminal(tester, settle: false);
await tester.pump(const Duration(milliseconds: 100));
expect(panelFinder, findsOneWidget);
// Midway: the app has given up part of the panel's height, and the
@@ -61,7 +61,7 @@ void main() {
expect(appBottom(tester), shown);
expect(panelOpacity(tester), 1);
await altT(tester, settle: false);
await toggleTerminal(tester, settle: false);
// The keyboard is back in the field at once; the panel leaves after.
expect(appFieldFocused(tester), isTrue);
await tester.pump(const Duration(milliseconds: 100));
@@ -77,7 +77,7 @@ void main() {
'shrinks away', (tester) async {
layout.setMode(TerminalMode.float);
await pumpTerminalApp(tester, hub: hub, layout: layout);
await altT(tester, settle: false);
await toggleTerminal(tester, settle: false);
// The window is built, then its motion starts.
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
@@ -87,7 +87,7 @@ void main() {
expect(appBottom(tester), 800);
await tester.pumpAndSettle();
expect(floatScale(tester).scale.value, 1);
await altT(tester, settle: false);
await toggleTerminal(tester, settle: false);
await tester.pump(const Duration(milliseconds: 100));
// Hiding: no longer shown, still painted while it shrinks away.
expect(floatFinder, findsNothing);
@@ -100,7 +100,7 @@ void main() {
// AnimationSpeedController plays animations at half speed this way.
timeDilation = 2;
await pumpTerminalApp(tester, hub: hub, layout: layout);
await altT(tester, settle: false);
await toggleTerminal(tester, settle: false);
await tester.pump(const Duration(milliseconds: 300));
expect(appBottom(tester), inExclusiveRange(shown, 800));
await tester.pumpAndSettle();
@@ -112,16 +112,16 @@ void main() {
tester,
) async {
await pumpTerminalApp(tester, hub: hub, layout: layout, reduceMotion: true);
await altT(tester, settle: false);
await toggleTerminal(tester, settle: false);
await tester.pump();
expect(appBottom(tester), shown);
expect(panelOpacity(tester), 1);
await altT(tester, settle: false);
await toggleTerminal(tester, settle: false);
await tester.pump();
expect(panelFinder, findsNothing);
expect(appBottom(tester), 800);
layout.setMode(TerminalMode.float);
await altT(tester, settle: false);
await toggleTerminal(tester, settle: false);
await tester.pump();
expect(floatScale(tester).scale.value, 1);
// The emulator's size reaches the server after its debounce.
@@ -31,16 +31,48 @@ void main() {
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),
'Ctrl+T': (LogicalKeyboardKey.keyT, LogicalKeyboardKey.controlLeft),
'Cmd+T': (LogicalKeyboardKey.keyT, LogicalKeyboardKey.metaLeft),
'Ctrl+`': (LogicalKeyboardKey.backquote, LogicalKeyboardKey.controlLeft),
'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))
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',
@@ -58,8 +90,7 @@ void main() {
// 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 instead of reaching the
// shell (Ctrl+T would be transpose-chars there).
// Inside the terminal the same keys hide it.
final typed = mac.bodies('terminal_input').length;
await chord(tester, letter, [modifier]);
expect(surface, findsNothing);
@@ -79,6 +110,54 @@ void main() {
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', (
@@ -127,7 +206,7 @@ void main() {
await tester.sendKeyEvent(LogicalKeyboardKey.keyX);
await tester.pump();
expect(mac.bodies('terminal_input').last['data'], 'x');
await altT(tester);
await toggleTerminal(tester);
expect(appFieldFocused(tester), isTrue);
// The bar button focuses it as well.
await tester.tap(find.byIcon(Icons.terminal));
@@ -170,7 +249,7 @@ void main() {
await ctrlTab(tester);
expect(hub.selected!.info.id, 'term_a');
expect(terminalFocused(key('term_a')), isTrue);
await altT(tester);
await toggleTerminal(tester);
await ctrlTab(tester);
expect(hub.selected!.info.id, 'term_a');
});
@@ -195,8 +274,8 @@ void main() {
expect(empty.bodies('terminal_open'), hasLength(1));
expect(bare.tabs, hasLength(1));
expect(terminalFocused(bare.tabs.single.key), isTrue);
await altT(tester);
await altT(tester);
await toggleTerminal(tester);
await toggleTerminal(tester);
expect(empty.bodies('terminal_open'), hasLength(1));
});
@@ -220,7 +299,7 @@ void main() {
await ctrlTab(tester, reverse: true);
expect(hub.selected!.info.id, 'term_c');
expect(terminalFocused(key('term_c')), isTrue);
await chord(tester, LogicalKeyboardKey.keyT, [
await chord(tester, LogicalKeyboardKey.backquote, [
LogicalKeyboardKey.controlLeft,
]);
expect(find.widgetWithText(AppBar, 'Terminal'), findsNothing);
@@ -284,6 +363,21 @@ void main() {
});
}
/// 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
@@ -265,6 +265,39 @@ void main() {
expect(layout.mode, TerminalMode.panel);
});
Future<void> pumpSettings(WidgetTester tester) => tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SingleChildScrollView(
child: SettingsTerminalSection(layout: layout),
),
),
),
);
const optionAsMeta = ValueKey('settings-terminal-option-as-meta');
desktopTestWidgets('on a Mac, Settings chooses whether Option is Meta, '
'saved with the layout', (tester) async {
await pumpSettings(tester);
expect(layout.optionAsMeta, isTrue);
await tester.tap(find.byKey(optionAsMeta));
await tester.pumpAndSettle();
expect(layout.optionAsMeta, isFalse);
expect(saved()['option_as_meta'], isFalse);
final restored = TerminalLayout(store: store);
await restored.restore();
expect(restored.optionAsMeta, isFalse);
});
testWidgets(
'elsewhere Alt is always Meta and there is no Option switch',
(tester) async {
await pumpSettings(tester);
expect(find.byKey(optionAsMeta), findsNothing);
},
variant: TargetPlatformVariant.only(TargetPlatform.linux),
);
desktopTestWidgets(
'a narrow window opens the terminal full screen with the key '
'toolbar',