87519985bf
The shortcut no longer waits for a running shell: on the Chat page it opens the terminal, starting one at its login prompt when none is open, and shows a count when more than one shell runs.
322 lines
12 KiB
Dart
322 lines
12 KiB
Dart
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_float.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_surface.dart';
|
|
import 'package:pipelines_app/features/terminal/terminal_toggle_button.dart';
|
|
import 'package:xterm/xterm.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<void> 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: TerminalSurface(
|
|
hub: hub,
|
|
layout: layout,
|
|
cupertino: false,
|
|
shortcutHere: onChat,
|
|
child: Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('App'),
|
|
actions: [TerminalToggleButton(layout: layout, cupertino: false)],
|
|
),
|
|
body: const TextField(key: Key('app-field')),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
await tester.pump();
|
|
}
|
|
|
|
Future<void> ctrlBacktick(WidgetTester tester) async {
|
|
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
|
await tester.sendKeyEvent(LogicalKeyboardKey.backquote);
|
|
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
Map<String, dynamic> saved() =>
|
|
jsonDecode(store.saved!) as Map<String, dynamic>;
|
|
|
|
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 window moves, resizes, minimises and maximises',
|
|
(tester) async {
|
|
layout
|
|
..setMode(TerminalMode.float)
|
|
..show();
|
|
await pumpApp(tester);
|
|
await tester.pump();
|
|
final window = find.byKey(const ValueKey('terminal-float'));
|
|
expect(window, findsOneWidget);
|
|
final start = layout.floatRect;
|
|
await tester.drag(
|
|
find.byKey(const ValueKey('terminal-float-title')),
|
|
const Offset(60, 40),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
expect(layout.floatRect.topLeft, start.topLeft + const Offset(60, 40));
|
|
await tester.drag(
|
|
find.byKey(const ValueKey('terminal-float-resize')),
|
|
const Offset(80, 50),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
expect(layout.floatRect.size, start.size + const Offset(80, 50));
|
|
expect(saved()['float'][2], closeTo(start.width + 80, 1));
|
|
await tester.tap(find.byTooltip('Minimise'));
|
|
await tester.pumpAndSettle();
|
|
expect(find.byType(TerminalView), findsNothing);
|
|
expect(hub.tabs, hasLength(2));
|
|
await tester.tap(find.byTooltip('Restore'));
|
|
await tester.pumpAndSettle();
|
|
expect(find.byType(TerminalView), findsOneWidget);
|
|
await tester.tap(find.byTooltip('Maximise'));
|
|
await tester.pumpAndSettle();
|
|
expect(
|
|
tester.getRect(
|
|
find.descendant(of: window, matching: find.byType(Material)).first,
|
|
),
|
|
const Rect.fromLTWH(0, 0, 1200, 800).deflate(terminalFloatMargin),
|
|
);
|
|
await tester.tap(find.byTooltip('Restore size'));
|
|
await tester.pumpAndSettle();
|
|
expect(layout.maximized, isFalse);
|
|
},
|
|
);
|
|
|
|
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(find.byKey(const ValueKey('terminal-float')), 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(find.byKey(const ValueKey('terminal-float')), 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),
|
|
);
|