Files
pipelines/app/test/features/terminal/terminal_flow_test.dart
T
alan 0963a071d0 Terminals: flow-controlled output, immediate echo, sliced emulator writes
Machine terminals no longer skip output for live viewers. Each connection's
subscription is a cursor into the 1 MiB retained tail, drained by the
connection's writer as soon as the PTY reader writes (no 50 ms tick; a
streaming frame gathers for at most 2 ms). A subscription opened with
flow: true gets window (256 KiB) in its reply and acknowledges drawn output
with terminal_ack; while one is full the reader stops reading the PTY, so
the program waits. A subscription full for 3 s without acking stops holding
the terminal and later sees a gap; a gone connection drops its
subscriptions. Clients that never ask for flow keep the old pacing (newest
64 KiB per 50 ms). terminal_open/terminal_output replies precede the frames
that follow them, which start exactly at the reply's output_end.

Chat shell runs share the buffer code: output is pushed as it arrives,
oldest first, while the chat's watchers keep short queues; 256 KiB unpushed
stops the reader, and watchers behind for 3 s get the old pacing.

The app asks for flow, takes offsets from the server (frame end, reply
output_end) instead of re-encoded text length, acks per quarter window, and
writes replays and bursts into the emulator in slices that yield to the
event loop (ChatShellTerminalFeed, shared with chat shells).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 23:57:18 +00:00

104 lines
3.1 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 'terminal_fixtures.dart';
String screen(TerminalTab tab) {
final buffer = tab.session.terminal.buffer;
return [
for (var y = 0; y < buffer.height; y++)
buffer.lines[y].getText().trimRight(),
].where((line) => line.isNotEmpty).join('\n');
}
void main() {
late TerminalGateway mac;
late TerminalHub hub;
setUp(() {
mac = TerminalGateway();
hub = TerminalHub(TestMachines([machine('mac', mac, name: 'Mac')]));
});
tearDown(() async {
hub.dispose();
await mac.close();
});
Future<void> settle() async {
for (var i = 0; i < 3; i++) {
await Future<void>.delayed(Duration.zero);
}
}
Future<TerminalTab> opened(String text) async {
mac.add('term_a', text: text);
hub.activate();
await settle();
return hub.tabs.single;
}
test('offsets come from the server, not the re-encoded text', () async {
// The server replaced two invalid bytes with one U+FFFD (three bytes):
// its stream holds 3 bytes where the text re-encodes to 4.
mac.byteShift['term_a'] = -1;
final tab = await opened('a�');
expect(tab.session.end, 3);
mac.pushOutput('term_a', 'b', 3);
await settle();
expect(screen(tab), 'a�b');
expect(tab.session.end, 4);
// A continuation, not a gap: nothing is read again.
expect(mac.bodies('terminal_output'), hasLength(1));
});
test('reads ask for flow; what is drawn is acknowledged per quarter '
'window', () async {
mac.window = 1024;
final tab = await opened('');
expect(mac.bodies('terminal_output').single['flow'], isTrue);
var end = 0;
void push(int bytes) {
mac.pushOutput('term_a', 'x' * bytes, end);
end += bytes;
}
push(300);
await settle();
expect(mac.bodies('terminal_ack'), [
{'terminal_id': 'term_a', 'after': 300},
]);
push(100);
await settle();
expect(mac.bodies('terminal_ack'), hasLength(1));
push(200);
await settle();
expect(mac.bodies('terminal_ack').last, {
'terminal_id': 'term_a',
'after': 600,
});
expect(tab.session.drawn, 600);
});
test('an older server (no window) is never acknowledged', () async {
await opened('');
mac.pushOutput('term_a', 'x' * 100000, 0);
await settle();
expect(mac.bodies('terminal_ack'), isEmpty);
});
test('a large retained tail is drawn over several turns', () async {
final big = List.filled(40000, 'line of output\r\n').join();
final tab = await opened('');
mac.output['term_a'] = big;
await tab.resume();
// The read is drawn in slices: not all of it in the turn it arrived.
expect(tab.session.drawn, lessThan(tab.session.end));
for (var turn = 0; turn < 1000 && tab.session.drawn < big.length; turn++) {
await Future<void>.delayed(Duration.zero);
}
expect(tab.session.drawn, big.length);
expect(screen(tab).split('\n').last, 'line of output');
});
}