Files
buzz/mobile/lib/shared/diagnostics/diagnostic_reporter.dart
Tom Brow b544fcea4a feat(mobile): add a diagnostics test report and test crash to Settings (#9676)
A way to check that a build's diagnostic reporting works end to end, per
[mobile diagnostic
reporting](https://github.com/block/buzz/blob/main/docs/mobile-diagnostic-reporting.md).
A build with a reporting destination shows a Diagnostics card in
Settings with **Send test report** and **Test crash**. A build without
one, which includes every build from this repository alone, never shows
it and still reports nothing.

| Before | After |
| --- | --- |
| ![Settings bottom,
before](https://github.com/user-attachments/assets/92fdef05-8d63-43db-8df1-32ccb92105c8)
| ![Settings bottom with the Diagnostics
card](https://github.com/user-attachments/assets/a205ab18-ed1e-49d5-8e34-88e29e4c91fa)
|

![Test crash
confirmation](https://github.com/user-attachments/assets/335eb362-3a61-4c8c-8b32-44de548cf788)

### What's in it

- The reporter gains `reporting` (whether the build has a destination),
`sendTestReport()` and `crashForTest()`. The do-nothing reporter returns
false and does nothing.
- **Send test report** sends a fixed `DiagnosticTestError` tagged with a
new `testReport` operation. It passes through the same filter as every
report.
- **Test crash** asks for confirmation, then crashes in native code
through the SDK, so native crash reports can be checked too. The report
goes out the next time the app opens.
- The spec gains a "A way to check a build" decision.

### Regressions

None expected: builds without a destination render Settings exactly as
before.

### Validation

Added widget tests for the card being absent without reporting, sending
a test report, and the crash happening only once confirmed, plus a test
of what a sent test report contains and one that a second tap sends a
second report. The screenshots come from simulator builds of main and
this branch, both with the same placeholder destination.

A full-app simulator run at dec267f (placeholder destination) passed:
channel list, opening and scrolling a channel, search, the other
Settings sections, two test reports (a toast each time), cancelling Test
crash, confirming it (a native crash in the Sentry SDK) and relaunching
signed in.

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
2026-11-18 14:48:25 -08:00

140 lines
3.7 KiB
Dart

import 'package:hooks_riverpod/hooks_riverpod.dart';
/// The one interface app code uses for diagnostic reporting.
///
/// See `docs/mobile-diagnostic-reporting.md`. Every call returns at once and
/// never throws: a failure inside reporting is dropped so it cannot reach the
/// code that made the call.
abstract interface class DiagnosticReporter {
/// Reports something that should never happen, such as a frame the app
/// cannot decode. [operation] names where it happened, from a fixed list.
void reportError(
Object error,
StackTrace? stackTrace, {
required DiagnosticOperation operation,
});
/// Counts one networking event, with how long it took when known.
void countNetworkEvent(NetworkEvent event);
/// Adds one entry to the trail attached to later reports.
void leaveBreadcrumb(DiagnosticBreadcrumb breadcrumb);
/// Attributes later reports and counts to [pubkey], or to no one.
void setMember(String? pubkey);
/// Whether this build has a reporting destination. Only then does Settings
/// offer the checks below.
bool get reporting;
/// Sends a [DiagnosticTestError], to check a build's reporting end to end.
void sendTestReport();
/// Crashes the app in native code, to check native crash reports.
void crashForTest();
}
/// The error a test report carries, so it is easy to tell apart.
///
/// Not const: each report needs its own instance, or the destination's
/// deduplication drops every report after the first.
class DiagnosticTestError implements Exception {
DiagnosticTestError();
@override
String toString() => 'DiagnosticTestError';
}
/// Where an explicit error report comes from.
enum DiagnosticOperation {
relayFrame,
relayQuery,
relayRejection,
accountResponse,
pushGatewayResponse,
storedData,
eventSubscriber,
testReport,
}
/// The operation a networking event belongs to.
enum NetworkEndpoint { relayConnection, relayQuery, relayPublish }
/// How a networking event ended.
enum NetworkOutcome { success, failure }
/// Why a networking event failed.
enum NetworkFailure {
timeout,
disconnected,
offline,
unreachable,
insecure,
rateLimited,
authRefused,
serverError,
relayError,
relayRefused,
}
/// One counted networking event. [backend] is the server's host name.
class NetworkEvent {
const NetworkEvent({
required this.backend,
required this.endpoint,
required this.outcome,
this.failure,
this.duration,
}) : assert((outcome == NetworkOutcome.failure) == (failure != null));
final String backend;
final NetworkEndpoint endpoint;
final NetworkOutcome outcome;
final NetworkFailure? failure;
final Duration? duration;
}
/// One entry in the trail of recent networking events.
class DiagnosticBreadcrumb {
const DiagnosticBreadcrumb(this.event);
/// Breadcrumbs from the fixed list are networking events.
final NetworkEvent event;
}
/// Used when the build has no reporting destination.
class NoopDiagnosticReporter implements DiagnosticReporter {
const NoopDiagnosticReporter();
@override
void reportError(
Object error,
StackTrace? stackTrace, {
required DiagnosticOperation operation,
}) {}
@override
void countNetworkEvent(NetworkEvent event) {}
@override
void leaveBreadcrumb(DiagnosticBreadcrumb breadcrumb) {}
@override
void setMember(String? pubkey) {}
@override
bool get reporting => false;
@override
void sendTestReport() {}
@override
void crashForTest() {}
}
/// The reporter chosen at startup. The root scope overrides it; tests
/// override it with a fake that records what it was given.
final diagnosticReporterProvider = Provider<DiagnosticReporter>(
(ref) => const NoopDiagnosticReporter(),
);