Files
pipelines/app/test/features/chat/windows/chat_windows_test.dart
T
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

279 lines
8.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/chat_runtime.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_controller.dart';
import 'package:pipelines_app/shell/windows/window_kind.dart';
import 'package:pipelines_app/shell/windows/window_manager.dart';
import 'package:pipelines_app/ui/input/keyboard_owner.dart';
class _Gateway implements ApiGateway {
final calls = <String>[];
@override
Stream<JsonMap> get frames => const Stream.empty();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
calls.add(type);
if (type == 'chat_sessions') {
return {
'chat_sessions': [
{'id': 'a', 'title': 'Chat a'},
{'id': 'b', 'title': 'Chat b'},
],
};
}
return {};
}
}
class _Legacy implements ChatFloatStore {
_Legacy(this.record);
ChatFloatRecord? record;
@override
Future<ChatFloatRecord?> read() async => record;
@override
Future<bool> write(ChatFloatRecord value) async {
record = value;
return true;
}
}
class _Harness {
_Harness() {
windows = ChatWindows(
manager: manager,
runtime: runtime,
openInChat: opened.add,
)..start();
}
final gateway = _Gateway();
late final runtime = ChatRuntime(gateway: gateway, serverKey: 'home');
final manager = WindowManager(kinds: WindowKinds(), server: 'home');
final opened = <String>[];
final docked = <String>[];
final floated = <String>[];
final tiled = <String>{};
bool canFloat = true;
late final ChatWindows windows;
void mountTiling() => windows.tiling = ChatTiling(
holds: tiled.contains,
dock: docked.add,
canFloatActive: () => canFloat,
floatActive: () {
floated.add('active');
windows.float('a');
},
);
List<String> get chats => [for (final w in windows.windows) w.contentId];
void dispose() {
windows.dispose();
runtime.dispose();
manager.dispose();
}
}
Future<void> _chord(
WidgetTester tester, [
LogicalKeyboardKey modifier = LogicalKeyboardKey.altLeft,
]) async {
await tester.sendKeyDownEvent(modifier);
await tester.sendKeyEvent(LogicalKeyboardKey.keyY);
await tester.sendKeyUpEvent(modifier);
await tester.pump();
}
void main() {
late _Harness h;
// Built inside each test: the chord's handler belongs to that test's
// focus manager.
void start() {
h = _Harness();
addTearDown(h.dispose);
}
Future<void> wide(WidgetTester tester, {Size size = const Size(1280, 800)}) {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
start();
return tester.pumpWidget(const MaterialApp(home: SizedBox()));
}
test('a window is a server-scoped chat entry; show raises it', () {
start();
h.windows.float('a');
h.windows.float('b');
final a = h.manager.find(chatWindowKind, 'a')!;
expect(a.serverKey, 'home');
expect(a.frame, chatFloatDefaultRect);
expect(h.windows.show('a'), isTrue);
expect(h.manager.active!.contentId, 'a');
expect(h.windows.show('zz'), isFalse);
h.manager.setGroupShown(chatWindowKind, false);
expect(h.windows.show('b'), isTrue);
expect(h.manager.groupShown(chatWindowKind), isTrue);
});
test('Dock returns the chat to the tiling, or opens Chat without one', () {
start();
h.windows.float('a');
h.windows.float('b');
h.windows.dock(h.manager.find(chatWindowKind, 'a')!);
expect(h.opened, ['a']);
h.mountTiling();
h.windows.dock(h.manager.find(chatWindowKind, 'b')!);
expect(h.docked, ['b']);
expect(h.chats, isEmpty);
});
test('/new moves the window to the new session; /close closes it', () {
start();
h.windows.float('a');
final id = h.manager.find(chatWindowKind, 'a')!.id;
h.windows.startedNewSession(id, 'a', {'id': 'a2', 'title': 'Fresh'}, 'New');
expect(h.chats, ['a2']);
expect(h.runtime.startNotice, (id: 'a2', notice: 'New'));
expect(h.runtime.titleFor('a2'), 'Fresh');
// Another device moves it again.
h.runtime.sessions.onSessionReplaced!('a2', {'id': 'a3'});
expect(h.chats, ['a3']);
// Into a session the tiling already shows: the duplicate window closes.
h.mountTiling();
h.tiled.add('t');
h.windows.startedNewSession(id, 'a3', {'id': 't'}, 'New');
expect(h.chats, isEmpty);
h.windows.float('b');
h.windows.sessionClosed(h.manager.find(chatWindowKind, 'b')!.id);
expect(h.chats, isEmpty);
});
test('windows read their rows without the Chat destination', () async {
start();
h.windows.float('a');
await pumpEventQueue();
expect(h.gateway.calls, contains('chat_sessions'));
expect(h.runtime.titleFor('a'), 'Chat a');
expect(h.runtime.opened.keys, ['a']);
});
test('saved floating chats of an older build become windows, once', () async {
start();
final legacy = _Legacy(
const ChatFloatRecord(
panes: [ChatWorkspacePane('p1', 'a'), ChatWorkspacePane('p2', 'b')],
shown: false,
frames: {'p2': Rect.fromLTWH(300, 200, 500, 400)},
),
);
await h.windows.migrate(legacy);
expect(h.chats, ['a', 'b']);
expect(
h.manager.find(chatWindowKind, 'b')!.frame,
const Rect.fromLTWH(300, 200, 500, 400),
);
expect(h.manager.groupShown(chatWindowKind), isFalse);
expect(legacy.record!.panes, isEmpty);
h.manager.close(h.manager.find(chatWindowKind, 'a')!.id);
await h.windows.migrate(legacy);
expect(h.chats, ['b']);
// Windows already saved by this build win over an old record.
final other = _Legacy(
const ChatFloatRecord(panes: [ChatWorkspacePane('p9', 'z')]),
);
await h.windows.migrate(other);
expect(h.chats, ['b']);
expect(other.record!.panes, isEmpty);
});
testWidgets('the chord floats the active pane, then hides and shows every '
'chat window', (tester) async {
await wide(tester);
// No tiling and no window: the key is left alone.
await _chord(tester);
expect(h.chats, isEmpty);
h.mountTiling();
h.canFloat = false;
await _chord(tester);
expect(h.floated, isEmpty);
h.canFloat = true;
await _chord(tester);
expect(h.floated, ['active']);
expect(h.chats, ['a']);
h.windows.float('b');
await _chord(tester);
expect(h.manager.groupShown(chatWindowKind), isFalse);
await _chord(tester, LogicalKeyboardKey.controlLeft);
expect(h.manager.groupShown(chatWindowKind), isTrue);
expect(h.floated, ['active']);
// A plain Y is typing.
await tester.sendKeyEvent(LogicalKeyboardKey.keyY);
expect(h.manager.groupShown(chatWindowKind), isTrue);
});
testWidgets('a held chord toggles once', (tester) async {
await wide(tester);
h.windows.float('a');
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
await tester.sendKeyDownEvent(LogicalKeyboardKey.keyY);
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.keyY);
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.keyY);
await tester.sendKeyUpEvent(LogicalKeyboardKey.keyY);
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
expect(h.manager.groupShown(chatWindowKind), isFalse);
});
testWidgets(
'on macOS Ctrl+Y from a terminal raises chat; Cmd+Y and Option+Y toggle',
(tester) async {
await wide(tester);
final typing = FocusNode();
addTearDown(typing.dispose);
final typed = <LogicalKeyboardKey>[];
await tester.pumpWidget(
MaterialApp(
home: TypingSurface(
child: Focus(
focusNode: typing,
onKeyEvent: (node, event) {
if (event is KeyDownEvent) typed.add(event.logicalKey);
return KeyEventResult.handled;
},
child: const SizedBox(height: 40),
),
),
),
);
h.windows.float('a');
typing.requestFocus();
await tester.pump();
await _chord(tester, LogicalKeyboardKey.controlLeft);
expect(h.manager.groupShown(chatWindowKind), isFalse);
expect(typed, isNot(contains(LogicalKeyboardKey.keyY)));
await _chord(tester, LogicalKeyboardKey.metaLeft);
expect(h.manager.groupShown(chatWindowKind), isTrue);
await _chord(tester);
expect(h.manager.groupShown(chatWindowKind), isFalse);
},
variant: const TargetPlatformVariant({TargetPlatform.macOS}),
);
testWidgets('a narrow app never takes the chord', (tester) async {
await wide(tester, size: const Size(430, 900));
h.mountTiling();
await _chord(tester);
expect(h.chats, isEmpty);
h.windows.float('a');
await _chord(tester);
expect(h.manager.groupShown(chatWindowKind), isTrue);
});
}