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.
This commit is contained in:
vjrj 2026-07-07 21:21:59 +02:00
parent 7ff4e38a15
commit 4e8b8293e0
22 changed files with 700 additions and 47 deletions

View file

@ -0,0 +1,83 @@
import 'package:commons_core/commons_core.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/data/species_catalog.dart';
import 'package:tane/data/species_repository.dart';
import 'package:tane/db/database.dart';
import '../support/test_support.dart';
const _seeds = [
SpeciesSeed(
scientificName: 'Solanum lycopersicum',
family: 'Solanaceae',
commonNames: {
'en': ['Tomato'],
'es': ['Tomate'],
},
),
SpeciesSeed(
scientificName: 'Phaseolus vulgaris',
family: 'Fabaceae',
commonNames: {
'en': ['Common bean'],
'es': ['Judía', 'Alubia'],
},
),
];
void main() {
late AppDatabase db;
late SpeciesRepository repo;
setUp(() {
db = newTestDatabase();
repo = SpeciesRepository(db, idGen: IdGen());
});
tearDown(() => db.close());
test(
'parseSpeciesCatalog reads scientific name, family and common names',
() {
const json = '''
{"version":1,"species":[
{"scientific_name":"Zea mays","family":"Poaceae","common":{"en":["Maize"],"es":["Maíz"]}}
]}''';
final seeds = parseSpeciesCatalog(json);
expect(seeds.single.scientificName, 'Zea mays');
expect(seeds.single.family, 'Poaceae');
expect(seeds.single.commonNames['es'], ['Maíz']);
},
);
test('seedBundled inserts species and is idempotent', () async {
await repo.seedBundled(_seeds);
await repo.seedBundled(_seeds); // second run must not duplicate
final species = await db.select(db.species).get();
expect(species.length, 2);
expect(species.every((s) => s.isBundled), isTrue);
});
test('search matches by scientific name', () async {
await repo.seedBundled(_seeds);
final results = await repo.search('phaseolus');
expect(results.single.scientificName, 'Phaseolus vulgaris');
});
test('search matches by common name and returns a localized label', () async {
await repo.seedBundled(_seeds);
final es = await repo.search('alubia', languageCode: 'es');
expect(es.single.scientificName, 'Phaseolus vulgaris');
expect(es.single.commonName, anyOf('Judía', 'Alubia'));
final en = await repo.search('tom', languageCode: 'en');
expect(en.single.commonName, 'Tomato');
expect(en.single.displayLabel, 'Tomato (Solanum lycopersicum)');
});
test('empty query returns nothing', () async {
await repo.seedBundled(_seeds);
expect(await repo.search(' '), isEmpty);
});
}

View file

@ -1,11 +1,20 @@
import 'package:async/async.dart';
import 'package:commons_core/commons_core.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 '../support/test_support.dart';
const _tomatoSeed = SpeciesSeed(
scientificName: 'Solanum lycopersicum',
family: 'Solanaceae',
commonNames: {
'en': ['Tomato'],
},
);
void main() {
late AppDatabase db;
late VarietyRepository repo;
@ -60,4 +69,32 @@ void main() {
expect(await repo.watchInventory().first, isEmpty);
},
);
test(
'linkSpecies exposes the scientific name and prefills empty category',
() async {
final species = newTestSpeciesRepository(db);
await species.seedBundled(const [_tomatoSeed]);
final match = (await species.search('tomato')).single;
final id = await repo.addQuickVariety(label: "Grandma's tomato");
await repo.linkSpecies(id, match.id);
final detail = await repo.watchVariety(id).first;
expect(detail!.scientificName, 'Solanum lycopersicum');
expect(detail.category, 'Solanaceae'); // prefilled from family
},
);
test('linkSpecies does not overwrite an existing category', () async {
final species = newTestSpeciesRepository(db);
await species.seedBundled(const [_tomatoSeed]);
final match = (await species.search('tomato')).single;
final id = await repo.addQuickVariety(label: 'T', category: 'My tomatoes');
await repo.linkSpecies(id, match.id);
final detail = await repo.watchVariety(id).first;
expect(detail!.category, 'My tomatoes');
});
}

View file

@ -4,6 +4,7 @@ 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';
@ -29,6 +30,9 @@ VarietyRepository newTestRepository(
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({
@ -61,12 +65,16 @@ Widget wrapScreen({
Widget wrapDetail({
required VarietyRepository repository,
required String varietyId,
SpeciesRepository? species,
AppLocale locale = AppLocale.en,
}) {
LocaleSettings.setLocaleSync(locale);
return TranslationProvider(
child: RepositoryProvider.value(
value: repository,
child: MultiRepositoryProvider(
providers: [
RepositoryProvider.value(value: repository),
if (species != null) RepositoryProvider.value(value: species),
],
child: BlocProvider(
create: (_) => VarietyDetailCubit(repository, varietyId),
child: MaterialApp(

View file

@ -15,7 +15,12 @@ void main() {
final repo = newTestRepository(db);
await tester.pumpWidget(
TranslationProvider(child: TaneApp(repository: repo)),
TranslationProvider(
child: TaneApp(
repository: repo,
species: newTestSpeciesRepository(db),
),
),
);
await tester.pumpAndSettle();

View file

@ -1,6 +1,7 @@
import 'package:commons_core/commons_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/data/species_repository.dart';
import 'package:tane/db/database.dart';
import '../support/test_support.dart';
@ -22,7 +23,13 @@ void main() {
quantity: const Quantity(kind: QuantityKind.cob),
);
await tester.pumpWidget(wrapDetail(repository: repo, varietyId: id));
await tester.pumpWidget(
wrapDetail(
repository: repo,
varietyId: id,
species: newTestSpeciesRepository(db),
),
);
await tester.pumpAndSettle();
expect(find.text('Maize'), findsOneWidget); // app bar title
@ -35,7 +42,13 @@ void main() {
final repo = newTestRepository(db);
final id = await repo.addQuickVariety(label: 'Old name');
await tester.pumpWidget(wrapDetail(repository: repo, varietyId: id));
await tester.pumpWidget(
wrapDetail(
repository: repo,
varietyId: id,
species: newTestSpeciesRepository(db),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('detail.edit')));
@ -56,7 +69,13 @@ void main() {
final repo = newTestRepository(db);
final id = await repo.addQuickVariety(label: 'Bean');
await tester.pumpWidget(wrapDetail(repository: repo, varietyId: id));
await tester.pumpWidget(
wrapDetail(
repository: repo,
varietyId: id,
species: newTestSpeciesRepository(db),
),
);
await tester.pumpAndSettle();
expect(find.text('No lots yet.'), findsOneWidget);
@ -77,7 +96,13 @@ void main() {
final repo = newTestRepository(db);
final id = await repo.addQuickVariety(label: 'Doomed');
await tester.pumpWidget(wrapDetail(repository: repo, varietyId: id));
await tester.pumpWidget(
wrapDetail(
repository: repo,
varietyId: id,
species: newTestSpeciesRepository(db),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('detail.delete')));
@ -89,4 +114,47 @@ void main() {
expect(find.text('This seed is no longer here.'), findsOneWidget);
await disposeTree(tester);
});
testWidgets(
'linking a species from the edit sheet shows its scientific name',
(tester) async {
final repo = newTestRepository(db);
final species = newTestSpeciesRepository(db);
await species.seedBundled(const [
SpeciesSeed(
scientificName: 'Solanum lycopersicum',
family: 'Solanaceae',
commonNames: {
'en': ['Tomato'],
},
),
]);
final id = await repo.addQuickVariety(label: "Grandma's tomato");
await tester.pumpWidget(
wrapDetail(repository: repo, varietyId: id, species: species),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('detail.edit')));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('editVariety.species')),
'toma',
);
await tester.pumpAndSettle();
await tester.tap(find.text('Tomato (Solanum lycopersicum)'));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('editVariety.save')));
await tester.pumpAndSettle();
// Detail now shows the scientific name and the family-prefilled category.
expect(find.text('Solanum lycopersicum'), findsOneWidget);
expect(find.text('Solanaceae'), findsOneWidget);
await disposeTree(tester);
},
);
}