tane/apps/app_seeds/test/support/test_support.dart
vjrj 4e8b8293e0 feat(block1): bundled species catalog + autocomplete link
Add a small curated catalog of Iberian horticultural species and let a variety
be linked to it from the edit sheet.

- assets/catalog/species.json: 14 species with botanical family and ES/EN
  common names (wikidata_qid/gbif_key deferred to the varilla enrichment).
- SpeciesRepository: idempotent seedBundled (is_bundled rows, keyed by
  scientific name) + search by scientific/common name with a locale-best label.
  Seeded on startup from DI.
- VarietyRepository.linkSpecies: sets species_id and prefills category from the
  species' family when empty (never overwrites an existing category).
  VarietyDetail now carries the scientific name.
- Edit sheet gains a live species-search field; the detail view shows the
  scientific name (italic). i18n strings added (ES/EN).

Tests: catalog parse, idempotent seeding, search by scientific/common name,
linkSpecies prefill semantics, and a widget test for the autocomplete → link →
scientific-name-shown flow. Full suite: 32 passing, 0 skipped.
2026-07-07 21:21:59 +02:00

93 lines
3.4 KiB
Dart

import 'package:commons_core/commons_core.dart';
import 'package:drift/native.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/data/species_repository.dart';
import 'package:tane/data/variety_repository.dart';
import 'package:tane/db/database.dart';
import 'package:tane/i18n/strings.g.dart';
import 'package:tane/state/inventory_cubit.dart';
import 'package:tane/state/variety_detail_cubit.dart';
import 'package:tane/ui/variety_detail_screen.dart';
/// A fresh in-memory database for host tests (unencrypted; encryption is
/// verified separately in the SQLCipher-only security test).
AppDatabase newTestDatabase() => AppDatabase(NativeDatabase.memory());
/// Unmounts the widget tree *inside* the test and drains the zero-duration
/// timer Drift schedules when its stream subscription is cancelled. Without
/// this, flutter_test reports "a Timer is still pending after disposal". Call
/// at the end of any widget test that mounts an [InventoryCubit].
Future<void> disposeTree(WidgetTester tester) async {
await tester.pumpWidget(const SizedBox.shrink());
await tester.pump(const Duration(milliseconds: 10));
}
VarietyRepository newTestRepository(
AppDatabase db, {
String nodeId = 'test-node',
}) => VarietyRepository(db, idGen: IdGen(), nodeId: nodeId);
SpeciesRepository newTestSpeciesRepository(AppDatabase db) =>
SpeciesRepository(db, idGen: IdGen());
/// Wraps [child] with the providers a screen expects (repository, inventory
/// cubit) plus i18n and Material localizations, pinned to [locale].
Widget wrapScreen({
required VarietyRepository repository,
required Widget child,
AppLocale locale = AppLocale.en,
}) {
LocaleSettings.setLocaleSync(locale);
return TranslationProvider(
child: RepositoryProvider.value(
value: repository,
child: BlocProvider(
create: (_) => InventoryCubit(repository),
child: MaterialApp(
locale: locale.flutterLocale,
supportedLocales: AppLocaleUtils.supportedLocales,
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: child,
),
),
),
);
}
/// Wraps the [VarietyDetailScreen] for [varietyId] with its cubit + i18n.
Widget wrapDetail({
required VarietyRepository repository,
required String varietyId,
SpeciesRepository? species,
AppLocale locale = AppLocale.en,
}) {
LocaleSettings.setLocaleSync(locale);
return TranslationProvider(
child: MultiRepositoryProvider(
providers: [
RepositoryProvider.value(value: repository),
if (species != null) RepositoryProvider.value(value: species),
],
child: BlocProvider(
create: (_) => VarietyDetailCubit(repository, varietyId),
child: MaterialApp(
locale: locale.flutterLocale,
supportedLocales: AppLocaleUtils.supportedLocales,
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: const VarietyDetailScreen(),
),
),
),
);
}