import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:aero_track/models/app_settings.dart'; import 'package:aero_track/models/dose_record.dart'; import 'package:aero_track/services/active_session_service.dart'; import 'package:aero_track/services/storage_service.dart'; // 模拟记录已写入,但后续步骤失败的可重试保存。 class FailAfterWriteStorage extends StorageService { FailAfterWriteStorage(super.prefs); bool failNext = true; @override Future recalculateCanisterDoses() async { await super.recalculateCanisterDoses(); if (failNext) { failNext = false; throw StateError('Simulated failure after persistence'); } } } void main() { TestWidgetsFlutterBinding.ensureInitialized(); final session = ActiveSessionService(); setUp(() { session.cancelSession(); SharedPreferences.setMockInitialValues({}); }); tearDown(() => session.cancelSession()); void finishSession(StorageService storage) { session.startNewSession( medications: storage.getActiveCanisters(), settings: AppSettings( enableSound: false, enableVibration: false, enableVoicePrompt: false, ), ); while (!session.isFinished) { if (session.isWaitingForNextMed) { session.startNextMedication(); } else { session.completeCurrentStep(); } } } DoseRecord record(StorageService storage, String id) { final now = DateTime.now().toUtc(); return DoseRecord( id: id, startTime: now, endTime: now, flixotidePuffs: 2, flixotideBottleId: storage.getActiveFlixotideBottle()!.id, ); } test('完成雾化保存后立即只扣一次,重算和重启均保持一致', () async { final storage = await StorageService.init(); finishSession(storage); await session.confirmAndSave(storage); expect(storage.getRecords(), hasLength(1)); expect(storage.getPrimaryFlixotide()!.remainingDoses, 58); expect(storage.getPrimaryVentolin()!.remainingDoses, 198); await storage.recalculateCanisterDoses(); final reopened = await StorageService.init(); expect(reopened.getPrimaryFlixotide()!.remainingDoses, 58); expect(reopened.getPrimaryVentolin()!.remainingDoses, 198); }); test('并发和保存完成后的重复确认均只生成一条记录', () async { final storage = await StorageService.init(); finishSession(storage); final first = session.confirmAndSave(storage); expect(session.isSaving, isTrue); final second = session.confirmAndSave(storage); expect(identical(first, second), isTrue); await Future.wait([first, second]); await session.confirmAndSave(storage); expect(session.isSaving, isFalse); expect(storage.getRecords(), hasLength(1)); expect(storage.getPrimaryFlixotide()!.remainingDoses, 58); }); test('写入后失败重试仍使用同一记录,下一次会话使用新记录', () async { await StorageService.init(); final storage = FailAfterWriteStorage(await SharedPreferences.getInstance()); finishSession(storage); await expectLater(session.confirmAndSave(storage), throwsStateError); final firstId = storage.getRecords().single.id; expect(session.isSaving, isFalse); expect(session.isActive, isTrue); await session.confirmAndSave(storage); expect(storage.getRecords().single.id, firstId); expect(storage.getPrimaryFlixotide()!.remainingDoses, 58); finishSession(storage); await session.confirmAndSave(storage); expect(storage.getRecords(), hasLength(2)); expect(storage.getPrimaryFlixotide()!.remainingDoses, 56); }); test('相同记录 ID 顺序或并发提交均只入账一次', () async { final storage = await StorageService.init(); final dose = record(storage, 'same-operation'); await Future.wait([storage.addRecord(dose), storage.addRecord(dose)]); await storage.addRecord(dose); expect(storage.getRecords(), hasLength(1)); expect(storage.getPrimaryFlixotide()!.remainingDoses, 58); }); test('重试不能重新启用已删除的记录', () async { final storage = await StorageService.init(); final dose = record(storage, 'deleted-operation'); await storage.addRecord(dose); await storage.deleteRecord(dose.id); await storage.addRecord(dose); expect(storage.getRecords(), isEmpty); expect(storage.getAllRecords(includeDeleted: true), hasLength(1)); expect(storage.getPrimaryFlixotide()!.remainingDoses, 60); }); test('并发保存不同记录不会覆盖或丢失用量', () async { final storage = await StorageService.init(); await Future.wait([ storage.addRecord(record(storage, 'operation-1')), storage.addRecord(record(storage, 'operation-2')), ]); expect(storage.getRecords(), hasLength(2)); expect(storage.getPrimaryFlixotide()!.remainingDoses, 56); }); }