mirror of
https://github.com/block/buzz.git
synced 2026-11-24 18:21:59 +00:00
Step one of [mobile diagnostic
reporting](https://github.com/block/buzz/blob/main/docs/mobile-diagnostic-reporting.md),
second piece: the explicit error reports the spec lists, through the
reporter added in [#9613 diagnostic reporting
module](https://github.com/block/buzz/pull/9613). A build without a
destination (every build from this repository alone) still reports
nothing.
### What's in it
Each report carries its `operation` tag, the error's type and stack
trace, and no error text (the filter replaces it).
- **Event subscriber (`eventSubscriber`):** a subscriber that throws
while the relay session delivers a buffered batch is reported, and the
rest of the batch is still delivered. In a build without a destination
the error still surfaces as uncaught, so it fails the test that hit it
and prints in development, as before. Before, one throw dropped the rest
of the batch, and those events never came back because their delivery
keys were already recorded.
- **Relay frames (`relayFrame`):** a frame the socket can't decode, or
whose body isn't a JSON array, is reported instead of silently skipped.
- **Relay queries (`relayQuery`):** an HTTP query body that can't be
decoded, including the read-state snapshot.
- **Invalid-request rejections (`relayRejection`):** an `OK false` or
`CLOSED` whose reason starts with `invalid:`, reported as a fixed
`RelayInvalidRequest` with no relay text, including an `OK` with no
pending publish (a typing indicator, or a reply after a timeout).
Refusals the app handles as expected outcomes (an archived channel, an
Unarchive of a channel that is no longer archived, an open channel name
that is taken, an agent whose `policy:` refuses being added, a sole
owner's Leave refused as the last owner, a guest's Add Members refused
because only members can add people, an event whose timestamp is outside
the relay's window, so a wrong device clock does not report every
publish) are left out. The long-lived relay session, the short-lived
sockets (hosted admission, the self-hosted probe, one-shot publishes
such as agent registration and push tombstones) and the account
assertion requests made for a community's key (relay access and push
stop assertions) report frames and rejections the same way, except while
reports name a member other than the key they sign with. A removed or
inactive community's tombstone, or the probe of a community being
joined, then reports nothing rather than naming the wrong member. With
no member set (first run, or after the last community is removed) it
reports under the install identifier.
- **Account responses (`accountResponse`):** a Builderlab account
response that isn't a JSON object, or a 2xx exchange or `/v1/auth/me`
response missing its credential, subject or expiry. Non-2xx statuses are
expected outcomes and aren't reported.
- **Push gateway responses (`pushGatewayResponse`):** a malformed push
capability descriptor from the relay. A relay that leaves
`supported_extensions` out or sends none has no push and isn't reported,
and a fetch that finishes after a community switch has begun isn't
reported either, since it would name the next member. Push gateway
replies aren't reported yet.
- **Stored data (`storedData`):** a saved community list that can't be
read back.
- **Member on switch:** switching communities now looks the new member
up in storage, not in the provider's list, which can still be the
previous one while it reloads after sign-in. It reads storage before
tearing anything down, so a failed read leaves the switch unchanged and
retryable.
- **Filter:** also drops a report's additional data (`extra`). Nothing
sets it today.
- The spec's status line now says what is implemented.
- Event delivery moves from `relay_session.dart` into a part file,
unchanged apart from the subscriber guard, so the session stays under
its size limit.
### Regressions
None expected: each change only adds a report where an error was already
caught or skipped. The one behavior change is that a throwing event
subscriber no longer costs the rest of its batch.
### Validation
Added tests for each report site (relay session, socket frames, one-shot
publishes, read-state snapshot, closed-reason policy, account client,
push descriptor and capability provider, community storage), for the
relay session (member changed mid-request), one-shot sockets and
assertion requests reporting nothing while another member is set and
reporting with none set, for the member on switch and a switch whose
read fails then retries, and for the filter dropping additional data and
leaving a message-less event without one.
Full-app run on an iPhone 17 Pro simulator at b4f05ad (debug build, no
reporting destination): signed-in launch and channel list, scrolling
history in several channels, a thread, sending a message and a reaction,
search, switching communities and back, background and foreground with
live updates afterwards, and kill and relaunch with saved communities
restored. No regressions and no exceptions from the changed code.
---------
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
422 lines
14 KiB
Dart
422 lines
14 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
import 'package:buzz/shared/auth/auth_provider.dart';
|
|
import 'package:buzz/shared/community/community.dart';
|
|
import 'package:buzz/shared/community/community_provider.dart';
|
|
import 'package:buzz/shared/community/community_storage.dart';
|
|
import 'package:buzz/shared/diagnostics/diagnostic_reporter.dart';
|
|
import 'package:buzz/shared/push/push_bridge.dart';
|
|
|
|
import '../community/community_storage_test.dart';
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
setUp(() => FlutterSecureStorage.setMockInitialValues({}));
|
|
test('waits for transition teardown before replacing credentials', () async {
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final active = Community.create(
|
|
name: 'Active',
|
|
relayUrl: 'https://active.example',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
);
|
|
final replacement = Community.create(
|
|
name: 'Replacement',
|
|
relayUrl: 'https://replacement.example',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
);
|
|
await storage.save(active);
|
|
await storage.saveActiveId(active.id);
|
|
final container = ProviderContainer(
|
|
overrides: [communityStorageProvider.overrideWithValue(storage)],
|
|
);
|
|
addTearDown(container.dispose);
|
|
await container.read(authProvider.future);
|
|
final teardown = Completer<void>();
|
|
container.read(communityTransitionProvider).register(() => teardown.future);
|
|
|
|
final authenticating = container
|
|
.read(authProvider.notifier)
|
|
.authenticateWithCommunity(replacement);
|
|
await Future<void>.delayed(Duration.zero);
|
|
|
|
expect(await storage.loadActiveId(), active.id);
|
|
expect(
|
|
(await storage.loadAll()).map((community) => community.id),
|
|
isNot(contains(replacement.id)),
|
|
);
|
|
teardown.complete();
|
|
await authenticating;
|
|
expect(await storage.loadActiveId(), replacement.id);
|
|
expect(
|
|
(await storage.loadAll()).map((community) => community.id),
|
|
contains(replacement.id),
|
|
);
|
|
});
|
|
|
|
test('signing in names the new member at once', () async {
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final reporter = _MemberRecorder();
|
|
final keys = nostr.Keys.generate();
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityStorageProvider.overrideWithValue(storage),
|
|
diagnosticReporterProvider.overrideWithValue(reporter),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
await container.read(authProvider.future);
|
|
|
|
await container
|
|
.read(authProvider.notifier)
|
|
.authenticateWithCommunity(
|
|
Community.create(
|
|
name: 'One',
|
|
relayUrl: 'https://one.example',
|
|
nsec: keys.nsec,
|
|
),
|
|
);
|
|
|
|
expect(reporter.member, keys.public);
|
|
});
|
|
|
|
test(
|
|
'background batch addition preserves the first active community',
|
|
() async {
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final first = Community.create(
|
|
name: 'First',
|
|
relayUrl: 'wss://first.example',
|
|
nsec: nostr.Keys('1'.padLeft(64, '0')).nsec,
|
|
);
|
|
final second = Community.create(
|
|
name: 'Second',
|
|
relayUrl: 'wss://second.example',
|
|
nsec: nostr.Keys('1'.padLeft(64, '0')).nsec,
|
|
);
|
|
final container = ProviderContainer(
|
|
overrides: [communityStorageProvider.overrideWithValue(storage)],
|
|
);
|
|
addTearDown(container.dispose);
|
|
await container.read(authProvider.future);
|
|
await container
|
|
.read(authProvider.notifier)
|
|
.authenticateWithCommunity(first);
|
|
var transitions = 0;
|
|
container.read(communityTransitionProvider).register(() async {
|
|
transitions++;
|
|
});
|
|
await container
|
|
.read(authProvider.notifier)
|
|
.authenticateWithCommunity(second, activate: false);
|
|
expect(await storage.loadActiveId(), first.id);
|
|
expect(container.read(authProvider).value?.community?.id, first.id);
|
|
expect((await storage.loadAll()).map((item) => item.id), [
|
|
first.id,
|
|
second.id,
|
|
]);
|
|
expect(transitions, 0);
|
|
},
|
|
);
|
|
|
|
test('waits for transition teardown before removing credentials', () async {
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final community = Community.create(
|
|
name: 'Active',
|
|
relayUrl: 'https://relay.example',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
);
|
|
await storage.save(community);
|
|
await storage.saveActiveId(community.id);
|
|
final container = ProviderContainer(
|
|
overrides: [communityStorageProvider.overrideWithValue(storage)],
|
|
);
|
|
addTearDown(container.dispose);
|
|
await container.read(authProvider.future);
|
|
final teardown = Completer<void>();
|
|
container.read(communityTransitionProvider).register(() => teardown.future);
|
|
|
|
final signingOut = container
|
|
.read(communityListProvider.notifier)
|
|
.removeCommunity(community.id);
|
|
await Future<void>.delayed(Duration.zero);
|
|
|
|
expect(await storage.loadActiveId(), community.id);
|
|
final saved = await storage.loadAll();
|
|
expect(saved, hasLength(1));
|
|
expect(saved.single.id, community.id);
|
|
teardown.complete();
|
|
await signingOut;
|
|
expect(await storage.loadActiveId(), isNull);
|
|
expect(await storage.loadAll(), isEmpty);
|
|
});
|
|
|
|
test('serializes overlapping switch and remove mutations', () async {
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final active = Community.create(
|
|
name: 'Active',
|
|
relayUrl: 'https://active.example',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
);
|
|
final replacement = Community.create(
|
|
name: 'Replacement',
|
|
relayUrl: 'https://replacement.example',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
);
|
|
await storage.save(active);
|
|
await storage.save(replacement);
|
|
await storage.saveActiveId(active.id);
|
|
final container = ProviderContainer(
|
|
overrides: [communityStorageProvider.overrideWithValue(storage)],
|
|
);
|
|
addTearDown(container.dispose);
|
|
await container.read(authProvider.future);
|
|
await container.read(communityListProvider.future);
|
|
final teardown = Completer<void>();
|
|
container.read(communityTransitionProvider).register(() => teardown.future);
|
|
|
|
final switching = container
|
|
.read(communityListProvider.notifier)
|
|
.switchCommunity(replacement.id);
|
|
final signingOut = container
|
|
.read(communityListProvider.notifier)
|
|
.removeCommunity(replacement.id);
|
|
await Future<void>.delayed(Duration.zero);
|
|
|
|
expect(await storage.loadActiveId(), active.id);
|
|
teardown.complete();
|
|
await Future.wait([switching, signingOut]);
|
|
|
|
expect(await storage.loadActiveId(), active.id);
|
|
expect(
|
|
(await storage.loadAll()).map((community) => community.id),
|
|
unorderedEquals([active.id]),
|
|
);
|
|
final auth = await container.read(authProvider.future);
|
|
expect(auth.status, AuthStatus.authenticated);
|
|
expect(auth.community?.id, active.id);
|
|
});
|
|
|
|
test(
|
|
'removes an invalid saved community instead of authenticating',
|
|
() async {
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final invalid = Community.create(
|
|
name: 'Invalid',
|
|
relayUrl: 'https://relay.example',
|
|
nsec: 'not-an-nsec',
|
|
);
|
|
await storage.save(invalid);
|
|
await storage.saveActiveId(invalid.id);
|
|
final snapshots = <List<Community>>[];
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityStorageProvider.overrideWithValue(storage),
|
|
communitySnapshotWriterProvider.overrideWithValue((
|
|
communities,
|
|
) async {
|
|
snapshots.add(List.of(communities));
|
|
}),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
|
|
final auth = await container.read(authProvider.future);
|
|
|
|
expect(auth.status, AuthStatus.unauthenticated);
|
|
expect(await storage.loadAll(), isEmpty);
|
|
expect(await storage.loadActiveId(), isNull);
|
|
expect(snapshots.last, isEmpty);
|
|
},
|
|
);
|
|
|
|
test('authenticate exports the complete stored community snapshot', () async {
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final existing = Community.create(
|
|
name: 'Existing',
|
|
relayUrl: 'https://existing.example',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
);
|
|
final added = Community.create(
|
|
name: 'Added',
|
|
relayUrl: 'https://added.example',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
);
|
|
await storage.save(existing);
|
|
final snapshots = <List<Community>>[];
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityStorageProvider.overrideWithValue(storage),
|
|
communitySnapshotWriterProvider.overrideWithValue((communities) async {
|
|
snapshots.add(List.of(communities));
|
|
}),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
|
|
await container
|
|
.read(authProvider.notifier)
|
|
.authenticateWithCommunity(added);
|
|
|
|
expect(snapshots.last.map((community) => community.id), {
|
|
existing.id,
|
|
added.id,
|
|
});
|
|
});
|
|
|
|
test('removing the active community updates the shared snapshot', () async {
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final first = Community.create(
|
|
name: 'First',
|
|
relayUrl: 'https://first.example',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
);
|
|
final second = Community.create(
|
|
name: 'Second',
|
|
relayUrl: 'https://second.example',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
);
|
|
await storage.save(first);
|
|
await storage.save(second);
|
|
await storage.saveActiveId(first.id);
|
|
final snapshots = <List<Community>>[];
|
|
final journaledCommunityIds = <String>[];
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityStorageProvider.overrideWithValue(storage),
|
|
communitySnapshotWriterProvider.overrideWithValue((communities) async {
|
|
snapshots.add(List.of(communities));
|
|
}),
|
|
communityPushLeaseRevocationEnqueuerProvider.overrideWithValue((
|
|
community,
|
|
) async {
|
|
journaledCommunityIds.add(community.id);
|
|
return true;
|
|
}),
|
|
communityPushLeaseRevocationTriggerProvider.overrideWithValue(
|
|
() async {},
|
|
),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
await container.read(authProvider.future);
|
|
|
|
await container
|
|
.read(communityListProvider.notifier)
|
|
.removeCommunity(first.id);
|
|
|
|
expect(
|
|
snapshots.any((snapshot) {
|
|
return snapshot.length == 1 && snapshot.single.id == second.id;
|
|
}),
|
|
isTrue,
|
|
);
|
|
expect(
|
|
snapshots.last.map((community) => community.id),
|
|
isNot(contains(first.id)),
|
|
);
|
|
expect(journaledCommunityIds, [first.id]);
|
|
});
|
|
|
|
test(
|
|
'snapshot export failure does not gate startup authentication',
|
|
() async {
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final community = Community.create(
|
|
name: 'Existing',
|
|
relayUrl: 'https://existing.example',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
);
|
|
await storage.save(community);
|
|
await storage.saveActiveId(community.id);
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityStorageProvider.overrideWithValue(storage),
|
|
communitySnapshotWriterProvider.overrideWithValue((_) async {
|
|
throw PlatformException(
|
|
code: 'save_failed',
|
|
message: 'Keychain unavailable',
|
|
);
|
|
}),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
|
|
final auth = await container.read(authProvider.future);
|
|
|
|
expect(auth.status, AuthStatus.authenticated);
|
|
expect(auth.community?.id, community.id);
|
|
expect(pushCommunitySnapshotError.value, contains('save_failed'));
|
|
},
|
|
);
|
|
|
|
test('snapshot export failure does not gate direct authentication', () async {
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final community = Community.create(
|
|
name: 'Added',
|
|
relayUrl: 'https://added.example',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
);
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityStorageProvider.overrideWithValue(storage),
|
|
communitySnapshotWriterProvider.overrideWithValue((_) async {
|
|
throw PlatformException(
|
|
code: 'save_failed',
|
|
message: 'Keychain unavailable',
|
|
);
|
|
}),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
|
|
await container
|
|
.read(authProvider.notifier)
|
|
.authenticateWithCommunity(community);
|
|
|
|
final auth = await container.read(authProvider.future);
|
|
expect(auth.status, AuthStatus.authenticated);
|
|
expect(auth.community?.id, community.id);
|
|
expect((await storage.loadAll()).single.id, community.id);
|
|
});
|
|
|
|
test('falls through to the next valid saved community', () async {
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final invalid = Community.create(
|
|
name: 'Invalid',
|
|
relayUrl: 'https://invalid.example',
|
|
);
|
|
final valid = Community.create(
|
|
name: 'Valid',
|
|
relayUrl: 'https://valid.example',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
);
|
|
await storage.save(invalid);
|
|
await storage.save(valid);
|
|
await storage.saveActiveId(invalid.id);
|
|
final container = ProviderContainer(
|
|
overrides: [communityStorageProvider.overrideWithValue(storage)],
|
|
);
|
|
addTearDown(container.dispose);
|
|
|
|
final auth = await container.read(authProvider.future);
|
|
|
|
expect(auth.status, AuthStatus.authenticated);
|
|
expect(auth.community?.id, valid.id);
|
|
expect(await storage.loadActiveId(), valid.id);
|
|
});
|
|
}
|
|
|
|
class _MemberRecorder extends NoopDiagnosticReporter {
|
|
@override
|
|
String? member;
|
|
|
|
@override
|
|
void setMember(String? pubkey) => member = pubkey;
|
|
}
|