feat(species): derived verified reference links (GBIF/Wikipedia/Wikispecies)

Add a read-only "Learn more" section on the variety detail that surfaces
verified external references for the linked species, built deterministically
from bundled identifiers — no titles bundled, no network, offline-first:

- GBIF taxon page from the backbone key
- Wikipedia via Wikidata Special:GoToLinkedPage (locale-aware, RTL-safe,
  falls back to the Wikidata item when a language lacks the article)
- Wikispecies from the scientific name

Enrich the bundled catalog (species.json v3) with gbif_key + wikidata_qid for
all 14 species, cross-checked (each QID carries GBIF ID P846 = the key).
Schema already had the columns; seedBundled now persists and backfills them.

The grower's own manual links stay but are demoted (progressive disclosure):
the section only shows when links exist and "Add link" is a low-emphasis
button, since hand-adding a link is the rarer power move.

Pure-Dart link builder is unit-tested; repo persistence/backfill and the
detail-screen section are covered too.
This commit is contained in:
vjrj 2026-07-09 22:44:12 +02:00
parent ba87bf2719
commit fde0ea6eab
14 changed files with 484 additions and 46 deletions

View file

@ -1,4 +1,5 @@
import 'package:commons_core/commons_core.dart';
import 'package:drift/drift.dart' show Value;
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/data/species_catalog.dart';
import 'package:tane/data/species_repository.dart';
@ -49,6 +50,65 @@ void main() {
},
);
test('parseSpeciesCatalog reads gbif and wikidata identifiers', () {
const json = '''
{"version":3,"species":[
{"scientific_name":"Zea mays","gbif_key":5290052,"wikidata_qid":"Q11575"},
{"scientific_name":"Unknown sp."}
]}''';
final seeds = parseSpeciesCatalog(json);
expect(seeds.first.gbifKey, 5290052);
expect(seeds.first.wikidataQid, 'Q11575');
// Missing identifiers stay null, they are optional.
expect(seeds.last.gbifKey, isNull);
expect(seeds.last.wikidataQid, isNull);
});
test('seedBundled persists gbif and wikidata identifiers', () async {
await repo.seedBundled(const [
SpeciesSeed(
scientificName: 'Zea mays',
gbifKey: 5290052,
wikidataQid: 'Q11575',
),
]);
final row = await db.select(db.species).getSingle();
expect(row.gbifKey, 5290052);
expect(row.wikidataQid, 'Q11575');
});
test(
'seedBundled backfills identifiers onto rows seeded before them',
() async {
// A pre-existing bundled row that predates the identifier fields.
await db
.into(db.species)
.insert(
SpeciesCompanion.insert(
id: 'legacy',
createdAt: 0,
updatedAt: '0',
lastAuthor: 'bundle',
scientificName: 'Zea mays',
isBundled: const Value(true),
),
);
await repo.seedBundled(const [
SpeciesSeed(
scientificName: 'Zea mays',
gbifKey: 5290052,
wikidataQid: 'Q11575',
),
]);
final rows = await db.select(db.species).get();
expect(rows.single.id, 'legacy'); // same row, not a duplicate
expect(rows.single.gbifKey, 5290052);
expect(rows.single.wikidataQid, 'Q11575');
},
);
test('seedBundled inserts species and is idempotent', () async {
await repo.seedBundled(_seeds);
await repo.seedBundled(_seeds); // second run must not duplicate

View file

@ -0,0 +1,84 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/domain/species_reference_links.dart';
void main() {
test('all identifiers present yields gbif, wikipedia and wikispecies', () {
final links = speciesReferenceLinks(
scientificName: 'Solanum lycopersicum',
gbifKey: 2930137,
wikidataQid: 'Q23501',
languageCode: 'es',
);
expect(links.map((l) => l.source), [
ReferenceSource.gbif,
ReferenceSource.wikipedia,
ReferenceSource.wikispecies,
]);
expect(links[0].url, 'https://www.gbif.org/species/2930137');
expect(
links[1].url,
'https://www.wikidata.org/wiki/Special:GoToLinkedPage/eswiki/Q23501',
);
expect(
links[2].url,
'https://species.wikimedia.org/wiki/Solanum_lycopersicum',
);
});
test('wikipedia link is locale-aware', () {
String wiki(String lang) => speciesReferenceLinks(
scientificName: 'Zea mays',
wikidataQid: 'Q11575',
languageCode: lang,
).firstWhere((l) => l.source == ReferenceSource.wikipedia).url;
expect(wiki('en'), contains('/enwiki/Q11575'));
expect(wiki('ar'), contains('/arwiki/Q11575'));
});
test('locale region and case are normalised to the bare wiki subtag', () {
String wiki(String lang) => speciesReferenceLinks(
scientificName: 'Zea mays',
wikidataQid: 'Q11575',
languageCode: lang,
).firstWhere((l) => l.source == ReferenceSource.wikipedia).url;
expect(wiki('pt_BR'), contains('/ptwiki/'));
expect(wiki('zh-Hans'), contains('/zhwiki/'));
expect(wiki('EN'), contains('/enwiki/'));
expect(wiki(''), contains('/enwiki/')); // empty locale English
});
test('a missing identifier simply drops that source', () {
final onlyGbif = speciesReferenceLinks(
scientificName: '', // no wikispecies without a name
gbifKey: 42,
);
expect(onlyGbif.map((l) => l.source), [ReferenceSource.gbif]);
final blankQid = speciesReferenceLinks(
scientificName: 'Daucus carota',
wikidataQid: ' ',
);
expect(
blankQid.map((l) => l.source),
isNot(contains(ReferenceSource.wikipedia)),
);
expect(blankQid.map((l) => l.source), [ReferenceSource.wikispecies]);
});
test('nothing identifiable yields no links', () {
expect(speciesReferenceLinks(scientificName: ' '), isEmpty);
});
test('scientific names with spaces and diacritics are URL-safe', () {
final link = speciesReferenceLinks(
scientificName: 'Allium × proliferum',
).single;
expect(link.source, ReferenceSource.wikispecies);
expect(
link.url,
'https://species.wikimedia.org/wiki/Allium_%C3%97_proliferum',
);
});
}

View file

@ -373,6 +373,56 @@ void main() {
await disposeTree(tester);
});
testWidgets(
'a linked species with identifiers shows verified reference links',
(tester) async {
final repo = newTestRepository(db);
final species = newTestSpeciesRepository(db);
await species.seedBundled(const [
SpeciesSeed(
scientificName: 'Solanum lycopersicum',
gbifKey: 2930137,
wikidataQid: 'Q23501',
),
]);
final match = (await species.search('Solanum')).single;
final id = await repo.addQuickVariety(label: 'Tomato');
await repo.linkSpecies(id, match.id);
await tester.pumpWidget(
wrapDetail(repository: repo, varietyId: id, species: species),
);
await tester.pumpAndSettle();
// The "Learn more" section renders one chip per available source.
expect(find.text('Learn more'), findsOneWidget);
expect(find.byKey(const Key('ref.gbif')), findsOneWidget);
expect(find.byKey(const Key('ref.wikipedia')), findsOneWidget);
expect(find.byKey(const Key('ref.wikispecies')), findsOneWidget);
await disposeTree(tester);
},
);
testWidgets('a variety with no species shows no reference section', (
tester,
) async {
final repo = newTestRepository(db);
final id = await repo.addQuickVariety(label: 'Mystery seed');
await tester.pumpWidget(
wrapDetail(
repository: repo,
varietyId: id,
species: newTestSpeciesRepository(db),
),
);
await tester.pumpAndSettle();
expect(find.text('Learn more'), findsNothing);
expect(find.byKey(const Key('ref.gbif')), findsNothing);
await disposeTree(tester);
});
testWidgets('plant lots show the Plants chip and hide germination', (
tester,
) async {