import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/terminal/terminal_hub.dart'; import 'package:pipelines_app/features/terminal/terminal_layout.dart'; import 'terminal_app.dart'; import 'terminal_fixtures.dart'; /// The first show of an app run starts one terminal (the machine's login /// prompt); toggling afterwards only hides and shows it. void main() { late TerminalGateway mac; late TerminalHub hub; late TerminalLayout layout; setUp(() { mac = TerminalGateway(); hub = TerminalHub(TestMachines([machine('mac', mac, name: 'Mac')])); layout = TerminalLayout(store: MemoryLayoutStore()); }); tearDown(() async { hub.dispose(); await mac.close(); }); Future button(WidgetTester tester) async { await tester.tap(find.byKey(const ValueKey('shell.terminal'))); await tester.pumpAndSettle(); } for (final mode in TerminalMode.values) { testWidgets('the first show opens exactly one terminal in the ' '${mode.label.toLowerCase()}; toggling opens no more', (tester) async { layout.setMode(mode); await pumpTerminalApp(tester, hub: hub, layout: layout); expect(mac.bodies('terminal_open'), isEmpty); await button(tester); expect(layout.visible, isTrue); expect(mac.bodies('terminal_open'), hasLength(1)); expect(hub.tabs, hasLength(1)); final first = hub.tabs.single; for (var i = 0; i < 6; i++) { await button(tester); } expect(layout.visible, isTrue); expect(mac.bodies('terminal_open'), hasLength(1)); expect(identical(hub.tabs.single, first), isTrue); // Hiding never closes it: it stays logged in. expect(mac.bodies('terminal_close'), isEmpty); expect(first.info.running, isTrue); }); } testWidgets('closing every tab and showing again opens none', (tester) async { await pumpTerminalApp(tester, hub: hub, layout: layout); await button(tester); await hub.close(hub.tabs.single); await tester.pumpAndSettle(); expect(hub.tabs, isEmpty); await button(tester); await button(tester); expect(layout.visible, isTrue); expect(mac.bodies('terminal_open'), hasLength(1)); expect(find.text('New terminal'), findsWidgets); }); testWidgets('a terminal still running from before is shown, not added ' 'to', (tester) async { mac.add('term_kept'); await pumpTerminalApp(tester, hub: hub, layout: layout); await button(tester); await button(tester); await button(tester); expect(mac.bodies('terminal_open'), isEmpty); expect(hub.tabs.single.info.id, 'term_kept'); }); testWidgets('with only ended terminals (the server restarted) the first ' 'show opens one login', (tester) async { mac ..add('term_old') ..terminals['term_old']!['status'] = 'exited'; await pumpTerminalApp(tester, hub: hub, layout: layout); await button(tester); await button(tester); await button(tester); expect(mac.bodies('terminal_open'), hasLength(1)); expect(hub.tabs, hasLength(2)); }); testWidgets('a terminal opened by hand first counts as the first', ( tester, ) async { await pumpTerminalApp(tester, hub: hub, layout: layout); expect(await hub.open(), isNull); await button(tester); await button(tester); await button(tester); expect(mac.bodies('terminal_open'), hasLength(1)); // Each New terminal is its own login. expect(await hub.open(), isNull); expect(mac.bodies('terminal_open'), hasLength(2)); }); testWidgets('showing twice quickly opens one', (tester) async { await pumpTerminalApp(tester, hub: hub, layout: layout); await tester.tap(find.byKey(const ValueKey('shell.terminal'))); await tester.pump(); await tester.tap(find.byKey(const ValueKey('shell.terminal'))); await tester.pump(); await tester.tap(find.byKey(const ValueKey('shell.terminal'))); await tester.pumpAndSettle(); expect(mac.bodies('terminal_open'), hasLength(1)); }); testWidgets('on a narrow window the page opens one and reopening adds ' 'none', (tester) async { await pumpTerminalApp( tester, hub: hub, layout: layout, size: const Size(390, 800), ); for (var i = 0; i < 3; i++) { await button(tester); expect(find.widgetWithText(AppBar, 'Terminal'), findsOneWidget); await tester.pageBack(); await tester.pumpAndSettle(); } expect(mac.bodies('terminal_open'), hasLength(1)); expect(hub.tabs, hasLength(1)); }); }