tane/apps/app_seeds/test/data/variety_repository_test.dart
vjrj 8a19a6c85a feat(block1): photo thumbnails in the inventory list
The list showed only initials even when a variety had a photo (quick-add stores
one as an encrypted BLOB). Now the avatar shows the first photo, falling back to
the initial — matching the "foto/inicial" list spec.

- VarietyListItem carries the first photo bytes; watchInventory loads them in a
  single query per emission.
- List tile renders a MemoryImage avatar when present.

Tests: repository (photo bytes surfaced, null when none) and a widget test
(avatar replaces the initial). Full suite: 37 passing, 0 skipped.
2026-07-07 22:03:36 +02:00

88 lines
2.8 KiB
Dart

import 'dart:typed_data';
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 'package:tane/db/enums.dart';
import '../support/test_support.dart';
void main() {
late AppDatabase db;
late VarietyRepository repo;
setUp(() {
db = newTestDatabase();
repo = newTestRepository(db);
});
tearDown(() => db.close());
test(
'addQuickVariety persists a variety that appears in the stream',
() async {
await repo.addQuickVariety(
label: 'Grandma tomato',
category: 'Solanaceae',
);
final items = await repo.watchInventory().first;
expect(items.single.label, 'Grandma tomato');
expect(items.single.category, 'Solanaceae');
},
);
test(
'a quantity creates a Lot and a photo creates an encrypted-BLOB Attachment',
() async {
final id = await repo.addQuickVariety(
label: 'Maize',
quantity: const Quantity(kind: QuantityKind.cob),
photoBytes: Uint8List.fromList([1, 2, 3]),
);
final lot = await db.select(db.lots).getSingle();
expect(lot.varietyId, id);
expect(lot.quantityKind, 'cob');
final attachment = await db.select(db.attachments).getSingle();
expect(attachment.parentType, ParentType.variety);
expect(attachment.parentId, id);
expect(attachment.kind, AttachmentKind.photo);
expect(attachment.bytes, [1, 2, 3]);
},
);
test('watchInventory carries the first photo bytes for the avatar', () async {
await repo.addQuickVariety(
label: 'With photo',
photoBytes: Uint8List.fromList([9, 8, 7]),
);
await repo.addQuickVariety(label: 'No photo');
final items = await repo.watchInventory().first;
expect(items.firstWhere((i) => i.label == 'With photo').photo, [9, 8, 7]);
expect(items.firstWhere((i) => i.label == 'No photo').photo, isNull);
});
test(
'stream is ordered by category then label and excludes soft-deleted rows',
() async {
await repo.addQuickVariety(label: 'Bean', category: 'Fabaceae');
await repo.addQuickVariety(label: 'Apple', category: 'Rosaceae');
await repo.addQuickVariety(label: 'Almond', category: 'Rosaceae');
final items = await repo.watchInventory().first;
expect(items.map((i) => i.label), ['Bean', 'Almond', 'Apple']);
},
);
test('writes stamp a monotonic HLC into updated_at', () async {
await repo.addQuickVariety(label: 'First');
await repo.addQuickVariety(label: 'Second');
final rows = await db.select(db.varieties).get();
final stamps = rows.map((r) => r.updatedAt).toList()..sort();
// Distinct, parseable, ordered HLC stamps.
expect(stamps.toSet().length, 2);
expect(() => stamps.map(Hlc.parse).toList(), returnsNormally);
});
}