Files
buzz/mobile/test/shared/push/push_lease_revocation_outbox_test.dart
Tom Brow 868e2f8f32 refactor(push): one app profile per gateway (#8206)
Buzz push currently selects an application profile, such as `dogfood`,
during client registration. We want each gateway to serve one Apple app
configured by the operator instead. This removes profile selection from
the client, relay, and gateway, while allowing internal and public
gateways to use their own Apple identities.

This PR implements that model in the Buzz source and upstream deployment
chart. Discovery, enrollment, grants, and leases no longer carry an
application-profile selector. Each gateway has one configured Apple
application and APNs transport.

The protocol changes are incompatible with the old registration format,
so relays advertise `buzz-push-v1` instead of `nip-pl`. Old clients do
not discover the new push capability. Updated clients use separate
Keychain records and register again. The [push
notes](https://github.com/block/buzz/blob/push-capability-v1/docs/buzz-push.md)
describe the experimental contract.

Landing this PR is the first step. Publish the matching gateway image
and upstream chart, then land the [shared chart
update](https://github.com/squareup/cash-helm-charts/pull/5740) and
[gateway deployment
update](https://github.com/squareup/builderbot-platform-core-infrastructure/pull/1085).
Before cutover, stop old gateway instances and check legacy registration
state. Migrations refuse to proceed if old gateway installations or
active relay leases remain; they do not delete that state.
Physical-device verification remains necessary before production
activation.

### Validation
Gateway and focused Flutter push tests pass, as do the Swift package,
simulator build, and native notification/enrichment tests. A local relay
advertises `buzz-push-v1` without `nip-pl` or `app_profiles`. The
PostgreSQL suite passed 868 tests; its two migration-test failures were
corrected and pass a focused rerun together with the gateway cutover
guard. Chart rendering and release-contract checks pass.

Desktop JavaScript tests, desktop and web builds, native desktop
compilation, mobile analysis/tests, and desktop native tests pass. The
full Rust unit lane and prescribed `just test` command pass, including
workspace integration tests against task-isolated local services. The
production overlay now requires an explicitly supplied APNs topic, with
omission and rendered-value regression coverage. The corrected Swift
checksum assertion passes the complete package suite: 78 XCTest tests
and 42 Swift Testing tests. No physical-device enrollment or production
cutover is claimed.

Local merge validation: the embedded migration test and formatting pass
after reserving profile migration 0060 behind rollback migration 0059.
The local pre-push Rust lane failed ACP timing/steering tests
(`idle_resets_on_stdout_activity`,
`keepalive_resets_idle_past_deadline`, and the failed/missing-outcome
steering tests). ACP and its dependency manifests are unchanged by this
branch; all four pass in an isolated single-thread rerun. The documented
local-check exception skips only the Rust pre-push lane for publication;
required CI remains mandatory. Mobile and desktop pre-push lanes passed.

Combined migration validation: after rollback 0059 landed, the main
merge preserves profile migration 0060 and both embedded migration
assertions. The focused migration test and formatting pass. The next
local push again failed only the unchanged ACP idle/keepalive timing
tests (1053 passed, 2 failed, 3 ignored); other lanes passed.
Publication uses the same documented local Rust-lane exception; required
CI remains mandatory.

Follow-up review fix: removed the legacy profile column from the
copied-lease fixture. All four event-follow-up contract tests pass on
isolated PostgreSQL databases, covering desired and migrated schemas in
dual and app-only modes. Formatting passes. Publication retains the
prior passing local-lane evidence and uses the authorized pre-push
exception for the known unrelated ACP timing failures; required CI
remains mandatory.

---------

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

331 lines
9.3 KiB
Dart

import 'dart:async';
import 'package:buzz/shared/community/community.dart';
import 'package:buzz/shared/push/push_bridge.dart';
import 'package:buzz/shared/push/push_lease_revocation_outbox.dart';
import 'package:buzz/shared/push/push_subscription.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:nostr/nostr.dart' as nostr;
import '../community/community_storage_test.dart';
void main() {
late FakeSecureStorage secure;
late BuzzPushLeaseRevocationStorage storage;
late _Clock clock;
late _WakeScheduler scheduler;
late nostr.Keys keys;
setUp(() {
secure = FakeSecureStorage();
storage = BuzzPushLeaseRevocationStorage(secure: secure);
clock = _Clock(1_000_000);
scheduler = _WakeScheduler();
keys = nostr.Keys.generate();
});
test('community removal journals one precise lease address', () async {
final outbox = _outbox(
storage: storage,
clock: clock,
scheduler: scheduler,
publisher: (_) async {},
);
final community =
Community.create(
name: 'Test',
relayUrl: 'wss://relay.example',
nsec: keys.nsec,
).copyWith(
pubkey: keys.public,
pushNotificationsEnabled: true,
pushSubscriptionState: const BuzzPushLeaseSubscriptionState.desired()
.withReservedGeneration(4),
);
final grant = _grant(expiresAt: clock.seconds + 3600);
await outbox.enqueueCommunity(community, readGrants: () async => [grant]);
await outbox.enqueueCommunity(community, readGrants: () async => [grant]);
final records = await storage.loadAll();
expect(records, hasLength(1));
expect(
records.single.leaseAddress,
'${keys.public}|wss://relay.example|${community.pushLeaseInstallationId}',
);
expect(records.single.generation, 6);
expect(records.single.expiresAt, grant.expiresAt);
expect(records.single.relayUrl, 'https://relay.example/');
});
test('legacy community revokes its grant-addressed lease', () async {
final outbox = _outbox(
storage: storage,
clock: clock,
scheduler: scheduler,
publisher: (_) async {},
);
final community = Community(
id: 'legacy',
name: 'Legacy',
relayUrl: 'wss://relay.example',
pubkey: keys.public,
nsec: keys.nsec,
pushNotificationsEnabled: true,
pushSubscriptionState: const BuzzPushLeaseSubscriptionState.desired()
.withReservedGeneration(4),
addedAt: DateTime.fromMillisecondsSinceEpoch(0),
);
final grant = _grant(expiresAt: clock.seconds + 3600);
await outbox.enqueueCommunity(community, readGrants: () async => [grant]);
expect(
(await storage.loadAll()).single.leaseAddress,
'${keys.public}|wss://relay.example|${grant.installationId}',
);
});
test('concurrent reconnect and resume triggers share one attempt', () async {
final started = Completer<void>();
final release = Completer<void>();
var calls = 0;
final outbox = _outbox(
storage: storage,
clock: clock,
scheduler: scheduler,
publisher: (_) async {
calls += 1;
if (!started.isCompleted) started.complete();
await release.future;
},
);
await outbox.enqueue(_record(keys: keys, clock: clock));
final startup = outbox.start();
await started.future;
final reconnect = outbox.trigger();
final resume = outbox.trigger();
expect(calls, 1);
expect(identical(reconnect, resume), isTrue);
release.complete();
await Future.wait([startup, reconnect, resume]);
expect(calls, 1);
expect(await storage.loadAll(), isEmpty);
});
test('failure reserves durable backoff and restart waits for it', () async {
var failedCalls = 0;
final first = _outbox(
storage: storage,
clock: clock,
scheduler: scheduler,
publisher: (_) async {
failedCalls += 1;
throw StateError('offline');
},
);
await first.enqueue(_record(keys: keys, clock: clock));
await first.trigger();
final pending = (await storage.loadAll()).single;
expect(failedCalls, 1);
expect(pending.attemptCount, 1);
expect(pending.generation, 8);
expect(pending.nextAttemptAt, clock.seconds + 15);
var restartedCalls = 0;
final restarted = _outbox(
storage: storage,
clock: clock,
scheduler: scheduler,
publisher: (_) async => restartedCalls += 1,
);
await restarted.start();
await restarted.trigger();
await restarted.trigger();
expect(restartedCalls, 0);
clock.seconds = pending.nextAttemptAt;
await restarted.trigger();
expect(restartedCalls, 1);
expect(await storage.loadAll(), isEmpty);
});
test('repeated failed restarts cannot create a retry storm', () async {
await storage.replaceAll([_record(keys: keys, clock: clock)]);
var calls = 0;
for (var restart = 0; restart < 4; restart += 1) {
final outbox = _outbox(
storage: storage,
clock: clock,
scheduler: scheduler,
publisher: (_) async {
calls += 1;
throw StateError('offline');
},
);
await outbox.start();
await Future.wait([outbox.trigger(), outbox.trigger(), outbox.trigger()]);
outbox.dispose();
}
expect(calls, 1);
final pending = (await storage.loadAll()).single;
expect(pending.attemptCount, 1);
expect(pending.nextAttemptAt, greaterThan(clock.seconds));
});
test('retry delay grows exponentially and remains bounded', () async {
await storage.replaceAll([
_record(
keys: keys,
clock: clock,
).copyWith(expiresAt: clock.seconds + 10_000_000),
]);
final outbox = _outbox(
storage: storage,
clock: clock,
scheduler: scheduler,
publisher: (_) async => throw StateError('offline'),
);
final delays = <int>[];
for (var attempt = 0; attempt < 14; attempt += 1) {
await outbox.trigger();
final pending = (await storage.loadAll()).single;
delays.add(pending.nextAttemptAt - clock.seconds);
clock.seconds = pending.nextAttemptAt;
}
expect(delays.take(4), [15, 30, 60, 120]);
expect(delays.every((delay) => delay <= 6 * 60 * 60), isTrue);
expect(delays.last, 3 * 60 * 60);
});
test('expired lease is erased without a relay attempt', () async {
final expired = _record(
keys: keys,
clock: clock,
).copyWith(expiresAt: clock.seconds);
await storage.replaceAll([expired]);
var calls = 0;
final outbox = _outbox(
storage: storage,
clock: clock,
scheduler: scheduler,
publisher: (_) async => calls += 1,
);
await outbox.start();
expect(calls, 0);
expect(await storage.loadAll(), isEmpty);
});
test('different records are globally serialized', () async {
final firstStarted = Completer<void>();
final releaseFirst = Completer<void>();
var inFlight = 0;
var maximumInFlight = 0;
var calls = 0;
final outbox = _outbox(
storage: storage,
clock: clock,
scheduler: scheduler,
publisher: (_) async {
calls += 1;
inFlight += 1;
maximumInFlight = inFlight > maximumInFlight
? inFlight
: maximumInFlight;
if (calls == 1) {
firstStarted.complete();
await releaseFirst.future;
}
inFlight -= 1;
},
);
await outbox.enqueue(_record(keys: keys, clock: clock));
final otherKeys = nostr.Keys.generate();
await outbox.enqueue(
_record(keys: otherKeys, clock: clock, installationId: '1' * 32),
);
final draining = outbox.trigger();
await firstStarted.future;
expect(calls, 1);
releaseFirst.complete();
await draining;
expect(calls, 2);
expect(maximumInFlight, 1);
expect(await storage.loadAll(), isEmpty);
});
}
BuzzPushLeaseRevocationOutbox _outbox({
required BuzzPushLeaseRevocationStorage storage,
required _Clock clock,
required _WakeScheduler scheduler,
required BuzzPushLeaseRevocationPublisher publisher,
}) => BuzzPushLeaseRevocationOutbox(
storage: storage,
publisher: publisher,
now: clock.call,
jitter: () => 0,
reportError: (_, _) {},
scheduleWake: scheduler.schedule,
);
BuzzPushLeaseRevocationRecord _record({
required nostr.Keys keys,
required _Clock clock,
String installationId = '00000000000000000000000000000000',
}) => BuzzPushLeaseRevocationRecord(
relayUrl: 'https://relay.example',
relayOrigin: 'wss://relay.example',
memberPubkey: keys.public,
nsec: keys.nsec,
installationId: installationId,
generation: 7,
expiresAt: clock.seconds + 3600,
attemptCount: 0,
nextAttemptAt: clock.seconds,
);
BuzzPushEndpointGrant _grant({required int expiresAt}) => BuzzPushEndpointGrant(
relayOrigin: 'wss://relay.example',
relayPubkey: 'a' * 64,
installationId: '0' * 32,
endpointGrant: 'opaque',
endpointHash: 'b' * 64,
endpointEpoch: 1,
generation: 1,
expiresAt: expiresAt,
);
class _Clock {
_Clock(this.seconds);
int seconds;
DateTime call() => DateTime.fromMillisecondsSinceEpoch(seconds * 1000);
}
class _WakeScheduler {
final List<_ScheduledWake> _wakes = [];
void Function() schedule(Duration delay, void Function() callback) {
final wake = _ScheduledWake();
_wakes.add(wake);
return () => wake.cancelled = true;
}
}
class _ScheduledWake {
bool cancelled = false;
}