diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index dca06d8d..e49add25 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -632,6 +632,11 @@ class _ChatThreadViewState extends State { _repair = repair; unawaited(repair); } + // Restored/deep-linked panes mount before the session list is fetched. + // The same session gaining metadata must update its retained explorer. + unawaited( + _files.updateInitialPath('${widget.session?['directory'] ?? ''}'), + ); } @override diff --git a/app/lib/features/chat/files/files_controller.dart b/app/lib/features/chat/files/files_controller.dart index 1791cc50..854e1588 100644 --- a/app/lib/features/chat/files/files_controller.dart +++ b/app/lib/features/chat/files/files_controller.dart @@ -6,7 +6,8 @@ import 'files_models.dart'; class FilesController extends ChangeNotifier { FilesController({required this.gateway, required this.initialPath}); final ApiGateway gateway; - final String initialPath; + String initialPath; + bool _started = false; DirectoryListing? listing; String? error; bool loading = false; @@ -32,7 +33,24 @@ class FilesController extends ChangeNotifier { ); } - Future start() => open(initialPath); + Future start() { + _started = true; + return open(initialPath); + } + + /// Session metadata can arrive after the retained conversation is mounted. + /// Keep a folder the operator navigated to; otherwise load the new root. + Future updateInitialPath(String path) async { + if (_disposed || path.trim().isEmpty || path == initialPath) return; + final previous = initialPath; + initialPath = path; + if (_started && (listing == null || listing!.path == previous)) { + listing = null; + await open(path); + } else { + _notify(); + } + } Future open(String? path) async { if (path == null || path.trim().isEmpty) return; diff --git a/app/test/features/chat/files/files_directory_metadata_test.dart b/app/test/features/chat/files/files_directory_metadata_test.dart new file mode 100644 index 00000000..a433dce7 --- /dev/null +++ b/app/test/features/chat/files/files_directory_metadata_test.dart @@ -0,0 +1,96 @@ +import 'package:flutter/material.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/conversation/chat_thread_view.dart'; +import 'package:pipelines_app/features/chat/files/files_controller.dart'; +import 'package:pipelines_app/features/chat/files/files_preferences.dart'; + +import '../conversation/chat_thread_fixtures.dart'; + +class _Gateway extends FakeChatGateway { + final directories = []; + + @override + Future call(String type, [JsonMap fields = const {}]) { + if (type != 'list_directory') return super.call(type, fields); + final path = (fields['list_directory'] as Map)['path'] as String; + directories.add(path); + return Future.value({ + 'directory_listing': { + 'path': path, + 'home': '/home', + 'entries': [ + {'name': 'loaded.txt', 'path': '$path/loaded.txt'}, + ], + }, + }); + } +} + +void main() { + test('late directory starts an already-open explorer', () async { + final gateway = _Gateway(); + addTearDown(gateway.close); + final controller = FilesController(gateway: gateway, initialPath: ''); + addTearDown(controller.dispose); + await controller.start(); + expect(gateway.directories, isEmpty); + await controller.updateInitialPath('/home/project'); + expect(controller.missingDirectory, isFalse); + expect(controller.listing?.path, '/home/project'); + expect(gateway.directories, ['/home/project']); + await controller.updateInitialPath('/home/project'); + await controller.updateInitialPath(''); + expect(gateway.directories, hasLength(1)); + expect(controller.initialPath, '/home/project'); + }); + + test('late metadata remains lazy and preserves folder navigation', () async { + final gateway = _Gateway(); + addTearDown(gateway.close); + final controller = FilesController(gateway: gateway, initialPath: ''); + addTearDown(controller.dispose); + await controller.updateInitialPath('/home/project'); + expect(gateway.directories, isEmpty); + await controller.start(); + await controller.open('/home/project/assets'); + await controller.updateInitialPath('/home/updated'); + expect(controller.initialPath, '/home/updated'); + expect(controller.listing?.path, '/home/project/assets'); + expect(gateway.directories, hasLength(2)); + }); + + testWidgets('restored thread gains its directory without remounting', ( + tester, + ) async { + SharedPreferences.setMockInitialValues({}); + await const FilesPreferences('').setVisible(true); + tester.view.physicalSize = const Size(1280, 900); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.resetPhysicalSize); + addTearDown(tester.view.resetDevicePixelRatio); + final gateway = _Gateway()..history = (_) async => chatPage([]); + addTearDown(gateway.close); + Widget app(JsonMap? session) => MaterialApp( + home: Scaffold( + body: ChatThreadView( + key: const ValueKey('retained-thread'), + gateway: gateway, + sessionId: 's', + session: session, + onBack: () {}, + ), + ), + ); + await tester.pumpWidget(app(null)); + await tester.pumpAndSettle(); + expect(find.text('This chat has no working directory.'), findsOneWidget); + await tester.pumpWidget(app({'id': 's', 'directory': '/home/project'})); + await tester.pumpAndSettle(); + expect(find.text('This chat has no working directory.'), findsNothing); + expect(find.text('loaded.txt'), findsOneWidget); + expect(gateway.directories, ['/home/project']); + await tester.pumpWidget(const SizedBox()); + }); +}