Files
pipelines/app/test/features/chat/shell/chat_shell_terminal_test.dart
T
alan 2332143e5b Fit chat terminals to their output, keep pastes and keys in the terminal
Finished terminals are sized synchronously from their rows at the view's
width (the post-frame fit made the transcript re-anchor forever); blank rows
xterm.dart keeps when rewrapping are dropped; desktop typing uses key events;
the composer leaves Cmd/Ctrl-V to a focused terminal; Ctrl-A reaches the
process. The probe fixture gains coloured terminal runs and an interactive
login. Notes record behaviour, protocol, security and verification.
2026-11-09 20:51:59 +00:00

212 lines
7.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/shell/chat_shell_runs_controller.dart';
import 'package:xterm/xterm.dart';
import 'chat_shell_fixtures.dart';
void main() {
late ShellGateway gateway;
setUp(() => gateway = ShellGateway());
tearDown(() => gateway.close());
/// Everything sent to the process, joined in order.
String typed() => [
for (final body in gateway.bodies('chat_shell_input')) body['data'],
].join();
Future<void> openRunning(WidgetTester tester, {String output = ''}) async {
gateway.runs = [
shellRecord(
'run_t',
command: 'gh auth login',
terminal: true,
output: output,
),
];
await tester.pumpWidget(shellThread(gateway));
await tester.pump();
}
testWidgets(
'keys typed into a focused desktop terminal reach the process',
(tester) async {
await openRunning(tester, output: 'Paste your token: ');
expect(terminalText(tester), 'Paste your token:');
// A desktop has no soft-keyboard field: the terminal takes typing itself.
expect(find.byKey(const Key('chat-shell-terminal-field')), findsNothing);
await tester.tap(find.byType(TerminalView));
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.keyG);
await tester.sendKeyEvent(LogicalKeyboardKey.digit7);
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyC);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pump();
expect(typed(), 'g7\x1b[A\x03');
// Cmd-V pastes the clipboard into the process.
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
(call) async =>
call.method == 'Clipboard.getData' ? {'text': 'tok'} : null,
);
addTearDown(
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
null,
),
);
await tester.sendKeyDownEvent(LogicalKeyboardKey.metaLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyV);
await tester.sendKeyUpEvent(LogicalKeyboardKey.metaLeft);
for (var i = 0; i < 5; i++) {
await tester.pump(const Duration(milliseconds: 20));
}
expect(typed(), 'g7\x1b[A\x03tok');
// The chat composer leaves a paste into the terminal alone.
expect(find.text('tok'), findsNothing);
expect(find.byType(EditableText), findsWidgets);
await tester.pump(const Duration(milliseconds: 400));
expect(gateway.bodies('chat_shell_input').first['run_id'], 'run_t');
},
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);
testWidgets('Paste types the clipboard text into the process', (
tester,
) async {
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
(call) async => call.method == 'Clipboard.getData'
? {'text': 'gho_pasted-token'}
: null,
);
addTearDown(
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
null,
),
);
await openRunning(tester);
await tester.tap(find.byKey(const Key('chat-shell-key-paste')));
await tester.pump();
await tester.tap(find.byKey(const Key('chat-shell-key-enter')));
await tester.pump();
expect(typed(), 'gho_pasted-token\r');
});
testWidgets('the touch key toolbar and field type into the process', (
tester,
) async {
await openRunning(tester, output: 'Password: ');
for (final key in ['ctrl-c', 'tab', 'esc', 'left', 'down', 'right']) {
await tester.tap(find.byKey(Key('chat-shell-key-$key')));
await tester.pump();
}
expect(typed(), '\x03\t\x1b\x1b[D\x1b[B\x1b[C');
gateway.calls.clear();
final field = find.byKey(const Key('chat-shell-terminal-field'));
expect(field, findsOneWidget);
await tester.tap(find.byKey(const Key('chat-shell-terminal-hide')));
await tester.pump();
expect(tester.widget<TextField>(field).obscureText, isTrue);
await tester.enterText(field, 'hunter2');
await tester.testTextInput.receiveAction(TextInputAction.send);
await tester.pump();
expect(typed(), 'hunter2\r');
// The field forgets the text once it is sent.
expect(tester.widget<TextField>(field).controller!.text, isEmpty);
});
testWidgets('a running terminal is resized to its view; a finished one is '
'read-only', (tester) async {
await openRunning(tester, output: 'working\r\n');
await tester.pump(const Duration(milliseconds: 200));
final resize = gateway.bodies('chat_shell_resize');
expect(resize, hasLength(1));
expect(resize.single['run_id'], 'run_t');
expect(resize.single['rows'], 12);
expect(resize.single['cols'], greaterThan(20));
gateway.emitRun(
shellRecord(
'run_t',
command: 'gh auth login',
status: 'exited',
exitCode: 0,
terminal: true,
output: 'working\r\n',
),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-shell-key-enter')), findsNothing);
expect(
tester.widget<TerminalView>(find.byType(TerminalView)).readOnly,
isTrue,
);
expect(terminalText(tester), 'working');
gateway.calls.clear();
await tester.tap(find.byType(TerminalView));
await tester.sendKeyEvent(LogicalKeyboardKey.keyA);
await tester.pump(const Duration(milliseconds: 400));
expect(gateway.bodies('chat_shell_input'), isEmpty);
});
testWidgets('coloured output is drawn without its escape codes', (
tester,
) async {
gateway.runs = [
shellRecord(
'run_c',
status: 'exited',
exitCode: 0,
terminal: true,
output: '\x1b[1;31merror\x1b[0m: bad\r\n10%\r100%\r\n',
),
];
await tester.pumpWidget(shellThread(gateway));
await tester.pumpAndSettle();
expect(terminalText(tester), 'error: bad\n100%');
final buffer = tester
.widget<TerminalView>(find.byType(TerminalView))
.terminal
.buffer;
// The red foreground survives as a cell attribute.
expect(
buffer.lines[0].getForeground(0),
isNot(buffer.lines[0].getForeground(7)),
);
});
test('a reconnect resumes the running output from its offset', () async {
gateway.runs = [shellRecord('run_t', terminal: true, output: 'one\r\n')];
final controller = ChatShellRunsController(gateway, 's');
addTearDown(controller.dispose);
await controller.refresh();
final session = controller.terminals.session(controller.run('run_t')!);
expect(session.end, 5);
gateway.runs = [
shellRecord('run_t', terminal: true, output: 'one\r\ntwo\r\n'),
];
gateway.resumeReply = shellRecord(
'run_t',
terminal: true,
output: 'two\r\n',
outputStart: 5,
);
await controller.refresh();
await Future<void>.delayed(Duration.zero);
final resume = gateway.bodies('chat_shell_run_output').single;
expect(resume['after'], 5);
expect(controller.run('run_t')!.output, 'one\r\ntwo\r\n');
expect(controller.run('run_t')!.gap, isFalse);
expect(session.end, 10);
expect(session.terminal.buffer.lines[1].getText().trimRight(), 'two');
});
}