import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; 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_run.dart'; import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_session.dart'; import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_view.dart'; import 'package:pipelines_app/features/terminal/terminal_layout.dart'; import 'package:xterm/xterm.dart'; /// A terminal layout saved in memory. class MemoryStore implements TerminalLayoutStore { String? saved; @override Future load() async => saved; @override Future save(String value) async { saved = value; return true; } } /// One live terminal view filling an 800x500 app, with what it sends to the /// process collected in [sent]. class TerminalViewHarness { TerminalViewHarness({TerminalLayout? layout, bool live = true}) : layout = layout ?? TerminalLayout(store: MemoryStore()) { session = ChatShellTerminalSession( ChatShellRun.fromJson({ 'id': 'run_1', 'command': 'sh', 'directory': '/w', 'status': live ? 'running' : 'exited', 'started_at': 1, 'output': '', 'output_bytes': 0, 'terminal': true, 'cols': 80, 'rows': 24, })!, onInput: sent.add, onResize: (cols, rows) => sizes.add((cols, rows)), layout: this.layout, ); } final TerminalLayout layout; late final ChatShellTerminalSession session; final List sent = []; final List<(int, int)> sizes = []; Terminal get terminal => session.terminal; String get typed => sent.join(); Future pump(WidgetTester tester, {bool reduceMotion = false}) async { tester.view.physicalSize = const Size(800, 500); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); addTearDown(session.dispose); await tester.pumpWidget( MaterialApp( home: Builder( builder: (context) => MediaQuery( data: MediaQuery.of( context, ).copyWith(disableAnimations: reduceMotion), child: Scaffold( body: ChatShellTerminalView( session: session, fill: true, autofocus: true, ), ), ), ), ), ); await tester.pump(); } TerminalView view(WidgetTester tester) => tester.widget(find.byType(TerminalView)); TerminalViewState viewState(WidgetTester tester) => tester.state(find.byType(TerminalView)); TerminalController controller(WidgetTester tester) => view(tester).controller!; ScrollPosition scroll(WidgetTester tester) => view(tester).scrollController!.position; /// The global centre of buffer cell ([x], [y]). Offset cellCentre(WidgetTester tester, int x, int y) { final render = viewState(tester).renderTerminal; final cell = render.cellSize; return render.localToGlobal( render.getOffset(CellOffset(x, y)) + Offset(cell.width / 2, cell.height / 2), ); } /// Writes [text] as live output and lets the view redraw. Future write(WidgetTester tester, String text) async { terminal.write(text); await tester.pump(); } /// Focuses the emulator. Future focus(WidgetTester tester) async { final node = view(tester).focusNode!; node.requestFocus(); await tester.pump(); } /// Right-clicks buffer cell ([x], [y]) with a mouse, holding Shift when /// [shift]. Future rightClick( WidgetTester tester, int x, int y, { bool shift = false, }) async { if (shift) await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft); final gesture = await tester.startGesture( cellCentre(tester, x, y), kind: PointerDeviceKind.mouse, buttons: kSecondaryMouseButton, ); await gesture.up(); if (shift) await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft); await tester.pumpAndSettle(); } } /// Presses [key] with [modifiers] held. Future chordKeys( WidgetTester tester, LogicalKeyboardKey key, [ List modifiers = const [], ]) async { for (final modifier in modifiers) { await tester.sendKeyDownEvent(modifier); } await tester.sendKeyEvent(key); for (final modifier in modifiers.reversed) { await tester.sendKeyUpEvent(modifier); } await tester.pump(); } /// The platform's command key: Cmd on Apple platforms, else Ctrl. LogicalKeyboardKey commandKey() => defaultTargetPlatform == TargetPlatform.macOS || defaultTargetPlatform == TargetPlatform.iOS ? LogicalKeyboardKey.metaLeft : LogicalKeyboardKey.controlLeft; bool isApple() => defaultTargetPlatform == TargetPlatform.macOS || defaultTargetPlatform == TargetPlatform.iOS; /// Collects what the app writes to the clipboard, answering reads with /// [paste]. List mockClipboard(WidgetTester tester, {String paste = ''}) { final texts = []; tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( SystemChannels.platform, (call) async { if (call.method == 'Clipboard.setData') { texts.add('${(call.arguments as Map)['text']}'); } if (call.method == 'Clipboard.getData') return {'text': paste}; return null; }, ); addTearDown( () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( SystemChannels.platform, null, ), ); return texts; } /// Desktop platforms: a Mac and one that is not. final desktops = TargetPlatformVariant({ TargetPlatform.macOS, TargetPlatform.linux, });