Files
buzz/mobile/test/shared/push/push_bootstrap_test.dart
Tom Brow c09730e874 fix(mobile): keep notification toggle usable and match row height (#8223)
Turning Notifications off invalidated the relay capability provider
because it watched the entire saved community. If the redundant NIP-11
request stalled or failed, the switch became disabled and reopening
Settings did not recover it. Discovery now follows community identity
and connection status, so preference and lease bookkeeping retain the
validated capability while a real reconnect still refreshes it. Each
opt-in also starts a fresh active lease immediately instead of waiting
for the old renewal timer. Failed attempts retain their gate until the
retry delay expires, preventing rebuilds from bypassing backoff.

Notifications now has the same single-line height as Set status, with
the full switch hit area and one combined accessibility label. This
follows [#8201](https://github.com/block/buzz/pull/8201); no duplicate
open PR was found.

### Validation

Added iOS widget regressions mount the production settings page and push
bootstrap with production storage logic, signing, and lease publication.
They verify a higher-generation active replacement after off/on with
accepted, failed, and pending cleanup, plus closing/reopening Settings,
switch and row taps, 2× text, accessibility, and rediscovery on
reconnect. A gate test verifies that rebuilds cannot bypass failure
backoff. Both UI failures and the missing active replacement were
reproduced before their fixes.

Mobile dependency installation, formatting/static checks, 3,234
configured tests, and three unconfigured-build tests passed. Captures
use the real Flutter settings widget at 375pt width, with synthetic data
and mocked native/network boundaries. Native navigation is outside the
capture. No device installation or deployment was performed.

After merge, validate the candidate on iOS: compare Notifications with
Set status, toggle off then immediately on, close/reopen Settings while
off and turn on, and repeat after reconnect. Confirm delivery resumes
after opt-in.

### Layout

Flutter widget captures with synthetic data; native navigation is
excluded.

| Before | After |
| --- | --- |
| ![Notifications row
before](https://raw.githubusercontent.com/block/buzz/b2cf4b8b1f0abaa95893af0e5a4e194c6af41333/pr-8223--01-before.png)
| ![Notifications row
after](https://raw.githubusercontent.com/block/buzz/b2cf4b8b1f0abaa95893af0e5a4e194c6af41333/pr-8223--02-after.png)
|

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
2026-10-12 09:39:53 -07:00

258 lines
7.6 KiB
Dart

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);
});
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();
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);
});
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 capability alone does not activate push without opt-in', () {
final disabled = Community.create(
name: 'Team',
relayUrl: 'wss://relay.example',
);
final enabled = disabled.copyWith(pushNotificationsEnabled: true);
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,
}) => BuzzPushLeaseDescriptor(
origin: 'wss://relay.example',
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();