Files
pipelines/app/test/features/terminal/terminal_snapshot_test.dart
T
alan e13cb99ade Terminals: keep screen state on the server; snapshot on reload and gaps
A headless xterm-compatible emulator (internal/termstate) follows each
machine terminal's PTY output under the same lock as the 1 MiB tail. A
terminal_output from before the retained tail (new page, reload, gap)
now gets output_snapshot: an escape stream that resets the client and
restores scrollback (1000 lines), main screen, the alternate screen when
in use (via ?1049h so the program's exit brings the shell back), saved
cursors, tabs, scroll region, cursor incl. pending wrap, modes (mouse
tracking/encoding, bracketed paste, DECCKM, keypad, autowrap, origin,
focus, ...), charsets, pen and title, as of output_end; frames continue
there. Reads inside the tail stay raw.

No Go library was adequate: charmbracelet/x/vt and vito/midterm scroll at
0.2-4 MB/s and hide modes/pen/region; vt10x is unmaintained. termstate
runs at 160-200 MB/s.

The app treats a snapshot reply as a replay without the dropped-output
line, and every replay now starts with RIS so stale modes end.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 00:56:49 +00:00

150 lines
4.8 KiB
Dart

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);
});
}