Merge main: keep pane moves and the floating chat; Float sits beside the move items
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
import 'dart:convert';
|
||||
|
||||
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/features/chat/screen.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/sessions/session_actions.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace_store.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);
|
||||
}
|
||||
}
|
||||
|
||||
Finder _thread(String id) => find.byWidgetPredicate(
|
||||
(widget) => widget is ChatThreadView && widget.sessionId == id,
|
||||
);
|
||||
Finder _input(String id) => find.descendant(
|
||||
of: find.byKey(ValueKey('chat-composer-$id')),
|
||||
matching: find.byType(EditableText),
|
||||
);
|
||||
|
||||
Future<_Gateway> _setup(WidgetTester tester, {Size? size}) async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
tester.view.physicalSize = size ?? const Size(1800, 1000);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final gateway = _Gateway()..history = (_) async => chatPage([]);
|
||||
addTearDown(gateway.close);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ChatScreen(gateway: gateway, persistWorkspace: true),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Chat a').first);
|
||||
await tester.pumpAndSettle();
|
||||
return gateway;
|
||||
}
|
||||
|
||||
Future<void> _split(WidgetTester tester, String id) async {
|
||||
await tester.tap(
|
||||
find.byWidgetPredicate(
|
||||
(widget) => widget is SessionActionsButton && widget.session['id'] == id,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Open to the right'));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
Future<void> _openMenu(WidgetTester tester, String session) async {
|
||||
await tester.tap(
|
||||
find.descendant(
|
||||
of: find.descendant(
|
||||
of: _thread(session),
|
||||
matching: find.byKey(const Key('chat-actions-island')),
|
||||
),
|
||||
matching: find.byTooltip('More actions'),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
MenuItemButton _item(WidgetTester tester, String label) =>
|
||||
tester.widget<MenuItemButton>(
|
||||
find.ancestor(of: find.text(label), matching: find.byType(MenuItemButton)),
|
||||
);
|
||||
|
||||
ChatWorkspaceController _controller(WidgetTester tester) =>
|
||||
tester.widget<ChatWorkspace>(find.byType(ChatWorkspace)).controller;
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'pane menu moves a chat beside its neighbour and keeps its state',
|
||||
(tester) async {
|
||||
final gateway = await _setup(tester);
|
||||
await _split(tester, 'b');
|
||||
final state = tester.state(_thread('a'));
|
||||
await tester.enterText(_input('a'), 'Keep this draft');
|
||||
gateway.emitEvent(
|
||||
chatTextFrame('Streamed to a', session: 'a', messageId: 'live-a'),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
tester.getCenter(_thread('a')).dx,
|
||||
lessThan(tester.getCenter(_thread('b')).dx),
|
||||
);
|
||||
|
||||
await _openMenu(tester, 'a');
|
||||
expect(_item(tester, 'Move right').onPressed, isNotNull);
|
||||
for (final label in ['Move up', 'Move down', 'Move left']) {
|
||||
expect(_item(tester, label).onPressed, isNull, reason: label);
|
||||
}
|
||||
expect(
|
||||
(_item(tester, 'Move right').shortcut! as SingleActivator)
|
||||
.debugDescribeKeys(),
|
||||
'Control + Alt + Shift + Arrow Right',
|
||||
);
|
||||
await tester.tap(find.text('Move right'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(
|
||||
tester.getCenter(_thread('a')).dx,
|
||||
greaterThan(tester.getCenter(_thread('b')).dx),
|
||||
);
|
||||
expect(tester.state(_thread('a')), same(state));
|
||||
expect(
|
||||
tester.widget<EditableText>(_input('a')).controller.text,
|
||||
'Keep this draft',
|
||||
);
|
||||
expect(
|
||||
find.descendant(of: _thread('a'), matching: find.text('Streamed to a')),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(_controller(tester).activePane?.sessionId, 'a');
|
||||
expect(
|
||||
tester.widget<EditableText>(_input('a')).focusNode.hasFocus,
|
||||
isTrue,
|
||||
);
|
||||
|
||||
// The new order is what a reload restores.
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
await tester.pumpAndSettle();
|
||||
final preferences = await SharedPreferences.getInstance();
|
||||
final saved = preferences
|
||||
.getKeys()
|
||||
.where((key) => key.startsWith(chatWorkspacePreferenceKey))
|
||||
.map(preferences.getString)
|
||||
.single!;
|
||||
final root = jsonDecode(saved)['root'] as Map;
|
||||
expect((root['first'] as Map)['session_id'], 'b');
|
||||
expect((root['second'] as Map)['session_id'], 'a');
|
||||
|
||||
await _openMenu(tester, 'a');
|
||||
expect(_item(tester, 'Move left').onPressed, isNotNull);
|
||||
expect(_item(tester, 'Move right').onPressed, isNull);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull);
|
||||
},
|
||||
variant: TargetPlatformVariant.only(TargetPlatform.linux),
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'Ctrl+Alt+Shift+Arrow moves the focused chat with its draft and focus',
|
||||
(tester) async {
|
||||
await _setup(tester);
|
||||
await _split(tester, 'b');
|
||||
final state = tester.state(_thread('b'));
|
||||
await tester.enterText(_input('b'), 'Typed in b');
|
||||
await tester.pumpAndSettle();
|
||||
final text = tester.widget<EditableText>(_input('b')).controller;
|
||||
final selection = text.selection;
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
tester.getCenter(_thread('b')).dx,
|
||||
lessThan(tester.getCenter(_thread('a')).dx),
|
||||
);
|
||||
expect(tester.state(_thread('b')), same(state));
|
||||
expect(text.text, 'Typed in b');
|
||||
expect(text.selection, selection);
|
||||
expect(
|
||||
tester.widget<EditableText>(_input('b')).focusNode.hasPrimaryFocus,
|
||||
isTrue,
|
||||
);
|
||||
expect(_controller(tester).activePane?.sessionId, 'b');
|
||||
expect(tester.takeException(), isNull);
|
||||
},
|
||||
variant: TargetPlatformVariant.only(TargetPlatform.linux),
|
||||
);
|
||||
|
||||
testWidgets('a phone shows no move items for its one-at-a-time panes', (
|
||||
tester,
|
||||
) async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
tester.view.physicalSize = const Size(560, 1000);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final gateway = _Gateway()..history = (_) async => chatPage([]);
|
||||
addTearDown(gateway.close);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(home: Scaffold(body: ChatScreen(gateway: gateway))),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Chat a').first);
|
||||
await tester.pumpAndSettle();
|
||||
_controller(tester).splitActive('b', ChatSplitAxis.horizontal);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byTooltip('More actions').hitTestable().first);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Close pane'), findsOneWidget);
|
||||
for (final label in ['Move up', 'Move down', 'Move left', 'Move right']) {
|
||||
expect(find.text(label), findsNothing, reason: label);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_pane_move_shortcuts.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace_geometry.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace_snapshot.dart';
|
||||
|
||||
/// a | (b over (c | d)): a on the left, b top right, c and d below b.
|
||||
ChatWorkspaceController _nested() {
|
||||
final controller = ChatWorkspaceController()..open('a');
|
||||
controller.splitActive('b', ChatSplitAxis.horizontal);
|
||||
controller.splitActive('c', ChatSplitAxis.vertical);
|
||||
controller.splitActive('d', ChatSplitAxis.horizontal);
|
||||
return controller;
|
||||
}
|
||||
|
||||
String _id(ChatWorkspaceController controller, String session) =>
|
||||
controller.panes.firstWhere((pane) => pane.sessionId == session).id;
|
||||
|
||||
Map<ChatPaneDirection, String?> _targets(
|
||||
ChatWorkspaceController controller,
|
||||
String session,
|
||||
) {
|
||||
final geometry = ChatWorkspaceGeometry(
|
||||
controller.root!,
|
||||
const Size(1200, 800),
|
||||
);
|
||||
final names = {for (final p in controller.panes) p.id: p.sessionId};
|
||||
return {
|
||||
for (final direction in ChatPaneDirection.values)
|
||||
direction: names[geometry.adjacent(_id(controller, session), direction)],
|
||||
};
|
||||
}
|
||||
|
||||
class _Draft extends StatefulWidget {
|
||||
const _Draft({super.key, required this.name});
|
||||
final String name;
|
||||
@override
|
||||
State<_Draft> createState() => _DraftState();
|
||||
}
|
||||
|
||||
class _DraftState extends State<_Draft> {
|
||||
final text = TextEditingController();
|
||||
@override
|
||||
void dispose() {
|
||||
text.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
TextField(key: ValueKey('input-${widget.name}'), controller: text);
|
||||
}
|
||||
|
||||
Widget _host(ChatWorkspaceController controller, {bool reduce = false}) =>
|
||||
MaterialApp(
|
||||
home: MediaQuery(
|
||||
data: MediaQueryData(
|
||||
size: const Size(1200, 800),
|
||||
disableAnimations: reduce,
|
||||
),
|
||||
child: Scaffold(
|
||||
body: SizedBox(
|
||||
width: 1200,
|
||||
height: 800,
|
||||
child: ChatWorkspace(
|
||||
controller: controller,
|
||||
paneBuilder: (_, pane, _) =>
|
||||
_Draft(key: ValueKey(pane.sessionId), name: pane.sessionId),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Future<void> _chord(WidgetTester tester, LogicalKeyboardKey arrow) async {
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await tester.sendKeyEvent(arrow);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('adjacency follows nested splits and stops at outer edges', () {
|
||||
final controller = _nested();
|
||||
addTearDown(controller.dispose);
|
||||
expect(_targets(controller, 'a'), {
|
||||
ChatPaneDirection.up: null,
|
||||
ChatPaneDirection.down: null,
|
||||
ChatPaneDirection.left: null,
|
||||
// b and c share a's right edge equally and a's centre line falls in
|
||||
// their divider: the tie goes to the first in reading order.
|
||||
ChatPaneDirection.right: 'b',
|
||||
});
|
||||
expect(_targets(controller, 'b'), {
|
||||
ChatPaneDirection.up: null,
|
||||
ChatPaneDirection.down: 'c',
|
||||
ChatPaneDirection.left: 'a',
|
||||
ChatPaneDirection.right: null,
|
||||
});
|
||||
// Once d is wider it spans b's centre line, so b's move down takes d.
|
||||
final bottom =
|
||||
((controller.root as ChatWorkspaceSplit).second as ChatWorkspaceSplit)
|
||||
.second
|
||||
as ChatWorkspaceSplit;
|
||||
controller.resize(bottom.id, .3);
|
||||
expect(_targets(controller, 'b')[ChatPaneDirection.down], 'd');
|
||||
controller.resize(bottom.id, .5);
|
||||
expect(_targets(controller, 'c'), {
|
||||
ChatPaneDirection.up: 'b',
|
||||
ChatPaneDirection.down: null,
|
||||
ChatPaneDirection.left: 'a',
|
||||
ChatPaneDirection.right: 'd',
|
||||
});
|
||||
expect(_targets(controller, 'd')[ChatPaneDirection.left], 'c');
|
||||
expect(_targets(controller, 'd')[ChatPaneDirection.up], 'b');
|
||||
});
|
||||
|
||||
test('swap trades places both ways, keeping ids, ratios and focus', () {
|
||||
final controller = _nested();
|
||||
addTearDown(controller.dispose);
|
||||
final outer = controller.root as ChatWorkspaceSplit;
|
||||
controller.resize(outer.id, .3);
|
||||
final ids = {for (final s in ['a', 'b', 'c', 'd']) s: _id(controller, s)};
|
||||
final serial = controller.moveSerial;
|
||||
controller.swap(ids['a']!, ids['c']!);
|
||||
expect(controller.panes.map((p) => p.sessionId), ['c', 'b', 'a', 'd']);
|
||||
expect({for (final p in controller.panes) p.sessionId: p.id}, ids);
|
||||
expect(controller.activePaneId, ids['a']);
|
||||
expect(controller.moveSerial, serial + 1);
|
||||
expect(controller.movedPaneIds, {ids['a'], ids['c']});
|
||||
expect((controller.root as ChatWorkspaceSplit).ratio, .3);
|
||||
expect((controller.root as ChatWorkspaceSplit).id, outer.id);
|
||||
controller.swap(ids['a']!, ids['c']!);
|
||||
expect(controller.panes.map((p) => p.sessionId), ['a', 'b', 'c', 'd']);
|
||||
controller.swap(ids['a']!, 'missing');
|
||||
controller.swap(ids['a']!, ids['a']!);
|
||||
expect(controller.moveSerial, serial + 2);
|
||||
});
|
||||
|
||||
test('a moved layout survives an encode/decode round trip', () {
|
||||
final controller = _nested();
|
||||
addTearDown(controller.dispose);
|
||||
controller.swap(_id(controller, 'b'), _id(controller, 'd'));
|
||||
final source = controller.snapshot(sidebarMode: 'compact')!.encode();
|
||||
final restored = ChatWorkspaceController();
|
||||
addTearDown(restored.dispose);
|
||||
expect(restored.restoreLayout(ChatWorkspaceSnapshot.decode(source)!), true);
|
||||
expect(restored.panes.map((p) => p.sessionId), ['a', 'd', 'c', 'b']);
|
||||
expect(restored.activePane?.sessionId, 'b');
|
||||
});
|
||||
|
||||
test('the move chord adds Alt to the focus chord, Command on macOS', () {
|
||||
expect(
|
||||
chatPaneMoveActivator(
|
||||
ChatPaneDirection.up,
|
||||
platform: TargetPlatform.macOS,
|
||||
).debugDescribeKeys(),
|
||||
'Alt + Meta + Shift + Arrow Up',
|
||||
);
|
||||
expect(
|
||||
chatPaneMoveActivator(
|
||||
ChatPaneDirection.down,
|
||||
platform: TargetPlatform.windows,
|
||||
).debugDescribeKeys(),
|
||||
'Control + Alt + Shift + Arrow Down',
|
||||
);
|
||||
});
|
||||
|
||||
test('the chord family: Alt turns focusing into moving', () {
|
||||
// Only real key events reach the handler; the parse is checked through
|
||||
// the widget tests below. Here, the menu's chord and the handler agree.
|
||||
final activator = chatPaneMoveActivator(
|
||||
ChatPaneDirection.left,
|
||||
platform: TargetPlatform.linux,
|
||||
);
|
||||
expect(activator.trigger, LogicalKeyboardKey.arrowLeft);
|
||||
expect(
|
||||
[activator.control, activator.alt, activator.shift, activator.meta],
|
||||
[true, true, true, false],
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'the chord moves the focused pane, keeps its draft, and is inert at edges',
|
||||
(tester) async {
|
||||
final controller = ChatWorkspaceController()
|
||||
..open('one')
|
||||
..splitActive('two', ChatSplitAxis.horizontal);
|
||||
addTearDown(controller.dispose);
|
||||
await tester.pumpWidget(_host(controller));
|
||||
final input = find.byKey(const ValueKey('input-one'));
|
||||
await tester.enterText(input, 'draft one');
|
||||
final state = tester.state(find.byKey(const ValueKey('one')));
|
||||
final before = tester.getRect(input);
|
||||
await _chord(tester, LogicalKeyboardKey.arrowRight);
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
// Mid-glide: the retained view is between its old and new tiles.
|
||||
final middle = tester.getRect(input);
|
||||
await tester.pumpAndSettle();
|
||||
final after = tester.getRect(input);
|
||||
expect(middle.left, greaterThan(before.left));
|
||||
expect(after.left, greaterThan(middle.left));
|
||||
expect(tester.state(find.byKey(const ValueKey('one'))), same(state));
|
||||
expect(tester.widget<TextField>(input).controller!.text, 'draft one');
|
||||
expect(controller.activePane?.sessionId, 'one');
|
||||
expect(controller.panes.map((p) => p.sessionId), ['two', 'one']);
|
||||
final serial = controller.moveSerial;
|
||||
await _chord(tester, LogicalKeyboardKey.arrowRight);
|
||||
await _chord(tester, LogicalKeyboardKey.arrowUp);
|
||||
await tester.pumpAndSettle();
|
||||
expect(controller.moveSerial, serial);
|
||||
expect(tester.widget<TextField>(input).controller!.text, 'draft one');
|
||||
},
|
||||
variant: TargetPlatformVariant.only(TargetPlatform.linux),
|
||||
);
|
||||
|
||||
testWidgets('reduced motion swaps the panes instantly', (tester) async {
|
||||
final controller = ChatWorkspaceController()
|
||||
..open('one')
|
||||
..splitActive('two', ChatSplitAxis.horizontal);
|
||||
addTearDown(controller.dispose);
|
||||
await tester.pumpWidget(_host(controller, reduce: true));
|
||||
final input = find.byKey(const ValueKey('input-one'));
|
||||
final twoBefore = tester.getRect(find.byKey(const ValueKey('input-two')));
|
||||
controller.swap(_id(controller, 'one'), _id(controller, 'two'));
|
||||
await tester.pump();
|
||||
expect(tester.getRect(input).left, twoBefore.left);
|
||||
expect(tester.hasRunningAnimations, isFalse);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user