feat(inventory): photo-first drafts + on-device OCR (digitization R2+R4)

Lower the bulk-digitization cliff with two more routes on top of the
already-landed CSV import and "save and add another":

- Photo-first drafts (capture now, catalogue later): burst-capture
  photos (camera or multi-gallery) into unnamed draft varieties, shown
  in a "to catalogue" tray, hidden from the main list until named.
  Adds Variety.isDraft (schema), addDraftVariety/watchDrafts/nameDraft,
  the triage sheet and the inventory banner.
- On-device OCR label suggestion (Tesseract, offline, no Google): a
  "Suggest name from photo" button in the naming dialog behind a
  LabelTextExtractor interface (Tesseract on Android/iOS, no-op
  elsewhere). Reads the largest print via hOCR bounding boxes, drops
  boilerplate/low-confidence noise, preprocesses (grayscale, contrast,
  upscale) and sweeps rotations (0-315 deg) so tilted packets still
  read. Bundles tessdata_fast eng+spa; validated on-device against real
  packets. The photo is written to a temp file deleted immediately in a
  finally block (the plugin needs a path) - a bounded, documented
  exception to no-plaintext-at-rest.

This commit also carries the co-developed schema evolution v5 to v8 that
shares these files (organic flag, species viability years, crop
calendar, lot provenance/abundance/preservation format, condition
checks) plus their exports/migrations and i18n.

Tests: CSV/draft/OCR unit + widget + migration green in isolation.
Note: the full widget suite currently hangs (>10 min) - under investigation.
This commit is contained in:
vjrj 2026-07-09 21:23:46 +02:00
parent 12a2ee2d64
commit 6809dc6143
89 changed files with 17141 additions and 228 deletions

View file

@ -66,24 +66,27 @@ void main() {
);
}
testWidgets('settings shows the backup section with its three actions', (
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'), findsOneWidget);
expect(find.text('Export as CSV'), findsOneWidget);
expect(find.text('Export as JSON'), findsOneWidget);
expect(find.text('Import from JSON'), findsOneWidget);
expect(find.text('Backup & restore'), findsOneWidget);
expect(find.text('Save a backup'), findsOneWidget);
expect(find.text('Restore a backup'), findsOneWidget);
expect(find.text('Export to a spreadsheet'), findsOneWidget);
expect(find.text('Import a list'), findsOneWidget);
});
testWidgets('export CSV saves a .csv file and confirms', (tester) async {
testWidgets('spreadsheet export saves a .csv file and confirms', (
tester,
) async {
await tester.pumpWidget(
wrap(Scaffold(body: BackupSection(service: service))),
);
await tester.tap(find.text('Export as CSV'));
await tester.tap(find.text('Export to a spreadsheet'));
await tester.pumpAndSettle();
expect(files.savedName, endsWith('.csv'));
@ -91,11 +94,11 @@ void main() {
expect(find.text('Copy saved'), findsOneWidget);
});
testWidgets('export JSON saves a versioned .json file', (tester) async {
testWidgets('saving a backup writes a versioned .json file', (tester) async {
await tester.pumpWidget(
wrap(Scaffold(body: BackupSection(service: service))),
);
await tester.tap(find.text('Export as JSON'));
await tester.tap(find.text('Save a backup'));
await tester.pumpAndSettle();
expect(files.savedName, endsWith('.json'));
@ -120,9 +123,9 @@ void main() {
await tester.pumpWidget(
wrap(Scaffold(body: BackupSection(service: service))),
);
await tester.tap(find.text('Import from JSON'));
await tester.tap(find.text('Restore a backup'));
await tester.pumpAndSettle();
expect(find.text('Import a saved copy?'), findsOneWidget);
expect(find.text('Restore a backup?'), findsOneWidget);
await tester.tap(find.text('Import'));
await tester.pumpAndSettle();
@ -137,7 +140,7 @@ void main() {
await tester.pumpWidget(
wrap(Scaffold(body: BackupSection(service: service))),
);
await tester.tap(find.text('Import from JSON'));
await tester.tap(find.text('Restore a backup'));
await tester.pumpAndSettle();
await tester.tap(find.text('Import'));
await tester.pumpAndSettle();

View file

@ -0,0 +1,95 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/i18n/strings.g.dart';
import 'package:tane/services/ocr/label_text_extractor.dart';
import 'package:tane/ui/draft_triage.dart';
/// A stand-in OCR engine so the dialog can be tested without a native backend.
class _FakeExtractor implements LabelTextExtractor {
_FakeExtractor({this.result, this.supported = true});
final String? result;
final bool supported;
@override
bool get isSupported => supported;
@override
Future<String?> suggestLabel(Uint8List photo) async => result;
}
Widget _host(Widget child) {
LocaleSettings.setLocaleSync(AppLocale.en);
return TranslationProvider(
child: MaterialApp(
locale: AppLocale.en.flutterLocale,
supportedLocales: AppLocaleUtils.supportedLocales,
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: Scaffold(body: child),
),
);
}
void main() {
// A valid 1×1 PNG so Image.memory can decode it in the widget test.
final photo = Uint8List.fromList([
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, //
0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4,
0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41,
0x54, 0x78, 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00,
0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00,
0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE,
0x42, 0x60, 0x82,
]);
testWidgets('a supported engine offers a suggestion that pre-fills the name',
(tester) async {
await tester.pumpWidget(
_host(
NameDraftDialog(
photo: photo,
extractor: _FakeExtractor(result: 'Aubergine Black Beauty'),
),
),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('draft.suggestFromPhoto')), findsOneWidget);
await tester.tap(find.byKey(const Key('draft.suggestFromPhoto')));
await tester.pumpAndSettle();
expect(find.text('Aubergine Black Beauty'), findsOneWidget);
});
testWidgets('an unsupported engine hides the suggestion button',
(tester) async {
await tester.pumpWidget(
_host(
NameDraftDialog(
photo: photo,
extractor: _FakeExtractor(supported: false),
),
),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('draft.suggestFromPhoto')), findsNothing);
});
testWidgets('a null suggestion leaves the field untouched (no crash)',
(tester) async {
await tester.pumpWidget(
_host(NameDraftDialog(photo: photo, extractor: _FakeExtractor())),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('draft.suggestFromPhoto')));
await tester.pumpAndSettle();
// Nothing was entered; the save still returns the (empty) field.
expect(find.byKey(const Key('draft.nameField')), findsOneWidget);
});
}

View file

@ -0,0 +1,72 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/i18n/strings.g.dart';
import 'package:tane/ui/inventory_list_screen.dart';
import '../support/test_support.dart';
void main() {
// A valid 1×1 PNG so Image.memory decodes it in the widget test (the seed
// only nudges a trailing byte to keep captures distinct).
Uint8List photo(int seed) => Uint8List.fromList([
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, //
0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4,
0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41,
0x54, 0x78, 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00,
0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00,
0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE,
0x42, 0x60, 0x82, seed,
]);
testWidgets(
'drafts show a triage banner, stay out of the list, and can be named',
(tester) async {
LocaleSettings.setLocaleSync(AppLocale.en);
final db = newTestDatabase();
addTearDown(db.close);
final repo = newTestRepository(db);
await repo.addDraftVariety(photo(1));
await repo.addDraftVariety(photo(2));
await tester.pumpWidget(
wrapScreen(repository: repo, child: const InventoryListScreen()),
);
await tester.pumpAndSettle();
// The two captures are in the tray, not the main list.
expect(find.byKey(const Key('inventory.triageBanner')), findsOneWidget);
expect(find.text('2 to catalogue'), findsOneWidget);
expect(find.text('No seeds yet. Tap + to add your first.'), findsOneWidget);
// Open the tray and name the first draft.
await tester.tap(find.byKey(const Key('inventory.triageBanner')));
await tester.pumpAndSettle();
expect(find.text('Unnamed'), findsNWidgets(2));
await tester.tap(find.text('Unnamed').first);
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('draft.nameField')),
'Grandma tomato',
);
await tester.tap(find.byKey(const Key('draft.nameSave')));
await tester.pumpAndSettle();
// One draft left in the tray
expect(find.text('Unnamed'), findsOneWidget);
// and close the sheet: the named seed is now in the inventory, the
// banner shows one remaining.
await tester.tapAt(const Offset(20, 20));
await tester.pumpAndSettle();
expect(find.text('Grandma tomato'), findsOneWidget);
expect(find.text('1 to catalogue'), findsOneWidget);
await disposeTree(tester);
},
);
}

View file

@ -10,6 +10,7 @@ void main() {
child: TaneApp(
repository: newTestRepository(db),
species: newTestSpeciesRepository(db),
onboarding: newTestOnboardingStore(),
),
);

View file

@ -0,0 +1,83 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/app.dart';
import 'package:tane/i18n/strings.g.dart';
import 'package:tane/ui/intro_screen.dart';
import '../support/test_support.dart';
void main() {
Widget standalone(VoidCallback onDone) => TranslationProvider(
child: MaterialApp(home: IntroScreen(onDone: onDone)),
);
testWidgets('carousel advances through slides and finishes', (tester) async {
LocaleSettings.setLocaleSync(AppLocale.en);
var done = false;
await tester.pumpWidget(standalone(() => done = true));
await tester.pumpAndSettle();
// First slide + a "Next" button (not "Get started" yet).
expect(find.text('The seed that brought you here'), findsOneWidget);
expect(find.text('Next'), findsOneWidget);
expect(find.text('Get started'), findsNothing);
// Advance to the last of the five slides.
for (var i = 0; i < 4; i++) {
await tester.tap(find.text('Next'));
await tester.pumpAndSettle();
}
expect(find.text('To sow is to multiply'), findsOneWidget);
expect(find.text('Get started'), findsOneWidget);
await tester.tap(find.text('Get started'));
await tester.pumpAndSettle();
expect(done, isTrue);
});
testWidgets('Skip finishes immediately', (tester) async {
LocaleSettings.setLocaleSync(AppLocale.en);
var done = false;
await tester.pumpWidget(standalone(() => done = true));
await tester.pumpAndSettle();
await tester.tap(find.text('Skip'));
await tester.pumpAndSettle();
expect(done, isTrue);
});
testWidgets('first launch opens on the intro and marks it seen', (
tester,
) async {
LocaleSettings.setLocaleSync(AppLocale.en);
final db = newTestDatabase();
addTearDown(db.close);
final onboarding = newTestOnboardingStore(introSeen: false);
await tester.pumpWidget(
TranslationProvider(
child: TaneApp(
repository: newTestRepository(db),
species: newTestSpeciesRepository(db),
onboarding: onboarding,
showIntro: true,
),
),
);
await tester.pumpAndSettle();
// The carousel is shown, not the home menu.
expect(find.text('The seed that brought you here'), findsOneWidget);
await tester.tap(find.text('Skip'));
await tester.pumpAndSettle();
// Lands on home, and the intro is now remembered as seen.
expect(find.text('Your inventory'), findsOneWidget);
expect(await onboarding.introSeen(), isTrue);
await disposeTree(tester);
});
}