Files
buzz/mobile/test/features/settings/settings_diagnostics_test.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

102 lines
3.2 KiB
Dart

import 'package:buzz/features/settings/settings_page.dart';
import 'package:buzz/shared/community/community_membership_provider.dart';
import 'package:buzz/shared/diagnostics/diagnostic_reporter.dart';
import 'package:buzz/shared/theme/theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
Future<void> pumpSettings(
WidgetTester tester,
DiagnosticReporter reporter,
) async {
tester.view.devicePixelRatio = 1;
tester.view.physicalSize = const Size(390, 1400);
addTearDown(tester.view.reset);
PackageInfo.setMockInitialValues(
appName: 'Buzz',
packageName: 'xyz.block.buzz',
version: '0.16.0',
buildNumber: '432',
buildSignature: '',
);
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
await tester.pumpWidget(
ProviderScope(
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
currentCommunityRoleProvider.overrideWithValue(
const AsyncData<CommunityMemberRole?>(CommunityMemberRole.member),
),
diagnosticReporterProvider.overrideWithValue(reporter),
],
child: MaterialApp(
theme: AppTheme.light(),
home: SettingsPage(
profileHeader: const SizedBox.shrink(),
identityRecoveryPageBuilder: (_) => const SizedBox.shrink(),
),
),
),
);
await tester.pumpAndSettle();
}
final sendRow = find.byKey(const Key('settings-send-test-report'));
final crashRow = find.byKey(const Key('settings-test-crash'));
testWidgets('a build without reporting offers no checks', (tester) async {
await pumpSettings(tester, const NoopDiagnosticReporter());
expect(sendRow, findsNothing);
expect(crashRow, findsNothing);
});
testWidgets('a reporting build sends a test report', (tester) async {
final reporter = _RecordingReporter();
await pumpSettings(tester, reporter);
await tester.ensureVisible(sendRow);
await tester.tap(sendRow);
await tester.pump();
expect(reporter.calls, ['sendTestReport']);
expect(find.text('Test report sent.'), findsOneWidget);
});
testWidgets('a test crash happens only once confirmed', (tester) async {
final reporter = _RecordingReporter();
await pumpSettings(tester, reporter);
await tester.ensureVisible(crashRow);
await tester.tap(crashRow);
await tester.pumpAndSettle();
await tester.tap(find.text('Cancel'));
await tester.pumpAndSettle();
expect(reporter.calls, isEmpty);
await tester.tap(crashRow);
await tester.pumpAndSettle();
await tester.tap(find.text('Crash'));
await tester.pumpAndSettle();
expect(reporter.calls, ['crashForTest']);
});
}
class _RecordingReporter extends NoopDiagnosticReporter {
final calls = <String>[];
@override
bool get reporting => true;
@override
void sendTestReport() => calls.add('sendTestReport');
@override
void crashForTest() => calls.add('crashForTest');
}