|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:buzz/features/profile/presence_cache_provider.dart'; |
| 4 | +import 'package:buzz/shared/relay/relay.dart'; |
| 5 | +import 'package:fake_async/fake_async.dart'; |
| 6 | +import 'package:flutter/widgets.dart'; |
| 7 | +import 'package:flutter_test/flutter_test.dart'; |
| 8 | +import 'package:hooks_riverpod/hooks_riverpod.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + void scenario(void Function(FakeAsync, ProviderContainer, _Relay) run) { |
| 12 | + fakeAsync((time) { |
| 13 | + final relay = _Relay(); |
| 14 | + final container = ProviderContainer( |
| 15 | + overrides: [ |
| 16 | + relaySessionProvider.overrideWith(() => relay), |
| 17 | + appLifecycleProvider.overrideWith(_Lifecycle.new), |
| 18 | + ], |
| 19 | + ); |
| 20 | + container.read(presenceCacheProvider.notifier).track(['alice']); |
| 21 | + time.elapse(Duration.zero); |
| 22 | + try { |
| 23 | + run(time, container, relay); |
| 24 | + } finally { |
| 25 | + container.dispose(); |
| 26 | + time.flushMicrotasks(); |
| 27 | + } |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + test('delayed pre-snapshot live event cannot regress a settled snapshot', () { |
| 32 | + scenario((time, container, relay) { |
| 33 | + relay.results.removeAt(0).complete([ |
| 34 | + _event('relay', 'online', subject: 'alice', timestamp: 20), |
| 35 | + ]); |
| 36 | + time.flushMicrotasks(); |
| 37 | + expect(container.read(presenceCacheProvider)['alice'], 'online'); |
| 38 | + |
| 39 | + // Delivered after the snapshot settled but signed ten seconds before |
| 40 | + // the relay observed it: the observation already folded it in. |
| 41 | + relay.emit(_event('alice', 'offline', timestamp: 10)); |
| 42 | + expect(container.read(presenceCacheProvider)['alice'], 'online'); |
| 43 | + |
| 44 | + time.elapse(Duration.zero); |
| 45 | + expect(relay.queries, hasLength(2)); |
| 46 | + expect(relay.queries.last.authors, ['alice']); |
| 47 | + relay.results.removeAt(0).complete([ |
| 48 | + _event('relay', 'online', subject: 'alice', timestamp: 22), |
| 49 | + ]); |
| 50 | + time.flushMicrotasks(); |
| 51 | + expect(container.read(presenceCacheProvider)['alice'], 'online'); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + test('live event from a lagging signer clock is confirmed, not dropped', () { |
| 56 | + scenario((time, container, relay) { |
| 57 | + relay.results.removeAt(0).complete([ |
| 58 | + _event('relay', 'online', subject: 'alice', timestamp: 200), |
| 59 | + ]); |
| 60 | + time.flushMicrotasks(); |
| 61 | + expect(container.read(presenceCacheProvider)['alice'], 'online'); |
| 62 | + |
| 63 | + // Signed behind the relay clock but ingested before fan-out, so the |
| 64 | + // fresh observation must carry its effect instead of trusting either |
| 65 | + // clock against the other. |
| 66 | + relay.emit(_event('alice', 'offline', timestamp: 190)); |
| 67 | + expect(container.read(presenceCacheProvider)['alice'], 'online'); |
| 68 | + |
| 69 | + time.elapse(Duration.zero); |
| 70 | + expect(relay.queries, hasLength(2)); |
| 71 | + relay.results.removeAt(0).complete([ |
| 72 | + _event('relay', 'offline', subject: 'alice', timestamp: 201), |
| 73 | + ]); |
| 74 | + time.flushMicrotasks(); |
| 75 | + expect(container.read(presenceCacheProvider)['alice'], 'offline'); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + test('equal-second live transition applies without a confirmation query', () { |
| 80 | + scenario((time, container, relay) { |
| 81 | + relay.results.removeAt(0).complete([ |
| 82 | + _event('relay', 'online', subject: 'alice', timestamp: 20), |
| 83 | + ]); |
| 84 | + time.flushMicrotasks(); |
| 85 | + // A transition in the observation second is at-or-after it: settle |
| 86 | + // last-arrival-wins like an equal-second live heartbeat. |
| 87 | + relay.emit(_event('alice', 'offline', timestamp: 20)); |
| 88 | + expect(container.read(presenceCacheProvider)['alice'], 'offline'); |
| 89 | + time.elapse(Duration.zero); |
| 90 | + expect(relay.queries, hasLength(1)); |
| 91 | + expect(container.read(presenceCacheProvider)['alice'], 'offline'); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + test('redelivered ambiguous event arms one confirmation', () { |
| 96 | + scenario((time, container, relay) { |
| 97 | + relay.results.removeAt(0).complete([ |
| 98 | + _event('relay', 'online', subject: 'alice', timestamp: 20), |
| 99 | + ]); |
| 100 | + time.flushMicrotasks(); |
| 101 | + final delayed = _event('alice', 'offline', timestamp: 10); |
| 102 | + relay.emit(delayed); |
| 103 | + time.elapse(Duration.zero); |
| 104 | + relay.results.removeAt(0).complete([ |
| 105 | + _event('relay', 'online', subject: 'alice', timestamp: 22), |
| 106 | + ]); |
| 107 | + time.flushMicrotasks(); |
| 108 | + expect(container.read(presenceCacheProvider)['alice'], 'online'); |
| 109 | + |
| 110 | + // The same frame is redelivered after the confirmation settled; a |
| 111 | + // bounded id memory must keep it from re-arming a refresh. |
| 112 | + relay.emit(delayed); |
| 113 | + time.elapse(Duration.zero); |
| 114 | + expect(relay.queries, hasLength(2)); |
| 115 | + expect(container.read(presenceCacheProvider)['alice'], 'online'); |
| 116 | + }); |
| 117 | + }); |
| 118 | +} |
| 119 | + |
| 120 | +NostrEvent _event( |
| 121 | + String author, |
| 122 | + String status, { |
| 123 | + String? subject, |
| 124 | + int timestamp = 10, |
| 125 | +}) => NostrEvent( |
| 126 | + id: '$author-$status-$timestamp', |
| 127 | + pubkey: author, |
| 128 | + createdAt: timestamp, |
| 129 | + kind: EventKind.presenceUpdate, |
| 130 | + tags: [ |
| 131 | + if (subject != null) ['p', subject], |
| 132 | + ], |
| 133 | + content: status, |
| 134 | + sig: 'sig', |
| 135 | +); |
| 136 | + |
| 137 | +class _Lifecycle extends AppLifecycleNotifier { |
| 138 | + @override |
| 139 | + AppLifecycleState build() => AppLifecycleState.resumed; |
| 140 | +} |
| 141 | + |
| 142 | +class _Relay extends RelaySessionNotifier { |
| 143 | + final queries = <NostrFilter>[]; |
| 144 | + final results = <Completer<List<NostrEvent>>>[]; |
| 145 | + late void Function(NostrEvent) emit; |
| 146 | + Completer<void>? ready; |
| 147 | + bool failSubscribe = false; |
| 148 | + int subscriptions = 0; |
| 149 | + int unsubscribes = 0; |
| 150 | + |
| 151 | + @override |
| 152 | + SessionState build() => const SessionState(status: SessionStatus.connected); |
| 153 | + |
| 154 | + @override |
| 155 | + Future<void Function()> subscribeWithStatus( |
| 156 | + NostrFilter filter, |
| 157 | + void Function(NostrEvent) onEvent, { |
| 158 | + void Function(String)? onClosed, |
| 159 | + required void Function(RelaySubscriptionStatus) onStatusChanged, |
| 160 | + }) async { |
| 161 | + subscriptions++; |
| 162 | + if (failSubscribe) throw StateError('unavailable'); |
| 163 | + emit = onEvent; |
| 164 | + onStatusChanged(RelaySubscriptionStatus.ready); |
| 165 | + if (ready != null) await ready!.future; |
| 166 | + return () => unsubscribes++; |
| 167 | + } |
| 168 | + |
| 169 | + @override |
| 170 | + Future<List<NostrEvent>> queryRelay( |
| 171 | + List<NostrFilter> filters, { |
| 172 | + Duration timeout = const Duration(seconds: 8), |
| 173 | + }) { |
| 174 | + queries.addAll(filters); |
| 175 | + final result = Completer<List<NostrEvent>>(); |
| 176 | + results.add(result); |
| 177 | + return result.future; |
| 178 | + } |
| 179 | +} |
0 commit comments