import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.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/core/servers/server_scope.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/stack/chat_stack_column.dart'; import 'package:pipelines_app/features/chat/stack/chat_stack_disc.dart'; import 'package:pipelines_app/features/chat/stack/chat_stack_drag.dart'; import 'package:pipelines_app/features/chat/stack/chat_stack_close_target.dart'; import 'package:pipelines_app/features/chat/stack/mobile_chat_navigation.dart'; import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart'; import 'package:pipelines_app/shell/adaptive_shell.dart'; import 'package:pipelines_app/shell/destination.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../conversation/chat_thread_fixtures.dart'; /// A chat server keeping the open-chat set, as the sync test's does. class _Server extends FakeChatGateway { List open = ['a', 'b', 'c']; int revision = 1; @override Future call(String type, [JsonMap fields = const {}]) { if (type == 'chat_sessions') { return Future.value({ 'chat_sessions': [ for (final id in ['a', 'b', 'c', 'd']) {'id': id, 'title': 'Chat $id', 'directory': '/w'}, ], }); } if (type == 'chat_shells') return Future.value({'chat_shells': []}); if (type == 'chat_open_workspace') { final request = fields['chat_open_workspace_request'] as Map?; var rejected = false; if (request != null && request['seed'] != true) { if (request['base_revision'] != revision) { rejected = true; } else { open = [ for (final e in (request['entries'] as List).cast()) '${e['session_id']}', ]; revision++; } } return Future.value({ 'type': 'chat_open_workspace', 'chat_open_workspace': { 'revision': revision, 'entries': [ for (final id in open) {'session_id': id}, ], if (rejected) 'rejected': true, }, }); } return super.call(type, fields); } } Future<_Server> _pump(WidgetTester tester, {bool reduced = false}) async { SharedPreferences.setMockInitialValues({}); unawaited( MobileChatNavigationController.instance.setValue( MobileChatNavigation.chatStack, ), ); tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final server = _Server()..history = (_) async => chatPage(const []); bindServerIdentity(server, 'server-stack'); addTearDown(server.close); await tester.pumpWidget( MaterialApp( builder: (context, child) => MediaQuery( data: MediaQuery.of(context).copyWith(disableAnimations: reduced), child: child!, ), home: AdaptiveShell( destination: AppDestination.chat, destinationBodies: { AppDestination.chat: (_) => ChatScreen(gateway: server, persistWorkspace: true), }, ), ), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('chat-row-a'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('chat-stack'))); await tester.pumpAndSettle(); return server; } ChatWorkspaceController _workspace(WidgetTester tester) => tester.widget(find.byType(ChatWorkspace)).controller; List _order(WidgetTester tester) => [ for (final pane in _workspace(tester).panes) pane.sessionId, ]; /// Chat [id]'s avatar in the open column. Finder _button(WidgetTester tester, String id) { final pane = _workspace( tester, ).panes.firstWhere((pane) => pane.sessionId == id); return find.byKey(ValueKey('chat-stack-button-${pane.id}')); } final _add = find.byKey(const ValueKey('chat-stack-button-add')); final _target = find.byType(ChatStackCloseTarget); /// Holds [finder] until it lifts. Future _lift(WidgetTester tester, Finder finder) async { final gesture = await tester.startGesture(tester.getCenter(finder)); await tester.pump(chatStackLiftDelay + const Duration(milliseconds: 20)); return gesture; } double _shown(WidgetTester tester) => tester.widget(_target).armed.value; void main() { final navigation = MobileChatNavigationController.instance; tearDown(() { unawaited(navigation.setValue(MobileChatNavigation.quickSwitch)); }); test('movePane moves a pane and keeps the active one', () { final controller = ChatWorkspaceController()..open('a'); controller ..splitActive('b', ChatSplitAxis.horizontal) ..splitActive('c', ChatSplitAxis.horizontal); final active = controller.activePaneId; final a = controller.panes.first.id; controller.movePane(a, 2); expect(controller.panes.map((p) => p.sessionId), ['b', 'c', 'a']); expect(controller.panes.last.id, a); expect(controller.activePaneId, active); controller.dispose(); }); testWidgets('hold and drag reorders, and the order reaches the server', ( tester, ) async { final server = await _pump(tester); expect(_order(tester), ['a', 'b', 'c']); final b = tester.getCenter(_button(tester, 'b')); final gesture = await _lift(tester, _button(tester, 'a')); // Lifted: the remove target comes in. await tester.pumpAndSettle(); expect(_shown(tester), 1); // Up two slots: b and c slide down to open the gap. await gesture.moveBy(const Offset(0, -kChatStackPitch * 2)); await tester.pumpAndSettle(); expect(tester.getCenter(_button(tester, 'b')).dy, b.dy + kChatStackPitch); await gesture.up(); await tester.pumpAndSettle(); expect(_order(tester), ['b', 'c', 'a']); expect(server.open, ['b', 'c', 'a']); // Everyone settles in the new slots; the column stays open. expect(tester.getCenter(_button(tester, 'b')).dy, closeTo(b.dy + 56, .5)); expect(_shown(tester), 0); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('dropping on the × removes; Undo restores the place', ( tester, ) async { final server = await _pump(tester); final gesture = await _lift(tester, _button(tester, 'b')); await tester.pumpAndSettle(); final from = tester.getCenter(_button(tester, 'b')); await gesture.moveBy(tester.getCenter(_target) - from); await tester.pumpAndSettle(); final drag = tester.widget(_target).hover; expect(drag.value, 1); await gesture.up(); await tester.pumpAndSettle(); expect(find.text('Removed Chat b from open chats'), findsOneWidget); expect(_order(tester), ['a', 'c']); expect(server.open, ['a', 'c']); await tester.tap(find.text('Undo')); await tester.pumpAndSettle(); expect(_order(tester), ['a', 'b', 'c']); expect(server.open, ['a', 'b', 'c']); // The chat on screen stays on screen. expect(_workspace(tester).activePane?.sessionId, 'a'); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('Escape, or a drop away from the column, puts the chat back', ( tester, ) async { final server = await _pump(tester); final home = tester.getCenter(_button(tester, 'a')); var gesture = await _lift(tester, _button(tester, 'a')); await gesture.moveBy(const Offset(0, -kChatStackPitch * 2)); await tester.pumpAndSettle(); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); await gesture.up(); await tester.pumpAndSettle(); expect(_order(tester), ['a', 'b', 'c']); expect(tester.getCenter(_button(tester, 'a')), home); // Escape only cancelled the drag: the column is still open. expect(find.byType(ChatStackColumn), findsOneWidget); gesture = await _lift(tester, _button(tester, 'a')); await gesture.moveBy(const Offset(-200, -kChatStackPitch * 3)); await tester.pumpAndSettle(); await gesture.up(); await tester.pumpAndSettle(); expect(_order(tester), ['a', 'b', 'c']); expect(server.open, ['a', 'b', 'c']); expect(tester.getCenter(_button(tester, 'a')), home); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('a tap still opens a chat; Add chat does not lift', ( tester, ) async { await _pump(tester); final add = tester.getCenter(_add); final gesture = await _lift(tester, _add); await tester.pumpAndSettle(); expect(_shown(tester), 0); await gesture.moveBy(const Offset(0, kChatStackPitch * 2)); await tester.pumpAndSettle(); expect(tester.getCenter(_add), add); await gesture.up(); await tester.pumpAndSettle(); expect(_order(tester), ['a', 'b', 'c']); // A drag that moved off "+" is no tap: the column is still open. expect(find.byType(ChatStackColumn), findsOneWidget); await tester.tap(_button(tester, 'c')); await tester.pumpAndSettle(); expect( find .byWidgetPredicate((w) => w is ChatThreadView && w.sessionId == 'c') .hitTestable(), findsOneWidget, ); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('Move up, Move down and Remove are semantic actions', ( tester, ) async { final semantics = tester.ensureSemantics(); final server = await _pump(tester); void act(String id, String label) { final node = tester.getSemantics(_button(tester, id)); final action = node .getSemanticsData() .customSemanticsActionIds! .map(CustomSemanticsAction.getAction) .firstWhere((action) => action!.label == label)!; tester.binding.renderViews.first.owner!.semanticsOwner!.performAction( node.id, SemanticsAction.customAction, CustomSemanticsAction.getIdentifier(action), ); } // The bottom chat cannot move down; the top one cannot move up. final bottom = tester.getSemantics(_button(tester, 'a')); expect( bottom.getSemanticsData().customSemanticsActionIds!.map( (id) => CustomSemanticsAction.getAction(id)!.label, ), unorderedEquals([ ChatStackColumn.moveUpLabel, ChatStackColumn.removeLabel, ]), ); act('a', ChatStackColumn.moveUpLabel); await tester.pumpAndSettle(); expect(server.open, ['b', 'a', 'c']); act('c', ChatStackColumn.moveDownLabel); await tester.pumpAndSettle(); expect(server.open, ['b', 'c', 'a']); act('b', ChatStackColumn.removeLabel); await tester.pumpAndSettle(); expect(server.open, ['c', 'a']); expect(find.text('Removed Chat b from open chats'), findsOneWidget); semantics.dispose(); await tester.pumpWidget(const SizedBox.shrink()); }); testWidgets('reduced motion: neighbours swap at once, the × only fades', ( tester, ) async { await _pump(tester, reduced: true); final b = tester.getCenter(_button(tester, 'b')); final gesture = await _lift(tester, _button(tester, 'a')); expect(_shown(tester), 1); await gesture.moveBy(const Offset(0, -kChatStackPitch)); await tester.pump(); expect(tester.getCenter(_button(tester, 'b')).dy, b.dy + kChatStackPitch); // No turn or growth: the button only fades. expect( find.descendant(of: _target, matching: find.byType(RotationTransition)), findsNothing, ); await gesture.up(); await tester.pump(); expect(_order(tester), ['b', 'a', 'c']); expect(_shown(tester), 0); await tester.pumpAndSettle(); await tester.pumpWidget(const SizedBox.shrink()); }); }