import 'dart:convert'; import 'package:flutter_test/flutter_test.dart'; import 'package:marianum_mobile/api/marianumcloud/webdav/queries/list_files/cacheable_file.dart'; import 'package:marianum_mobile/api/marianumcloud/webdav/queries/list_files/list_files_response.dart'; import 'package:marianum_mobile/view/pages/files/search/local_cache_search.dart'; CacheableFile _file({ required String path, required String name, bool isDirectory = false, }) => CacheableFile(path: path, isDirectory: isDirectory, name: name); String _payload(ListFilesResponse listing) => jsonEncode(listing.toJson()); void main() { group('local cache index', () { final root = ListFilesResponse({ _file(path: 'Documents/', name: 'Documents', isDirectory: true), _file(path: 'Photos/', name: 'Photos', isDirectory: true), _file(path: 'Reports.pdf', name: 'Reports.pdf'), }); final documents = ListFilesResponse({ _file(path: 'Documents/Tax-Report.pdf', name: 'Tax-Report.pdf'), _file(path: 'Documents/Notes.txt', name: 'Notes.txt'), }); final index = buildLocalCacheIndex([ _payload(root), _payload(documents), ]); test('matches by name case-insensitively across all caches', () { final hits = searchLocalCacheIndex(index, 'report'); final paths = hits.map((f) => f.path).toSet(); expect(paths, {'Reports.pdf', 'Documents/Tax-Report.pdf'}); }); test('returns empty list for empty query', () { expect(searchLocalCacheIndex(index, ' '), isEmpty); }); test('respects pathScope prefix', () { final hits = searchLocalCacheIndex( index, 'report', pathScope: ['Documents'], ); expect(hits.map((f) => f.path), ['Documents/Tax-Report.pdf']); }); test('skips payloads that are not folder listings', () { expect(buildLocalCacheIndex(['not json', '{"foo": 1}']), isEmpty); }); test('deduplicates entries that appear in multiple cached folders', () { final shared = _file( path: 'Documents/Tax-Report.pdf', name: 'Tax-Report.pdf', ); final dedupRoot = ListFilesResponse({shared}); final dedupIndex = buildLocalCacheIndex([ _payload(dedupRoot), _payload(dedupRoot), ]); expect(searchLocalCacheIndex(dedupIndex, 'tax'), hasLength(1)); }); }); }