mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +00:00
## What An Android push wake that runs out of time can leave a claim still running. If that claim then wins, the message is recorded as shown but was never returned. No later wake finds it, so the user is never told. This makes the wake report that case, and the native handler shows its generic notification for it. - `resolvePushWake` tracks the message each community is claiming. After the claim grace it sets `PushWakeResult.unreported` when a claim is still running for a message with something to show (a message whose body is empty would not be returned anyway). It is read in the same synchronous step as the claimed-message snapshot, so a claim that finishes before the snapshot is returned, and one that finishes after it is counted. - `PushWakeRunner` reports `unreported` when any pass does, because a message lost in one pass stays lost. - The channel payload carries `unreported`. The native handler posts the named notifications, and the generic notification beside them, silently when something already alerted. If the user already cleared the generic card this wake posted at 2 seconds, it stays cleared. The generic is marked as standing in for a lost message, so later wakes, which clear an unmarked generic as before, leave it until the user clears it. A later wake that re-posts the generic keeps the mark. - It stays separate from `incomplete`, which means a later wake retries. Folding the two would show the generic beside named notifications on every retryable partial wake. - `ShownMessageStore.claim` documents that a caller that stops waiting for a claim must report it. A claim that later returns false still costs one unnecessary generic notification, which is accepted. The realistic trigger is storage that stalls after the claim marker is created, during the watermark and prune I/O in `DirectoryShownMessageStore.claim` (a pre-existing finding from the review of the Android wake handler). The store's order is unchanged. The marker has to come first for atomicity, and this signal turns the stall into a generic notification instead of silence. ## Validation - The push-wake journey test's stalled-storage step now asserts `unreported`, and a stalled claim for a message with an empty body asserts it is not set. A wake cut short in the name lookup, outside any claim, asserts it is not set and still returns its messages. The runner step asserts that a message lost in one pass is still reported after a clean rerun. The channel payload test covers both values. - Forcing the signal off fails the stalled-storage step. - `WakeNotifierGenericTest` (instrumented) drives the generic notification against the real notification manager on a Pixel 9a: an unmarked card clears on a clean wake, a kept card survives clean wakes and a later 2-second re-post, and a card the user cleared before the result is not posted again. Each step waits for the state it expects, a re-post check waits for a newer post, and checks that nothing changed run after a barrier notification. Its two updates wait 700 ms first, so Android's update rate limit can't drop them on quick reruns (12 back-to-back runs passed). Removing the cleared-card check, or dropping the mark on re-post, fails it. --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
217 lines
7.3 KiB
Dart
217 lines
7.3 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:buzz/shared/push/android_push_wake.dart';
|
|
import 'package:buzz/shared/push/push_wake_resolver.dart';
|
|
import 'package:buzz/shared/push/push_wake_runner.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http/testing.dart';
|
|
|
|
import '../../helpers/push_wake_fixture.dart';
|
|
|
|
void main() {
|
|
late Directory directory;
|
|
late PushWakeFixture fixture;
|
|
|
|
setUp(() {
|
|
directory = Directory.systemTemp.createTempSync('android-push-wake-');
|
|
fixture = PushWakeFixture(directory);
|
|
});
|
|
tearDown(() => directory.deleteSync(recursive: true));
|
|
|
|
group('the wake the native handler receives', () {
|
|
test('carries each notification with where its tap leads', () async {
|
|
final relay = fixture.addCommunity();
|
|
final room = relay.channel();
|
|
final friend = relay.person('Friend');
|
|
final dm = relay.dmChannel();
|
|
final mention = relay.message(room, friend, 'look', mention: relay.me);
|
|
final direct = relay.message(dm, friend, 'hello');
|
|
final handler = PushWakeChannelHandler(
|
|
PushWakeRunner((_) => fixture.wake()),
|
|
);
|
|
|
|
final payload = await handler.wake();
|
|
|
|
expect(payload['incomplete'], isFalse);
|
|
expect(payload['notifications'], [
|
|
{
|
|
'eventId': direct.id,
|
|
'createdAtMillis': direct.createdAt * 1000,
|
|
'title': 'Friend',
|
|
'body': 'hello',
|
|
'conversationId': '${relay.communityId}:${dm.id}',
|
|
'category': 'directMessage',
|
|
'target': {
|
|
'communityId': relay.communityId,
|
|
'channelId': dm.id,
|
|
'eventId': direct.id,
|
|
},
|
|
},
|
|
{
|
|
'eventId': mention.id,
|
|
'createdAtMillis': mention.createdAt * 1000,
|
|
'title': 'Friend',
|
|
'body': 'look',
|
|
'conversationId': '${relay.communityId}:${room.id}',
|
|
'conversationTitle': '#${room.name}',
|
|
'category': 'mention',
|
|
'target': {
|
|
'communityId': relay.communityId,
|
|
'channelId': room.id,
|
|
'eventId': mention.id,
|
|
},
|
|
},
|
|
]);
|
|
});
|
|
|
|
test('reports a wake that could not read every community', () async {
|
|
final relay = fixture.addCommunity();
|
|
relay.unreachable = true;
|
|
final handler = PushWakeChannelHandler(
|
|
PushWakeRunner((_) => fixture.wake()),
|
|
);
|
|
|
|
final payload = await handler.wake();
|
|
|
|
expect(payload['notifications'], isEmpty);
|
|
expect(payload['incomplete'], isTrue);
|
|
expect(payload['unreported'], isFalse);
|
|
|
|
// A message the wake lost track of is reported too, so the handler
|
|
// shows its generic notification beside any named ones.
|
|
final lost = await PushWakeChannelHandler(
|
|
PushWakeRunner(
|
|
(_) async =>
|
|
const PushWakeResult(notifications: [], unreported: true),
|
|
),
|
|
).wake();
|
|
expect(lost['unreported'], isTrue);
|
|
});
|
|
|
|
test(
|
|
'gives a wake that arrives mid-run no notifications of its own',
|
|
() async {
|
|
final relay = fixture.addCommunity();
|
|
final dm = relay.dmChannel();
|
|
final friend = relay.person('Friend');
|
|
relay.message(dm, friend, 'hello');
|
|
final hold = Completer<void>();
|
|
relay.hold = hold;
|
|
final handler = PushWakeChannelHandler(
|
|
PushWakeRunner((_) => fixture.wake()),
|
|
);
|
|
|
|
final first = handler.wake();
|
|
final second = handler.wake();
|
|
hold.complete();
|
|
final payloads = await Future.wait([first, second]);
|
|
|
|
expect((payloads[0]['notifications'] as List), hasLength(1));
|
|
expect(payloads[1], joinedWake);
|
|
},
|
|
);
|
|
|
|
test('after a run ends, the next wake gets its own result', () async {
|
|
final relay = fixture.addCommunity();
|
|
final dm = relay.dmChannel();
|
|
final friend = relay.person('Friend');
|
|
final handler = PushWakeChannelHandler(
|
|
PushWakeRunner((_) => fixture.wake()),
|
|
);
|
|
relay.message(dm, friend, 'one');
|
|
expect((await handler.wake())['notifications'] as List, hasLength(1));
|
|
|
|
relay.message(dm, friend, 'two', age: const Duration(minutes: 4));
|
|
final next = await handler.wake();
|
|
|
|
expect(
|
|
[for (final n in next['notifications'] as List) (n as Map)['body']],
|
|
['two'],
|
|
);
|
|
});
|
|
|
|
test('a run looks up no longer than the handler has left', () async {
|
|
fixture.addCommunity();
|
|
final budgets = <Duration>[];
|
|
final handler = PushWakeChannelHandler(
|
|
PushWakeRunner((budget) {
|
|
budgets.add(budget);
|
|
return fixture.wake(timeouts: PushWakeTimeouts(total: budget));
|
|
}),
|
|
);
|
|
// A slow engine start leaves less than the runner's own budget; a sent
|
|
// time past it, a missing one, or one already spent fall back to the
|
|
// budget, or to nothing.
|
|
for (final (arguments, expected) in [
|
|
({'remainingMillis': 1200}, const Duration(milliseconds: 1200)),
|
|
({'remainingMillis': 60000}, const Duration(seconds: 5)),
|
|
(null, const Duration(seconds: 5)),
|
|
({'remainingMillis': -5}, Duration.zero),
|
|
]) {
|
|
budgets.clear();
|
|
await handler.wake(remaining: wakeRemaining(arguments));
|
|
// The pass gets what is left of the run's budget, a hair under it.
|
|
expect(
|
|
(expected - budgets.single).inMilliseconds,
|
|
inInclusiveRange(0, 100),
|
|
reason: '$arguments',
|
|
);
|
|
}
|
|
});
|
|
});
|
|
|
|
test('a wake sends the relay the headers the community needs', () async {
|
|
final relay = fixture.addCommunity();
|
|
final dm = relay.dmChannel();
|
|
relay.message(dm, relay.person('Friend'), 'hello');
|
|
final seen = <String?>[];
|
|
final client = MockClient((request) async {
|
|
seen.add(request.headers['Nostr-Federated-Identity']);
|
|
return relay.handle(request);
|
|
});
|
|
final asked = <String>[];
|
|
|
|
final result = await resolvePushWake(
|
|
store: fixture.openStore(),
|
|
loadCommunities: () async => [relay.community],
|
|
httpClient: client,
|
|
relayHeaders: (community) async {
|
|
asked.add(community.id);
|
|
return {'Nostr-Federated-Identity': 'Bearer assertion'};
|
|
},
|
|
now: () => pushWakeClock,
|
|
);
|
|
|
|
expect(result.notifications, hasLength(1));
|
|
// Once for the wake, whichever queries follow.
|
|
expect(asked, [relay.communityId]);
|
|
expect(seen, isNotEmpty);
|
|
expect(seen.toSet(), {'Bearer assertion'});
|
|
});
|
|
|
|
test(
|
|
'a community whose headers cannot be read is reported incomplete',
|
|
() async {
|
|
final refused = fixture.addCommunity();
|
|
refused.message(refused.dmChannel(), refused.person('Friend'), 'hello');
|
|
final reachable = fixture.addCommunity();
|
|
final other = reachable.dmChannel();
|
|
reachable.message(other, reachable.person('Other'), 'hi');
|
|
|
|
final result = await resolvePushWake(
|
|
store: fixture.openStore(),
|
|
loadCommunities: () async => [refused.community, reachable.community],
|
|
httpClient: fixture.client,
|
|
relayHeaders: (community) async => community.id == refused.communityId
|
|
? throw StateError('no session')
|
|
: const {},
|
|
now: () => pushWakeClock,
|
|
);
|
|
|
|
expect(result.failedCommunityIds, [refused.communityId]);
|
|
expect([for (final n in result.notifications) n.body], ['hi']);
|
|
},
|
|
);
|
|
}
|