Files
pipelines/app/test/features/chat/shell/chat_shell_terminal_test.dart
T
alan 89882d90f3 Draw chat shell runs in an xterm terminal that takes typed input
The card's output area is now an xterm.dart emulator kept per run by the
controller: it is fed the raw stream in order, takes typing and paste while
the command runs (with a key toolbar and a soft-keyboard field on touch
platforms), resizes the process to its layout, opens printed links, shrinks
finished runs to their output and opens full screen. A reconnect resumes from
the byte the card holds; Copy output copies the server's plain text.
2026-11-09 20:08:33 +00:00

188 lines
6.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.arrowUp);
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyC);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pump();
expect(typed(), '\x1b[A\x03');
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');
});
}