import 'dart:convert'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:marianum_mobile/push/push_tap_router.dart'; import 'package:marianum_mobile/push/push_target.dart'; void main() { group('resolvePushTarget', () { test('a parent letter push routes by its letter id', () { final target = resolvePushTarget({ 'source': 'connect', 'type': 'parent-letter', 'parentLetterId': 'l1', 'event': 'reply', 'title': 'Hr. Müller: Wandertag', }); expect( target, isA().having((t) => t.letterId, 'letterId', 'l1'), ); }); test('newsletter pushes route by newsletterId', () { expect( resolvePushTarget({'source': 'connect', 'newsletterId': 'n1'}), isA().having((t) => t.newsletterId, 'id', 'n1'), ); }); test('chat pushes accept every token key in use', () { for (final key in ['chatToken', 'token', 'roomToken']) { expect( resolvePushTarget({key: 'abc', 'nid': 5}), isA().having((t) => t.chatToken, 'chatToken', 'abc'), reason: key, ); } }); test('empty ids and unrelated data name no target', () { expect(resolvePushTarget({'parentLetterId': ''}), isNull); expect(resolvePushTarget({'type': 'widget-refresh'}), isNull); expect(resolvePushTarget({'chatToken': 7}), isNull); }); }); group('PushTapRouter.handleResponse', () { setUp(() => PushTapRouter.pendingTarget.value = null); NotificationResponse tap(String payload) => NotificationResponse( notificationResponseType: NotificationResponseType.selectedNotification, payload: payload, ); test('publishes the target of a tapped notification', () { PushTapRouter.handleResponse(tap(jsonEncode({'parentLetterId': 'l1'}))); expect(PushTapRouter.pendingTarget.value, isA()); }); test('ignores broken payloads', () { PushTapRouter.handleResponse(tap('not json')); expect(PushTapRouter.pendingTarget.value, isNull); }); }); }