utils/sync_settings_ui/test/sync_settings_screen_backup_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

176 lines
5.3 KiB
Dart

import 'dart:async';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:sync_settings_ui/sync_settings_ui.dart';
FirebaseRestClient _fakeClient() => FirebaseRestClient(
databaseUrl: 'https://example.firebasedatabase.app',
auth: FirebaseTokenProvider(
apiKey: 'test-key',
store: InMemoryCredentialStore(),
),
);
Widget _wrap(Widget child) => MaterialApp(home: child);
void main() {
group('Backup section', () {
testWidgets('a cancelled export or import claims no success', (
tester,
) async {
await tester.pumpWidget(
_wrap(
SyncSettingsScreen(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
backup: BackupSlot(
label: 'notes',
export: () async => throw const BackupCancelled(),
import: () async => throw const BackupCancelled(),
),
),
),
);
await tester.pumpAndSettle();
await tester.tap(find.text('Export notes'));
await tester.pumpAndSettle();
expect(find.text('Exported notes.'), findsNothing);
expect(find.textContaining('failed'), findsNothing);
await tester.tap(find.text('Import notes'));
await tester.pumpAndSettle();
expect(find.text('Imported notes.'), findsNothing);
expect(find.textContaining('failed'), findsNothing);
});
testWidgets('omitted entirely when backup is null', (tester) async {
await tester.pumpWidget(
_wrap(
SyncSettingsScreen(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
),
),
);
await tester.pumpAndSettle();
expect(find.text('Backup'), findsNothing);
});
testWidgets('export and import call the injected callbacks', (
tester,
) async {
var exported = false;
var imported = false;
await tester.pumpWidget(
_wrap(
SyncSettingsScreen(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
backup: BackupSlot(
label: 'notes',
export: () async => exported = true,
import: () async => imported = true,
),
),
),
);
await tester.pumpAndSettle();
expect(find.text('Backup'), findsOneWidget);
await tester.tap(find.text('Export notes'));
await tester.pumpAndSettle();
expect(exported, isTrue);
expect(find.text('Exported notes.'), findsOneWidget);
await tester.tap(find.text('Import notes'));
await tester.pumpAndSettle();
expect(imported, isTrue);
expect(find.text('Imported notes.'), findsOneWidget);
});
testWidgets('export failure reports the error', (tester) async {
await tester.pumpWidget(
_wrap(
SyncSettingsScreen(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
backup: BackupSlot(
label: 'notes',
export: () async => throw Exception('disk full'),
import: () async {},
),
),
),
);
await tester.pumpAndSettle();
await tester.tap(find.text('Export notes'));
await tester.pumpAndSettle();
expect(find.textContaining('Export failed'), findsOneWidget);
});
testWidgets('import failure reports the error', (tester) async {
await tester.pumpWidget(
_wrap(
SyncSettingsScreen(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
backup: BackupSlot(
label: 'notes',
export: () async {},
import: () async => throw Exception('corrupt file'),
),
),
),
);
await tester.pumpAndSettle();
await tester.tap(find.text('Import notes'));
await tester.pumpAndSettle();
expect(find.textContaining('Import failed'), findsOneWidget);
});
});
testWidgets('shows a loading indicator until initial load completes', (
tester,
) async {
final completer = Completer<FirebaseAccount?>();
await tester.pumpWidget(
_wrap(
SyncSettingsScreen(
accountLoader: () => completer.future,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
),
),
);
await tester.pump();
expect(find.byType(CircularProgressIndicator), findsOneWidget);
completer.complete(null);
await tester.pumpAndSettle();
expect(find.byType(CircularProgressIndicator), findsNothing);
});
}