Connect independent resizable chat panes with routed focus and narrow layouts
This commit is contained in:
@@ -0,0 +1,263 @@
|
||||
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 '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, {bool offstage = false}) => find.byWidgetPredicate(
|
||||
(widget) => widget is ChatThreadView && widget.sessionId == id,
|
||||
skipOffstage: !offstage,
|
||||
);
|
||||
Finder _input(String id, {bool offstage = false}) => find.descendant(
|
||||
of: find.byKey(ValueKey('chat-composer-$id'), skipOffstage: !offstage),
|
||||
matching: find.byType(EditableText, skipOffstage: !offstage),
|
||||
skipOffstage: !offstage,
|
||||
);
|
||||
Widget _app(_Gateway gateway, {int sync = 0}) => MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ChatScreen(gateway: gateway, syncToken: sync),
|
||||
),
|
||||
);
|
||||
Future<_Gateway> _setup(WidgetTester tester) async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
tester.view.physicalSize = const Size(1800, 1000);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final gateway = _Gateway()
|
||||
..history = (fields) async {
|
||||
final id = (fields['chat_query'] as Map)['session_id'];
|
||||
return chatPage([
|
||||
chatMessage(
|
||||
'$id-history',
|
||||
'assistant',
|
||||
parts: [
|
||||
{'type': 'text', 'text': 'History for $id'},
|
||||
],
|
||||
),
|
||||
]);
|
||||
};
|
||||
addTearDown(gateway.close);
|
||||
await tester.pumpWidget(_app(gateway));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Chat a').first);
|
||||
await tester.pumpAndSettle();
|
||||
return gateway;
|
||||
}
|
||||
|
||||
Future<void> _split(WidgetTester tester, String id, String action) async {
|
||||
await tester.tap(
|
||||
find.byWidgetPredicate(
|
||||
(widget) => widget is SessionActionsButton && widget.session['id'] == id,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text(action));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'nested splits preserve drafts, resize, collapse and narrow restore',
|
||||
(tester) async {
|
||||
final gateway = await _setup(tester);
|
||||
final stateA = tester.state(_thread('a'));
|
||||
await tester.enterText(_input('a'), 'Draft A');
|
||||
await _split(tester, 'b', 'Open to the right');
|
||||
final stateB = tester.state(_thread('b'));
|
||||
await tester.enterText(_input('b'), 'Draft B');
|
||||
await _split(tester, 'c', 'Open below');
|
||||
final workspace = tester.widget<ChatWorkspace>(
|
||||
find.byType(ChatWorkspace),
|
||||
);
|
||||
expect(workspace.controller.paneCount, 3);
|
||||
expect(tester.state(_thread('a')), same(stateA));
|
||||
expect(tester.state(_thread('b')), same(stateB));
|
||||
expect(
|
||||
tester.getCenter(_thread('a')).dx,
|
||||
lessThan(tester.getCenter(_thread('b')).dx),
|
||||
);
|
||||
expect(
|
||||
tester.getCenter(_thread('b')).dy,
|
||||
lessThan(tester.getCenter(_thread('c')).dy),
|
||||
);
|
||||
final root = workspace.controller.root as ChatWorkspaceSplit;
|
||||
final divider = find.byKey(ValueKey('chat-workspace-${root.id}'));
|
||||
final width = tester.getSize(_thread('a')).width;
|
||||
await tester.drag(divider, const Offset(80, 0));
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.getSize(_thread('a')).width, greaterThan(width));
|
||||
expect(tester.state(_thread('a')), same(stateA));
|
||||
tester.view.physicalSize = const Size(560, 1000);
|
||||
await tester.pumpAndSettle();
|
||||
expect(_thread('c'), findsOneWidget);
|
||||
expect(_thread('a'), findsNothing);
|
||||
expect(_thread('b'), findsNothing);
|
||||
tester.view.physicalSize = const Size(390, 844);
|
||||
await tester.pumpAndSettle();
|
||||
expect(_thread('c'), findsOneWidget);
|
||||
expect(_thread('a'), findsNothing);
|
||||
expect(tester.state(_thread('a', offstage: true)), same(stateA));
|
||||
tester.view.physicalSize = const Size(1800, 1000);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.state(_thread('a')), same(stateA));
|
||||
expect(
|
||||
tester.widget<EditableText>(_input('a')).controller.text,
|
||||
'Draft A',
|
||||
);
|
||||
expect(
|
||||
tester.widget<EditableText>(_input('b')).controller.text,
|
||||
'Draft B',
|
||||
);
|
||||
await tester.tap(
|
||||
find.descendant(
|
||||
of: _thread('c'),
|
||||
matching: find.byTooltip('Close pane'),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(workspace.controller.paneCount, 2);
|
||||
expect(_thread('c'), findsNothing);
|
||||
expect(tester.state(_thread('b')), same(stateB));
|
||||
expect(
|
||||
gateway.calls.where(
|
||||
(call) => [
|
||||
'chat_prompt',
|
||||
'chat_close',
|
||||
'chat_interrupt',
|
||||
].contains(call.type),
|
||||
),
|
||||
isEmpty,
|
||||
);
|
||||
expect(tester.takeException(), isNull);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('pane keyboard focus, session-isolated streams and reconnects', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = await _setup(tester);
|
||||
await _split(tester, 'b', 'Open to the right');
|
||||
await tester.tap(_input('b'));
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
tester.widget<EditableText>(_input('a')).focusNode.hasPrimaryFocus,
|
||||
isTrue,
|
||||
);
|
||||
gateway.emitEvent(
|
||||
chatTextFrame('Alpha only', session: 'a', messageId: 'live-a'),
|
||||
);
|
||||
gateway.emitEvent(
|
||||
chatTextFrame('Beta only', session: 'b', messageId: 'live-b'),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
find.descendant(of: _thread('a'), matching: find.text('Alpha only')),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.descendant(of: _thread('b'), matching: find.text('Alpha only')),
|
||||
findsNothing,
|
||||
);
|
||||
expect(
|
||||
find.descendant(of: _thread('b'), matching: find.text('Beta only')),
|
||||
findsOneWidget,
|
||||
);
|
||||
final states = [tester.state(_thread('a')), tester.state(_thread('b'))];
|
||||
final subscriptions = gateway.calls
|
||||
.where((call) => call.type == 'chat_subscribe')
|
||||
.length;
|
||||
await tester.pumpWidget(_app(gateway, sync: 1));
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.state(_thread('a')), same(states[0]));
|
||||
expect(tester.state(_thread('b')), same(states[1]));
|
||||
expect(
|
||||
gateway.calls.where((call) => call.type == 'chat_subscribe').length,
|
||||
subscriptions + 2,
|
||||
);
|
||||
expect(tester.takeException(), isNull);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
});
|
||||
testWidgets(
|
||||
'route focus and asynchronous new sessions affect the owning pane',
|
||||
(tester) async {
|
||||
final gateway = await _setup(tester);
|
||||
final route = ValueNotifier<String?>('a');
|
||||
addTearDown(route.dispose);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ValueListenableBuilder<String?>(
|
||||
valueListenable: route,
|
||||
builder: (_, id, _) => ChatScreen(
|
||||
gateway: gateway,
|
||||
initialSessionId: id,
|
||||
onSessionSelected: (id) => route.value = id,
|
||||
onBack: () => route.value = null,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await _split(tester, 'b', 'Open to the right');
|
||||
await _split(tester, 'c', 'Open below');
|
||||
expect(route.value, 'c');
|
||||
final stateA = tester.state(_thread('a'));
|
||||
final stateC = tester.state(_thread('c'));
|
||||
final finishedNew = tester
|
||||
.widget<ChatThreadView>(_thread('b'))
|
||||
.onNewSession!;
|
||||
await tester.tap(_input('a'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(route.value, 'a');
|
||||
finishedNew('b', {'id': 'b-new', 'title': 'Fresh B'}, 'Started fresh');
|
||||
await tester.pumpAndSettle();
|
||||
expect(route.value, 'a');
|
||||
expect(_thread('b'), findsNothing);
|
||||
expect(_thread('b-new'), findsOneWidget);
|
||||
expect(tester.state(_thread('a')), same(stateA));
|
||||
expect(tester.state(_thread('c')), same(stateC));
|
||||
tester.view.physicalSize = const Size(560, 1000);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.binding.handlePopRoute();
|
||||
await tester.pumpAndSettle();
|
||||
expect(route.value, isNull);
|
||||
expect(find.text('Chat a'), findsOneWidget);
|
||||
expect(
|
||||
gateway.calls.where(
|
||||
(call) => ['chat_close', 'chat_interrupt'].contains(call.type),
|
||||
),
|
||||
isEmpty,
|
||||
);
|
||||
expect(tester.takeException(), isNull);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user