import 'dart:async'; import 'package:flutter/material.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/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/stack/mobile_chat_navigation.dart'; import 'package:pipelines_app/features/chat/windows/chat_windows.dart'; import 'package:pipelines_app/features/chat/workspace/chat_workspace.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:shared_preferences/shared_preferences.dart'; import 'conversation/chat_thread_fixtures.dart'; /// A chat server with an open-chat set whose entries may float. class _Server extends FakeChatGateway { _Server(this.open, this.floating); List open; Set floating; 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']) {'id': id, 'title': 'Chat $id', 'directory': '/home/$id'}, ], }); } 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 { final entries = (request['entries'] as List).cast(); open = [for (final e in entries) '${e['session_id']}']; floating = { for (final e in entries) if (e['floating'] == true) '${e['session_id']}', }; revision++; } } return Future.value({ 'type': 'chat_open_workspace', 'chat_open_workspace': _body(rejected), }); } return super.call(type, fields); } JsonMap _body([bool rejected = false]) => { 'revision': revision, 'entries': [ for (final id in open) {'session_id': id, if (floating.contains(id)) 'floating': true}, ], if (rejected) 'rejected': true, }; void change(List ids, Set floats) { open = ids; floating = floats; revision++; emitRaw({ 'type': 'chat_open_workspace_changed', 'chat_open_workspace': _body(), }); } } class _Store implements WindowStore { String? value; @override Future read() async => value; @override Future write(String record) async { value = record; return true; } } const _wide = Size(1800, 1000); const _phone = Size(390, 844); class _App { late final _Server server; late final ChatRuntime runtime; late final WindowManager manager; late final ChatWindows windows; Future start(WidgetTester tester, {Size size = _wide}) async { SharedPreferences.setMockInitialValues({}); _resize(tester, size); addTearDown(tester.view.reset); server = _Server(['a', 'b'], {'b'})..history = (_) async => chatPage([]); bindServerIdentity(server, 'server-a'); runtime = ChatRuntime(gateway: server, 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: server, persistWorkspace: true, runtime: runtime, windows: windows, ), Positioned.fill(child: WindowHost(manager: manager)), ], ), ), ), ); await tester.pumpAndSettle(); } Future stop(WidgetTester tester) async { await tester.pumpWidget(const SizedBox.shrink()); windows.dispose(); runtime.dispose(); manager.dispose(); server.close(); } List get floatingWindows => [ for (final w in windows.windows) w.contentId, ]; } void _resize(WidgetTester tester, Size size) { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; } List _panes(WidgetTester tester) { final workspace = find.byType(ChatWorkspace, skipOffstage: false); if (workspace.evaluate().isEmpty) return const []; return tester .widget(workspace) .controller .panes .map((pane) => pane.sessionId) .toList(); } Finder _shownThread(String id) => find .byWidgetPredicate((w) => w is ChatThreadView && w.sessionId == id) .hitTestable(); void main() { final navigation = MobileChatNavigationController.instance; tearDown(() { unawaited(navigation.setValue(MobileChatNavigation.quickSwitch)); }); testWidgets('a phone tiles a floating chat; a wide layout floats it again', ( tester, ) async { final app = _App(); await app.start(tester); expect(app.floatingWindows, ['b']); _resize(tester, _phone); await tester.pumpAndSettle(); // No window at phone width: the chat is an open chat like the others, // and the server still has it floating. expect(app.floatingWindows, isEmpty); expect(app.server.floating, {'b'}); // The list opens it full screen. await tester.tap(find.byKey(const Key('chat-row-b'))); await tester.pumpAndSettle(); expect(_shownThread('b'), findsOneWidget); expect(_panes(tester), ['a', 'b']); _resize(tester, _wide); await tester.pumpAndSettle(); expect(app.floatingWindows, ['b']); expect(_panes(tester), ['a']); expect(_shownThread('a'), findsOneWidget); expect(app.server.floating, {'b'}); expect(app.server.open, ['a', 'b']); expect(tester.takeException(), isNull); await app.stop(tester); }); testWidgets('a floating chat opens from the list on a phone from the start', ( tester, ) async { final app = _App(); await app.start(tester, size: _phone); expect(app.floatingWindows, isEmpty); await tester.tap(find.byKey(const Key('chat-row-b'))); await tester.pumpAndSettle(); expect(_shownThread('b'), findsOneWidget); expect(_panes(tester), ['a', 'b']); expect(app.server.floating, {'b'}); await app.stop(tester); }); testWidgets('quick switch reaches a floating chat on a phone', ( tester, ) async { final app = _App(); await app.start(tester, size: _phone); await tester.tap(find.byKey(const Key('chat-row-a'))); await tester.pumpAndSettle(); expect(_shownThread('a'), findsOneWidget); final controller = tester .widget(find.byType(ChatWorkspace)) .controller; final b = controller.panes.firstWhere((pane) => pane.sessionId == 'b'); await tester.tap(find.byKey(ValueKey('chat-peek-${b.id}'))); await tester.pumpAndSettle(); expect(_shownThread('b'), findsOneWidget); expect(app.floatingWindows, isEmpty); await app.stop(tester); }); testWidgets('the chat stack reaches a floating chat on a phone', ( tester, ) async { unawaited(navigation.setValue(MobileChatNavigation.chatStack)); final app = _App(); await app.start(tester, size: _phone); 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(); final controller = tester .widget(find.byType(ChatWorkspace)) .controller; final b = controller.panes.firstWhere((pane) => pane.sessionId == 'b'); await tester.tap(find.byKey(ValueKey('chat-stack-button-${b.id}'))); await tester.pumpAndSettle(); expect(_shownThread('b'), findsOneWidget); await app.stop(tester); }); testWidgets('a chat another device floats stays tiled on a phone', ( tester, ) async { final app = _App(); await app.start(tester, size: _phone); await tester.tap(find.byKey(const Key('chat-row-a'))); await tester.pumpAndSettle(); app.server.change(['a', 'b', 'c'], {'b', 'c'}); await tester.pumpAndSettle(); expect(app.floatingWindows, isEmpty); expect(_panes(tester), ['a', 'b', 'c']); // Removing a held chat on the phone drops it from the open chats. final controller = tester .widget(find.byType(ChatWorkspace, skipOffstage: false)) .controller; controller.close(controller.panes.last.id); await tester.pumpAndSettle(); expect(app.server.open, ['a', 'b']); expect(app.server.floating, {'b'}); await app.stop(tester); }); }