import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:machweis/core/storage/local_storage.dart';
import 'package:machweis/core/sync/sync_queue_store.dart';
import 'package:machweis/features/reports/data/demo_report_store.dart';
import 'package:machweis/features/reports/data/report_photo_store.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUpAll(() async {
SharedPreferences.setMockInitialValues({});
await LocalStorage.instance.initialize(forcePreferences: true);
await SyncQueueStore.instance.initialize();
await ReportPhotoStore.instance.initialize();
});
test('Fotodaten werden einem Bericht zugeordnet und entfernt', () async {
final bytes = Uint8List.fromList([1, 2, 3, 4]);
final photo = ReportPhoto(
id: 'photo-1',
reportId: 'report-1',
fileName: 'anlage.jpg',
mimeType: 'image/jpeg',
dataBase64: base64Encode(bytes),
createdAt: DateTime(2026, 8, 29),
);
await ReportPhotoStore.instance.add(photo);
final index = await LocalStorage.instance.getString(
'machweis_report_photos_v1',
);
expect(index, isNot(contains('dataBase64')));
expect(
await LocalStorage.instance.getString(
'machweis_report_photo_data_photo-1',
),
base64Encode(bytes),
);
final stored = ReportPhotoStore.instance.photoById(photo.id);
expect(stored, isNotNull);
expect(stored!.bytes, bytes);
expect(ReportPhotoStore.instance.photosFor('report-1'), hasLength(1));
await ReportPhotoStore.instance.remove(photo.id);
expect(ReportPhotoStore.instance.photoById(photo.id), isNull);
expect(
SyncQueueStore.instance.operations.where(
(operation) => operation.entityId == photo.id,
),
isEmpty,
);
});
test('Berichtsserialisierung bewahrt stabile Fotoreferenzen', () {
final report = DemoReport(
id: 'report-photo-json',
customer: 'Kunde',
site: 'Baustelle',
activity: 'Dokumentiert',
material: '',
quantity: '',
date: DateTime(2026, 8, 29),
start: const TimeOfDay(hour: 8, minute: 0),
end: const TimeOfDay(hour: 9, minute: 0),
photoIds: const ['photo-a', 'photo-b'],
fileIds: const ['file-a'],
fileNames: const ['beleg.pdf'],
);
final restored = DemoReport.fromJson(
jsonDecode(jsonEncode(report.toJson())) as Map<String, dynamic>,
);
expect(restored.photoIds, ['photo-a', 'photo-b']);
expect(restored.fileIds, ['file-a']);
expect(restored.fileNames, ['beleg.pdf']);
expect(restored.withVersion(2).photoIds, restored.photoIds);
expect(restored.withVersion(2).fileIds, restored.fileIds);
final finalized = restored.finalized(const [
[0.1, 0.2],
[0.2, 0.3],
[-1, -1],
]);
expect(finalized.signatureAssetId, isNotEmpty);
expect(
DemoReport.fromJson(finalized.toJson()).signatureAssetId,
finalized.signatureAssetId,
);
});
}
