import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_keys.dart'; import 'package:pipelines_app/features/settings/settings_terminal_section.dart'; import 'package:pipelines_app/features/terminal/terminal_hub.dart'; import 'package:pipelines_app/features/terminal/terminal_layout.dart'; import 'package:pipelines_app/features/terminal/terminal_toggle_button.dart'; import 'package:xterm/xterm.dart'; import 'terminal_app.dart'; import 'terminal_fixtures.dart'; void main() { late TerminalGateway mac; late TerminalHub hub; late MemoryLayoutStore store; late TerminalLayout layout; setUp(() { mac = TerminalGateway() ..add('term_a', cwd: '/Users/op/src', text: 'alpha\r\n') ..add('term_b', cwd: '/Users/op/docs', text: 'beta\r\n'); hub = TerminalHub(TestMachines([machine('mac', mac, name: 'Mac')])); store = MemoryLayoutStore(); layout = TerminalLayout(store: store); }); tearDown(() async { hub.dispose(); await mac.close(); }); Future pumpApp( WidgetTester tester, { Size size = const Size(1200, 800), bool onChat = true, }) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); await tester.pumpWidget( MaterialApp( home: TerminalTestApp(hub: hub, layout: layout, shortcutHere: onChat), ), ); await tester.pump(); } Future ctrlBacktick(WidgetTester tester) async { await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); await tester.sendKeyEvent(LogicalKeyboardKey.backquote); await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); await tester.pumpAndSettle(); } Map saved() => jsonDecode(store.saved!) as Map; desktopTestWidgets( 'Ctrl+` and the bar button toggle the bottom panel; hiding ' 'keeps every shell', (tester) async { await pumpApp(tester); expect(find.byKey(const ValueKey('terminal-panel')), findsNothing); await ctrlBacktick(tester); expect(find.byKey(const ValueKey('terminal-panel')), findsOneWidget); expect(find.text('src'), findsOneWidget); expect(find.text('docs'), findsOneWidget); expect(saved()['visible'], isTrue); // The terminal has the keyboard, yet the shortcut still hides it. await tester.tap(find.byType(TerminalView)); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.keyX); await tester.pump(); expect(mac.bodies('terminal_input').last['data'], 'x'); await ctrlBacktick(tester); expect(find.byKey(const ValueKey('terminal-panel')), findsNothing); expect(hub.tabs, hasLength(2)); expect(mac.bodies('terminal_close'), isEmpty); await tester.tap(find.byTooltip(TerminalToggleButton.label)); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('terminal-panel')), findsOneWidget); // Reopening draws the same emulator, output and all. expect(find.byType(TerminalView), findsOneWidget); }, ); desktopTestWidgets( 'the panel resizes from its top edge and remembers its height', (tester) async { await pumpApp(tester); await ctrlBacktick(tester); final before = tester .getSize(find.byKey(const ValueKey('terminal-panel'))) .height; expect(before, terminalPanelDefaultHeight); await tester.drag( find.byKey(const ValueKey('terminal-panel-resize')), const Offset(0, -120), ); await tester.pumpAndSettle(); final after = tester .getSize(find.byKey(const ValueKey('terminal-panel'))) .height; expect(after, closeTo(before + 120, 1)); expect(saved()['panel_height'], closeTo(after, 1)); // The app above shrinks rather than hiding under it. final field = tester.getRect(find.byType(Scaffold).first); expect(field.bottom, closeTo(800 - after, 1)); final restored = TerminalLayout(store: store); await restored.restore(); expect(restored.panelHeight, closeTo(after, 1)); expect(restored.visible, isTrue); // The resized terminal tells the server its new size. await tester.pump(const Duration(milliseconds: 200)); expect(mac.bodies('terminal_resize'), isNotEmpty); }, ); desktopTestWidgets( 'the floating terminal is a window: it moves, resizes, maximises, and ' 'Minimise and Close hide the terminal', (tester) async { layout ..setMode(TerminalMode.float) ..show(); await pumpApp(tester); await tester.pumpAndSettle(); expect(floatFinder, findsOneWidget); final manager = terminalWindowManager(tester); Rect frame() => manager.windows.single.frame; expect(frame(), terminalFloatDefaultRect); final start = frame(); await tester.drag( find.byKey(const ValueKey('window-title')), const Offset(60, 40), ); await tester.pumpAndSettle(); expect(frame().topLeft, start.topLeft + const Offset(60, 40)); await tester.drag( find.byKey(const ValueKey('window-resize')), const Offset(80, 50), ); await tester.pumpAndSettle(); expect(frame().size, start.size + const Offset(80, 50)); // The window shows the terminal's tabs and the selected terminal. expect(find.text('src'), findsWidgets); expect(find.byType(TerminalView), findsOneWidget); await tester.tap(find.byTooltip('Maximise')); await tester.pumpAndSettle(); expect(manager.windows.single.maximized, isTrue); await tester.tap(find.byTooltip('Restore size')); await tester.pumpAndSettle(); // Minimise hides the terminal; the toggle brings the window back // where it was, with its shells. await tester.tap(find.byTooltip('Minimise')); await tester.pumpAndSettle(); expect(layout.visible, isFalse); expect(floatFinder, findsNothing); expect(hub.tabs, hasLength(2)); await ctrlBacktick(tester); expect(layout.visible, isTrue); expect(floatFinder, findsOneWidget); expect(frame().size, start.size + const Offset(80, 50)); await tester.tap(find.byTooltip('Close')); await tester.pumpAndSettle(); expect(layout.visible, isFalse); expect(manager.windows, isEmpty); expect(hub.tabs, hasLength(2)); await ctrlBacktick(tester); expect(floatFinder, findsOneWidget); expect(frame().size, start.size + const Offset(80, 50)); }, ); desktopTestWidgets( 'the first window takes the frame the terminal saved before windows', (tester) async { store.saved = jsonEncode({ 'mode': 'float', 'visible': true, 'panel_height': 300, 'float': [200, 150, 640, 360], 'minimized': false, 'maximized': true, }); await layout.restore(); await pumpApp(tester); await tester.pumpAndSettle(); final entry = terminalWindowManager(tester).windows.single; expect(entry.frame, const Rect.fromLTWH(200, 150, 640, 360)); expect(entry.maximized, isTrue); // The old record is still written, unchanged, for an older build. layout.hide(); await tester.pumpAndSettle(); expect(saved()['float'], [200, 150, 640, 360]); }, ); desktopTestWidgets( 'tabs select and close their terminals; New terminal opens one', (tester) async { layout.show(); await pumpApp(tester); await tester.pumpAndSettle(); expect(hub.selected!.info.id, 'term_a'); await tester.tap(find.text('docs')); await tester.pumpAndSettle(); expect(hub.selected!.info.id, 'term_b'); await tester.tap(find.byTooltip('New terminal')); await tester.pumpAndSettle(); expect(mac.bodies('terminal_open'), hasLength(1)); expect(hub.tabs, hasLength(3)); expect(hub.selected!.info.id, 'term_1'); await tester.tap(find.byTooltip('Close terminal').first); await tester.pumpAndSettle(); expect(mac.bodies('terminal_close').single['terminal_id'], 'term_a'); expect(find.text('src'), findsNothing); }, ); desktopTestWidgets('the options menu switches between panel and window', ( tester, ) async { layout.show(); await pumpApp(tester); await tester.tap(find.byTooltip('Terminal options')); await tester.pumpAndSettle(); await tester.tap(find.byKey(const ValueKey('terminal-mode-float'))); await tester.pumpAndSettle(); expect(layout.mode, TerminalMode.float); expect(floatFinder, findsOneWidget); expect(find.byKey(const ValueKey('terminal-panel')), findsNothing); expect(saved()['mode'], 'float'); await tester.tap(find.byTooltip('Terminal options')); await tester.pumpAndSettle(); await tester.tap(find.byKey(const ValueKey('terminal-hide'))); await tester.pumpAndSettle(); expect(floatFinder, findsNothing); }); desktopTestWidgets('Settings chooses the mode', (tester) async { await tester.pumpWidget( MaterialApp( home: Scaffold( body: SingleChildScrollView( child: SettingsTerminalSection(layout: layout), ), ), ), ); expect(find.text('Terminal'), findsOneWidget); await tester.tap(find.byKey(const ValueKey('settings-terminal-float'))); await tester.pumpAndSettle(); expect(layout.mode, TerminalMode.float); await tester.tap(find.byKey(const ValueKey('settings-terminal-panel'))); await tester.pumpAndSettle(); expect(layout.mode, TerminalMode.panel); }); desktopTestWidgets( 'a narrow window opens the terminal full screen with the key ' 'toolbar', (tester) async { layout.show(); await pumpApp(tester, size: const Size(390, 800)); // The panel is not drawn on a phone even when left shown. expect(find.byKey(const ValueKey('terminal-panel')), findsNothing); await tester.tap(find.byTooltip(TerminalToggleButton.label)); await tester.pumpAndSettle(); expect(find.widgetWithText(AppBar, 'Terminal'), findsOneWidget); expect(find.byType(TerminalView), findsOneWidget); expect(find.byType(ChatShellTerminalKeys), findsOneWidget); await tester.tap(find.byKey(const Key('chat-shell-key-ctrl-c'))); await tester.pump(); expect(mac.bodies('terminal_input').last['data'], '\x03'); await tester.pageBack(); await tester.pumpAndSettle(); expect(find.byType(TerminalView), findsNothing); expect(hub.tabs, hasLength(2)); }, ); desktopTestWidgets( 'a phone with running shells shows a shortcut below the top bar that ' 'reopens the terminal', (tester) async { await pumpApp(tester, size: const Size(390, 800)); final bubble = find.byKey(const Key('terminal-shortcut-bubble')); // The shells load when the terminal is first shown. await tester.tap(find.byTooltip(TerminalToggleButton.label)); await tester.pumpAndSettle(); expect(bubble, findsNothing, reason: 'hidden while the page is open'); await tester.pageBack(); await tester.pumpAndSettle(); expect(bubble, findsOneWidget); expect(find.bySemanticsLabel('Open terminal, 2 running'), findsOneWidget); final rect = tester.getRect(bubble); expect(rect.right, greaterThan(390 - 80), reason: 'on the right'); expect(rect.top, greaterThan(40), reason: 'below the top bar'); expect(rect.top, lessThan(160)); await tester.tap(bubble); await tester.pumpAndSettle(); expect(find.widgetWithText(AppBar, 'Terminal'), findsOneWidget); expect(bubble, findsNothing); // Wide windows use the panel instead, so no shortcut there. await tester.pageBack(); await tester.pumpAndSettle(); tester.view.physicalSize = const Size(1200, 800); await tester.pumpAndSettle(); expect(bubble, findsNothing); }, ); desktopTestWidgets('the phone shortcut shows on the Chat tab only', ( tester, ) async { await pumpApp(tester, size: const Size(390, 800), onChat: false); final bubble = find.byKey(const Key('terminal-shortcut-bubble')); expect(bubble, findsNothing, reason: 'not on Pipelines, Runs or Search'); await pumpApp(tester, size: const Size(390, 800)); await tester.pumpAndSettle(); // Shown on Chat even before any shell runs; tapping opens the terminal. expect(hub.tabs, isEmpty); expect(bubble, findsOneWidget, reason: 'shown on Chat'); expect(find.bySemanticsLabel('Open terminal'), findsOneWidget); await tester.tap(bubble); await tester.pumpAndSettle(); expect(find.widgetWithText(AppBar, 'Terminal'), findsOneWidget); expect(bubble, findsNothing, reason: 'hidden while the page is open'); }); } /// Hardware keys reach the terminal on desktop; phones type through the /// on-screen keyboard instead. These tests drive desktop key events. void desktopTestWidgets(String description, WidgetTesterCallback callback) => testWidgets( description, callback, variant: TargetPlatformVariant.only(TargetPlatform.macOS), );