mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-09-30 05:12:00 +02:00
feat(backup): allow selecting categories and settings-only backups
This commit is contained in:
@@ -4,6 +4,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:spotiflac_android/l10n/app_localizations.dart';
|
||||
import 'package:spotiflac_android/screens/settings/backup_restore_page.dart';
|
||||
import 'package:spotiflac_android/widgets/settings_group.dart';
|
||||
|
||||
class _BackupFilePicker extends FilePickerPlatform {
|
||||
bool opened = false;
|
||||
@@ -32,6 +33,45 @@ class _BackupFilePicker extends FilePickerPlatform {
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'settings-only preset and empty selection update export controls',
|
||||
(tester) async {
|
||||
await tester.pumpWidget(
|
||||
const ProviderScope(
|
||||
child: MaterialApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: BackupRestorePage(),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.tap(find.text('Settings only'));
|
||||
await tester.pumpAndSettle();
|
||||
final switches = tester
|
||||
.widgetList<SettingsSwitchItem>(find.byType(SettingsSwitchItem))
|
||||
.toList();
|
||||
expect(switches.map((item) => item.value), [
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
]);
|
||||
expect(switches.last.onChanged, isNull);
|
||||
switches.first.onChanged!(false);
|
||||
await tester.pumpAndSettle();
|
||||
final export = tester
|
||||
.widgetList<SettingsItem>(find.byType(SettingsItem))
|
||||
.firstWhere((item) => item.icon == Icons.ios_share);
|
||||
expect(export.onTap, isNull);
|
||||
expect(
|
||||
export.subtitle,
|
||||
'Select at least one category to create a backup.',
|
||||
);
|
||||
expect(tester.takeException(), isNull);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('restore does not exclude backups with an unknown MIME type', (
|
||||
tester,
|
||||
) async {
|
||||
@@ -49,7 +89,7 @@ void main() {
|
||||
),
|
||||
);
|
||||
final restore = find.text('Choose backup file');
|
||||
await tester.ensureVisible(restore);
|
||||
await tester.scrollUntilVisible(restore, 300);
|
||||
await tester.tap(restore);
|
||||
await tester.pumpAndSettle();
|
||||
expect(picker.opened, isTrue);
|
||||
|
||||
@@ -1,74 +1,176 @@
|
||||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:archive/archive_io.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:spotiflac_android/services/backup_service.dart';
|
||||
|
||||
void main() {
|
||||
test('ZIP v2 pages history and streams cover files during restore', () async {
|
||||
final root = await Directory.systemTemp.createTemp(
|
||||
'spotiflac-backup-test-',
|
||||
);
|
||||
addTearDown(() async {
|
||||
if (await root.exists()) await root.delete(recursive: true);
|
||||
});
|
||||
final output = await Directory(p.join(root.path, 'output')).create();
|
||||
final temporary = await Directory(p.join(root.path, 'temporary')).create();
|
||||
final cover = File(p.join(root.path, 'cover.jpg'));
|
||||
await cover.writeAsBytes([0xff, 0xd8, 0xff, 0xd9]);
|
||||
final history = List.generate(
|
||||
1203,
|
||||
(index) => <String, dynamic>{'id': 'item-$index', 'value': index},
|
||||
);
|
||||
final offsets = <int>[];
|
||||
test(
|
||||
'ZIP backup pages history and streams cover files during restore',
|
||||
() async {
|
||||
final root = await Directory.systemTemp.createTemp(
|
||||
'spotiflac-backup-test-',
|
||||
);
|
||||
addTearDown(() async {
|
||||
if (await root.exists()) await root.delete(recursive: true);
|
||||
});
|
||||
final output = await Directory(p.join(root.path, 'output')).create();
|
||||
final temporary = await Directory(
|
||||
p.join(root.path, 'temporary'),
|
||||
).create();
|
||||
final cover = File(p.join(root.path, 'cover.jpg'));
|
||||
await cover.writeAsBytes([0xff, 0xd8, 0xff, 0xd9]);
|
||||
final history = List.generate(
|
||||
1203,
|
||||
(index) => <String, dynamic>{'id': 'item-$index', 'value': index},
|
||||
);
|
||||
final offsets = <int>[];
|
||||
|
||||
final file = await BackupService.writeBackupArchive(
|
||||
settings: const {'theme': 'dark'},
|
||||
loadHistoryPage: (limit, offset) async {
|
||||
offsets.add(offset);
|
||||
if (offset >= history.length) {
|
||||
return const <Map<String, dynamic>>[];
|
||||
}
|
||||
final end = (offset + limit).clamp(0, history.length);
|
||||
return history.sublist(offset, end);
|
||||
},
|
||||
collections: const {
|
||||
'loved': <dynamic>[],
|
||||
'wishlist': <dynamic>[],
|
||||
'playlists': <dynamic>[],
|
||||
},
|
||||
playlistCoverFiles: {
|
||||
'playlist-1': {'ext': '.jpg', 'path': cover.path},
|
||||
},
|
||||
extensions: const {'items': <dynamic>[]},
|
||||
outputDirectory: output,
|
||||
temporaryDirectory: temporary,
|
||||
);
|
||||
final file = await BackupService.writeBackupArchive(
|
||||
settings: const {'theme': 'dark'},
|
||||
loadHistoryPage: (limit, offset) async {
|
||||
offsets.add(offset);
|
||||
if (offset >= history.length) {
|
||||
return const <Map<String, dynamic>>[];
|
||||
}
|
||||
final end = (offset + limit).clamp(0, history.length);
|
||||
return history.sublist(offset, end);
|
||||
},
|
||||
collections: const {
|
||||
'loved': <dynamic>[],
|
||||
'wishlist': <dynamic>[],
|
||||
'playlists': <dynamic>[],
|
||||
},
|
||||
playlistCoverFiles: {
|
||||
'playlist-1': {'ext': '.jpg', 'path': cover.path},
|
||||
},
|
||||
extensions: const {'items': <dynamic>[]},
|
||||
outputDirectory: output,
|
||||
temporaryDirectory: temporary,
|
||||
);
|
||||
|
||||
expect(file.path, endsWith('.${BackupService.fileExtension}'));
|
||||
expect(offsets, [0, 500, 1000]);
|
||||
final bundle = await BackupService.parseFile(
|
||||
file.path,
|
||||
temporaryDirectory: temporary,
|
||||
);
|
||||
expect(bundle, isNotNull);
|
||||
expect(bundle!.formatVersion, 2);
|
||||
expect(bundle.historyCount, history.length);
|
||||
final restoredHistory = await bundle.streamHistory().toList();
|
||||
expect(restoredHistory, hasLength(history.length));
|
||||
expect(restoredHistory.last['id'], 'item-1202');
|
||||
final restoredCover = bundle.playlistCovers['playlist-1'];
|
||||
expect(restoredCover, isA<Map<String, dynamic>>());
|
||||
final restoredCoverPath =
|
||||
(restoredCover as Map<String, dynamic>)['path'] as String;
|
||||
expect(await File(restoredCoverPath).readAsBytes(), [
|
||||
0xff,
|
||||
0xd8,
|
||||
0xff,
|
||||
0xd9,
|
||||
]);
|
||||
expect(file.path, endsWith('.${BackupService.fileExtension}'));
|
||||
expect(offsets, [0, 500, 1000]);
|
||||
final bundle = await BackupService.parseFile(
|
||||
file.path,
|
||||
temporaryDirectory: temporary,
|
||||
);
|
||||
expect(bundle, isNotNull);
|
||||
expect(bundle!.formatVersion, 3);
|
||||
expect(bundle.historyCount, history.length);
|
||||
final restoredHistory = await bundle.streamHistory().toList();
|
||||
expect(restoredHistory, hasLength(history.length));
|
||||
expect(restoredHistory.last['id'], 'item-1202');
|
||||
final restoredCover = bundle.playlistCovers['playlist-1'];
|
||||
expect(restoredCover, isA<Map<String, dynamic>>());
|
||||
final restoredCoverPath =
|
||||
(restoredCover as Map<String, dynamic>)['path'] as String;
|
||||
expect(await File(restoredCoverPath).readAsBytes(), [
|
||||
0xff,
|
||||
0xd8,
|
||||
0xff,
|
||||
0xd9,
|
||||
]);
|
||||
|
||||
await bundle.cleanup();
|
||||
expect(await File(restoredCoverPath).exists(), isFalse);
|
||||
await bundle.cleanup();
|
||||
expect(await File(restoredCoverPath).exists(), isFalse);
|
||||
},
|
||||
);
|
||||
|
||||
test('selective backups round trip every category combination', () async {
|
||||
final root = await Directory.systemTemp.createTemp('selective-backup-');
|
||||
addTearDown(() => root.delete(recursive: true));
|
||||
for (var mask = 1; mask < 16; mask++) {
|
||||
final settings = mask & 1 != 0;
|
||||
final history = mask & 2 != 0;
|
||||
final collections = mask & 4 != 0;
|
||||
final extensions = mask & 8 != 0;
|
||||
var historyReads = 0;
|
||||
final file = await BackupService.writeBackupArchive(
|
||||
settings: settings ? {'theme': 'dark'} : null,
|
||||
includeHistory: history,
|
||||
loadHistoryPage: (_, _) async {
|
||||
historyReads++;
|
||||
return [
|
||||
{'id': 'download-1'},
|
||||
];
|
||||
},
|
||||
collections: collections
|
||||
? {
|
||||
'loved': ['track-1'],
|
||||
}
|
||||
: {},
|
||||
playlistCoverFiles: {},
|
||||
extensions: extensions
|
||||
? {
|
||||
'items': [
|
||||
{'id': 'example-extension'},
|
||||
],
|
||||
}
|
||||
: {},
|
||||
outputDirectory: root,
|
||||
temporaryDirectory: root,
|
||||
);
|
||||
final archive = ZipDecoder().decodeBytes(await file.readAsBytes());
|
||||
final metadata =
|
||||
jsonDecode(utf8.decode(archive.find('metadata.json')!.content))
|
||||
as Map<String, dynamic>;
|
||||
final data = metadata['data'] as Map<String, dynamic>;
|
||||
expect(data.containsKey('settings'), settings);
|
||||
expect(data.containsKey('collections'), collections);
|
||||
expect(data.containsKey('extensions'), extensions);
|
||||
expect(archive.find('history.ndjson') != null, history);
|
||||
expect(historyReads, history ? 1 : 0);
|
||||
final bundle = (await BackupService.parseFile(
|
||||
file.path,
|
||||
temporaryDirectory: root,
|
||||
))!;
|
||||
expect(bundle.hasSettings, settings);
|
||||
expect(bundle.hasHistory, history);
|
||||
expect(bundle.hasCollections, collections);
|
||||
expect(bundle.hasExtensions, extensions);
|
||||
expect(
|
||||
await bundle.streamHistory().toList(),
|
||||
history
|
||||
? [
|
||||
{'id': 'download-1'},
|
||||
]
|
||||
: isEmpty,
|
||||
);
|
||||
await bundle.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
test(
|
||||
'version 2 archives and legacy JSON retain history restore semantics',
|
||||
() async {
|
||||
final root = await Directory.systemTemp.createTemp('legacy-backup-');
|
||||
addTearDown(() => root.delete(recursive: true));
|
||||
final envelope = BackupService.buildEnvelope(
|
||||
settings: {'theme': 'light'},
|
||||
history: [],
|
||||
collections: {},
|
||||
playlistCovers: {},
|
||||
extensions: {},
|
||||
);
|
||||
expect(BackupService.parse(jsonEncode(envelope))!.hasHistory, isTrue);
|
||||
envelope['format_version'] = 2;
|
||||
final metadata = utf8.encode(jsonEncode(envelope));
|
||||
final archive = Archive()
|
||||
..addFile(ArchiveFile('metadata.json', metadata.length, metadata))
|
||||
..addFile(ArchiveFile('history.ndjson', 0, <int>[]));
|
||||
final file = File(p.join(root.path, 'old.sflb'));
|
||||
await file.writeAsBytes(ZipEncoder().encode(archive));
|
||||
final bundle = (await BackupService.parseFile(
|
||||
file.path,
|
||||
temporaryDirectory: root,
|
||||
))!;
|
||||
expect(bundle.formatVersion, 2);
|
||||
expect(bundle.hasHistory, isTrue);
|
||||
expect(bundle.hasSettings, isTrue);
|
||||
await bundle.cleanup();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user