Files
pipelines/app/test/features/chat/chat_floating_pane_test.dart
alan 4668deeaf7 Focus floating chat from terminal shortcut
Raise the most recently used chat when Ctrl+Y is invoked from another window, then rely on the existing host lifecycle to focus its composer. Preserve toggle behavior while chat is active and retain the Apple shortcut aliases.
2026-11-11 19:40:30 +00:00

514 lines
18 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/core/servers/server_scope.dart';
import 'package:pipelines_app/features/chat/chat_runtime.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'package:pipelines_app/features/chat/screen.dart';
import 'package:pipelines_app/features/chat/windows/chat_windows.dart';
import 'package:pipelines_app/features/chat/workspace/chat_float_store.dart';
import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart';
import 'package:pipelines_app/features/chat/workspace/chat_workspace_store.dart';
import 'package:pipelines_app/shell/windows/window_frame.dart';
import 'package:pipelines_app/shell/windows/window_host.dart';
import 'package:pipelines_app/shell/windows/window_kind.dart';
import 'package:pipelines_app/shell/windows/window_manager.dart';
import 'package:pipelines_app/shell/windows/window_store.dart';
import 'package:pipelines_app/shell/windows/window_entry.dart';
import 'package:pipelines_app/ui/motion/fade_through_stack.dart';
import 'package:pipelines_app/ui/input/keyboard_owner.dart';
import 'conversation/chat_thread_fixtures.dart';
class _Gateway extends FakeChatGateway {
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type == 'chat_sessions') {
return Future.value({
'chat_sessions': [
for (final id in ['a', 'b', 'c']) {'id': id, 'title': 'Chat $id'},
],
});
}
if (type == 'chat_shells') return Future.value({'chat_shells': []});
return super.call(type, fields);
}
}
class _Store implements WindowStore {
String? value;
@override
Future<String?> read() async => value;
@override
Future<bool> write(String record) async {
value = record;
return true;
}
}
class _TerminalKind extends WindowKind {
@override
String get key => 'terminal';
@override
bool get serverScoped => false;
@override
IconData get icon => Icons.terminal;
@override
String title(BuildContext context, WindowEntry entry) => entry.contentId;
@override
Widget build(BuildContext context, WindowEntry entry) => const SizedBox();
}
Finder _thread(String id) => find.byWidgetPredicate(
(widget) => widget is ChatThreadView && widget.sessionId == id,
skipOffstage: false,
);
Finder _input(String id) => find.descendant(
of: find.byKey(ValueKey('chat-composer-$id'), skipOffstage: false),
matching: find.byType(EditableText, skipOffstage: false),
skipOffstage: false,
);
ChatWorkspaceController _workspace(WidgetTester tester) => tester
.widget<ChatWorkspace>(find.byType(ChatWorkspace, skipOffstage: false))
.controller;
/// The window whose title is [title].
Finder _window(String title) =>
find.ancestor(of: find.text(title), matching: find.byType(WindowFrame));
/// That window's title bar, where it shows and takes input.
Finder _shown(String title) => find
.descendant(
of: _window(title),
matching: find.byKey(const ValueKey('window-title')),
)
.hitTestable();
/// The app in miniature: the Chat destination beside another one, and the
/// window host above both, with app-lifetime chat state.
class _App {
_App(this.store);
final _Store store;
final gateway = _Gateway()..history = (_) async => chatPage([]);
final destination = ValueNotifier(0);
final chatBuilt = ValueNotifier(true);
final opened = <String>[];
late final ChatRuntime runtime;
late final WindowManager manager;
late final ChatWindows windows;
final terminalFocus = FocusNode(debugLabel: 'test terminal');
final terminalTyped = <LogicalKeyboardKey>[];
Future<void> start(
WidgetTester tester, {
Size size = const Size(1800, 1000),
bool terminalSurface = false,
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
bindServerIdentity(gateway, 'server-a');
runtime = ChatRuntime(gateway: gateway, serverKey: 'server-a');
final kinds = WindowKinds()..register(_TerminalKind());
manager = WindowManager(kinds: kinds, store: store, server: 'server-a');
windows = ChatWindows(
manager: manager,
runtime: runtime,
openInChat: opened.add,
)..start();
await manager.restore();
await windows.migrate(
const SharedPreferencesChatFloatStore(serverScope: 'server-a'),
);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Stack(
fit: StackFit.expand,
children: [
ListenableBuilder(
listenable: Listenable.merge([destination, chatBuilt]),
builder: (context, _) => FadeThroughStack(
index: destination.value,
children: [
if (chatBuilt.value)
ChatScreen(
gateway: gateway,
persistWorkspace: true,
runtime: runtime,
windows: windows,
)
else
const Center(child: Text('Not connected')),
const Center(child: Text('Runs page')),
],
),
),
if (terminalSurface)
Positioned.fill(
child: TypingSurface(
child: Focus(
focusNode: terminalFocus,
onKeyEvent: (node, event) {
if (event is KeyDownEvent) {
terminalTyped.add(event.logicalKey);
}
return KeyEventResult.handled;
},
child: const SizedBox(),
),
),
),
Positioned.fill(child: WindowHost(manager: manager)),
],
),
),
),
);
await tester.pumpAndSettle();
}
Future<void> stop(WidgetTester tester) async {
await tester.pumpWidget(const SizedBox.shrink());
windows.dispose();
runtime.dispose();
manager.dispose();
destination.dispose();
chatBuilt.dispose();
terminalFocus.dispose();
gateway.close();
}
List<String> get floating => [for (final w in windows.windows) w.contentId];
}
/// Opens [session]'s pane menu (wherever it is) and chooses [label].
Future<void> _menu(WidgetTester tester, String session, String label) async {
await tester.tap(
find
.descendant(
of: _thread(session),
matching: find.byTooltip('More actions'),
)
.hitTestable(),
);
await tester.pumpAndSettle();
await tester.tap(find.text(label).last);
await tester.pumpAndSettle();
}
Future<void> _saved(String a, String b, [String? c]) async {
final layout = ChatWorkspaceController()
..open(a)
..splitActive(b, ChatSplitAxis.horizontal);
if (c != null) layout.splitActive(c, ChatSplitAxis.horizontal);
SharedPreferences.setMockInitialValues({
scopedServerKey(chatWorkspacePreferenceKey, 'server-a'): layout
.snapshot()!
.encode(),
});
}
Future<void> _chord(WidgetTester tester) async {
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyY);
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
await tester.pumpAndSettle();
}
Future<void> _controlChord(WidgetTester tester) async {
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyY);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pumpAndSettle();
}
void main() {
testWidgets('Float and Dock move the live chat with its draft', (
tester,
) async {
await _saved('a', 'b');
final app = _App(_Store());
await app.start(tester);
final b = tester.state(_thread('b'));
await tester.enterText(_input('b'), 'Draft B');
await _menu(tester, 'b', 'Float (Alt+Y)');
final controller = _workspace(tester);
expect(app.floating, ['b']);
expect(controller.panes.map((p) => p.sessionId), ['a']);
expect(tester.state(_thread('b')), same(b));
expect(tester.widget<EditableText>(_input('b')).controller.text, 'Draft B');
// A window over the app, titled by its chat, not a pane.
expect(tester.getSize(_window('Chat b')).width, lessThan(1800 / 2));
expect(
find.descendant(of: _window('Chat b'), matching: find.byTooltip('Dock')),
findsOneWidget,
);
// The lone tiled pane cannot float: the tiling would be empty.
await tester.tap(
find
.descendant(
of: _thread('a'),
matching: find.byTooltip('More actions'),
)
.hitTestable(),
);
await tester.pumpAndSettle();
expect(find.textContaining('Float'), findsNothing);
await tester.tapAt(const Offset(5, 995));
await tester.pumpAndSettle();
await tester.tap(find.byTooltip('Dock'));
await tester.pumpAndSettle();
expect(app.floating, isEmpty);
expect(controller.panes.map((p) => p.sessionId), ['a', 'b']);
expect(tester.state(_thread('b')), same(b));
expect(tester.widget<EditableText>(_input('b')).controller.text, 'Draft B');
expect(
app.gateway.calls.where(
(call) => ['chat_close', 'chat_interrupt'].contains(call.type),
),
isEmpty,
);
expect(tester.takeException(), isNull);
await app.stop(tester);
});
testWidgets('X closes the window and returns nothing to the tiling', (
tester,
) async {
await _saved('a', 'b');
final app = _App(_Store());
await app.start(tester);
await _menu(tester, 'b', 'Float (Alt+Y)');
await tester.tap(find.byTooltip('Close'));
await tester.pumpAndSettle();
expect(app.floating, isEmpty);
expect(_workspace(tester).panes.map((p) => p.sessionId), ['a']);
expect(_thread('b'), findsNothing);
expect(app.gateway.calls.where((c) => c.type == 'chat_close'), isEmpty);
await app.stop(tester);
});
testWidgets('popups from a floating chat open above its window', (
tester,
) async {
await _saved('a', 'b');
final app = _App(_Store());
await app.start(tester);
await _menu(tester, 'b', 'Float (Alt+Y)');
// The More actions menu, opened from inside the window.
await tester.tap(
find
.descendant(
of: _thread('b'),
matching: find.byTooltip('More actions'),
)
.hitTestable(),
);
await tester.pumpAndSettle();
expect(find.text('Chat details').hitTestable(), findsOneWidget);
// Chat details, which used to open behind the window: a dialog on the
// root navigator, over the window.
await tester.tap(find.text('Chat details'));
await tester.pumpAndSettle();
expect(find.byType(Dialog), findsOneWidget);
expect(find.text('Chat information').hitTestable(), findsOneWidget);
expect(_shown('Chat b'), findsNothing);
expect(tester.takeException(), isNull);
await app.stop(tester);
});
testWidgets('windows and their frames survive a reload, without the Chat '
'destination being built', (tester) async {
await _saved('a', 'b');
final store = _Store();
var app = _App(store);
await app.start(tester);
await _menu(tester, 'b', 'Float (Alt+Y)');
await tester.drag(
find.descendant(
of: _window('Chat b'),
matching: find.byKey(const ValueKey('window-title')),
),
const Offset(200, 40),
);
await tester.pumpAndSettle();
await tester.tap(find.byTooltip('Minimise'));
await tester.pumpAndSettle();
expect(_shown('Chat b'), findsNothing);
await app.stop(tester);
app = _App(store)..chatBuilt.value = false;
await app.start(tester);
expect(find.text('Not connected'), findsOneWidget);
final entry = app.windows.windows.single;
expect(entry.contentId, 'b');
expect(entry.minimized, isTrue);
expect(entry.frame.left, chatFloatDefaultRect.left + 200);
await _chord(tester);
expect(_shown('Chat b'), findsOneWidget);
expect(tester.getTopLeft(_window('Chat b')).dx, entry.frame.left);
expect(_thread('b').hitTestable(), findsOneWidget);
// Dock with no tiling opens the chat in the Chat destination.
await tester.tap(find.byTooltip('Dock'));
await tester.pumpAndSettle();
expect(app.opened, ['b']);
expect(app.floating, isEmpty);
await app.stop(tester);
});
testWidgets('several chats float; the chord hides and shows them all '
'where they were', (tester) async {
await _saved('a', 'b', 'c');
final app = _App(_Store());
await app.start(tester);
await _menu(tester, 'b', 'Float (Alt+Y)');
await _menu(tester, 'c', 'Float (Alt+Y)');
final controller = _workspace(tester);
expect(app.floating, ['b', 'c']);
expect(controller.panes.map((p) => p.sessionId), ['a']);
// The second window opens stepped from the first, so both show.
final first = tester.getTopLeft(_window('Chat b'));
final second = tester.getTopLeft(_window('Chat c'));
expect(second.dx, greaterThan(first.dx));
await _chord(tester);
expect(
find.byKey(const ValueKey('window-title')).hitTestable(),
findsNothing,
);
await _chord(tester);
expect(tester.getTopLeft(_window('Chat b')), first);
expect(tester.getTopLeft(_window('Chat c')), second);
// Docking one leaves the other floating.
await tester.tap(
find.descendant(of: _window('Chat b'), matching: find.byTooltip('Dock')),
);
await tester.pumpAndSettle();
expect(app.floating, ['c']);
expect(controller.panes.map((p) => p.sessionId), ['a', 'b']);
expect(tester.takeException(), isNull);
await app.stop(tester);
});
testWidgets('a floating chat types, runs its tickers, hides and shows over '
'another destination', (tester) async {
await _saved('a', 'b');
final app = _App(_Store());
await app.start(tester);
await _menu(tester, 'b', 'Float (Alt+Y)');
app.destination.value = 1;
await tester.pumpAndSettle();
expect(find.text('Runs page'), findsOneWidget);
expect(_shown('Chat b'), findsOneWidget);
// Not muted by the hidden Chat destination.
expect(TickerMode.valuesOf(tester.element(_thread('b'))).enabled, isTrue);
// The composer takes the keyboard there.
final input = _input('b');
await tester.tapAt(tester.getCenter(input));
await tester.pump();
final field = tester.widget<EditableText>(input);
expect(field.focusNode.hasFocus, isTrue);
tester.testTextInput.enterText('From runs');
await tester.pump();
expect(field.controller.text, 'From runs');
await _chord(tester);
expect(_shown('Chat b'), findsNothing);
await _chord(tester);
expect(_shown('Chat b'), findsOneWidget);
// Shown again by the chord, the composer has the keyboard at once.
expect(tester.widget<EditableText>(_input('b')).focusNode.hasFocus, isTrue);
expect(tester.takeException(), isNull);
await app.stop(tester);
});
testWidgets(
'Ctrl+Y from a terminal raises chat and focuses its composer',
(tester) async {
await _saved('a', 'b');
final app = _App(_Store());
await app.start(tester, terminalSurface: true);
await _menu(tester, 'b', 'Float (Option+Y)');
app.manager.open('terminal', 'terminal');
app.terminalFocus.requestFocus();
await tester.pump();
await _controlChord(tester);
final input = _input('b');
expect(_shown('Chat b'), findsOneWidget);
expect(
tester.widget<EditableText>(input).focusNode.hasPrimaryFocus,
isTrue,
);
tester.testTextInput.enterText('from terminal');
await tester.pump();
expect(
tester.widget<EditableText>(input).controller.text,
'from terminal',
);
expect(app.terminalTyped, isEmpty);
await app.stop(tester);
},
variant: const TargetPlatformVariant({TargetPlatform.macOS}),
);
testWidgets('floating chats saved by an older build come back as windows', (
tester,
) async {
await _saved('a', 'c');
final preferences = await SharedPreferences.getInstance();
await preferences.setString(
scopedServerKey(chatFloatPreferenceKey, 'server-a'),
const ChatFloatRecord(
panes: [ChatWorkspacePane('chat-pane-9', 'b')],
shown: true,
frames: {'chat-pane-9': Rect.fromLTWH(400, 300, 500, 420)},
).encode(),
);
final app = _App(_Store());
await app.start(tester);
expect(app.floating, ['b']);
expect(tester.getTopLeft(_window('Chat b')), const Offset(400, 300));
expect(_workspace(tester).panes.map((p) => p.sessionId), ['a', 'c']);
await app.stop(tester);
});
testWidgets('a chat is drawn once: opening a floating chat from the list '
'shows its window', (tester) async {
await _saved('a', 'b');
final app = _App(_Store());
await app.start(tester);
await _menu(tester, 'b', 'Float (Alt+Y)');
app.manager.setGroupShown(chatWindowKind, false);
await tester.pumpAndSettle();
_workspace(tester).open('b');
await tester.pumpAndSettle();
expect(_shown('Chat b'), findsOneWidget);
expect(_workspace(tester).panes.map((p) => p.sessionId), ['a']);
expect(_thread('b'), findsOneWidget);
expect(tester.takeException(), isNull);
await app.stop(tester);
});
testWidgets('narrow layouts offer no Float and show no window', (
tester,
) async {
await _saved('a', 'b');
final app = _App(_Store());
await app.start(tester, size: const Size(430, 900));
final finder = find.byTooltip('More actions').hitTestable();
await tester.tap(finder.first);
await tester.pumpAndSettle();
expect(find.textContaining('Float'), findsNothing);
expect(find.text('Open chats (2)'), findsOneWidget);
await app.stop(tester);
});
}