utils/sync_settings_ui/test/backup_slot_test.dart
Krzysztof kuhy Rudnicki 648bde44b1
Some checks failed
dependency freshness / dependencies on newest stable (push) Waiting to run
file length / file-length (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
music_theory tests / test (push) Waiting to run
palette drift / palette (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
web_ui tests / test (push) Waiting to run
data_transfer_flutter tests / test (push) Has been cancelled
sync_settings_ui tests / test (push) Has been cancelled
sync_settings_ui 0.5.2: cancelled backup is not success
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWXTUM9dpnCS2wtFkx4
2026-11-01 19:46:05 +01:00

117 lines
2.9 KiB
Dart

import 'dart:convert';
import 'package:data_transfer_flutter/data_transfer_flutter.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:sync_settings_ui/sync_settings_ui.dart';
void main() {
test('carries the label and invokes the injected callbacks', () async {
var exported = false;
var imported = false;
final slot = BackupSlot(
label: 'notes',
export: () async => exported = true,
import: () async => imported = true,
);
expect(slot.label, 'notes');
await slot.export();
await slot.import();
expect(exported, isTrue);
expect(imported, isTrue);
});
group('BackupSlot.json', () {
late _FakePlatform fake;
late DataTransferPlatform original;
setUp(() {
original = DataTransfer.platform;
fake = _FakePlatform();
DataTransfer.platform = fake;
});
tearDown(() => DataTransfer.platform = original);
test('export saves the built map under fileName', () async {
final slot = BackupSlot.json(
label: 'inventory',
fileName: 'inv.json',
build: () async => {'items': 3},
restore: (_) async {},
);
await slot.export();
expect(fake.saved.single.$1, 'inv.json');
expect(jsonDecode(fake.saved.single.$2), {'items': 3});
});
test('import hands the picked map to restore', () async {
fake.picked = '{"items":3}';
Map<String, Object?>? restored;
final slot = BackupSlot.json(
label: 'inventory',
fileName: 'inv.json',
build: () async => {},
restore: (json) async => restored = json,
);
await slot.import();
expect(restored, {'items': 3});
});
test('a cancelled picker restores nothing', () async {
var called = false;
final slot = BackupSlot.json(
label: 'inventory',
fileName: 'inv.json',
build: () async => {},
restore: (_) async => called = true,
);
await expectLater(slot.import(), throwsA(isA<BackupCancelled>()));
expect(called, isFalse);
});
test('a cancelled save dialog reports the cancellation', () async {
fake.acceptSave = false;
final slot = BackupSlot.json(
label: 'inventory',
fileName: 'inv.json',
build: () async => {'items': 3},
restore: (_) async {},
);
await expectLater(slot.export(), throwsA(isA<BackupCancelled>()));
});
});
}
class _FakePlatform implements DataTransferPlatform {
String? picked;
bool acceptSave = true;
final saved = <(String, String)>[];
@override
Future<bool> saveText({
required String fileName,
required String text,
String mimeType = 'application/json',
}) async {
saved.add((fileName, text));
return acceptSave;
}
@override
Future<bool> shareText({
required String fileName,
required String text,
String mimeType = 'application/json',
}) async => true;
@override
Future<String?> pickText() async => picked;
}