|
| 1 | +import 'package:buzz/shared/community/community.dart'; |
| 2 | +import 'package:buzz/shared/community/community_provider.dart'; |
| 3 | +import 'package:buzz/shared/community/community_storage.dart'; |
| 4 | +import 'package:buzz/shared/relay/relay_provider.dart'; |
| 5 | +import 'package:flutter_test/flutter_test.dart'; |
| 6 | +import 'package:hooks_riverpod/hooks_riverpod.dart'; |
| 7 | + |
| 8 | +import 'community_storage_test.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + test( |
| 12 | + 'reserving a push lease does not notify relay config listeners', |
| 13 | + () async { |
| 14 | + final storage = CommunityStorage(secure: FakeSecureStorage()); |
| 15 | + final community = Community.create( |
| 16 | + name: 'Tom Town', |
| 17 | + relayUrl: 'https://relay.example.com', |
| 18 | + ).copyWith(pushNotificationsEnabled: true); |
| 19 | + await storage.save(community); |
| 20 | + await storage.saveActiveId(community.id); |
| 21 | + final container = ProviderContainer( |
| 22 | + overrides: [ |
| 23 | + communityStorageProvider.overrideWithValue(storage), |
| 24 | + communitySnapshotWriterProvider.overrideWithValue((_) async {}), |
| 25 | + ], |
| 26 | + ); |
| 27 | + addTearDown(container.dispose); |
| 28 | + await container.read(activeCommunityProvider.future); |
| 29 | + final changes = <RelayConfig>[]; |
| 30 | + container.listen(relayConfigProvider, (_, next) => changes.add(next)); |
| 31 | + await container |
| 32 | + .read(communityListProvider.notifier) |
| 33 | + .reservePushLeaseGeneration(community.id); |
| 34 | + await container.read(activeCommunityProvider.future); |
| 35 | + await container.pump(); |
| 36 | + expect( |
| 37 | + changes, |
| 38 | + isEmpty, |
| 39 | + reason: 'Push metadata must not reset the connected relay session', |
| 40 | + ); |
| 41 | + }, |
| 42 | + ); |
| 43 | + for (final change in ['community', 'relay', 'identity']) { |
| 44 | + test('$change change notifies relay config listeners', () async { |
| 45 | + final storage = CommunityStorage(secure: FakeSecureStorage()); |
| 46 | + final original = Community.create( |
| 47 | + name: 'Original', |
| 48 | + relayUrl: 'https://relay.example.com', |
| 49 | + nsec: 'first', |
| 50 | + ); |
| 51 | + await storage.save(original); |
| 52 | + await storage.saveActiveId(original.id); |
| 53 | + final container = ProviderContainer( |
| 54 | + overrides: [ |
| 55 | + communityStorageProvider.overrideWithValue(storage), |
| 56 | + communitySnapshotWriterProvider.overrideWithValue((_) async {}), |
| 57 | + ], |
| 58 | + ); |
| 59 | + addTearDown(container.dispose); |
| 60 | + await container.read(activeCommunityProvider.future); |
| 61 | + final changes = <RelayConfig>[]; |
| 62 | + container.listen(relayConfigProvider, (_, next) => changes.add(next)); |
| 63 | + final updated = switch (change) { |
| 64 | + 'community' => Community.create( |
| 65 | + name: 'Other', |
| 66 | + relayUrl: original.relayUrl, |
| 67 | + nsec: original.nsec, |
| 68 | + ), |
| 69 | + 'relay' => original.copyWith(relayUrl: 'https://other.example.com'), |
| 70 | + _ => original.copyWith(nsec: 'second'), |
| 71 | + }; |
| 72 | + await storage.save(updated); |
| 73 | + await storage.saveActiveId(updated.id); |
| 74 | + container.invalidate(communityListProvider); |
| 75 | + await container.read(activeCommunityProvider.future); |
| 76 | + await container.pump(); |
| 77 | + expect(changes, hasLength(1)); |
| 78 | + expect(changes.single.baseUrl, updated.relayUrl); |
| 79 | + expect(changes.single.nsec, updated.nsec); |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
0 commit comments