Merge branch 'worktree-agent-a26df8b0059ab1b12'
This commit is contained in:
@@ -2,9 +2,11 @@ import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
||||
import 'package:xterm/xterm.dart';
|
||||
|
||||
import '../conversation/chat_thread_fixtures.dart';
|
||||
|
||||
@@ -15,6 +17,9 @@ class ShellGateway implements ApiGateway {
|
||||
final _frames = StreamController<JsonMap>.broadcast();
|
||||
List<JsonMap> runs = const [];
|
||||
String? refusal;
|
||||
|
||||
/// The reply to a `chat_shell_run_output` that resumes from an offset.
|
||||
JsonMap? resumeReply;
|
||||
int _serial = 0;
|
||||
|
||||
@override
|
||||
@@ -47,6 +52,9 @@ class ShellGateway implements ApiGateway {
|
||||
),
|
||||
};
|
||||
case 'chat_shell_run_output':
|
||||
if (body['after'] != null && resumeReply != null) {
|
||||
return {'chat_shell_run': resumeReply};
|
||||
}
|
||||
return {
|
||||
'chat_shell_run': runs.firstWhere((r) => r['id'] == body['run_id']),
|
||||
};
|
||||
@@ -84,6 +92,9 @@ JsonMap shellRecord(
|
||||
int? exitCode,
|
||||
String output = '',
|
||||
int durationMs = 0,
|
||||
bool terminal = false,
|
||||
String text = '',
|
||||
int outputStart = 0,
|
||||
}) => {
|
||||
'id': id,
|
||||
'session_id': 's',
|
||||
@@ -95,9 +106,27 @@ JsonMap shellRecord(
|
||||
'ended_at': ?(status == 'running' ? null : 1000 + durationMs),
|
||||
'duration_ms': durationMs,
|
||||
'output': output,
|
||||
'output_bytes': output.length,
|
||||
'output_start': outputStart,
|
||||
'output_bytes': outputStart + output.length,
|
||||
if (terminal) ...{'terminal': true, 'cols': 100, 'rows': 30},
|
||||
if (text.isNotEmpty) 'text': text,
|
||||
};
|
||||
|
||||
/// The text the run card's terminal draws, one row per line, trailing blank
|
||||
/// rows dropped.
|
||||
String terminalText(WidgetTester tester, {int index = 0}) {
|
||||
final view = tester.widgetList<TerminalView>(find.byType(TerminalView));
|
||||
final buffer = view.elementAt(index).terminal.buffer;
|
||||
final lines = [
|
||||
for (var y = 0; y < buffer.height; y++)
|
||||
buffer.lines[y].getText().trimRight(),
|
||||
];
|
||||
while (lines.isNotEmpty && lines.last.isEmpty) {
|
||||
lines.removeLast();
|
||||
}
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
Widget shellThread(ShellGateway gateway, {bool cupertino = false}) {
|
||||
final thread = ChatThreadView(
|
||||
gateway: gateway,
|
||||
|
||||
@@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'package:xterm/xterm.dart';
|
||||
|
||||
import 'chat_shell_fixtures.dart';
|
||||
|
||||
void main() {
|
||||
@@ -27,7 +29,7 @@ void main() {
|
||||
expect(find.byKey(const Key('chat-shell-run-running')), findsOneWidget);
|
||||
gateway.emitOutput('run_a', 'line 2\n', 7);
|
||||
await tester.pump();
|
||||
expect(find.text('line 1\nline 2'), findsOneWidget);
|
||||
expect(terminalText(tester), 'line 1\nline 2');
|
||||
expect(find.byKey(const Key('chat-shell-run-share')), findsNothing);
|
||||
|
||||
await tester.tap(find.byKey(const Key('chat-shell-run-stop')));
|
||||
@@ -43,7 +45,7 @@ void main() {
|
||||
expect(find.text('Stopped · 2.4 s'), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-shell-run-stop')), findsNothing);
|
||||
// The streamed output stays after the lifecycle frame.
|
||||
expect(find.text('line 1\nline 2'), findsOneWidget);
|
||||
expect(terminalText(tester), 'line 1\nline 2');
|
||||
});
|
||||
|
||||
testWidgets('$host: finished commands show their exit status', (
|
||||
@@ -69,6 +71,8 @@ void main() {
|
||||
];
|
||||
await tester.pumpWidget(shellThread(gateway, cupertino: cupertino));
|
||||
await tester.pump();
|
||||
// Finished terminals shrink to their output after their first frame.
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(card(), findsNWidgets(2));
|
||||
expect(find.text('Exit 0 · 120 ms'), findsOneWidget);
|
||||
@@ -94,7 +98,7 @@ void main() {
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('Copy output copies the retained output', (tester) async {
|
||||
testWidgets('Copy output copies the plain-text rendering', (tester) async {
|
||||
final copied = <String>[];
|
||||
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||
SystemChannels.platform,
|
||||
@@ -112,10 +116,18 @@ void main() {
|
||||
),
|
||||
);
|
||||
gateway.runs = [
|
||||
shellRecord('run_a', status: 'exited', exitCode: 0, output: 'a\nb\n'),
|
||||
shellRecord(
|
||||
'run_a',
|
||||
status: 'exited',
|
||||
exitCode: 0,
|
||||
terminal: true,
|
||||
output: '\x1b[32ma\x1b[0m\r\nb\r\n',
|
||||
text: 'a\nb\n',
|
||||
),
|
||||
];
|
||||
await tester.pumpWidget(shellThread(gateway));
|
||||
await tester.pump();
|
||||
expect(terminalText(tester), 'a\nb');
|
||||
await tester.tap(find.byKey(const Key('chat-shell-run-copy')));
|
||||
await tester.pump();
|
||||
expect(copied, ['a\nb\n']);
|
||||
@@ -151,7 +163,14 @@ void main() {
|
||||
await tester.ensureVisible(expand);
|
||||
await tester.pump();
|
||||
await tester.tap(expand);
|
||||
await tester.pump();
|
||||
expect(find.text('Collapse'), findsOneWidget);
|
||||
await tester.pumpAndSettle();
|
||||
// Full screen draws the same terminal over the window.
|
||||
expect(find.byType(TerminalView), findsOneWidget);
|
||||
expect(find.byType(TerminalView, skipOffstage: false), findsNWidgets(2));
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(terminalText(tester), contains('line 79'));
|
||||
await tester.pageBack();
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(TerminalView), findsOneWidget);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/shell/chat_shell_run.dart';
|
||||
import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_links.dart';
|
||||
import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_session.dart';
|
||||
import 'package:xterm/core.dart';
|
||||
|
||||
ChatShellRun _run(String output, {String status = 'running'}) =>
|
||||
ChatShellRun.fromJson({
|
||||
'id': 'run_1',
|
||||
'command': 'login',
|
||||
'directory': '/w',
|
||||
'status': status,
|
||||
'started_at': 1,
|
||||
'output': output,
|
||||
'output_bytes': output.length,
|
||||
'terminal': true,
|
||||
'cols': 40,
|
||||
'rows': 10,
|
||||
})!;
|
||||
|
||||
void main() {
|
||||
test('a printed link is found under the tap, across a wrapped row', () {
|
||||
final terminal = Terminal()..resize(20, 5);
|
||||
terminal.write('Open https://example.com/login/device?code=ABCD, then\r\n');
|
||||
// Row 0 holds "Open https://example." and row 1 continues it.
|
||||
expect(
|
||||
chatShellLinkAt(terminal.buffer, const CellOffset(8, 0)),
|
||||
Uri.parse('https://example.com/login/device?code=ABCD'),
|
||||
);
|
||||
expect(
|
||||
chatShellLinkAt(terminal.buffer, const CellOffset(10, 1)),
|
||||
Uri.parse('https://example.com/login/device?code=ABCD'),
|
||||
);
|
||||
expect(chatShellLinkAt(terminal.buffer, const CellOffset(1, 0)), isNull);
|
||||
expect(chatShellLinkAt(terminal.buffer, const CellOffset(0, 4)), isNull);
|
||||
});
|
||||
|
||||
test('only http and https links are offered', () {
|
||||
final terminal = Terminal()..resize(40, 3);
|
||||
terminal.write('javascript:alert(1) file:///etc/passwd');
|
||||
for (var x = 0; x < 38; x++) {
|
||||
expect(chatShellLinkAt(terminal.buffer, CellOffset(x, 0)), isNull);
|
||||
}
|
||||
});
|
||||
|
||||
test('typing reaches a live run; replayed query answers do not', () {
|
||||
final sent = <String>[];
|
||||
// The output asks for the cursor position, which the emulator answers.
|
||||
final session = ChatShellTerminalSession(
|
||||
_run('prompt\x1b[6n'),
|
||||
onInput: sent.add,
|
||||
onResize: (_, _) {},
|
||||
);
|
||||
expect(session.live, isTrue);
|
||||
expect(sent, isEmpty);
|
||||
session.terminal.textInput('y');
|
||||
session.append(_run('prompt\x1b[6nok\x1b[6n'), 'ok\x1b[6n', 10, 14);
|
||||
// The answer is one-based: row 1, column 9.
|
||||
expect(sent, ['y', '\x1b[1;9R']);
|
||||
expect(session.end, 14);
|
||||
|
||||
// A finished run draws read-only and sends nothing.
|
||||
session.finish(_run('prompt ok', status: 'exited'));
|
||||
session.replay(_run('prompt ok', status: 'exited'));
|
||||
session.terminal.textInput('n');
|
||||
expect(session.live, isFalse);
|
||||
expect(sent, hasLength(2));
|
||||
});
|
||||
|
||||
test('a chunk after a hole redraws the held copy', () {
|
||||
final session = ChatShellTerminalSession(
|
||||
_run('a\r\n'),
|
||||
onInput: (_) {},
|
||||
onResize: (_, _) {},
|
||||
);
|
||||
session.append(_run('a\r\nb\r\n'), 'b\r\n', 3, 6);
|
||||
expect(session.terminal.buffer.lines[1].getText().trim(), 'b');
|
||||
// Bytes 6..9 were missed; the run's copy says so and is redrawn.
|
||||
final gapped = _run('a\r\nb\r\n').withChunk('d\r\n', 9, 12);
|
||||
session.append(gapped, 'd\r\n', 9, 12);
|
||||
expect(session.end, 12);
|
||||
expect(
|
||||
session.terminal.buffer.lines[0].getText(),
|
||||
contains('earlier output was dropped'),
|
||||
);
|
||||
});
|
||||
|
||||
test('narrowing rewraps without pushing the first line out of view', () {
|
||||
final session = ChatShellTerminalSession(
|
||||
_run('first\r\n${'u' * 30}\r\nprompt: '),
|
||||
onInput: (_) {},
|
||||
onResize: (_, _) {},
|
||||
);
|
||||
session.terminal.resize(20, 6);
|
||||
final buffer = session.terminal.buffer;
|
||||
expect(buffer.height, 6);
|
||||
expect(buffer.lines[0].getText().trim(), 'first');
|
||||
expect(buffer.lines[3].getText().trim(), 'prompt:');
|
||||
expect(buffer.absoluteCursorY, 3);
|
||||
expect(buffer.cursorX, 8);
|
||||
});
|
||||
|
||||
test('a finished run counts the rows its output takes at a width', () {
|
||||
final session = ChatShellTerminalSession(
|
||||
_run('a\r\n${'x' * 50}\r\nend\r\n', status: 'exited'),
|
||||
onInput: (_) {},
|
||||
onResize: (_, _) {},
|
||||
);
|
||||
// The 50-character line wraps at the terminal's 40 columns.
|
||||
expect(session.rowsAt(40), 4);
|
||||
expect(session.rowsAt(20), 5);
|
||||
expect(session.rowsAt(100), 3);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
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');
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user