tane/apps/app_seeds/test/ui/backup_section_test.dart
vjrj 9bf5c5cfbc feat(i18n): add Asturian (asturianu) as a language
Adds Asturianu, translated in full (345 strings), plus the wiring to
make a minority language work end to end — the OS language picker is an
unreliable way to reach it, so the in-app choice must be explicit and
remembered.

- Model Asturian as the real `ast` locale (honest, international-by-design),
  not a fake `es-AST` region — slang rejects a 3-letter region anyway.
- Flutter ships no Material/Cupertino/intl localizations for `ast`, so map
  the framework chrome to Spanish (`materialLocaleFor`); the app's own text
  stays Asturian via slang. Fixes an "Invalid locale ast" crash when the
  auto-backup date was formatted with intl.
- Persist the picked language in the keystore (LocaleStore) and restore it
  at startup so it survives restarts and wins over the device locale.
- Settings: add the Asturianu tile; compare the full AppLocale so `es` and
  `ast` aren't both marked selected.

Tests: LocaleStore round-trip, locale switch + Spanish-chrome fallback,
and a regression for the intl date crash under Asturian.
2026-07-10 15:43:33 +02:00

333 lines
11 KiB
Dart

import 'dart:convert';
import 'dart:typed_data';
import 'package:commons_core/commons_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/data/export_import/inventory_json_codec.dart';
import 'package:tane/db/database.dart';
import 'package:tane/i18n/strings.g.dart';
import 'package:tane/security/secure_key_store.dart';
import 'package:tane/services/auto_backup_service.dart';
import 'package:tane/services/auto_backup_store.dart';
import 'package:tane/services/export_import_service.dart';
import 'package:tane/services/file_service.dart';
import 'package:tane/services/recovery_sheet_service.dart';
import 'package:tane/app.dart';
import 'package:tane/ui/backup_section.dart';
import 'package:tane/ui/settings_screen.dart';
import '../support/test_support.dart';
/// Records calls instead of opening real dialogs.
class FakeFileService implements FileService {
String? savedName;
Uint8List? savedBytes;
Uint8List? bytesToPick;
@override
Future<String?> saveFile({
required String suggestedName,
required Uint8List bytes,
}) async {
savedName = suggestedName;
savedBytes = bytes;
return '/picked/$suggestedName';
}
@override
Future<Uint8List?> pickFileBytes({List<String>? allowedExtensions}) async =>
bytesToPick;
}
void main() {
late AppDatabase db;
late FakeFileService files;
late SecureKeyStore keys;
late ExportImportService service;
late RecoverySheetService sheet;
setUp(() {
db = newTestDatabase();
files = FakeFileService();
keys = SecureKeyStore(store: InMemorySecretStore());
service = ExportImportService(
repository: newTestRepository(db),
files: files,
keys: keys,
);
sheet = RecoverySheetService(files: files);
});
tearDown(() => db.close());
Widget wrap(Widget child) {
LocaleSettings.setLocaleSync(AppLocale.en);
return TranslationProvider(
child: MaterialApp(
supportedLocales: AppLocaleUtils.supportedLocales,
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: child,
),
);
}
Widget section() => wrap(
Scaffold(
body: BackupSection(service: service, sheet: sheet),
),
);
testWidgets('settings shows the backup section with its actions', (
tester,
) async {
await tester.binding.setSurfaceSize(const Size(800, 1400));
addTearDown(() => tester.binding.setSurfaceSize(null));
await tester.pumpWidget(wrap(SettingsScreen(exportImport: service)));
expect(find.text('Backup & restore'), findsOneWidget);
expect(find.text('Save a backup'), findsOneWidget);
expect(find.text('Restore a backup'), findsOneWidget);
expect(find.text('Your recovery code'), findsOneWidget);
expect(find.text('Export to a spreadsheet'), findsOneWidget);
expect(find.text('Import a list'), findsOneWidget);
});
testWidgets('spreadsheet export saves a .csv file and confirms', (
tester,
) async {
await tester.pumpWidget(section());
await tester.tap(find.text('Export to a spreadsheet'));
await tester.pumpAndSettle();
expect(files.savedName, endsWith('.csv'));
expect(utf8.decode(files.savedBytes!), startsWith('variety_id,'));
expect(find.text('Copy saved'), findsOneWidget);
});
testWidgets('saving a backup writes a sealed .tanemaki file', (tester) async {
await tester.pumpWidget(section());
await tester.tap(find.text('Save a backup'));
await tester.pumpAndSettle();
expect(files.savedName, endsWith('.tanemaki'));
expect(BackupBox.looksSealed(files.savedBytes!), isTrue);
expect(find.text('Copy saved'), findsOneWidget);
});
testWidgets('restoring my own sealed copy needs no code', (tester) async {
final otherDb = newTestDatabase();
addTearDown(otherDb.close);
final otherFiles = FakeFileService();
final mine = ExportImportService(
repository: newTestRepository(otherDb, nodeId: 'other'),
files: otherFiles,
keys: keys, // same identity
);
final other = newTestRepository(otherDb, nodeId: 'other');
await other.addQuickVariety(label: 'Imported bean');
await mine.exportBackup();
files.bytesToPick = otherFiles.savedBytes;
await tester.pumpWidget(section());
await tester.tap(find.text('Restore a backup'));
await tester.pumpAndSettle();
expect(find.text('Restore a backup?'), findsOneWidget);
await tester.tap(find.text('Import'));
await tester.pumpAndSettle();
expect(find.text('Imported: 1 new, 0 updated'), findsOneWidget);
final varieties = await db.select(db.varieties).get();
expect(varieties.single.label, 'Imported bean');
});
testWidgets('a copy from another identity asks for the recovery code', (
tester,
) async {
final otherDb = newTestDatabase();
addTearDown(otherDb.close);
final otherFiles = FakeFileService();
final otherKeys = SecureKeyStore(store: InMemorySecretStore());
final theirs = ExportImportService(
repository: newTestRepository(otherDb, nodeId: 'other'),
files: otherFiles,
keys: otherKeys,
);
final other = newTestRepository(otherDb, nodeId: 'other');
await other.addQuickVariety(label: 'Recovered bean');
await theirs.exportBackup();
final code = await theirs.recoveryCode();
files.bytesToPick = otherFiles.savedBytes;
await tester.pumpWidget(section());
await tester.tap(find.text('Restore a backup'));
await tester.pumpAndSettle();
await tester.tap(find.text('Import'));
await tester.pumpAndSettle();
// Wrong seed → the code prompt appears; type the printed code.
expect(find.text('Enter your recovery code'), findsOneWidget);
await tester.enterText(find.byKey(const Key('backup.recoveryField')), code);
await tester.tap(find.byKey(const Key('backup.recoveryConfirm')));
await tester.pumpAndSettle();
expect(find.text('Imported: 1 new, 0 updated'), findsOneWidget);
final varieties = await db.select(db.varieties).get();
expect(varieties.single.label, 'Recovered bean');
// The identity travelled with the code.
expect(await keys.rootSeedHex(), await otherKeys.rootSeedHex());
});
testWidgets('legacy plain exports still restore', (tester) async {
final otherDb = newTestDatabase();
addTearDown(otherDb.close);
final other = newTestRepository(otherDb, nodeId: 'node-other');
await other.addQuickVariety(label: 'Old bean');
files.bytesToPick = utf8.encode(
const InventoryJsonCodec().encode(await other.exportInventory()),
);
await tester.pumpWidget(section());
await tester.tap(find.text('Restore a backup'));
await tester.pumpAndSettle();
await tester.tap(find.text('Import'));
await tester.pumpAndSettle();
expect(find.text('Imported: 1 new, 0 updated'), findsOneWidget);
});
testWidgets('an unreadable file reports a friendly error', (tester) async {
files.bytesToPick = utf8.encode('this is not json');
await tester.pumpWidget(section());
await tester.tap(find.text('Restore a backup'));
await tester.pumpAndSettle();
await tester.tap(find.text('Import'));
await tester.pumpAndSettle();
expect(
find.text('This file could not be read as a Tanemaki copy'),
findsOneWidget,
);
});
testWidgets('the recovery dialog shows the code and saves the sheet', (
tester,
) async {
await tester.pumpWidget(section());
await tester.tap(find.byKey(const Key('backup.recovery')));
await tester.pumpAndSettle();
final code = await service.recoveryCode();
expect(find.text(code), findsOneWidget);
await tester.tap(find.byKey(const Key('backup.recoverySave')));
await tester.pumpAndSettle();
expect(files.savedName, 'tanemaki-recovery.pdf');
expect(String.fromCharCodes(files.savedBytes!.take(5)), '%PDF-');
// The seed itself never appears in the clear in the PDF stream metadata;
// the QR encodes the human code, which IS the secret — that's the point
// of the sheet. Just assert it saved and confirmed.
expect(find.text('Copy saved'), findsOneWidget);
});
testWidgets('tapping the automatic-backups line makes a copy now', (
tester,
) async {
final spy = _SpyAutoBackup(db);
await tester.pumpWidget(
wrap(
Scaffold(
body: BackupSection(service: service, sheet: sheet, autoBackup: spy),
),
),
);
await tester.pump(); // resolve the "last copy" future
await tester.tap(find.text('Automatic backups'));
await tester.pumpAndSettle();
expect(spy.calls, 1);
expect(find.text('Copy saved'), findsOneWidget);
});
testWidgets('the last-backup date renders under Asturian without an '
'invalid-locale crash', (tester) async {
// Asturian (`ast`) has no `intl` date symbols; formatting the "last copy"
// date used to throw "Invalid locale ast". The framework locale is mapped
// to Spanish, so the date must format through that instead.
LocaleSettings.setLocaleSync(AppLocale.ast);
addTearDown(() => LocaleSettings.setLocaleSync(AppLocale.en));
await tester.pumpWidget(
TranslationProvider(
child: MaterialApp(
locale: materialLocaleFor(const Locale('ast')),
supportedLocales: AppLocaleUtils.supportedLocales,
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: Scaffold(
body: BackupSection(
service: service,
sheet: sheet,
autoBackup: _DatedAutoBackup(db),
),
),
),
),
);
await tester.pump(); // resolve the "last copy" future
// The Asturian tile (and its Spanish-formatted date subtitle) built fine.
expect(find.text('Copies automátiques'), findsOneWidget);
expect(tester.takeException(), isNull);
});
}
/// Counts explicit "back up now" taps without touching the disk.
class _SpyAutoBackup extends AutoBackupService {
_SpyAutoBackup(AppDatabase db)
: super(
exporter: ExportImportService(
repository: newTestRepository(db),
files: FakeFileService(),
keys: SecureKeyStore(store: InMemorySecretStore()),
),
store: AutoBackupStore(InMemorySecretStore()),
directory: () async => throw UnimplementedError(),
);
int calls = 0;
@override
Future<bool> backupNow() async {
calls++;
return true;
}
}
/// Reports a fixed "last copy" date so the auto-backup subtitle formats a real
/// date — the path that crashed under Asturian.
class _DatedAutoBackup extends AutoBackupService {
_DatedAutoBackup(AppDatabase db)
: super(
exporter: ExportImportService(
repository: newTestRepository(db),
files: FakeFileService(),
keys: SecureKeyStore(store: InMemorySecretStore()),
),
store: AutoBackupStore(InMemorySecretStore()),
directory: () async => throw UnimplementedError(),
);
@override
Future<DateTime?> lastBackupAt() async => DateTime.utc(2026, 3, 14);
}