Refresh retained explorer when chat directory metadata arrives

This commit is contained in:
2026-11-07 02:41:52 +00:00
parent b33970182c
commit 9e6d55942f
3 changed files with 121 additions and 2 deletions
@@ -632,6 +632,11 @@ class _ChatThreadViewState extends State<ChatThreadView> {
_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
@@ -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<void> start() => open(initialPath);
Future<void> 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<void> 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<void> open(String? path) async {
if (path == null || path.trim().isEmpty) return;
@@ -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 = <String>[];
@override
Future<JsonMap> 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());
});
}