import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import 'package:iron_arc/src/billing/billing_service.dart'; import 'package:iron_arc/src/billing/entitlements.dart'; import 'package:iron_arc/src/billing/models.dart'; import 'package:iron_arc/src/billing/store_client.dart'; import 'package:iron_arc/src/ui/links.dart'; import 'package:shared_preferences/shared_preferences.dart'; /// Stand-in for Google Play: the module is testable before the Console has any /// products, which is the only way to verify the grant/complete logic here. class FakeStore implements StoreClient { FakeStore({ this.available = true, List? products, this.purchaseStarts = true, this.throwOnQuery = false, }) : products = products ?? _defaultProducts; static const List _defaultProducts = [ StoreProduct( id: ProductIds.removeAds, title: 'Werbung entfernen', price: '4,99 €'), ]; final bool available; final bool purchaseStarts; final bool throwOnQuery; final List products; final StreamController _events = StreamController.broadcast(); int completedCalls = 0; int restoreCalls = 0; int buyCalls = 0; bool disposed = false; @override Stream get events => _events.stream; void emit(PurchaseEvent event) => _events.add(event); @override Future isAvailable() async => available; @override Future> queryProducts(Set ids) async { if (throwOnQuery) throw StateError('store down'); return products.where((p) => ids.contains(p.id)).toList(); } @override Future buy(StoreProduct product) async { buyCalls++; return purchaseStarts; } /// Mirrors Play: acknowledging is only required (and only counted) when the /// platform flags `pendingCompletePurchase`. @override Future complete(PurchaseEvent event) async { if (event.complete) completedCalls++; } @override Future restore() async => restoreCalls++; @override Future dispose() async { disposed = true; await _events.close(); } } Future _service(FakeStore store) async { final entitlements = await Entitlements.load(); final billing = BillingService(entitlements: entitlements, client: store); await billing.init(); return billing; } void main() { setUp(() => SharedPreferences.setMockInitialValues({})); test('query on start exposes the catalogue and restores silently', () async { final store = FakeStore(); final billing = await _service(store); expect(billing.available, isTrue); expect(billing.canPurchase, isTrue); expect(billing.product(ProductIds.removeAds)?.price, '4,99 €'); // Restoring on start is the documented rule (monetization.md §3). expect(store.restoreCalls, 1); billing.dispose(); await store.dispose(); }); test('purchase grants the entitlement and is completed', () async { final store = FakeStore(); final billing = await _service(store); expect(await billing.buy(ProductIds.removeAds), isTrue); expect(billing.adsRemoved, isFalse, reason: 'nothing is granted before the store answers'); store.emit(const PurchaseEvent( productId: ProductIds.removeAds, state: PurchaseState.purchased, complete: true, )); await pumpEventQueue(); expect(billing.entitlements.adsRemoved, isTrue); expect(store.completedCalls, 1, reason: 'Play refunds purchases that are not acknowledged'); billing.dispose(); await store.dispose(); }); test('pending purchases are reported but never granted', () async { final store = FakeStore(); final billing = await _service(store); store.emit(const PurchaseEvent( productId: ProductIds.removeAds, state: PurchaseState.pending, complete: true, )); await pumpEventQueue(); expect(billing.entitlements.adsRemoved, isFalse); expect(store.completedCalls, 0); expect(billing.error, contains('pending')); billing.dispose(); await store.dispose(); }); test('cancel and error leave the entitlement untouched', () async { final store = FakeStore(); final billing = await _service(store); store.emit(const PurchaseEvent( productId: ProductIds.removeAds, state: PurchaseState.canceled)); await pumpEventQueue(); expect(billing.entitlements.adsRemoved, isFalse); expect(billing.error, isNull); store.emit(const PurchaseEvent( productId: ProductIds.removeAds, state: PurchaseState.error, error: 'ITEM_ALREADY_OWNED')); await pumpEventQueue(); expect(billing.entitlements.adsRemoved, isFalse); expect(billing.error, 'ITEM_ALREADY_OWNED'); billing.dispose(); await store.dispose(); }); test('restored purchases grant too (reinstall path)', () async { final store = FakeStore(); final billing = await _service(store); store.emit(const PurchaseEvent( productId: ProductIds.removeAds, state: PurchaseState.restored, complete: false, )); await pumpEventQueue(); expect(billing.adsRemoved, isTrue); expect(store.completedCalls, 0, reason: 'restores need no acknowledgement'); billing.dispose(); await store.dispose(); }); test('entitlement survives a restart', () async { final first = await _service(FakeStore()); await first.entitlements.grant(ProductIds.removeAds); expect(first.entitlements.adsRemoved, isTrue); first.dispose(); final second = await Entitlements.load(); expect(second.adsRemoved, isTrue, reason: 'written through to disk'); await second.clearAll(); expect(second.adsRemoved, isFalse); }); test('missing store: no crash, clear error, purchase disabled', () async { final store = FakeStore(available: false); final billing = await _service(store); expect(billing.available, isFalse); expect(billing.canPurchase, isFalse); expect(await billing.buy(ProductIds.removeAds), isFalse); expect(billing.error, isNotNull); billing.dispose(); await store.dispose(); }); test('store that throws while querying is handled', () async { final store = FakeStore(throwOnQuery: true); final billing = await _service(store); expect(billing.canPurchase, isFalse); expect(billing.error, 'Produkte konnten nicht geladen werden'); billing.dispose(); await store.dispose(); }); test('deletion request carries the device id and the contact address', () { final uri = deletionRequestUri('dev-abc123'); expect(uri.scheme, 'mailto'); expect(uri.path, kContactEmail); expect(Uri.decodeComponent(uri.query), contains('dev-abc123')); expect(Uri.decodeComponent(uri.query), contains('Datenlöschung')); }); }