mirror of
https://github.com/block/buzz.git
synced 2026-11-30 05:02:57 +00:00
Follow-ups to the mobile diagnostic reporting work in
docs/mobile-diagnostic-reporting.md.
## Changes
- **Install ID repair:** if the install ID file holds something that
isn't text, the app now replaces it with a new ID, as it already does
for an empty file. Before, every report went out without an install ID.
A read that fails (for example, no file descriptor free) still keeps the
file, so a passing error never replaces a good ID.
- **Clock-window refusals are not reported as errors:** the spec treats
relay refusals for a timestamp outside the relay's clock window as
expected (counting them is still pending). The app only recognized one
of the relay's wordings ("timestamp too far from server time"), so
refusals for canvas events, relay-admin commands, leave requests and
observer frames were reported as invalid requests. The five wordings are
matched from the start of the reason, so unrelated `invalid:` reasons
that mention a timestamp, such as the feedback timestamp overflow or one
that echoes a value, are still reported.
- **Diagnostics card:** rows get subtitles, and the divider indent
matches the neighbouring Settings cards.
- **Comments:** explain why push stop gates its current-identity request
on the community key, and correct a test comment about who drops a
repeated error object.
## Validation
New tests cover:
- member attribution on relay sessions, including a missing key and an
answer that arrives after the member changed;
- push stop reports while signed out;
- reporter wiring for hosted admission and the self-hosted probe;
- frames the socket can't decode;
- a table of relay refusal reasons, each either reported or not, for
both `OK` and `CLOSED`;
- an install ID file whose reads fail keeps its ID.
Full-app run on an iPhone 17 Pro simulator at 61b04f6: startup, channel
list, sending and reacting, Settings, the Diagnostics card and Send test
report, and install ID repair (a file that isn't valid UTF-8 and an
empty file each got a new ID that stayed the same on the next launch).
Not covered: a relay clock-window refusal, which the live relay would
not produce.
## Diagnostics card
| Before | After |
| --- | --- |
| 
| 
|
---------
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
228 lines
6.7 KiB
Dart
228 lines
6.7 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:buzz/shared/auth/auth_provider.dart';
|
|
import 'package:buzz/shared/community/community.dart';
|
|
import 'package:buzz/shared/diagnostics/diagnostic_reporter.dart';
|
|
import 'package:buzz/shared/relay/relay.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart' as http_testing;
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
|
|
// A community switch names the next member while the old session is still
|
|
// live, so each report is checked against the member when it is made.
|
|
void main() {
|
|
for (final (label, member, reported) in [
|
|
('another member', _Member.other, false),
|
|
('its own key', _Member.signer, true),
|
|
('no one', _Member.none, true),
|
|
]) {
|
|
test('a session reports only while $label is the member', () async {
|
|
final harness = await _connectedSession();
|
|
final session = harness.session;
|
|
final reporter = harness.reporter;
|
|
void nameMember() => reporter.setMember(switch (member) {
|
|
_Member.other => nostr.Keys.generate().public,
|
|
_Member.signer => harness.keys.public,
|
|
_Member.none => null,
|
|
});
|
|
|
|
// Each request starts while the session's own key is the member.
|
|
final rejected = session.publish(_event('event-1'));
|
|
final query = session.queryRelay(const [
|
|
NostrFilter(kinds: [1]),
|
|
]);
|
|
await Future<void>.delayed(Duration.zero);
|
|
nameMember();
|
|
|
|
// The relay's refusal arrives over the socket after the member changed.
|
|
harness.refuse.complete();
|
|
await expectLater(rejected, throwsA(isA<Exception>()));
|
|
harness.decode.complete();
|
|
await expectLater(query, throwsA(isA<FormatException>()));
|
|
harness.sendFrames();
|
|
await harness.noticed.future;
|
|
|
|
expect(reporter.errors, [
|
|
if (reported) ...[
|
|
(RelayInvalidRequest, DiagnosticOperation.relayRejection),
|
|
(FormatException, DiagnosticOperation.relayQuery),
|
|
(FormatException, DiagnosticOperation.relayFrame),
|
|
],
|
|
]);
|
|
});
|
|
}
|
|
}
|
|
|
|
enum _Member { other, signer, none }
|
|
|
|
class _Harness {
|
|
_Harness({
|
|
required this.session,
|
|
required this.reporter,
|
|
required this.keys,
|
|
required this.refuse,
|
|
required this.decode,
|
|
required this.noticed,
|
|
required this.sendFrames,
|
|
});
|
|
|
|
final RelaySessionNotifier session;
|
|
final _RecordingDiagnostics reporter;
|
|
final nostr.Keys keys;
|
|
final Completer<void> refuse;
|
|
final Completer<void> decode;
|
|
final Completer<void> noticed;
|
|
final void Function() sendFrames;
|
|
}
|
|
|
|
/// A session connected through a real socket to a loopback relay, with its
|
|
/// own key as the member.
|
|
Future<_Harness> _connectedSession() async {
|
|
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
|
|
addTearDown(() => server.close(force: true));
|
|
final clients = <WebSocket>[];
|
|
final refuse = Completer<void>();
|
|
server.listen((request) async {
|
|
final ws = await WebSocketTransformer.upgrade(request);
|
|
clients.add(ws);
|
|
ws.listen((frame) {
|
|
final message = jsonDecode(frame as String) as List<dynamic>;
|
|
if (message.first == 'AUTH') {
|
|
final id = (message[1] as Map<String, dynamic>)['id'];
|
|
ws.add(jsonEncode(['OK', id, true, '']));
|
|
} else if (message.first == 'EVENT') {
|
|
final id = (message[1] as Map<String, dynamic>)['id'];
|
|
unawaited(
|
|
refuse.future.then(
|
|
(_) =>
|
|
ws.add(jsonEncode(['OK', id, false, 'invalid: bad signature'])),
|
|
),
|
|
);
|
|
}
|
|
});
|
|
ws.add(jsonEncode(['AUTH', 'challenge']));
|
|
});
|
|
|
|
final keys = nostr.Keys.generate();
|
|
final reporter = _RecordingDiagnostics()..setMember(keys.public);
|
|
final decode = Completer<void>();
|
|
final noticed = Completer<void>();
|
|
final connected = Completer<void>();
|
|
final session = RelaySessionNotifier(
|
|
httpClient: http_testing.MockClient((_) async => http.Response('[]', 200)),
|
|
queryDecoder: (_) async {
|
|
await decode.future;
|
|
throw const FormatException('bad body');
|
|
},
|
|
socketFactory:
|
|
({
|
|
required wsUrl,
|
|
required nsec,
|
|
required onMessage,
|
|
required onConnected,
|
|
required onDisconnected,
|
|
}) => RelaySocket(
|
|
wsUrl: 'ws://127.0.0.1:${server.port}',
|
|
nsec: nsec,
|
|
onMessage: (message) {
|
|
if (message.first == 'NOTICE' && !noticed.isCompleted) {
|
|
noticed.complete();
|
|
}
|
|
onMessage(message);
|
|
},
|
|
onConnected: () {
|
|
onConnected();
|
|
if (!connected.isCompleted) connected.complete();
|
|
},
|
|
onDisconnected: onDisconnected,
|
|
),
|
|
);
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
relaySessionProvider.overrideWith(() => session),
|
|
relayConfigProvider.overrideWith(
|
|
() => _RelayConfig(
|
|
RelayConfig(baseUrl: 'https://relay.example', nsec: keys.nsec),
|
|
),
|
|
),
|
|
authProvider.overrideWith(() => _Authenticated(keys.public)),
|
|
diagnosticReporterProvider.overrideWithValue(reporter),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
await container.read(authProvider.future);
|
|
final listener = container.listen(relaySessionProvider, (_, _) {});
|
|
addTearDown(listener.close);
|
|
await connected.future;
|
|
return _Harness(
|
|
session: session,
|
|
reporter: reporter,
|
|
keys: keys,
|
|
refuse: refuse,
|
|
decode: decode,
|
|
noticed: noticed,
|
|
sendFrames: () => clients.last
|
|
..add('not json')
|
|
..add('["NOTICE","still here"]'),
|
|
);
|
|
}
|
|
|
|
NostrEvent _event(String id) => NostrEvent(
|
|
id: id,
|
|
pubkey: 'alice',
|
|
createdAt: 20,
|
|
kind: 1,
|
|
tags: const [],
|
|
content: 'hello',
|
|
sig: 'sig',
|
|
);
|
|
|
|
class _RelayConfig extends RelayConfigNotifier {
|
|
_RelayConfig(this._config);
|
|
|
|
final RelayConfig _config;
|
|
|
|
@override
|
|
RelayConfig build() => _config;
|
|
}
|
|
|
|
class _Authenticated extends AuthNotifier {
|
|
_Authenticated(this._pubkey);
|
|
|
|
final String _pubkey;
|
|
|
|
@override
|
|
Future<AuthState> build() async => AuthState(
|
|
status: AuthStatus.authenticated,
|
|
community: Community(
|
|
id: 'community-1',
|
|
name: 'Example',
|
|
relayUrl: 'https://relay.example',
|
|
pubkey: _pubkey,
|
|
addedAt: DateTime(2026),
|
|
),
|
|
);
|
|
}
|
|
|
|
class _RecordingDiagnostics extends NoopDiagnosticReporter {
|
|
final errors = <(Type, DiagnosticOperation)>[];
|
|
String? _member;
|
|
|
|
@override
|
|
String? get member => _member;
|
|
|
|
@override
|
|
void setMember(String? pubkey) => _member = pubkey;
|
|
|
|
@override
|
|
void reportError(
|
|
Object error,
|
|
StackTrace? stackTrace, {
|
|
required DiagnosticOperation operation,
|
|
}) => errors.add((error.runtimeType, operation));
|
|
}
|