Merge branch 'worktree-agent-a2220433c67c864f1'
This commit is contained in:
@@ -28,6 +28,12 @@ class TerminalGateway implements ApiGateway {
|
||||
/// where runs of invalid bytes became one shorter replacement character).
|
||||
final Map<String, int> byteShift = {};
|
||||
|
||||
/// A terminal's state as the server's snapshot: when set, a read from
|
||||
/// before [snapshotFrom] gets it (as of the output's end) instead of raw
|
||||
/// output.
|
||||
final Map<String, String> snapshots = {};
|
||||
int snapshotFrom = 1 << 30;
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => _frames.stream;
|
||||
|
||||
@@ -56,6 +62,19 @@ class TerminalGateway implements ApiGateway {
|
||||
final text = output[id] ?? '';
|
||||
final bytes = utf8.encode(text);
|
||||
final start = after.clamp(0, bytes.length);
|
||||
final snapshot = snapshots[id];
|
||||
if (withOutput && snapshot != null && after < snapshotFrom) {
|
||||
final end = bytes.length + (byteShift[id] ?? 0);
|
||||
return {
|
||||
...terminals[id]!,
|
||||
'stream': stream,
|
||||
'output': snapshot,
|
||||
'output_start': end,
|
||||
'output_end': end,
|
||||
'output_snapshot': true,
|
||||
if (window > 0) 'window': window,
|
||||
};
|
||||
}
|
||||
return {
|
||||
...terminals[id]!,
|
||||
'stream': stream,
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/terminal/terminal_hub.dart';
|
||||
import 'package:pipelines_app/features/terminal/terminal_tab.dart';
|
||||
import 'package:xterm/core.dart';
|
||||
|
||||
import 'terminal_fixtures.dart';
|
||||
|
||||
/// What the server's emulator (internal/termstate) writes for a 40x8
|
||||
/// terminal where a shell ran twelve commands and then nvim, which set
|
||||
/// mouse, bracketed paste and application cursor/keypad modes on the
|
||||
/// alternate screen.
|
||||
const _snapshot =
|
||||
'\x1bc\x1b[3J\x1b]2;nvim notes.txt\x07'
|
||||
'\$ echo 1\r\n1\r\n\$ echo 2\r\n2\r\n\$ echo 3\r\n3\r\n\$ echo 4\r\n4\r\n'
|
||||
'\$ echo 5\r\n5\r\n\$ echo 6\r\n6\r\n\$ echo 7\r\n7\r\n\$ echo 8\r\n8\r\n'
|
||||
'\$ echo 9\r\n9\r\n\$ echo 10\r\n10\r\n\$ echo 11\r\n11\r\n'
|
||||
'\$ echo 12\r\n12\r\n\$ nvim notes.txt\r\n'
|
||||
'\x1b[8;1H\x1b[?1049h\x1b[1;1H\x1b[0;1;34mFRAME\x1b[2;1H\x1b[0mline two'
|
||||
'\x1b[8;1H\x1b[0;7m-- INSERT --\x1b[2;9H\x1b[?1h\x1b=\x1b[?25l'
|
||||
'\x1b[?1002h\x1b[?1006h\x1b[?2004h\x1b[0m';
|
||||
|
||||
/// What nvim writes as it exits, then the shell.
|
||||
const _exit =
|
||||
'\x1b[?2004l\x1b[?1006l\x1b[?1002l\x1b[?1l\x1b>\x1b[?25h\x1b[?1049l'
|
||||
'\$ ls\r\nnotes.txt\r\n\$ ';
|
||||
|
||||
/// The rows on screen (not the scrollback), trailing blanks trimmed.
|
||||
List<String> rows(Terminal terminal) {
|
||||
final buffer = terminal.buffer;
|
||||
final top = buffer.height - terminal.viewHeight;
|
||||
return [
|
||||
for (var y = top; y < buffer.height; y++)
|
||||
buffer.lines[y].getText().trimRight(),
|
||||
];
|
||||
}
|
||||
|
||||
void main() {
|
||||
late TerminalGateway mac;
|
||||
late TerminalHub hub;
|
||||
|
||||
setUp(() {
|
||||
mac = TerminalGateway()..window = 1 << 18;
|
||||
hub = TerminalHub(TestMachines([machine('mac', mac, name: 'Mac')]));
|
||||
});
|
||||
tearDown(() async {
|
||||
hub.dispose();
|
||||
await mac.close();
|
||||
});
|
||||
|
||||
Future<void> settle() async {
|
||||
for (var i = 0; i < 5; i++) {
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
}
|
||||
}
|
||||
|
||||
/// A terminal whose stream is longer than the server's tail.
|
||||
void longTerminal(String raw) {
|
||||
mac.add('term_a', text: raw);
|
||||
mac.terminals['term_a']!['cols'] = 40;
|
||||
mac.terminals['term_a']!['rows'] = 8;
|
||||
}
|
||||
|
||||
void expectNvim(TerminalTab tab) {
|
||||
final terminal = tab.session.terminal;
|
||||
expect(terminal.isUsingAltBuffer, isTrue);
|
||||
expect(rows(terminal), [
|
||||
'FRAME',
|
||||
'line two',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'-- INSERT --',
|
||||
]);
|
||||
expect(terminal.buffer.cursorX, 8);
|
||||
expect(terminal.buffer.cursorY, 1);
|
||||
expect(terminal.mouseMode, MouseMode.upDownScrollDrag);
|
||||
expect(terminal.mouseReportMode, MouseReportMode.sgr);
|
||||
expect(terminal.bracketedPasteMode, isTrue);
|
||||
expect(terminal.cursorKeysMode, isTrue);
|
||||
expect(terminal.appKeypadMode, isTrue);
|
||||
expect(terminal.cursorVisibleMode, isFalse);
|
||||
expect(tab.title, 'nvim notes.txt');
|
||||
}
|
||||
|
||||
void expectShellAfterExit(TerminalTab tab) {
|
||||
final terminal = tab.session.terminal;
|
||||
expect(terminal.isUsingAltBuffer, isFalse);
|
||||
expect(rows(terminal), [
|
||||
r'$ echo 11',
|
||||
'11',
|
||||
r'$ echo 12',
|
||||
'12',
|
||||
r'$ nvim notes.txt',
|
||||
r'$ ls',
|
||||
'notes.txt',
|
||||
r'$',
|
||||
]);
|
||||
// The shell's history above the screen came back with it.
|
||||
expect(terminal.buffer.lines[0].getText().trimRight(), r'$ echo 1');
|
||||
expect(terminal.mouseMode, MouseMode.none);
|
||||
expect(terminal.bracketedPasteMode, isFalse);
|
||||
expect(terminal.cursorKeysMode, isFalse);
|
||||
expect(terminal.cursorVisibleMode, isTrue);
|
||||
}
|
||||
|
||||
test('a reload draws the snapshot: the program on the alternate screen, '
|
||||
'its modes, and the shell underneath once it exits', () async {
|
||||
longTerminal('x' * 3000);
|
||||
mac.snapshots['term_a'] = _snapshot;
|
||||
hub.activate();
|
||||
await settle();
|
||||
final tab = hub.tabs.single;
|
||||
expect(mac.bodies('terminal_output').single['after'], isNull);
|
||||
expect(tab.session.end, 3000);
|
||||
expectNvim(tab);
|
||||
// No "earlier output was dropped" line: the snapshot misses nothing.
|
||||
expect(
|
||||
tab.session.terminal.mainBuffer.lines[0].getText(),
|
||||
isNot(contains('dropped')),
|
||||
);
|
||||
mac.write('term_a', _exit);
|
||||
await settle();
|
||||
expectShellAfterExit(tab);
|
||||
});
|
||||
|
||||
test('after a gap the snapshot replaces what the tab drew, modes '
|
||||
'included', () async {
|
||||
longTerminal('stale\x1b[?1000h\x1b[?1049h');
|
||||
hub.activate();
|
||||
await settle();
|
||||
final tab = hub.tabs.single;
|
||||
expect(tab.session.terminal.mouseMode, MouseMode.upDownScroll);
|
||||
final held = tab.session.end;
|
||||
// The tab falls behind past the server's tail, which now starts later.
|
||||
mac.write('term_a', 'y' * 5000, push: false);
|
||||
mac.snapshots['term_a'] = _snapshot;
|
||||
mac.snapshotFrom = held + 100;
|
||||
final end = held + 5000;
|
||||
mac.pushOutput('term_a', 'z', end, end: end + 1);
|
||||
await settle();
|
||||
expect(mac.bodies('terminal_output').last['after'], held);
|
||||
expectNvim(tab);
|
||||
mac.write('term_a', _exit);
|
||||
await settle();
|
||||
expectShellAfterExit(tab);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user