mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +00:00
## Summary Turning push notifications off could stay stuck forever. If the relay kept rejecting the opt-out tombstone, `setPushNotificationsEnabled(false)` swallowed the rejection, and the push bootstrap retried the tombstone on every connect, including after restarts. Settings showed the switch off the whole time, while the relay still held the lease. [docs/mobile-push-suppression.md](https://github.com/block/buzz/blob/main/docs/mobile-push-suppression.md) (rule 29) asks for the opposite: a bounded pending state, then a clear warning that notifications may continue, with no automatic retry, and the relay's actual setting on the next sync. - **Bounded pending.** An opt-out ends about 10 seconds after the tap, across every step (capability lookup, connect, authenticate, publish). While it runs, the row says "Turning off…" and the switch is held, so Off, On, Off can't race the first attempt. - **Definite vs. maybe stored.** A one-shot publish now says whether the relay could have seen the event: a refusal, or a failure before the EVENT went out, is definite; a lost acknowledgment after it went out is not. - **Refused: back to the confirmed setting.** When the relay never stored the tombstone, the saved setting goes back to On (the confirmed lease) and Settings shows "Could not confirm that notifications were turned off. Notifications may continue." with a **Retry** below it. The warning lasts for this app run; after a restart, Settings just shows On. - **Maybe stored: kept as a guard, then read back.** When the acknowledgment was lost, the saved opt-out stays as a guard (no lease or enrollment is published, and the community stays in the iOS notification snapshot), while Settings shows the confirmed On with the same warning. On the next sync, the app reads its own lease from the relay: an inactive lease or none confirms Off; an active one turns the switch back On and keeps the warning. A read that fails changes nothing and is tried again on the next connect. The read asks the relay for strong consistency, so a lagging replica can't answer with the lease the tombstone replaced. It fails if the active community changes while it waits, and it settles only the opt-out it was started for, so a Retry made during the read stays pending. - **No flash on success.** Settings reads the community from the community list, which changes in the same step as each opt-out result. The active-community projection trails it by a keychain read, which could show an accepted opt-out as failed for a frame. - **Explicit retry only.** Retry, or turning the switch off again, makes one attempt at a new generation. The bootstrap's automatic tombstone retry is removed. The warning is announced to screen readers when it appears, and Retry has its own label. This applies to every platform: the opt-out path is shared. ### Related issue None found. ### Testing - The Settings recovery journey covers a refusal restoring On with the warning, a restart that drops the warning and makes no attempt, an attempt that never answers (held switch, then the deadline, kept as a guard), a restart that keeps that warning, and a Retry that succeeds. It runs with loading, absent and failed capability, and on a small phone at text scale 2. - The toggle journey, with the real push bootstrap, covers a refusal (a reconnect neither retries nor reads), an unanswered attempt and a lease that's still active: the read on reconnect, a failed read that changes nothing, then Off or On from what the relay holds. - One-shot publish rows: a relay that stalls at authentication fails as never sent; one that stalls at OK, or closes after the EVENT, fails as maybe stored, all by the deadline. - Lease reader rows: the exact strong-read filter, an empty answer, and a community switch during the descriptor fetch or the query. A provider row holds a read while a Retry fails unanswered: the old read leaves the new opt-out pending. - A widget test with slow secure storage checks every frame of an accepted opt-out for the warning. - A relay database test reads back the author's inactive replacement lease with the filter the app uses, and the lease decryption is checked against the member's own and forged events. ### Screenshots After the relay refuses the opt-out (no relay push capability, so the toggle is reachable only because push is on). | Before | After | | --- | --- | | <img src="https://raw.githubusercontent.com/block/buzz/d2e3c753b3068a68728199096bf30e45e8948af7/pr-9854--before.png" width="300" alt="Before: switch shows off while notifications may continue"> | <img src="https://raw.githubusercontent.com/block/buzz/ebe70a39406cf2c2104788c4cbc2f3302ba5dbbb/pr-9854--after.png" width="300" alt="After: switch back on, with the warning and Retry below it"> | At text scale 2 on a small phone, Retry sits below the warning: <img src="https://raw.githubusercontent.com/block/buzz/ebe70a39406cf2c2104788c4cbc2f3302ba5dbbb/pr-9854--after-large-text.png" width="250" alt="Large text: warning readable, Retry below it"> While an opt-out attempt is in flight (new), the switch is held: <img src="https://raw.githubusercontent.com/block/buzz/ebe70a39406cf2c2104788c4cbc2f3302ba5dbbb/pr-9854--turning-off.png" width="300" alt="Notifications row with the subtitle Turning off and the switch held"> --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
413 lines
12 KiB
Dart
413 lines
12 KiB
Dart
import 'dart:math';
|
|
|
|
import 'package:buzz/shared/push/dev_push_lease.dart';
|
|
import 'package:buzz/shared/community/community.dart';
|
|
import 'package:buzz/shared/push/push_bootstrap.dart';
|
|
import 'package:buzz/shared/push/push_subscription.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
void main() {
|
|
testWidgets('first opt-in starts registration without switching community', (
|
|
tester,
|
|
) async {
|
|
var registrations = 0;
|
|
Widget bootstrap(bool enabled) => BuzzPushRegistrationBootstrap(
|
|
shouldRegister: enabled,
|
|
attemptKey: 'same-community|same-relay',
|
|
startRegistration: () async {
|
|
registrations += 1;
|
|
},
|
|
child: const SizedBox(),
|
|
);
|
|
await tester.pumpWidget(bootstrap(false));
|
|
expect(registrations, 0);
|
|
await tester.pumpWidget(bootstrap(true));
|
|
expect(registrations, 1);
|
|
await tester.pumpWidget(bootstrap(true));
|
|
expect(registrations, 1);
|
|
});
|
|
|
|
testWidgets('new community starts registration once capability arrives', (
|
|
tester,
|
|
) async {
|
|
final community = Community.create(
|
|
name: 'Team',
|
|
relayUrl: 'wss://relay.example',
|
|
);
|
|
var registrations = 0;
|
|
Widget bootstrap(BuzzPushLeaseDescriptor? descriptor) =>
|
|
BuzzPushRegistrationBootstrap(
|
|
shouldRegister: buzzPushLifecycleEnabled(
|
|
community: community,
|
|
descriptor: descriptor,
|
|
),
|
|
attemptKey: '${community.id}|${community.relayUrl}',
|
|
startRegistration: () async => registrations += 1,
|
|
child: const SizedBox(),
|
|
);
|
|
await tester.pumpWidget(bootstrap(null));
|
|
expect(registrations, 0);
|
|
final descriptor = _descriptor(keyId: 'relay-v1', pubkey: _hex('b'));
|
|
await tester.pumpWidget(bootstrap(descriptor));
|
|
expect(registrations, 1);
|
|
await tester.pumpWidget(bootstrap(descriptor));
|
|
expect(registrations, 1);
|
|
});
|
|
|
|
test(
|
|
'superseded lease acceptance is a retryable publication failure',
|
|
() async {
|
|
await expectLater(
|
|
publishBuzzPushLeaseRecoverably(
|
|
reserveGeneration: () async => 1,
|
|
publish: (_) async {},
|
|
markAccepted: (_) async => false,
|
|
),
|
|
throwsStateError,
|
|
);
|
|
},
|
|
);
|
|
|
|
test('failed bootstrap attempt becomes retryable after the delay', () async {
|
|
final gate = BuzzPushAttemptGate(retryDelay: Duration.zero);
|
|
addTearDown(gate.dispose);
|
|
var retries = 0;
|
|
|
|
expect(gate.tryBegin('attempt'), isTrue);
|
|
gate.failed('attempt', retry: () => retries += 1);
|
|
await Future<void>.delayed(Duration.zero);
|
|
|
|
expect(retries, 1);
|
|
expect(gate.tryBegin('attempt'), isTrue);
|
|
});
|
|
|
|
testWidgets('a failed attempt stays blocked until the retry delay', (
|
|
tester,
|
|
) async {
|
|
final gate = BuzzPushAttemptGate(jitter: () => 1);
|
|
addTearDown(gate.dispose);
|
|
var retries = 0;
|
|
expect(gate.tryBegin('attempt'), isTrue);
|
|
gate.failed('attempt', retry: () => retries++);
|
|
expect(gate.tryBegin('attempt'), isFalse);
|
|
await tester.pump(const Duration(seconds: 4));
|
|
expect(gate.tryBegin('attempt'), isFalse);
|
|
expect(retries, 0);
|
|
await tester.pump(const Duration(seconds: 1));
|
|
expect(retries, 1);
|
|
expect(gate.tryBegin('attempt'), isTrue);
|
|
});
|
|
|
|
/// Fails [attempt] once more and returns how long its retry took to fire.
|
|
Future<Duration> failAndTimeRetry(
|
|
WidgetTester tester,
|
|
BuzzPushAttemptGate gate,
|
|
String attempt,
|
|
) async {
|
|
expect(gate.tryBegin(attempt), isTrue);
|
|
var fired = false;
|
|
gate.failed(attempt, retry: () => fired = true);
|
|
var waited = Duration.zero;
|
|
while (!fired) {
|
|
await tester.pump(const Duration(milliseconds: 500));
|
|
waited += const Duration(milliseconds: 500);
|
|
}
|
|
return waited;
|
|
}
|
|
|
|
testWidgets('repeated failures back off exponentially up to a cap', (
|
|
tester,
|
|
) async {
|
|
for (final jitter in [0.0, 1.0]) {
|
|
final gate = BuzzPushAttemptGate(jitter: () => jitter);
|
|
final delays = [
|
|
for (var i = 0; i < 12; i++)
|
|
await failAndTimeRetry(tester, gate, 'attempt'),
|
|
];
|
|
gate.dispose();
|
|
|
|
// Each delay lies between half and all of 5 s doubled per prior
|
|
// failure, capped at 15 minutes.
|
|
for (final (index, delay) in delays.indexed) {
|
|
final ceiling = min(5000 * (1 << index), 15 * 60 * 1000);
|
|
expect(
|
|
delay.inMilliseconds,
|
|
inInclusiveRange(ceiling ~/ 2, ceiling),
|
|
reason: 'failure $index with jitter $jitter',
|
|
);
|
|
}
|
|
expect(delays.last, lessThanOrEqualTo(const Duration(minutes: 15)));
|
|
expect(delays.last, greaterThan(const Duration(minutes: 7)));
|
|
}
|
|
});
|
|
|
|
testWidgets('a success ends the failure streak', (tester) async {
|
|
final gate = BuzzPushAttemptGate(jitter: () => 1);
|
|
addTearDown(gate.dispose);
|
|
for (var i = 0; i < 4; i++) {
|
|
await failAndTimeRetry(tester, gate, 'attempt');
|
|
}
|
|
|
|
expect(gate.tryBegin('attempt'), isTrue);
|
|
gate.retryAfter('attempt', delay: Duration.zero, retry: () {});
|
|
await tester.pump(const Duration(milliseconds: 1));
|
|
|
|
expect(
|
|
await failAndTimeRetry(tester, gate, 'attempt'),
|
|
const Duration(seconds: 5),
|
|
);
|
|
});
|
|
|
|
testWidgets('a registration success ends its failure streak', (tester) async {
|
|
var failing = true;
|
|
var registrations = 0;
|
|
Widget bootstrap(String key) => BuzzPushRegistrationBootstrap(
|
|
shouldRegister: true,
|
|
attemptKey: key,
|
|
startRegistration: () async {
|
|
registrations += 1;
|
|
if (failing) throw StateError('refused');
|
|
},
|
|
child: const SizedBox(),
|
|
);
|
|
|
|
// Community A fails long enough to back off past a minute.
|
|
await tester.pumpWidget(bootstrap('a'));
|
|
for (var i = 0; i < 5; i++) {
|
|
await tester.pump(const Duration(minutes: 3));
|
|
}
|
|
expect(registrations, 6);
|
|
failing = false;
|
|
await tester.pump(const Duration(minutes: 15));
|
|
expect(registrations, 7);
|
|
|
|
// Switch to B and back. A's next failure retries within the first delay.
|
|
await tester.pumpWidget(bootstrap('b'));
|
|
expect(registrations, 8);
|
|
failing = true;
|
|
await tester.pumpWidget(bootstrap('a'));
|
|
expect(registrations, 9);
|
|
await tester.pump(const Duration(seconds: 5));
|
|
await tester.pump();
|
|
expect(registrations, 10);
|
|
|
|
await tester.pumpWidget(const SizedBox());
|
|
});
|
|
|
|
testWidgets('a different attempt starts its own backoff', (tester) async {
|
|
final gate = BuzzPushAttemptGate(jitter: () => 1);
|
|
addTearDown(gate.dispose);
|
|
for (var i = 0; i < 4; i++) {
|
|
await failAndTimeRetry(tester, gate, 'old');
|
|
}
|
|
|
|
expect(
|
|
await failAndTimeRetry(tester, gate, 'new'),
|
|
const Duration(seconds: 5),
|
|
);
|
|
});
|
|
|
|
test('a new attempt cancels an obsolete scheduled retry', () async {
|
|
final gate = BuzzPushAttemptGate(retryDelay: Duration.zero);
|
|
addTearDown(gate.dispose);
|
|
var retries = 0;
|
|
|
|
expect(gate.tryBegin('old'), isTrue);
|
|
gate.failed('old', retry: () => retries += 1);
|
|
expect(gate.tryBegin('new'), isTrue);
|
|
await Future<void>.delayed(Duration.zero);
|
|
|
|
expect(retries, 0);
|
|
expect(gate.tryBegin('new'), isFalse);
|
|
});
|
|
|
|
test('successful bootstrap becomes retryable at renewal time', () async {
|
|
final gate = BuzzPushAttemptGate(retryDelay: Duration.zero);
|
|
addTearDown(gate.dispose);
|
|
var retries = 0;
|
|
|
|
expect(gate.tryBegin('attempt'), isTrue);
|
|
gate.retryAfter('attempt', delay: Duration.zero, retry: () => retries += 1);
|
|
await Future<void>.delayed(Duration.zero);
|
|
|
|
expect(retries, 1);
|
|
expect(gate.tryBegin('attempt'), isTrue);
|
|
});
|
|
|
|
test('completed bootstrap attempt can run again for later work', () {
|
|
final gate = BuzzPushAttemptGate();
|
|
addTearDown(gate.dispose);
|
|
|
|
expect(gate.tryBegin('attempt'), isTrue);
|
|
gate.complete('attempt');
|
|
expect(gate.tryBegin('attempt'), isTrue);
|
|
});
|
|
|
|
test('publication attempt changes when the relay executor rotates', () {
|
|
final subscription = BuzzPushSubscription(
|
|
filter: BuzzPushFilter(kinds: const [9], pTags: [_hex('a')]),
|
|
notificationClass: 'default',
|
|
);
|
|
final original = buzzPushPublicationAttemptKey(
|
|
communityId: 'community',
|
|
relayBaseUrl: 'https://relay.example',
|
|
token: 'token',
|
|
descriptor: _descriptor(keyId: 'relay-v1', pubkey: _hex('b')),
|
|
subscriptions: [subscription],
|
|
);
|
|
|
|
expect(
|
|
buzzPushPublicationAttemptKey(
|
|
communityId: 'community',
|
|
relayBaseUrl: 'https://relay.example',
|
|
token: 'token',
|
|
descriptor: _descriptor(keyId: 'relay-v2', pubkey: _hex('b')),
|
|
subscriptions: [subscription],
|
|
),
|
|
isNot(original),
|
|
);
|
|
expect(
|
|
buzzPushPublicationAttemptKey(
|
|
communityId: 'community',
|
|
relayBaseUrl: 'https://relay.example',
|
|
token: 'token',
|
|
descriptor: _descriptor(keyId: 'relay-v1', pubkey: _hex('c')),
|
|
subscriptions: [subscription],
|
|
),
|
|
isNot(original),
|
|
);
|
|
});
|
|
|
|
test('relay grammar upgrade changes publication attempt identity', () {
|
|
String key(String grammar) => buzzPushPublicationAttemptKey(
|
|
communityId: 'community',
|
|
relayBaseUrl: 'https://relay.example',
|
|
token: 'token',
|
|
descriptor: _descriptor(
|
|
keyId: 'relay-v1',
|
|
pubkey: _hex('b'),
|
|
hGrammar: grammar,
|
|
),
|
|
subscriptions: const [],
|
|
);
|
|
expect(key('uuid-lowercase'), isNot(key('uuid-v4-lowercase')));
|
|
});
|
|
|
|
test('default push requires relay capability and respects opt-out', () {
|
|
final enabled = Community.create(
|
|
name: 'Team',
|
|
relayUrl: 'wss://relay.example',
|
|
);
|
|
final disabled = enabled.copyWith(pushNotificationsEnabled: false);
|
|
|
|
expect(
|
|
buzzPushLifecycleEnabled(
|
|
community: disabled,
|
|
descriptor: _descriptor(keyId: 'relay-v1', pubkey: _hex('b')),
|
|
),
|
|
isFalse,
|
|
);
|
|
expect(
|
|
buzzPushLifecycleEnabled(
|
|
community: enabled,
|
|
descriptor: _descriptor(keyId: 'relay-v1', pubkey: _hex('b')),
|
|
),
|
|
isTrue,
|
|
);
|
|
expect(
|
|
buzzPushLifecycleEnabled(community: enabled, descriptor: null),
|
|
isFalse,
|
|
);
|
|
});
|
|
|
|
test('pending opt-out tombstone keeps active push lifecycle disabled', () {
|
|
final subscription = BuzzPushSubscription(
|
|
filter: BuzzPushFilter(kinds: const [9], pTags: [_hex('a')]),
|
|
notificationClass: 'default',
|
|
);
|
|
final community =
|
|
Community.create(
|
|
name: 'Team',
|
|
relayUrl: 'wss://relay.example',
|
|
).copyWith(
|
|
pushNotificationsEnabled: false,
|
|
pushSubscriptionState:
|
|
BuzzPushLeaseSubscriptionState.desired(desired: [subscription])
|
|
.withAccepted(subscriptions: [subscription], generation: 3)
|
|
.withPendingTombstone(4),
|
|
);
|
|
|
|
expect(
|
|
buzzPushLifecycleEnabled(
|
|
community: community,
|
|
descriptor: _descriptor(keyId: 'relay-v1', pubkey: _hex('b')),
|
|
),
|
|
isFalse,
|
|
);
|
|
});
|
|
|
|
test(
|
|
'relay commit followed by local failure retries at a newer generation',
|
|
() async {
|
|
var durableCursor = 0;
|
|
var relayGeneration = 0;
|
|
var acceptedGeneration = 0;
|
|
var failLocalSave = true;
|
|
|
|
Future<int> reserve() async => ++durableCursor;
|
|
Future<void> publish(int generation) async {
|
|
expect(generation, greaterThan(relayGeneration));
|
|
relayGeneration = generation;
|
|
}
|
|
|
|
Future<bool> markAccepted(int generation) async {
|
|
if (failLocalSave) {
|
|
failLocalSave = false;
|
|
throw StateError('injected local persistence failure');
|
|
}
|
|
acceptedGeneration = generation;
|
|
return true;
|
|
}
|
|
|
|
await expectLater(
|
|
publishBuzzPushLeaseRecoverably(
|
|
reserveGeneration: reserve,
|
|
publish: publish,
|
|
markAccepted: markAccepted,
|
|
),
|
|
throwsStateError,
|
|
);
|
|
expect(relayGeneration, 1);
|
|
expect(acceptedGeneration, 0);
|
|
|
|
await publishBuzzPushLeaseRecoverably(
|
|
reserveGeneration: reserve,
|
|
publish: publish,
|
|
markAccepted: markAccepted,
|
|
);
|
|
expect(relayGeneration, 2);
|
|
expect(acceptedGeneration, 2);
|
|
},
|
|
);
|
|
}
|
|
|
|
BuzzPushLeaseDescriptor _descriptor({
|
|
required String keyId,
|
|
required String pubkey,
|
|
String hGrammar = 'uuid-v4-lowercase',
|
|
}) => BuzzPushLeaseDescriptor(
|
|
origin: 'wss://relay.example',
|
|
hGrammar: hGrammar,
|
|
executorKeyId: keyId,
|
|
executorPubkey: pubkey,
|
|
transport: 'apns',
|
|
maxLeaseTtlSeconds: 3600,
|
|
maxContentLength: 4096,
|
|
maxPlaintextLength: 4096,
|
|
maxEndpointLength: 2048,
|
|
maxStringLength: 512,
|
|
);
|
|
|
|
String _hex(String character) => List.filled(64, character).join();
|