fix(alarm): handle snooze reschedule failures instead of silently dropping them
Build & Deploy PluriWave / Análisis de código (push) Successful in 47s
Build & Deploy PluriWave / Build APK + AAB release (push) Successful in 2m1s

posponerAlarma() and posponerProximaDesdePreaviso() called the native
scheduler with no error handling, unlike guardarAlarma(). When the
native call failed (e.g. revoked exact-alarm permission), the
exception escaped before notifyListeners() ran, leaving the alarm
list stuck on stale data with no real alarm scheduled and no snooze
countdown notification.

Both methods now mirror guardarAlarma()'s pattern: permission
pre-check, try/catch into _error, and an unconditional
notifyListeners() so the UI always reflects the outcome. Failures
surface via SnackBar in the ringing screen and in app.dart's
postpone-next handler.
This commit is contained in:
2026-07-01 00:22:58 +02:00
parent cc98f3f331
commit 6acbd7ca93
6 changed files with 263 additions and 32 deletions
+113
View File
@@ -249,6 +249,119 @@ void main() {
},
);
test('posponerAlarma: cuando android.programar falla, no relanza, notifica y '
'registra el error (sin corromper el estado en memoria)', () async {
final ahora = DateTime(2026, 6, 11, 7, 0);
final android = FakePuertoAlarmasAndroid();
final estado = EstadoAlarmas(
servicio: ServicioAlarmas(reloj: () => ahora),
android: android,
iniciarAutomaticamente: false,
);
addTearDown(estado.dispose);
addTearDown(android.dispose);
await estado.guardarAlarma(alarmaDiaria('fail1'));
final alarma = estado.alarmas.single;
android.fallaProgramar = true;
var notificaciones = 0;
estado.addListener(() => notificaciones++);
await estado.posponerAlarma(alarma, 5);
expect(estado.error, isNotNull);
expect(notificaciones, greaterThanOrEqualTo(1));
expect(
estado.alarmas.single.snoozeHasta,
DateTime(2026, 6, 11, 7, 35),
reason: 'el estado en memoria se aplica antes de la llamada nativa',
);
});
test('posponerAlarma: tras un fallo previo, un reintento exitoso limpia '
'estado.error (D5)', () async {
final ahora = DateTime(2026, 6, 11, 7, 0);
final android = FakePuertoAlarmasAndroid();
final estado = EstadoAlarmas(
servicio: ServicioAlarmas(reloj: () => ahora),
android: android,
iniciarAutomaticamente: false,
);
addTearDown(estado.dispose);
addTearDown(android.dispose);
await estado.guardarAlarma(alarmaDiaria('fail2'));
final alarma = estado.alarmas.single;
android.fallaProgramar = true;
await estado.posponerAlarma(alarma, 5);
expect(estado.error, isNotNull);
android.fallaProgramar = false;
await estado.posponerAlarma(estado.alarmas.single, 5);
expect(estado.error, isNull);
});
test('posponerProximaDesdePreaviso: cuando android.programar falla, no '
'relanza, notifica y registra el error (sin corromper el estado en '
'memoria)', () async {
final ahora = DateTime(2026, 6, 11, 7, 0);
final android = FakePuertoAlarmasAndroid();
final estado = EstadoAlarmas(
servicio: ServicioAlarmas(reloj: () => ahora),
android: android,
iniciarAutomaticamente: false,
);
addTearDown(estado.dispose);
addTearDown(android.dispose);
await estado.guardarAlarma(alarmaDiaria('preaviso-fail1'));
final alarma = estado.alarmas.single;
final ejecucion = alarma.proximaEjecucion!;
android.fallaProgramar = true;
var notificaciones = 0;
estado.addListener(() => notificaciones++);
await estado.posponerProximaDesdePreaviso(alarma, 5, ejecucion);
expect(estado.error, isNotNull);
expect(notificaciones, greaterThanOrEqualTo(1));
expect(
estado.alarmas.single.snoozeHasta,
ejecucion.add(const Duration(minutes: 5)),
reason: 'el estado en memoria se aplica antes de la llamada nativa',
);
});
test('posponerProximaDesdePreaviso: tras un fallo previo, un reintento '
'exitoso limpia estado.error (D5)', () async {
final ahora = DateTime(2026, 6, 11, 7, 0);
final android = FakePuertoAlarmasAndroid();
final estado = EstadoAlarmas(
servicio: ServicioAlarmas(reloj: () => ahora),
android: android,
iniciarAutomaticamente: false,
);
addTearDown(estado.dispose);
addTearDown(android.dispose);
await estado.guardarAlarma(alarmaDiaria('preaviso-fail2'));
final alarma = estado.alarmas.single;
final ejecucion = alarma.proximaEjecucion!;
android.fallaProgramar = true;
await estado.posponerProximaDesdePreaviso(alarma, 5, ejecucion);
expect(estado.error, isNotNull);
android.fallaProgramar = false;
await estado.posponerProximaDesdePreaviso(
estado.alarmas.single,
5,
ejecucion,
);
expect(estado.error, isNull);
});
test(
'evento nativo snoozeCancelled limpia el snooze y avanza sin reprogramar',
() async {