import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/shell/chat_shell_terminals.dart'; import 'package:pipelines_app/features/terminal/terminal_info.dart'; import 'package:pipelines_app/features/terminal/terminal_tab.dart'; import 'terminal_fixtures.dart'; void main() { testWidgets('answers are marked, typing carries the size, focus activates', ( tester, ) async { final gateway = TerminalGateway(); final tab = TerminalTab( machineId: 'm', machineName: 'm', gateway: gateway, info: TerminalInfo.fromJson(gateway.add('t1')), ); // Before a view lays it out, focus has no size to claim. tab.session.activate(); await tester.pump(); expect(gateway.bodies('terminal_resize'), isEmpty); tab.session.terminal.resize(100, 30); await tester.pump(chatShellResizeDelay); expect(gateway.bodies('terminal_resize'), [ {'terminal_id': 't1', 'cols': 100, 'rows': 30}, ]); // A cursor position query in the output: the answer is marked. tab.frame({'output': '\x1b[6n', 'offset': 0, 'end': 4}); await tester.pump(); // Typing carries the size this client draws at. tab.session.terminal.textInput('ls'); await tester.pump(); expect(gateway.bodies('terminal_input'), [ {'terminal_id': 't1', 'data': '\x1b[1;1R', 'reply': true}, {'terminal_id': 't1', 'data': 'ls', 'cols': 100, 'rows': 30}, ]); // A view gaining focus claims the terminal at once, at its size. tab.session.activate(); await tester.pump(); expect(gateway.bodies('terminal_resize').last, { 'terminal_id': 't1', 'cols': 100, 'rows': 30, 'activate': true, }); // A new connection reports its size again. tab.attach(gateway, TerminalInfo.fromJson(gateway.terminals['t1']!)); await tester.pump(chatShellResizeDelay); expect(gateway.bodies('terminal_resize'), hasLength(3)); expect(gateway.bodies('terminal_resize').last, { 'terminal_id': 't1', 'cols': 100, 'rows': 30, }); tab.dispose(); }); test('the input pipe keeps answers and typing apart, in order', () async { final sent = <(String, bool)>[]; final pipe = ChatShellInputPipe((data, reply) async { sent.add((data, reply)); await Future.delayed(Duration.zero); }, (_) {}); pipe.add('a'); // While the first is in flight, typing, an answer and typing wait. pipe.add('b'); pipe.add('c'); pipe.add('\x1b[1;1R', reply: true); pipe.add('d'); pipe.add('${'x' * chatShellInputChunk}y'); await pumpEventQueue(); expect(sent, [ ('a', false), ('bc', false), ('\x1b[1;1R', true), ('d${'x' * (chatShellInputChunk - 1)}', false), ('xy', false), ]); }); test('a refusal drops what was waiting and reports once', () async { final failures = []; final sent = []; final pipe = ChatShellInputPipe((data, reply) async { sent.add(data); await Future.delayed(Duration.zero); throw StateError('not running'); }, failures.add); pipe.add('a'); pipe.add('b'); pipe.add('?', reply: true); await pumpEventQueue(); expect(sent, ['a']); expect(failures, hasLength(1)); }); }