Root-cause fix for the unstoppable-alarm incident (alarm rang 15 minutes, only uninstall silenced it) plus systematic hardening of every stop path. Native (Kotlin): - Verified stop: stopActiveAlarm now derives its result from the real post-teardown state (companion instance + synchronous stopEverything + activeRingingId check) instead of reporting unconditional success. - Atomic teardown: every stop path (stop action, notification button, snooze, missed, onDestroy, startForeground failure) funnels through one stopEverything() covering audio, wakelock, notification, foreground state and firing-record cleanup; player.release() guarded. - Bounded ringing: 10-minute auto-silence armed via AlarmManager fires a FIRED->MISSED transition with a localized missed-alarm notification; repeating alarms keep their native rearm, deleted alarms never produce ghost MISSED notifications. - Durable firing record with onStartCommand re-validation (resurrection guard) and boot-time stale cleanup; firing records cleared on every refuse/mismatch/cancel path. - New notification-only dismissal channel (dismissAlarmNotificationOnly) so UI-level dedup can never kill a live ring's audio. Flutter (Dart): - Stop/disable/edit/delete of a ringing alarm always attempt to silence it; on native-query failure the stop falls back toward silence via the id-scoped legacy stop. - Verified-stop results surface failures: the ringing screen keeps dismiss-by-design on success, but on a verified failure it stays up with a persistent force-stop banner (guarded against double-dismiss) and auto-dismisses if the ring ends externally (missed/notification). - Missed events sync alarm bookkeeping without opening the ringing UI. - 4 new l10n keys translated across all 13 locales (ARB guard green). 550 tests green, analyzer clean. Reviewed in 3 adversarial 4-lens rounds (2 deterministic + 1 refuter-corroborated critical fixed); formal gentle-ai receipt waived by maintainer authorization (correction scope legitimately exceeded the frozen genesis paths). On-device QA checklist in openspec/changes/alarm-system-overhaul/tasks.md pending before archive.
165 lines
5.2 KiB
Dart
165 lines
5.2 KiB
Dart
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pluriwave/modelos/alarma_musical.dart';
|
|
import 'package:pluriwave/modelos/emisora.dart';
|
|
import 'package:pluriwave/servicios/servicio_alarmas_android.dart';
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
const channel = MethodChannel('pluriwave/alarm_scheduler');
|
|
late List<MethodCall> llamadas;
|
|
|
|
setUp(() {
|
|
llamadas = [];
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (call) async {
|
|
llamadas.add(call);
|
|
switch (call.method) {
|
|
case 'scheduleAlarm':
|
|
return true;
|
|
case 'requestIgnoreBatteryOptimizations':
|
|
return true;
|
|
case 'getActiveRingingAlarmId':
|
|
return 'ring1';
|
|
case 'stopActiveAlarm':
|
|
return {'stopped': true, 'wasRinging': true, 'activeAlarmId': 'ring1'};
|
|
}
|
|
return null;
|
|
});
|
|
});
|
|
|
|
tearDown(() {
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, null);
|
|
});
|
|
|
|
test(
|
|
'programar incluye emisora de respaldo y fade en el payload nativo',
|
|
() async {
|
|
final servicio = ServicioAlarmasAndroid(channel: channel);
|
|
final alarma = AlarmaMusical(
|
|
id: 'a1',
|
|
nombre: 'Con respaldo',
|
|
hora: 7,
|
|
minuto: 30,
|
|
tipoProgramacion: TipoProgramacionAlarma.diaria,
|
|
diasSemana: const [],
|
|
proximaEjecucion: DateTime(2099, 1, 1, 7, 30),
|
|
emisora: const Emisora(
|
|
uuid: 'uuid-principal',
|
|
nombre: 'Principal FM',
|
|
url: 'https://principal.example/stream',
|
|
),
|
|
emisoraFallback: const Emisora(
|
|
uuid: 'uuid-respaldo',
|
|
nombre: 'Respaldo FM',
|
|
url: 'https://respaldo.example/stream',
|
|
),
|
|
fadeInSegundos: 12,
|
|
);
|
|
|
|
await servicio.programar(alarma);
|
|
|
|
final llamada = llamadas.singleWhere((c) => c.method == 'scheduleAlarm');
|
|
final args = llamada.arguments as Map<Object?, Object?>;
|
|
expect(args['fallbackStationName'], 'Respaldo FM');
|
|
expect(args['fallbackStationUrl'], 'https://respaldo.example/stream');
|
|
expect(args['fadeInSegundos'], 12);
|
|
expect(args['fallbackSound'], SonidoInternoAlarma.amanecer.name);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'programar sin emisora de respaldo envia campos de respaldo nulos',
|
|
() async {
|
|
final servicio = ServicioAlarmasAndroid(channel: channel);
|
|
final alarma = AlarmaMusical(
|
|
id: 'a2',
|
|
nombre: 'Sin respaldo',
|
|
hora: 8,
|
|
minuto: 0,
|
|
tipoProgramacion: TipoProgramacionAlarma.diaria,
|
|
diasSemana: const [],
|
|
proximaEjecucion: DateTime(2099, 1, 1, 8, 0),
|
|
);
|
|
|
|
await servicio.programar(alarma);
|
|
|
|
final llamada = llamadas.singleWhere((c) => c.method == 'scheduleAlarm');
|
|
final args = llamada.arguments as Map<Object?, Object?>;
|
|
expect(args.containsKey('fallbackStationName'), isTrue);
|
|
expect(args['fallbackStationName'], isNull);
|
|
expect(args.containsKey('fallbackStationUrl'), isTrue);
|
|
expect(args['fallbackStationUrl'], isNull);
|
|
expect(args['fadeInSegundos'], 0);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'solicitarExencionBateria invoca requestIgnoreBatteryOptimizations',
|
|
() async {
|
|
final servicio = ServicioAlarmasAndroid(channel: channel);
|
|
|
|
final abierto = await servicio.solicitarExencionBateria();
|
|
|
|
expect(abierto, isTrue);
|
|
expect(
|
|
llamadas.map((c) => c.method),
|
|
contains('requestIgnoreBatteryOptimizations'),
|
|
);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'detenerSonidoActivo mapea el resultado nativo confirmado a ResultadoDetencion',
|
|
() async {
|
|
final servicio = ServicioAlarmasAndroid(channel: channel);
|
|
|
|
final resultado = await servicio.detenerSonidoActivo();
|
|
final sonando = await servicio.alarmaSonandoId();
|
|
|
|
expect(resultado.detenido, isTrue);
|
|
expect(resultado.estabaSonando, isTrue);
|
|
expect(resultado.alarmaId, 'ring1');
|
|
expect(sonando, 'ring1');
|
|
},
|
|
);
|
|
|
|
test(
|
|
'detenerSonidoActivo retorna un resultado fallido cuando el canal lanza error',
|
|
() async {
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (call) async {
|
|
llamadas.add(call);
|
|
throw PlatformException(code: 'STOP_FAILED', message: 'boom');
|
|
});
|
|
final servicio = ServicioAlarmasAndroid(channel: channel);
|
|
|
|
final resultado = await servicio.detenerSonidoActivo();
|
|
|
|
expect(resultado.detenido, isFalse);
|
|
expect(resultado.estabaSonando, isFalse);
|
|
expect(resultado.alarmaId, isNull);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'alarmaSonandoId propaga el error del canal (fail-toward-silence, '
|
|
'Finding 2)',
|
|
() async {
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (call) async {
|
|
llamadas.add(call);
|
|
throw PlatformException(code: 'QUERY_FAILED', message: 'boom');
|
|
});
|
|
final servicio = ServicioAlarmasAndroid(channel: channel);
|
|
|
|
expect(
|
|
() => servicio.alarmaSonandoId(),
|
|
throwsA(isA<PlatformException>()),
|
|
);
|
|
},
|
|
);
|
|
}
|