fix(compras): revocar el PRO tras un reembolso sin penalizar a quien pago
Verificacion silenciosa en segundo plano con queryPastPurchases, al abrir o volver a la app y al cargar Android Auto. Solo revoca tras dos respuestas validas de Play sin la compra separadas 12h; sin red o con error no toca nada. Reactiva el PRO automaticamente si Play confirma la compra.
This commit is contained in:
@@ -2,7 +2,10 @@ import 'dart:async';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pluriwave/estado/estado_entitlement.dart';
|
||||
import 'package:pluriwave/servicios/servicio_audio.dart'
|
||||
show registrarInvalidacionArbolAuto;
|
||||
import 'package:pluriwave/servicios/servicio_compras.dart';
|
||||
import 'package:pluriwave/servicios/verificacion_licencia.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// Fake [PuertoCompras] (Design ADR-2): never touches `in_app_purchase`,
|
||||
@@ -11,6 +14,12 @@ class _PuertoComprasFalso implements PuertoCompras {
|
||||
final _eventos = StreamController<EventoCompra>.broadcast();
|
||||
int comprasIntentadas = 0;
|
||||
int restaurosIntentados = 0;
|
||||
int consultasPropiedad = 0;
|
||||
|
||||
/// What the silent ownership query answers. Defaults to [desconocido] so
|
||||
/// every pre-existing test keeps its old behavior (fail-open: no change).
|
||||
ResultadoVerificacionLicencia propiedad =
|
||||
ResultadoVerificacionLicencia.desconocido;
|
||||
|
||||
@override
|
||||
Stream<EventoCompra> get eventos => _eventos.stream;
|
||||
@@ -25,11 +34,32 @@ class _PuertoComprasFalso implements PuertoCompras {
|
||||
restaurosIntentados++;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ResultadoVerificacionLicencia> consultarPropiedad() async {
|
||||
consultasPropiedad++;
|
||||
return propiedad;
|
||||
}
|
||||
|
||||
void emitir(EventoCompra evento) => _eventos.add(evento);
|
||||
|
||||
Future<void> dispose() => _eventos.close();
|
||||
}
|
||||
|
||||
/// Mutable clock for the throttle/spacing rules of the license check.
|
||||
class _Reloj {
|
||||
DateTime ahora = DateTime(2026, 9, 18, 10);
|
||||
|
||||
DateTime call() => ahora;
|
||||
}
|
||||
|
||||
/// Lets every microtask/async continuation of the fire-and-forget license
|
||||
/// check settle (mock prefs + fake port complete immediately).
|
||||
Future<void> _asentar() async {
|
||||
for (var i = 0; i < 10; i++) {
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
@@ -327,6 +357,135 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
group('verificacion silenciosa de licencia (reembolsos)', () {
|
||||
late _PuertoComprasFalso compras;
|
||||
late _Reloj reloj;
|
||||
late int invalidaciones;
|
||||
|
||||
setUp(() {
|
||||
compras = _PuertoComprasFalso();
|
||||
reloj = _Reloj();
|
||||
invalidaciones = 0;
|
||||
registrarInvalidacionArbolAuto(() => invalidaciones++);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
registrarInvalidacionArbolAuto(() {});
|
||||
await compras.dispose();
|
||||
});
|
||||
|
||||
Future<EstadoEntitlement> crear({required bool premium}) async {
|
||||
SharedPreferences.setMockInitialValues({'compra_premium_v1': premium});
|
||||
final estado = EstadoEntitlement(
|
||||
prefs: await SharedPreferences.getInstance(),
|
||||
compras: compras,
|
||||
reloj: reloj.call,
|
||||
);
|
||||
addTearDown(estado.dispose);
|
||||
await _asentar();
|
||||
return estado;
|
||||
}
|
||||
|
||||
test('se dispara sola al cargar, sin bloquear la carga', () async {
|
||||
final estado = await crear(premium: true);
|
||||
|
||||
expect(estado.esPremium, isTrue);
|
||||
expect(compras.consultasPropiedad, 1);
|
||||
});
|
||||
|
||||
test('desconocido (offline) conserva premium sin notificar nada', () async {
|
||||
final estado = await crear(premium: true);
|
||||
var notificaciones = 0;
|
||||
estado.addListener(() => notificaciones++);
|
||||
|
||||
reloj.ahora = reloj.ahora.add(const Duration(days: 2));
|
||||
await estado.refrescarLicencia();
|
||||
|
||||
expect(estado.esPremium, isTrue);
|
||||
expect(notificaciones, 0);
|
||||
expect(invalidaciones, 0);
|
||||
});
|
||||
|
||||
test('una sola ausencia no revoca', () async {
|
||||
compras.propiedad = ResultadoVerificacionLicencia.noPoseida;
|
||||
final estado = await crear(premium: true);
|
||||
|
||||
expect(estado.esPremium, isTrue);
|
||||
});
|
||||
|
||||
test('revocacion confirmada: notifica, invalida el arbol de Auto y NUNCA '
|
||||
'toca resultadoUsuario ni compraEnCurso', () async {
|
||||
compras.propiedad = ResultadoVerificacionLicencia.noPoseida;
|
||||
final estado = await crear(premium: true);
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
// A restore the user started stays in flight, untouched.
|
||||
unawaited(estado.restaurar());
|
||||
await _asentar();
|
||||
expect(estado.compraEnCurso, isTrue);
|
||||
|
||||
var notificaciones = 0;
|
||||
estado.addListener(() => notificaciones++);
|
||||
|
||||
reloj.ahora = reloj.ahora.add(const Duration(days: 1));
|
||||
await estado.refrescarLicencia();
|
||||
|
||||
expect(estado.esPremium, isFalse);
|
||||
expect(prefs.getBool('compra_premium_v1'), isFalse);
|
||||
expect(notificaciones, greaterThan(0));
|
||||
expect(invalidaciones, 1);
|
||||
expect(estado.resultadoUsuario, isNull);
|
||||
expect(estado.compraEnCurso, isTrue);
|
||||
});
|
||||
|
||||
test('poseida con la flag en false desbloquea en silencio', () async {
|
||||
compras.propiedad = ResultadoVerificacionLicencia.poseida;
|
||||
final estado = await crear(premium: false);
|
||||
|
||||
expect(estado.esPremium, isTrue);
|
||||
expect(invalidaciones, 1);
|
||||
expect(estado.resultadoUsuario, isNull);
|
||||
expect(estado.compraEnCurso, isFalse);
|
||||
});
|
||||
|
||||
test('refrescarLicencia recoge un cambio hecho por otra via (Android '
|
||||
'Auto) en prefs', () async {
|
||||
final estado = await crear(premium: true);
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
await prefs.setBool('compra_premium_v1', false);
|
||||
await estado.refrescarLicencia();
|
||||
|
||||
expect(estado.esPremium, isFalse);
|
||||
});
|
||||
|
||||
test('una compra real reinicia el contador de ausencias', () async {
|
||||
compras.propiedad = ResultadoVerificacionLicencia.noPoseida;
|
||||
final estado = await crear(premium: false);
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(claveAusenciasLicencia, 1);
|
||||
|
||||
compras.emitir(const EventoCompra(TipoEventoCompra.comprada));
|
||||
await _asentar();
|
||||
|
||||
expect(estado.esPremium, isTrue);
|
||||
expect(prefs.getInt(claveAusenciasLicencia), isNull);
|
||||
});
|
||||
|
||||
test('sin puerto de compras no verifica nada', () async {
|
||||
SharedPreferences.setMockInitialValues({'compra_premium_v1': true});
|
||||
final estado = EstadoEntitlement(
|
||||
prefs: await SharedPreferences.getInstance(),
|
||||
reloj: reloj.call,
|
||||
);
|
||||
addTearDown(estado.dispose);
|
||||
await _asentar();
|
||||
await estado.refrescarLicencia();
|
||||
|
||||
expect(estado.esPremium, isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('esPremiumPersistido (headless, sin BuildContext)', () {
|
||||
test('lee la flag persistida directamente desde prefs', () async {
|
||||
SharedPreferences.setMockInitialValues({'compra_premium_v1': true});
|
||||
|
||||
Reference in New Issue
Block a user