Add a home-restricted folder picker to the new-chat dialog

Server: list_directory returns a directory_listing (path, home, parent,
entries with is_dir/is_git_repo/hidden, truncated) confined to the server
user's home subtree after cleaning and resolving symlinks, refusing pseudo
filesystems and capping entries at 2000. Documented in docs/cli.md.

App: "Choose folder…" in the create dialog opens a picker (full-screen page
on phones, large dialog on wide windows, Material and Cupertino) with
breadcrumbs, filter, Show hidden, git badges, dimmed files, recent folders,
inline errors and "Start chat here". The probe app gains a folder fixture
for captures.
This commit is contained in:
2026-11-01 21:51:48 +00:00
parent eadd017bdc
commit eed3806efb
25 changed files with 2333 additions and 9 deletions
@@ -0,0 +1,52 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/data/directory_listing.dart';
void main() {
test('parses the directory_listing reply, null parent at home', () {
final listing = DirectoryListing.fromReply({
'type': 'directory_listing',
'directory_listing': {
'path': '/home/op',
'home': '/home/op',
'parent': null,
'entries': [
{
'name': 'src',
'path': '/home/op/src',
'is_dir': true,
'is_git_repo': true,
'hidden': false,
},
{'name': '.zshrc', 'path': '/home/op/.zshrc', 'hidden': true},
],
'truncated': true,
},
});
expect(listing.parent, isNull);
expect(listing.truncated, isTrue);
expect(listing.entries.first.isGitRepo, isTrue);
expect(listing.entries.last.isDir, isFalse);
expect(listing.entries.last.hidden, isTrue);
});
test('a reply without a listing is an error, not an empty folder', () {
expect(
() => DirectoryListing.fromReply({'type': 'pong'}),
throwsStateError,
);
});
test('crumbs run from ~ to the folder', () {
expect(folderCrumbs('/home/op', '/home/op/a/b'), [
(label: '~', path: '/home/op'),
(label: 'a', path: '/home/op/a'),
(label: 'b', path: '/home/op/a/b'),
]);
expect(folderCrumbs('/home/op', '/home/opal'), [
(label: '~', path: '/home/op'),
]);
expect(isInsideHome('/home/op', '/home/opal'), isFalse);
expect(homeRelativeLabel('/home/op', '/home/op/a'), '~/a');
expect(homeRelativeLabel('/home/op', '/srv'), '/srv');
});
}
@@ -0,0 +1,84 @@
import 'dart:async';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/core/transport/connection_state.dart';
const fakeHome = '/home/op';
JsonMap _entry(
String parent,
String name, {
bool dir = true,
bool git = false,
}) => {
'name': name,
'path': '$parent/$name',
'is_dir': dir,
'is_git_repo': git,
'hidden': name.startsWith('.'),
};
/// A server-shaped home tree for `list_directory`; anything else is refused
/// with the server's words, like the real handler.
final Map<String, List<JsonMap>> fakeTree = {
fakeHome: [
_entry(fakeHome, '.config'),
_entry(fakeHome, 'projects'),
_entry(fakeHome, 'src', git: true),
_entry(fakeHome, '.zshrc', dir: false),
_entry(fakeHome, 'notes.txt', dir: false),
],
'$fakeHome/projects': [
_entry('$fakeHome/projects', 'app', git: true),
_entry('$fakeHome/projects', 'web'),
_entry('$fakeHome/projects', 'README.md', dir: false),
],
'$fakeHome/projects/app': [_entry('$fakeHome/projects/app', 'lib')],
'$fakeHome/projects/web': const [],
'$fakeHome/src': const [],
};
String? _parent(String path) {
if (path == fakeHome) return null;
return path.substring(0, path.lastIndexOf('/'));
}
/// Answers `list_directory` from [fakeTree] and records requested paths.
class FakeDirectoryGateway implements ApiGateway {
final requests = <String?>[];
/// Paths that are refused like the server refuses a locked folder.
final denied = <String>{'$fakeHome/locked'};
@override
Stream<JsonMap> get frames => const Stream.empty();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
if (type != 'list_directory') {
throw ProtocolError('Fake does not implement $type');
}
final body = fields['list_directory'] as Map? ?? const {};
final requested = body['path'] as String?;
requests.add(requested);
final path = requested == null || requested == '~' ? fakeHome : requested;
if (denied.contains(path)) throw ProtocolError('permission denied: $path');
if (path != fakeHome && !path.startsWith('$fakeHome/')) {
throw ProtocolError(
'path $path is outside the home directory $fakeHome; only folders '
'inside home can be listed',
);
}
final entries = fakeTree[path];
if (entries == null) throw ProtocolError('$path does not exist');
return {
'type': 'directory_listing',
'directory_listing': {
'path': path,
'home': fakeHome,
'parent': _parent(path),
'entries': entries,
},
};
}
}
@@ -0,0 +1,237 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/ui/folder_picker/folder_picker_route.dart';
import 'fake_directory_gateway.dart';
/// Hosts a button that opens the picker and records what it returns.
class _Launcher extends StatelessWidget {
const _Launcher({
required this.gateway,
required this.onResult,
this.initialPath,
this.recents = const [],
});
final FakeDirectoryGateway gateway;
final ValueChanged<String?> onResult;
final String? initialPath;
final List<String> recents;
@override
Widget build(BuildContext context) {
return Center(
child: CupertinoButton(
child: const Text('Open picker'),
onPressed: () async => onResult(
await showFolderPicker(
context,
gateway: gateway,
initialPath: initialPath,
recents: recents,
),
),
),
);
}
}
enum _Host { material, cupertino }
Future<void> _pump(
WidgetTester tester,
_Host host,
Widget launcher, {
Size size = const Size(420, 900),
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(
host == _Host.material
? MaterialApp(
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(body: launcher),
)
: CupertinoApp(home: CupertinoPageScaffold(child: launcher)),
);
await tester.tap(find.text('Open picker'));
await tester.pumpAndSettle();
}
Finder _crumb(String path) => find.byKey(ValueKey('folder-crumb-$path'));
void main() {
for (final host in _Host.values) {
testWidgets('${host.name}: opens at home, navigates, jumps up and returns '
'the folder', (tester) async {
final gateway = FakeDirectoryGateway();
String? result = 'unset';
await _pump(
tester,
host,
_Launcher(gateway: gateway, onResult: (value) => result = value),
);
expect(gateway.requests, [null]);
expect(find.text('Choose folder'), findsOneWidget);
expect(find.text('projects'), findsOneWidget);
expect(find.text('src'), findsOneWidget);
expect(find.text('git'), findsOneWidget);
expect(find.text('notes.txt'), findsOneWidget);
expect(find.text('Start in ~'), findsOneWidget);
await tester.tap(find.text('projects'));
await tester.pumpAndSettle();
expect(gateway.requests.last, '$fakeHome/projects');
expect(find.text('app'), findsOneWidget);
expect(find.text('README.md'), findsOneWidget);
expect(find.text('Start in ~/projects'), findsOneWidget);
await tester.tap(find.text('app'));
await tester.pumpAndSettle();
expect(find.text('lib'), findsOneWidget);
// The home crumb jumps straight back up two levels.
await tester.tap(_crumb(fakeHome));
await tester.pumpAndSettle();
expect(gateway.requests.last, fakeHome);
expect(find.text('Start in ~'), findsOneWidget);
// Back climbs one folder instead of leaving the picker.
await tester.tap(find.text('projects'));
await tester.pumpAndSettle();
await tester.tap(find.text('web'));
await tester.pumpAndSettle();
expect(find.text('This folder is empty.'), findsOneWidget);
await tester.tap(
host == _Host.material
? find.byType(BackButton)
: find.byType(CupertinoNavigationBarBackButton),
);
await tester.pumpAndSettle();
expect(find.text('Start in ~/projects'), findsOneWidget);
expect(result, 'unset');
await tester.tap(find.byKey(const Key('folder-picker-start')));
await tester.pumpAndSettle();
expect(result, '$fakeHome/projects');
expect(find.text('Choose folder'), findsNothing);
});
testWidgets('${host.name}: hidden toggle and filter', (tester) async {
final gateway = FakeDirectoryGateway();
await _pump(tester, host, _Launcher(gateway: gateway, onResult: (_) {}));
expect(find.text('.config'), findsNothing);
expect(find.text('.zshrc'), findsNothing);
expect(find.text('Show hidden (2)'), findsOneWidget);
await tester.tap(find.byKey(const Key('folder-picker-hidden')));
await tester.pumpAndSettle();
expect(find.text('.config'), findsOneWidget);
expect(find.text('.zshrc'), findsOneWidget);
await tester.enterText(
find.byKey(const Key('folder-picker-filter')),
'PRO',
);
await tester.pumpAndSettle();
expect(find.text('projects'), findsOneWidget);
expect(find.text('src'), findsNothing);
await tester.enterText(
find.byKey(const Key('folder-picker-filter')),
'zzz',
);
await tester.pumpAndSettle();
expect(find.textContaining('Nothing here matches'), findsOneWidget);
});
testWidgets('${host.name}: a refused folder is shown inline and home '
'recovers', (tester) async {
final gateway = FakeDirectoryGateway();
String? result = 'unset';
await _pump(
tester,
host,
_Launcher(
gateway: gateway,
initialPath: '/etc',
onResult: (value) => result = value,
),
);
expect(find.textContaining('outside the home directory'), findsOneWidget);
expect(find.text('No folder selected'), findsOneWidget);
await tester.tap(find.byKey(const Key('folder-picker-start')));
await tester.pumpAndSettle();
expect(result, 'unset');
await tester.tap(find.text('Go to home'));
await tester.pumpAndSettle();
expect(find.text('projects'), findsOneWidget);
gateway.denied.add('$fakeHome/projects');
await tester.tap(find.text('projects'));
await tester.pumpAndSettle();
expect(
find.text('permission denied: $fakeHome/projects'),
findsOneWidget,
);
await tester.tap(find.text('Back to ~'));
await tester.pumpAndSettle();
expect(find.text('projects'), findsOneWidget);
expect(find.text('Start in ~'), findsOneWidget);
});
}
testWidgets('opens at an initial folder and offers recents at home', (
tester,
) async {
final gateway = FakeDirectoryGateway();
await _pump(
tester,
_Host.material,
_Launcher(
gateway: gateway,
initialPath: '$fakeHome/projects',
recents: const ['$fakeHome/projects/app', '/elsewhere'],
onResult: (_) {},
),
);
expect(gateway.requests, ['$fakeHome/projects']);
expect(find.text('Recent folders'), findsNothing);
await tester.tap(_crumb(fakeHome));
await tester.pumpAndSettle();
expect(find.text('Recent folders'), findsOneWidget);
expect(find.text('~/projects/app'), findsOneWidget);
expect(find.text('/elsewhere'), findsNothing);
await tester.tap(find.text('~/projects/app'));
await tester.pumpAndSettle();
expect(find.text('lib'), findsOneWidget);
});
for (final host in _Host.values) {
testWidgets('${host.name}: wide windows get a dialog, phones a page', (
tester,
) async {
final gateway = FakeDirectoryGateway();
await _pump(
tester,
host,
_Launcher(gateway: gateway, onResult: (_) {}),
size: const Size(1000, 900),
);
final page = tester.getSize(find.text('Choose folder'));
expect(page.width, greaterThan(0));
final scaffoldType = host == _Host.material
? Scaffold
: CupertinoPageScaffold;
final picker = find.ancestor(
of: find.text('Choose folder'),
matching: find.byType(scaffoldType),
);
expect(tester.getSize(picker.first).width, lessThan(1000));
expect(
host == _Host.material ? find.byType(Dialog) : find.byType(ClipRRect),
findsWidgets,
);
});
}
}
@@ -0,0 +1,88 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import '../folder_picker/fake_directory_gateway.dart';
import 'chat_test_harness.dart';
import 'fake_chat_gateway.dart';
/// A chat gateway whose `list_directory` answers come from the fake home tree.
FakeChatGateway _gateway(FakeDirectoryGateway directories) {
final gateway = FakeChatGateway();
gateway.directoriesHandler = () async => {
'chat_directories': [fakeHome, '$fakeHome/projects/app', '/srv/other'],
};
gateway.actionHandler = (type, fields) => directories.call(type, fields);
return gateway;
}
void main() {
final hosts = <String, Future<void> Function(WidgetTester, FakeChatGateway)>{
'material': (tester, gateway) => pumpMaterialChat(tester, gateway),
'cupertino': (tester, gateway) => pumpCupertinoChat(tester, gateway),
'ios material': (tester, gateway) => pumpIosMaterialChat(tester, gateway),
};
for (final MapEntry(key: name, value: pump) in hosts.entries) {
testWidgets('$name: Choose folder fills the directory and creates there', (
tester,
) async {
usePhoneSurface(tester);
final directories = FakeDirectoryGateway();
final gateway = _gateway(directories);
await pump(tester, gateway);
await tester.tap(find.byKey(const Key('chat-new-chat')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-create-choose-folder')));
await tester.pumpAndSettle();
// Opened at home (the dialog's current choice is ~) with the recent
// folder inside home offered first.
expect(directories.requests, [null]);
expect(find.text('Recent folders'), findsOneWidget);
expect(find.text('~/projects/app'), findsOneWidget);
expect(find.text('/srv/other'), findsNothing);
await tester.tap(find.text('projects'));
await tester.pumpAndSettle();
await tester.tap(find.text('web'));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('folder-picker-start')));
await tester.pumpAndSettle();
expect(find.text('Choose folder'), findsNothing);
expect(find.text('Start in $fakeHome/projects/web'), findsOneWidget);
await tester.tap(find.byKey(const Key('chat-create-confirm')));
await tester.pumpAndSettle();
final fields =
gateway.lastFields('chat_create')!['chat_create']! as JsonMap;
expect(fields['directory'], '$fakeHome/projects/web');
expect(fields['title'], 'Web OpenCode');
});
}
testWidgets('the picker reopens at the chosen folder; cancel keeps it', (
tester,
) async {
usePhoneSurface(tester);
final directories = FakeDirectoryGateway();
final gateway = _gateway(directories);
await pumpMaterialChat(tester, gateway);
await tester.tap(find.byKey(const Key('chat-new-chat')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-create-choose-folder')));
await tester.pumpAndSettle();
await tester.tap(find.text('src'));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('folder-picker-start')));
await tester.pumpAndSettle();
expect(find.text('Start in $fakeHome/src'), findsOneWidget);
await tester.tap(find.byKey(const Key('chat-create-choose-folder')));
await tester.pumpAndSettle();
expect(directories.requests.last, '$fakeHome/src');
await tester.tap(find.text('Cancel').last);
await tester.pumpAndSettle();
expect(find.text('Start in $fakeHome/src'), findsOneWidget);
});
}