diff --git a/app/lib/application/app_router.dart b/app/lib/application/app_router.dart index cd3df947..0773d024 100644 --- a/app/lib/application/app_router.dart +++ b/app/lib/application/app_router.dart @@ -248,7 +248,13 @@ class AppRouterDelegate extends RouterDelegate with ChangeNotifier { /// Records a user move; the Router turns the new configuration into a /// history entry (or a replacement when it already matches the location). void goTo(AppRoute route) { - if (route == _route) return; + // A repeated deep link is still an explicit request to show that route. + // The chat workspace may have changed independently while the URL stayed + // the same (for example, a different split pane became active). + if (route == _route) { + notifyListeners(); + return; + } _rememberParent(route); _route = route; notifyListeners(); diff --git a/app/lib/features/chat/chat_screen_workspace.dart b/app/lib/features/chat/chat_screen_workspace.dart index ab52eb84..5514c364 100644 --- a/app/lib/features/chat/chat_screen_workspace.dart +++ b/app/lib/features/chat/chat_screen_workspace.dart @@ -10,6 +10,7 @@ mixin _ChatScreenWorkspace on State { bool _chatOwnsKeyboard(); JsonMap? _rowFor(String id); void _back(); + void _openRouted(String id); bool get _pageOpen; Widget _thread(ChatWorkspacePane pane); @@ -185,6 +186,17 @@ mixin _ChatScreenWorkspace on State { _usedWorkspace = true; _workspaceVisible = _workspace.paneCount > 0; }); + // A route can arrive while the local workspace is being restored. + // Re-apply it after restore so the saved active pane cannot overwrite + // an explicit notification or Search selection. + final id = widget.initialSessionId; + if (id != null && + id.isNotEmpty && + _workspace.activePane?.sessionId != id) { + _workspaceVisible = true; + _publishedWorkspaceSession = id; + _openRouted(id); + } _publishWorkspaceRoute(); }, ); diff --git a/app/lib/features/chat/screen.dart b/app/lib/features/chat/screen.dart index 1e7495cd..499e1476 100644 --- a/app/lib/features/chat/screen.dart +++ b/app/lib/features/chat/screen.dart @@ -357,6 +357,7 @@ class _ChatScreenState extends State /// Opens the chat the route names. A chat opened for work from elsewhere /// (Open in chat) goes beside the active chat on a wide layout instead of /// replacing it, and takes the keyboard once its composer is built. + @override void _openRouted(String id) { if (!_runtime.openedForWork.remove(id)) { _workspace.open(id); @@ -400,17 +401,34 @@ class _ChatScreenState extends State void didUpdateWidget(covariant ChatScreen oldWidget) { super.didUpdateWidget(oldWidget); final reboundWorkspace = _updateWorkspacePersistence(oldWidget); - if (!reboundWorkspace && - oldWidget.initialSessionId != widget.initialSessionId) { - _workspaceBinding?.changed(); - final id = widget.initialSessionId; - if (id != null && id.isNotEmpty) { - _workspaceVisible = true; - _publishedWorkspaceSession = id; - _openRouted(id); - } else if (widget.onSessionSelected != null) { - _workspaceVisible = false; - } + final id = widget.initialSessionId; + final routeChanged = oldWidget.initialSessionId != id; + final targetIsNotActive = + id != null && + id.isNotEmpty && + (_workspace.activePane?.sessionId != id || !_workspaceVisible); + if (!reboundWorkspace && (routeChanged || targetIsNotActive)) { + // The parent is rebuilding while didUpdateWidget runs. Opening a pane + // notifies the workspace's ListenableBuilder, which must happen after + // this build rather than during it. + WidgetsBinding.instance.addPostFrameCallback((_) { + if (!mounted) return; + final currentId = widget.initialSessionId; + _workspaceBinding?.changed(); + if (currentId != null && currentId.isNotEmpty) { + if (_workspace.activePane?.sessionId == currentId && + _workspaceVisible) { + return; + } + setState(() { + _workspaceVisible = true; + _publishedWorkspaceSession = currentId; + }); + _openRouted(currentId); + } else if (routeChanged && widget.onSessionSelected != null) { + setState(() => _workspaceVisible = false); + } + }); } // Transcripts read through another gateway belong to that server. _runtime.gateway = widget.gateway; diff --git a/app/test/application/app_router_test.dart b/app/test/application/app_router_test.dart index 07e43b6b..e5d7ab8d 100644 --- a/app/test/application/app_router_test.dart +++ b/app/test/application/app_router_test.dart @@ -50,6 +50,17 @@ void main() { expect(router.route, AppRoute.settings); }, ); + test('repeated chat deep links notify the mounted destination', () { + final router = delegate(AppRoute.chatSession('target')); + addTearDown(router.dispose); + var notifications = 0; + router.addListener(() => notifications++); + + router.goTo(AppRoute.chatSession('target')); + + expect(notifications, 1); + expect(router.route, AppRoute.chatSession('target')); + }); TestWidgetsFlutterBinding.ensureInitialized(); test('with nothing pushed the router leaves Back to the platform', () async { for (final route in [ diff --git a/app/test/application/chat_registration_test.dart b/app/test/application/chat_registration_test.dart index 4e090acb..93b2e293 100644 --- a/app/test/application/chat_registration_test.dart +++ b/app/test/application/chat_registration_test.dart @@ -381,6 +381,75 @@ void main() { }, ); + testWidgets( + 'a mounted chat notification route activates a different chat', + (tester) async { + tester.view.physicalSize = const Size(900, 900); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.resetPhysicalSize); + addTearDown(tester.view.resetDevicePixelRatio); + + final channel = FakeChannel()..readyCompleter.complete(); + final client = PipelinesClient(channelFactory: (_) => channel); + final provider = PlatformRouteInformationProvider( + initialRouteInformation: RouteInformation(uri: Uri.parse('/chat/old')), + ); + addTearDown(provider.dispose); + + await tester.pumpWidget( + PipelinesApp( + androidSupported: false, + server: webServer(), + client: client, + routeInformationProvider: provider, + ), + ); + await tester.pump(); + + await replyChatSessions(tester, channel, ['old', 'target']); + await replyChatHistory( + tester, + channel, + subscribeIndex: 0, + historyIndex: 0, + text: 'The retained chat.', + ); + expect(find.text('The retained chat.'), findsOneWidget); + + // This is the route delivered by a native chat notification, not a + // direct ChatScreen replacement. + await provider.didPushRouteInformation( + RouteInformation(uri: Uri.parse('/chat/target')), + ); + await tester.pump(); + expect(provider.value.uri.path, '/chat/target'); + + await replyChatHistory( + tester, + channel, + subscribeIndex: 1, + historyIndex: 1, + text: 'The notified chat.', + ); + await tester.pumpAndSettle(); + + expect(requestOf(channel, 'chat_subscribe', index: 1)!['chat_subscribe'], { + 'session_id': 'target', + }); + expect(requestOf(channel, 'chat_messages', index: 1)!['chat_query'], { + 'session_id': 'target', + 'limit': 200, + 'cursor': '', + }); + expect(find.text('The notified chat.'), findsOneWidget); + expect(find.text('The retained chat.'), findsNothing); + expect(provider.value.uri.path, '/chat/target'); + + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pump(); + }, + ); + testWidgets( 'mobile Chats retap reopens the retained multi-pane workspace after tabs', (tester) async { diff --git a/app/test/application/search_chat_open_test.dart b/app/test/application/search_chat_open_test.dart index 1ebd3025..03eced06 100644 --- a/app/test/application/search_chat_open_test.dart +++ b/app/test/application/search_chat_open_test.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/transport/pipelines_client.dart'; import 'package:pipelines_app/features/chat/screen.dart'; +import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart'; import 'package:pipelines_app/features/search/search_screen.dart'; import '../core/transport/pipelines_client_test.dart' show FakeChannel; @@ -98,6 +99,14 @@ void main() { expect(find.byType(SearchScreen), findsNothing); final screen = tester.widget(find.byType(ChatScreen)); expect(screen.initialSessionId, _session); + expect( + tester + .widget(find.byType(ChatWorkspace)) + .controller + .activePane + ?.sessionId, + _session, + ); await reply( tester, diff --git a/app/test/features/chat/chat_screen_model_test.dart b/app/test/features/chat/chat_screen_model_test.dart index ee6c448c..659b9356 100644 --- a/app/test/features/chat/chat_screen_model_test.dart +++ b/app/test/features/chat/chat_screen_model_test.dart @@ -3,6 +3,7 @@ import 'package:flutter_test/flutter_test.dart'; import '../../native_back.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/screen.dart'; +import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart'; import 'package:pipelines_app/ui/adaptive/segmented_bar.dart' show segmentedMoreKey; @@ -188,4 +189,79 @@ void main() { expect(gateway.count('chat_models'), 0); }, ); + + for (final width in [390.0, 1200.0]) { + testWidgets( + 'a new route opens the target after Chat is already mounted on $width px', + (tester) async { + final gateway = _ScreenGateway() + ..sessions = [ + {'id': 'old', 'title': 'Old chat'}, + {'id': 'target', 'title': 'Target chat'}, + ]; + tester.view.physicalSize = Size(width, 844); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ChatScreen(gateway: gateway, initialSessionId: 'old'), + ), + ), + ); + await tester.pumpAndSettle(); + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ChatScreen(gateway: gateway, initialSessionId: 'target'), + ), + ), + ); + await tester.pumpAndSettle(); + + final workspace = tester.widget( + find.byType(ChatWorkspace), + ); + expect(workspace.controller.activePane?.sessionId, 'target'); + expect( + find.byKey(const ValueKey('chat-session-target')), + findsOneWidget, + ); + }, + ); + } + + testWidgets( + 'a repeated route reactivates its chat after another pane became active', + (tester) async { + final gateway = _ScreenGateway() + ..sessions = [ + {'id': 'a', 'title': 'Chat a'}, + {'id': 'b', 'title': 'Chat b'}, + ]; + await _pumpScreen(tester, gateway, initialSessionId: 'a'); + final workspace = tester.widget( + find.byType(ChatWorkspace), + ); + workspace.controller.splitActive('b', ChatSplitAxis.horizontal); + final b = workspace.controller.panes.firstWhere( + (pane) => pane.sessionId == 'b', + ); + workspace.controller.activate(b.id); + await tester.pump(); + expect(workspace.controller.activePane?.sessionId, 'b'); + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ChatScreen(gateway: gateway, initialSessionId: 'a'), + ), + ), + ); + await tester.pumpAndSettle(); + + expect(workspace.controller.activePane?.sessionId, 'a'); + }, + ); } diff --git a/app/test/features/chat/chat_workspace_restore_test.dart b/app/test/features/chat/chat_workspace_restore_test.dart index 4b4d6e5c..569529e9 100644 --- a/app/test/features/chat/chat_workspace_restore_test.dart +++ b/app/test/features/chat/chat_workspace_restore_test.dart @@ -179,4 +179,32 @@ void main() { await tester.pumpWidget(const SizedBox.shrink()); await tester.pumpAndSettle(); }); + + testWidgets('repeated deep link reactivates the routed pane after restore', ( + tester, + ) async { + tester.view.physicalSize = const Size(1800, 1000); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + final saved = ChatWorkspaceController()..open('a'); + saved.splitActive('b', ChatSplitAxis.vertical); + SharedPreferences.setMockInitialValues({ + chatWorkspacePreferenceKey: saved.snapshot()!.encode(), + }); + final gateway = _Gateway()..history = (_) async => chatPage([]); + addTearDown(gateway.close); + + await _mount(tester, gateway, route: 'a'); + final controller = _workspace(tester); + final other = controller.panes.firstWhere((pane) => pane.sessionId == 'b'); + controller.activate(other.id); + await tester.pump(); + expect(controller.activePane?.sessionId, 'b'); + + await _mount(tester, gateway, route: 'a'); + expect(_workspace(tester).activePane?.sessionId, 'a'); + + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pumpAndSettle(); + }); }