|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | +import 'package:on_time_front/domain/entities/alarm_entities.dart'; |
| 3 | +import 'package:on_time_front/domain/use-cases/cancel_schedule_alarm_use_case.dart'; |
| 4 | +import 'package:on_time_front/domain/use-cases/reconcile_alarms_use_case.dart'; |
| 5 | +import 'package:on_time_front/domain/use-cases/schedule_mutation_alarm_effects_coordinator.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + test( |
| 9 | + 'created and updated schedules reconcile without targeted cancellation', |
| 10 | + () async { |
| 11 | + final cancel = _FakeCancelScheduleAlarmUseCase(); |
| 12 | + final reconcile = _FakeReconcileAlarmsUseCase(); |
| 13 | + final coordinator = ScheduleMutationAlarmEffectsCoordinator( |
| 14 | + cancel, |
| 15 | + reconcile, |
| 16 | + ); |
| 17 | + |
| 18 | + await coordinator( |
| 19 | + operation: ScheduleMutationAlarmOperation.created, |
| 20 | + scheduleId: 'schedule-1', |
| 21 | + ); |
| 22 | + await coordinator( |
| 23 | + operation: ScheduleMutationAlarmOperation.updated, |
| 24 | + scheduleId: 'schedule-1', |
| 25 | + ); |
| 26 | + await pumpEventQueue(); |
| 27 | + |
| 28 | + expect(cancel.cancelledScheduleIds, isEmpty); |
| 29 | + expect(reconcile.callCount, 2); |
| 30 | + }, |
| 31 | + ); |
| 32 | + |
| 33 | + test( |
| 34 | + 'deleted and finished schedules cancel targeted alarm before reconciling', |
| 35 | + () async { |
| 36 | + final events = <String>[]; |
| 37 | + final cancel = _FakeCancelScheduleAlarmUseCase(events: events); |
| 38 | + final reconcile = _FakeReconcileAlarmsUseCase(events: events); |
| 39 | + final coordinator = ScheduleMutationAlarmEffectsCoordinator( |
| 40 | + cancel, |
| 41 | + reconcile, |
| 42 | + ); |
| 43 | + |
| 44 | + await coordinator( |
| 45 | + operation: ScheduleMutationAlarmOperation.deleted, |
| 46 | + scheduleId: 'schedule-1', |
| 47 | + ); |
| 48 | + await coordinator( |
| 49 | + operation: ScheduleMutationAlarmOperation.finished, |
| 50 | + scheduleId: 'schedule-2', |
| 51 | + ); |
| 52 | + await pumpEventQueue(); |
| 53 | + |
| 54 | + expect(events, [ |
| 55 | + 'cancel:schedule-1', |
| 56 | + 'reconcile', |
| 57 | + 'cancel:schedule-2', |
| 58 | + 'reconcile', |
| 59 | + ]); |
| 60 | + }, |
| 61 | + ); |
| 62 | + |
| 63 | + test('reconciliation failures remain best-effort', () async { |
| 64 | + final cancel = _FakeCancelScheduleAlarmUseCase(); |
| 65 | + final reconcile = _FakeReconcileAlarmsUseCase()..throwOnCall = true; |
| 66 | + final coordinator = ScheduleMutationAlarmEffectsCoordinator( |
| 67 | + cancel, |
| 68 | + reconcile, |
| 69 | + ); |
| 70 | + |
| 71 | + await coordinator( |
| 72 | + operation: ScheduleMutationAlarmOperation.created, |
| 73 | + scheduleId: 'schedule-1', |
| 74 | + ); |
| 75 | + await pumpEventQueue(); |
| 76 | + |
| 77 | + expect(reconcile.callCount, 1); |
| 78 | + }); |
| 79 | +} |
| 80 | + |
| 81 | +class _FakeCancelScheduleAlarmUseCase implements CancelScheduleAlarmUseCase { |
| 82 | + _FakeCancelScheduleAlarmUseCase({List<String>? events}) : _events = events; |
| 83 | + |
| 84 | + final cancelledScheduleIds = <String>[]; |
| 85 | + final List<String>? _events; |
| 86 | + |
| 87 | + @override |
| 88 | + Future<void> call(String scheduleId) async { |
| 89 | + cancelledScheduleIds.add(scheduleId); |
| 90 | + _events?.add('cancel:$scheduleId'); |
| 91 | + } |
| 92 | + |
| 93 | + @override |
| 94 | + dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 95 | +} |
| 96 | + |
| 97 | +class _FakeReconcileAlarmsUseCase implements ReconcileAlarmsUseCase { |
| 98 | + _FakeReconcileAlarmsUseCase({List<String>? events}) : _events = events; |
| 99 | + |
| 100 | + int callCount = 0; |
| 101 | + bool throwOnCall = false; |
| 102 | + final List<String>? _events; |
| 103 | + |
| 104 | + @override |
| 105 | + Future<AlarmReconciliationResult> call() async { |
| 106 | + callCount += 1; |
| 107 | + _events?.add('reconcile'); |
| 108 | + if (throwOnCall) { |
| 109 | + throw Exception('reconcile failed'); |
| 110 | + } |
| 111 | + return AlarmReconciliationResult( |
| 112 | + status: AlarmReconciliationStatus.armed, |
| 113 | + nativeAlarmProvider: AlarmProvider.none, |
| 114 | + fallbackProvider: AlarmProvider.localNotification, |
| 115 | + armedScheduleIds: const [], |
| 116 | + skippedScheduleCount: 0, |
| 117 | + failures: const [], |
| 118 | + scheduleWindowStart: DateTime.utc(2026, 5, 15), |
| 119 | + scheduleWindowEnd: DateTime.utc(2026, 5, 23), |
| 120 | + alarmCoverageStart: DateTime.utc(2026, 5, 15), |
| 121 | + alarmCoverageEnd: DateTime.utc(2026, 5, 22), |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + @override |
| 126 | + dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 127 | +} |
0 commit comments