feat(block1): variety detail + edit screen with reactive lots

Tapping an inventory item now opens its detail (the /variety/:id route was a
placeholder). Read + edit one variety end-to-end:

- Detail view: photo, category, "also known as" names, notes and lots, driven
  by a reactive VarietyDetailCubit.
- Edit core fields (label/category/notes, LWW) via a bottom sheet.
- Add a lot (harvest year + quantity) — the list refreshes reactively.
- Soft-delete (tombstone) with a confirm dialog.

Repository:
- watchVariety(id): merges Drift table-change streams (variety, lots, names,
  attachments) via StreamGroup and reloads the full detail on any change, so a
  lot added to a *different* table still refreshes the view.
- updateVariety (LWW), addLot, softDeleteVariety.

i18n: detail/edit/addLot strings (ES/EN); switched slang to {param} braces
interpolation (translator- and Weblate-friendly).

Tests: repository (reactive watchVariety, update, soft-delete) and widget tests
(render, edit updates title, add-lot appears, delete shows not-found). Full
suite green: 23 passing, 1 skipped (SQLCipher-only encryption guard).
This commit is contained in:
vjrj 2026-07-07 15:56:03 +02:00
parent 040f15a898
commit 7ff4e38a15
15 changed files with 1093 additions and 18 deletions

View file

@ -0,0 +1,63 @@
import 'package:async/async.dart';
import 'package:commons_core/commons_core.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/data/variety_repository.dart';
import 'package:tane/db/database.dart';
import '../support/test_support.dart';
void main() {
late AppDatabase db;
late VarietyRepository repo;
setUp(() {
db = newTestDatabase();
repo = newTestRepository(db);
});
tearDown(() => db.close());
test('watchVariety emits null for a missing variety', () async {
expect(await repo.watchVariety('does-not-exist').first, isNull);
});
test('watchVariety reacts to a lot being added', () async {
final id = await repo.addQuickVariety(label: 'Maize', category: 'Poaceae');
final queue = StreamQueue(repo.watchVariety(id));
final initial = await queue.next;
expect(initial!.label, 'Maize');
expect(initial.lots, isEmpty);
await repo.addLot(
varietyId: id,
harvestYear: 2024,
quantity: const Quantity(kind: QuantityKind.cob),
);
final updated = await queue.next;
expect(updated!.lots.single.harvestYear, 2024);
expect(updated.lots.single.quantity?.kind, QuantityKind.cob);
await queue.cancel();
});
test('updateVariety changes scalar fields (last-writer-wins)', () async {
final id = await repo.addQuickVariety(label: 'Old name');
await repo.updateVariety(id: id, label: 'New name', notes: 'a note');
final detail = await repo.watchVariety(id).first;
expect(detail!.label, 'New name');
expect(detail.notes, 'a note');
});
test(
'softDeleteVariety hides it from watchVariety and the inventory',
() async {
final id = await repo.addQuickVariety(label: 'Doomed');
await repo.softDeleteVariety(id);
expect(await repo.watchVariety(id).first, isNull);
expect(await repo.watchInventory().first, isEmpty);
},
);
}

View file

@ -8,6 +8,8 @@ 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).
@ -54,3 +56,30 @@ Widget wrapScreen({
),
);
}
/// Wraps the [VarietyDetailScreen] for [varietyId] with its cubit + i18n.
Widget wrapDetail({
required VarietyRepository repository,
required String varietyId,
AppLocale locale = AppLocale.en,
}) {
LocaleSettings.setLocaleSync(locale);
return TranslationProvider(
child: RepositoryProvider.value(
value: repository,
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(),
),
),
),
);
}

View file

@ -0,0 +1,92 @@
import 'package:commons_core/commons_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/db/database.dart';
import '../support/test_support.dart';
void main() {
late AppDatabase db;
setUp(() => db = newTestDatabase());
tearDown(() => db.close());
testWidgets('renders the variety label, category and its lots', (
tester,
) async {
final repo = newTestRepository(db);
final id = await repo.addQuickVariety(label: 'Maize', category: 'Poaceae');
await repo.addLot(
varietyId: id,
harvestYear: 2024,
quantity: const Quantity(kind: QuantityKind.cob),
);
await tester.pumpWidget(wrapDetail(repository: repo, varietyId: id));
await tester.pumpAndSettle();
expect(find.text('Maize'), findsOneWidget); // app bar title
expect(find.text('Poaceae'), findsOneWidget); // category chip
expect(find.text('Year 2024 · a cob'), findsOneWidget); // lot line
await disposeTree(tester);
});
testWidgets('editing the name updates the title', (tester) async {
final repo = newTestRepository(db);
final id = await repo.addQuickVariety(label: 'Old name');
await tester.pumpWidget(wrapDetail(repository: repo, varietyId: id));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('detail.edit')));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('editVariety.name')),
'New name',
);
await tester.tap(find.byKey(const Key('editVariety.save')));
await tester.pumpAndSettle();
expect(find.text('New name'), findsOneWidget);
await disposeTree(tester);
});
testWidgets('adding a lot shows it in the list', (tester) async {
final repo = newTestRepository(db);
final id = await repo.addQuickVariety(label: 'Bean');
await tester.pumpWidget(wrapDetail(repository: repo, varietyId: id));
await tester.pumpAndSettle();
expect(find.text('No lots yet.'), findsOneWidget);
await tester.tap(find.byKey(const Key('detail.addLot')));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(const Key('addLot.year')), '2023');
await tester.tap(find.text('a handful'));
await tester.tap(find.byKey(const Key('addLot.save')));
await tester.pumpAndSettle();
expect(find.text('Year 2023 · a handful'), findsOneWidget);
await disposeTree(tester);
});
testWidgets('deleting the variety shows the not-found state', (tester) async {
final repo = newTestRepository(db);
final id = await repo.addQuickVariety(label: 'Doomed');
await tester.pumpWidget(wrapDetail(repository: repo, varietyId: id));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('detail.delete')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('detail.deleteConfirm')));
await tester.pumpAndSettle();
// The variety is soft-deleted: the reactive view re-emits null.
expect(find.text('This seed is no longer here.'), findsOneWidget);
await disposeTree(tester);
});
}