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 settle() async { for (var i = 0; i < 3; i++) { await Future.delayed(Duration.zero); } } Future 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.delayed(Duration.zero); } expect(tab.session.drawn, big.length); expect(screen(tab).split('\n').last, 'line of output'); }); }