Files
pipelines/app/test/features/chat/chat_pane_cycle_test.dart
T
2026-12-03 22:18:36 +00:00

330 lines
11 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_pane_focus_flash.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_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/ui/input/keyboard_owner.dart';
import 'conversation/chat_thread_fixtures.dart';
const _ids = ['a', 'b', 'c', 'd'];
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 _ids)
{'id': id, 'title': 'Chat $id', 'directory': '/home/$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;
}
}
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,
);
bool _composerFocused(WidgetTester tester, String id) =>
tester.widget<EditableText>(_input(id)).focusNode.hasPrimaryFocus;
ChatWorkspaceController _workspace(WidgetTester tester) => tester
.widget<ChatWorkspace>(find.byType(ChatWorkspace, skipOffstage: false))
.controller;
/// The chats whose focus ring is showing.
List<String> _flashing(WidgetTester tester) => [
for (final id in _ids)
if (find
.descendant(
of: find.ancestor(
of: _thread(id),
matching: find.byType(ChatPaneFocusFlash),
),
matching: find.byKey(const ValueKey('chat-pane-focus-flash')),
)
.evaluate()
.isNotEmpty)
id,
];
/// Saves a horizontal layout of [ids], left to right.
void _saved(List<String> ids) {
final layout = ChatWorkspaceController()..open(ids.first);
for (final id in ids.skip(1)) {
layout.splitActive(id, ChatSplitAxis.horizontal);
}
SharedPreferences.setMockInitialValues({
scopedServerKey(chatWorkspacePreferenceKey, 'server-a'): layout
.snapshot()!
.encode(),
});
}
/// The Chat destination with its floating windows over it, and optionally a
/// terminal that takes typing.
class _App {
final gateway = _Gateway()..history = (_) async => chatPage([]);
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 terminal = 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');
manager = WindowManager(
kinds: WindowKinds(),
store: _Store(),
server: 'server-a',
);
windows = ChatWindows(
manager: manager,
runtime: runtime,
openInChat: (_) {},
)..start();
await manager.restore();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Stack(
fit: StackFit.expand,
children: [
ChatScreen(
gateway: gateway,
persistWorkspace: true,
runtime: runtime,
windows: windows,
),
if (terminal)
Positioned(
left: 0,
top: 0,
width: 10,
height: 10,
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();
terminalFocus.dispose();
gateway.close();
}
List<String> get floating => [for (final w in windows.windows) w.contentId];
}
/// Floats [session] from its pane menu.
Future<void> _float(WidgetTester tester, String session) async {
await tester.tap(
find
.descendant(
of: _thread(session),
matching: find.byTooltip('More actions'),
)
.hitTestable(),
);
await tester.pumpAndSettle();
await tester.tap(find.text('Float (Alt+Y)').last);
await tester.pumpAndSettle();
}
/// Alt+] (or Alt+[ with [back]), leaving the focus ring mid-flash.
Future<void> _cycle(WidgetTester tester, {bool back = false}) async {
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
final physical = back
? PhysicalKeyboardKey.bracketLeft
: PhysicalKeyboardKey.bracketRight;
final logical = back
? LogicalKeyboardKey.bracketLeft
: LogicalKeyboardKey.bracketRight;
await tester.sendKeyDownEvent(logical, physicalKey: physical);
await tester.sendKeyUpEvent(logical, physicalKey: physical);
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
for (var i = 0; i < 3; i++) {
await tester.pump();
}
}
/// Lets the flash finish.
Future<void> _settle(WidgetTester tester) async {
await tester.pump(chatPaneFlashDuration);
await tester.pumpAndSettle();
}
void main() {
testWidgets('Alt+] and Alt+[ cycle three tiled panes and a floating '
'window, wrapping, each composer focused and its ring flashing', (
tester,
) async {
_saved(['a', 'b', 'c', 'd']);
final app = _App();
await app.start(tester);
await _float(tester, 'd');
final workspace = _workspace(tester);
expect(workspace.panes.map((p) => p.sessionId), ['a', 'b', 'c']);
expect(app.floating, ['d']);
await tester.tapAt(tester.getCenter(_input('a')));
await tester.pumpAndSettle();
expect(_composerFocused(tester, 'a'), isTrue);
// Forward: the tiles left to right, then the window, then around.
for (final id in ['b', 'c', 'd', 'a']) {
await _cycle(tester);
expect(_composerFocused(tester, id), isTrue, reason: id);
expect(_flashing(tester), [id]);
if (id != 'd') expect(workspace.activePane?.sessionId, id);
await _settle(tester);
expect(_flashing(tester), isEmpty);
}
// Back: around to the window, then the tiles right to left.
for (final id in ['d', 'c', 'b', 'a']) {
await _cycle(tester, back: true);
expect(_composerFocused(tester, id), isTrue, reason: id);
expect(_flashing(tester), [id]);
await _settle(tester);
}
// Nothing opened, closed or moved.
expect(workspace.panes.map((p) => p.sessionId), ['a', 'b', 'c']);
expect(app.floating, ['d']);
expect(tester.takeException(), isNull);
await app.stop(tester);
});
testWidgets('two tiled panes and a floating window cycle in the same order', (
tester,
) async {
_saved(['a', 'b', 'c']);
final app = _App();
await app.start(tester);
await _float(tester, 'c');
await tester.tapAt(tester.getCenter(_input('b')));
await tester.pumpAndSettle();
for (final id in ['c', 'a', 'b']) {
await _cycle(tester);
expect(_composerFocused(tester, id), isTrue, reason: id);
await _settle(tester);
}
expect(tester.takeException(), isNull);
await app.stop(tester);
});
testWidgets('with one pane on screen the keys do nothing', (tester) async {
_saved(['a']);
final app = _App();
await app.start(tester);
await tester.tapAt(tester.getCenter(_input('a')));
await tester.pumpAndSettle();
await _cycle(tester);
await _cycle(tester, back: true);
expect(_composerFocused(tester, 'a'), isTrue);
expect(_flashing(tester), isEmpty);
expect(_workspace(tester).paneCount, 1);
expect(tester.takeException(), isNull);
await app.stop(tester);
});
testWidgets('on a phone layout the keys do nothing', (tester) async {
_saved(['a', 'b']);
final app = _App();
await app.start(tester, size: const Size(400, 800));
final workspace = _workspace(tester);
final active = workspace.activePane?.sessionId;
await _cycle(tester);
await _cycle(tester);
expect(workspace.activePane?.sessionId, active);
expect(_flashing(tester), isEmpty);
expect(tester.takeException(), isNull);
await app.stop(tester);
});
testWidgets('a focused terminal keeps Alt+] and Alt+[', (tester) async {
_saved(['a', 'b']);
final app = _App();
await app.start(tester, terminal: true);
final workspace = _workspace(tester);
await tester.tapAt(tester.getCenter(_input('a')));
await tester.pumpAndSettle();
app.terminalFocus.requestFocus();
await tester.pumpAndSettle();
await _cycle(tester);
await _cycle(tester, back: true);
expect(app.terminalTyped, [
LogicalKeyboardKey.altLeft,
LogicalKeyboardKey.bracketRight,
LogicalKeyboardKey.altLeft,
LogicalKeyboardKey.bracketLeft,
]);
expect(app.terminalFocus.hasPrimaryFocus, isTrue);
expect(workspace.activePane?.sessionId, 'a');
expect(_flashing(tester), isEmpty);
expect(tester.takeException(), isNull);
await app.stop(tester);
});
}