tane/apps/app_seeds/test/services/label_sheet_service_test.dart
vjrj efb369eb81 feat(labels): print seed labels with QR from an inventory selection
Add multi-select to the inventory and a "Print labels" flow that renders a
PDF sheet of labels (small stickers or large cards), each carrying the seed's
common name, variety, scientific name, year/origin and a tanemaki://seed QR
that holds the seed's data offline (future scan-to-import).

- SeedLabelCodec: compact, versioned tanemaki://seed URI (encode/decode)
- LabelSheetService: pure-Dart PDF grid (pdf + barcode + DejaVu), RTL-aware
- VarietyRepository.labelRows: one row per lot, locale-picked common name
- InventoryCubit selection mode; contextual app bar + label print sheet
- i18n en/es/pt (printLabels.*)

Tests: codec, PDF service (both formats + RTL), labelRows, selection cubit,
inventory widget selection.
2026-07-10 19:14:04 +02:00

92 lines
2.9 KiB
Dart

import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/services/file_service.dart';
import 'package:tane/services/label_sheet_service.dart';
/// Captures the bytes handed to the save dialog instead of touching disk.
class _RecordingFileService implements FileService {
Uint8List? saved;
String? savedName;
@override
Future<String?> saveFile({
required String suggestedName,
required Uint8List bytes,
}) async {
saved = bytes;
savedName = suggestedName;
return '/dev/null/$suggestedName';
}
@override
Future<Uint8List?> pickFileBytes({List<String>? allowedExtensions}) async =>
null;
}
const _labels = [
LabelSheetLabel(
varietyLabel: 'Acelga de Perales',
commonName: 'Acelga',
scientificName: 'Beta vulgaris',
details: '2006 · Perales',
qrData: 'tanemaki://seed?v=Acelga+de+Perales&y=2006',
),
LabelSheetLabel(
varietyLabel: 'Alubia de Tolosa',
qrData: 'tanemaki://seed?v=Alubia+de+Tolosa',
),
];
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
for (final format in LabelSheetFormat.values) {
test('buildPdf renders a well-formed PDF for $format', () async {
final service = LabelSheetService(files: _RecordingFileService());
final bytes = await service.buildPdf(labels: _labels, format: format);
// %PDF header + non-trivial body; text content is compressed, so the
// human-readable assertions live in the widget/data tests.
expect(String.fromCharCodes(bytes.take(5)), '%PDF-');
expect(bytes.length, greaterThan(1000));
});
}
test('buildPdf renders a name-only label (no optional fields)', () async {
final service = LabelSheetService(files: _RecordingFileService());
final bytes = await service.buildPdf(
labels: const [
LabelSheetLabel(varietyLabel: 'Maíz', qrData: 'tanemaki://seed?v=Maíz'),
],
format: LabelSheetFormat.stickers,
);
expect(String.fromCharCodes(bytes.take(5)), '%PDF-');
});
test('buildPdf honours an RTL text direction', () async {
final service = LabelSheetService(files: _RecordingFileService());
final bytes = await service.buildPdf(
labels: const [
LabelSheetLabel(varietyLabel: 'بامية', qrData: 'tanemaki://seed?v=x'),
],
format: LabelSheetFormat.cards,
rtl: true,
);
expect(String.fromCharCodes(bytes.take(5)), '%PDF-');
});
test('saveLabels hands the PDF to the save dialog', () async {
final files = _RecordingFileService();
final service = LabelSheetService(files: files);
final saved = await service.saveLabels(
labels: _labels,
format: LabelSheetFormat.cards,
suggestedName: 'tanemaki-labels-2026-07-10.pdf',
);
expect(saved, isTrue);
expect(files.savedName, 'tanemaki-labels-2026-07-10.pdf');
expect(String.fromCharCodes(files.saved!.take(5)), '%PDF-');
});
}