Files
buzz/mobile/test/shared/diagnostics/diagnostic_install_id_test.dart
Tom Brow ec1b786ef0 fix(mobile): diagnostic reporting follow-ups (#9808)
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 |
| --- | --- |
| ![Diagnostics card
before](https://github.com/user-attachments/assets/b8fa7100-3efd-4c36-ac4d-006d487f4902)
| ![Diagnostics card after, with row
subtitles](https://github.com/user-attachments/assets/9ca7e95b-6061-4efa-b476-d7bb4c113a9b)
|

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
2026-11-20 12:49:57 -08:00

96 lines
2.8 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:buzz/shared/diagnostics/diagnostic_startup.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
// A damaged file is replaced, so reports still carry one install ID from
// then on.
for (final (name, contents) in <(String, List<int>)>[
('empty', []),
('blank', [0x20, 0x0a]),
('not text', [0xff, 0xfe, 0xc3, 0x28]),
]) {
test('a $name install ID file is replaced with one that is kept', () async {
final directory = await Directory.systemTemp.createTemp('install');
addTearDown(() => directory.delete(recursive: true));
await File(
'${directory.path}/diagnostics_install_id',
).writeAsBytes(contents);
final id = await diagnosticInstallId(directory: () async => directory);
expect(id, isNotNull);
expect(id, isNotEmpty);
expect(await diagnosticInstallId(directory: () async => directory), id);
});
}
// A read that fails, for instance with no file descriptor free, leaves the
// saved ID for the next launch rather than replacing it.
test('a file that fails to read keeps its install ID', () async {
final directory = await Directory.systemTemp.createTemp('install');
addTearDown(() => directory.delete(recursive: true));
const saved = '6c0fb3a6-5b7e-4d58-9a40-1c2e2f0b7d11';
final file = File('${directory.path}/diagnostics_install_id');
await file.writeAsString(saved);
final id = await IOOverrides.runZoned(
() => diagnosticInstallId(directory: () async => directory),
createFile: (_) => _UnreadableFile(file),
);
expect(id, isNull);
expect(await file.readAsString(), saved);
});
}
/// [file], except that every read fails as the operating system refused it,
/// while writes still succeed.
class _UnreadableFile implements File {
_UnreadableFile(this.file);
final File file;
FileSystemException get _failure =>
FileSystemException('Cannot open file', path, const OSError('', 24));
@override
String get path => file.path;
@override
Future<bool> exists() => file.exists();
@override
Future<Uint8List> readAsBytes() async => throw _failure;
@override
Future<String> readAsString({Encoding encoding = utf8}) async =>
throw _failure;
@override
Future<File> writeAsString(
String contents, {
FileMode mode = FileMode.write,
Encoding encoding = utf8,
bool flush = false,
}) => file.writeAsString(
contents,
mode: mode,
encoding: encoding,
flush: flush,
);
@override
Future<File> writeAsBytes(
List<int> bytes, {
FileMode mode = FileMode.write,
bool flush = false,
}) => file.writeAsBytes(bytes, mode: mode, flush: flush);
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}