Files
buzz/mobile/test/features/channels/notification_reply_test.dart
Will Pfleger 890824f072 feat(read-state): sync mark read, channel mark unread and thread follows (#9501)
Reading, mark read, whole-channel mark unread and thread follow choices
now sync across a person's installations. This PR specifies the rules in
NIP-RS and implements them in the mobile app.

## Spec (`docs/nips/NIP-RS.md`)

- Defines *catch-up* and *displayed*.
- A catch-up advances the channel's own frontier (top-level messages) or
`thread:<root>`. `activity:` and `thread-activity:` become read-only
legacy keys.
- Explicit mark read is synchronized state. A first explicit read of a
message with no override group writes only its `msg:` frontier. Display,
dwell, scroll, catch-up and mention visibility create no `msg:` entries.
- Only whole-channel override groups are published, merged and cleared
with the counter. Readers ignore peer `msg:` and `thread:` groups.
- Single-message and thread mark unread stay on the device. They end
once the merged frontier covering them passes their baseline, from any
installation, or on display, catch-up or explicit read. The section also
states clearing scope, the accepted concurrency limitation and capacity.
- When the merged override union no longer fits the primary, the client
rewrites the primary compactly from the groups its last published
primary carried, each canonical against current frontiers. It never
omits merged groups or tombstone floors that primary carried, and
override actions fail visibly.
- Adds Buzz Thread Follow Choices: `thread-follows:<32hex>` coordinates,
validation, three-state verdict, last-writer merge with ties going to
unfollow, publication and recovery, legacy import at `at = 0`, and
capacity.

## Mobile

- Catch-up writes the main frontier. A flat timeline reads up to the
newest top-level message it reached.
- Explicit mark read publishes. Passive `msg:` marks stay on the device.
- Whole-channel mark unread publishes an override group and merges peer
groups. An explicit read or catch-up clears it, even when no frontier
moves. The action fails visibly at the uint32 limit or when the primary
is full. A synced clear the primary can't carry is rolled back, and the
channel view reports it once while the failure stands.
- Device-local message and Inbox unread overrides record a baseline and
clear when a covering frontier passes it, including one merged from a
peer.
- Thread follows move from local storage to the synced `thread-follows:`
slots. They share the read-state `client_id`, journal, `created_at`
sequence, snapshot and live subscription, and spill across up to 16
slots with stable placement. An unreadable coordinate marks follow state
incomplete and blocks this installation's follow publication.
- Follow and unfollow are refused on every cold start of the writer
until its first complete recovery of its own state succeeds and every
owned follow head is readable. Cached follows show at once. A refused
tap changes nothing and shows "Follow choices are not ready. Connect and
try again." Reads, catch-up and both kinds of mark unread are not gated.
- A follow tap is accepted only once journaled. The accepted own head,
pending edits and the exact in-flight packet are kept apart, so a retry
or restore never drops or re-attributes an edit.
- Pre-sync local follows are imported at `at = 0` after a complete
merge, once their channel is known from a relay event. An imported key
is settled, and its legacy copy retired, only when the journal write the
import awaited covered it and that entry is still current.
- An explicit unfollow suppresses ordinary replies at live and catch-up
admission. A change in follow choices drops the unread replies already
counted for an unfollowed thread. A newly followed thread re-runs
catch-up only for its own channel.
- A reading clear refused because the view hasn't completely loaded is
retried after the next complete snapshot, including when publishing was
shut off at the time. The view also counts as not completely loaded
while the live subscription is reconnecting, so a read made while the
phone is offline can't erase a newer unread mark set on another device.
Offline, whole-channel mark unread is still refused, with a message.
- Read-state events, including live ones, are verified with native
libsecp256k1 before they are used.
- A pending unread packet is counted as published only when no newer own
head outranks it. When a newer head supersedes it, a successor is built
above that head from the saved intent, and a late echo of a replaced
packet doesn't overwrite what was published.
- When the merged unread state overfills the primary and the compact
fallback can't carry a pending mark, the packet stays pending and the
"read state full" error shows. Other queued reads and follows in the
same flush still publish.
- When this installation's own unread record can't be decoded, only the
unread step is held: that record isn't overwritten, its pending marks
stay queued and the sync error shows. Other queued reads and follows in
the same flush still publish.
- The sync notice stays up after an unsupported refusal until a new
session. Offline, it can't be tapped and says it will retry when back
online.
- Two-device fixtures against an in-memory relay cover channel
round-trips and clears, stray message groups, the first explicit message
read, the primary cap, follow propagation, same-second and
backward-clock follow publication, legacy import, retry after a failed
publish, unreadable coordinates and slot packing. Recovery fixtures
cover the startup gate, journal failures during flush, interleaved
legacy imports, and restore of an older journal.

## Relay

- Conforming `thread-follows:<32 hex>` records get the NIP-RS treatment:
a replaced record is purged, and a stale resend of an older one is
refused. Records with a mismatched `t` tag keep ordinary
replaceable-event behavior.

## Known gap

- An unread packet held for capacity can age past the relay's timestamp
window. The relay then refuses it, and the refusal path retires it
without the capacity check, so the error clears and other installations
don't get that mark. The mark stays on the device.

Related:
[block/buzz-app#3552](https://github.com/block/buzz-app/pull/3552)

---------

Signed-off-by: Will Pfleger <wpfleger@block.xyz>
Signed-off-by: Will Pfleger <wpfleger@squareup.com>
Co-authored-by: Duncan <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz>
Co-authored-by: Tom Brow <tomb@block.xyz>
2026-11-19 17:02:58 -05:00

535 lines
19 KiB
Dart

import 'dart:async';
import 'package:buzz/shared/drafts/compose_drafts_provider.dart';
import 'package:buzz/features/channels/channel.dart';
import 'package:buzz/features/channels/channel_management_provider.dart';
import 'package:buzz/features/channels/channels_provider.dart';
import 'package:buzz/features/channels/notification_reply.dart';
import 'package:buzz/features/channels/send_message_provider.dart';
import 'package:buzz/shared/auth/auth.dart';
import 'package:buzz/shared/push/push_bridge.dart';
import 'package:buzz/shared/read_state/read_state_format.dart';
import 'package:buzz/shared/read_state/read_state_provider.dart';
import 'package:buzz/shared/relay/relay.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:buzz/shared/theme/theme_provider.dart';
import 'package:fake_async/fake_async.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:nostr/nostr.dart' as nostr;
import 'package:shared_preferences/shared_preferences.dart';
const _push = MethodChannel('buzz/push');
final _root = 'a' * 64;
final _message = 'b' * 64;
final _sender = 'c' * 64;
final _community = Community(
id: 'community',
name: 'Example',
relayUrl: 'https://relay.example.org',
addedAt: DateTime.utc(2026),
);
final _elsewhere = Community(
id: 'elsewhere',
name: 'Elsewhere',
relayUrl: 'https://other.example.org',
addedAt: DateTime.utc(2026),
);
const _selectedDrafts = 'compose_drafts_v1:https://relay.example.org:anon';
const _elsewhereDrafts = 'compose_drafts_v1:https://other.example.org:anon';
Channel _channel(String id, {bool solo = false, bool member = true}) => Channel(
id: id,
name: id,
description: '',
channelType: 'stream',
visibility: solo ? 'private' : 'open',
createdBy: 'owner',
createdAt: DateTime.utc(2026),
memberCount: 2,
isMember: member,
labels: solo ? const ['buzz.solo'] : const [],
);
ChannelMember _member(String pubkey) =>
ChannelMember(pubkey: pubkey, role: 'member', joinedAt: DateTime.utc(2026));
Map<String, String> _reply(
String id, {
String communityId = 'community',
String channelId = 'general',
String? threadRootId,
String? replyRootId,
int createdAt = 1000,
bool inSolo = false,
}) => {
'id': id,
'text': 'reply $id',
'eventId': _message,
'communityId': communityId,
'channelId': channelId,
'threadRootId': ?threadRootId,
'senderPubkey': _sender,
'replyRootId': ?replyRootId,
'messageCreatedAt': '$createdAt',
if (inSolo) 'inSolo': 'true',
'deadline':
'${DateTime.now().add(const Duration(seconds: 25)).millisecondsSinceEpoch}',
};
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test(
'replies typed into notifications are sent, read and reported',
() async {
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
final messenger =
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
final waiting = <Map<String, String>>[
// A top-level channel message: a new subthread under it.
_reply('1'),
// A message inside a thread: the subthread keeps the thread's root.
_reply('2', threadRootId: _root, replyRootId: _root, createdAt: 2000),
// A Solo chat takes no thread replies.
_reply('3', channelId: 'solo', createdAt: 3000),
// Another community's notification can't be answered without
// switching; its text is kept as a draft in that community, in the
// subthread under the notified message even though that community's
// channels aren't loaded.
_reply('4', communityId: 'elsewhere'),
_reply(
'12',
communityId: 'elsewhere',
channelId: 'nested',
threadRootId: _root,
replyRootId: _root,
),
// A notification that knew it was a Solo chat keeps it in the chat.
_reply('13', communityId: 'elsewhere', channelId: 'own', inSolo: true),
// A send that fails before publishing keeps the text in the
// subthread under the notified message, where the reply would go.
_reply(
'5',
channelId: 'broken',
threadRootId: _root,
replyRootId: _root,
),
// A channel the user isn't in isn't replied to, and its text goes
// after the draft already there.
_reply('6', channelId: 'outside'),
// A sender who has left isn't mentioned, but still gets the reply.
_reply('10', channelId: 'left'),
// A reply published without an acknowledgement may have gone out, so
// no draft is kept.
_reply('11', channelId: 'lost'),
// A reply an earlier launch never finished is never sent again: it's
// kept as a draft, or, if it may have gone out then, left alone.
{..._reply('14'), 'recovered': 'true'},
{..._reply('15'), 'recovered': 'true', 'publishing': 'true'},
// A draft that can't be saved isn't reported, so iOS keeps the reply
// for a later launch.
{..._reply('16', communityId: 'gone'), 'recovered': 'true'},
];
// What iOS hears, in order, alongside each message sent.
final log = <String>[];
installBuzzPushMethodHandler();
final outcomes = <String, Object>{};
final selected = <Object?>[];
messenger.setMockMethodCallHandler(_push, (call) async {
switch (call.method) {
case 'takePendingNotificationReplies':
final taken = [...waiting];
waiting.clear();
return taken;
case 'completeNotificationReply':
final args = call.arguments as Map;
outcomes[args['id'] as String] = args['unknown'] == true
? 'unknown'
: args['sent'] == true
? 'sent'
: 'draft in ${args['draftThreadRootId']}';
case 'markNotificationReplyPublishing':
log.add('may publish ${call.arguments}');
case 'setSelectedCommunity':
selected.add(call.arguments);
}
return null;
});
addTearDown(() {
messenger.setMockMethodCallHandler(_push, null);
debugDefaultTargetPlatformOverride = null;
});
SharedPreferences.setMockInitialValues({
_selectedDrafts:
'[{"key":"outside:$_message","channel_id":"outside",'
'"thread_head_id":"$_message","text":"earlier draft",'
'"updated_at":1}]',
});
final prefs = await SharedPreferences.getInstance();
final sender = _RecordingSender(onSend: (text) => log.add('sent $text'));
final session = _Session();
var active = _community;
final container = ProviderContainer(
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
authProvider.overrideWith(_Auth.new),
communityListProvider.overrideWith(_Communities.new),
activeCommunityProvider.overrideWith((ref) async => active),
channelsProvider.overrideWith(
() => _Channels([
_channel('general'),
_channel('solo', solo: true),
_channel('broken'),
_channel('outside', member: false),
_channel('left'),
_channel('lost'),
]),
),
channelMembersProvider.overrideWith((ref, channelId) async {
if (channelId == 'broken') throw StateError('members unavailable');
return [if (channelId != 'left') _member(_sender), _member('me')];
}),
sendMessageProvider.overrideWithValue(sender),
readStateProvider.overrideWith(_ReadState.new),
relaySessionProvider.overrideWith(() => session),
],
);
addTearDown(container.dispose);
container.listen(notificationReplySenderProvider, (_, _) {});
await pumpEventQueue();
final drafts = container.read(composeDraftsProvider.notifier);
expect(selected, ['community']);
expect(outcomes, {
'1': 'sent',
'2': 'sent',
'3': 'sent',
'4': 'draft in $_message',
'5': 'draft in $_message',
'6': 'draft in $_message',
'10': 'sent',
'11': 'unknown',
'12': 'draft in $_message',
'13': 'draft in null',
'14': 'draft in $_message',
'15': 'unknown',
});
// iOS knows a reply may go out before anything is published.
expect(log, [
for (final id in ['1', '2', '3', '10', '11']) ...[
'may publish $id',
'sent reply $id',
],
]);
expect(sender.calls, [
('general', 'reply 1', _message, _message, _sender),
('general', 'reply 2', _message, _root, _sender),
('solo', 'reply 3', null, null, _sender),
('left', 'reply 10', _message, _message, ''),
('lost', 'reply 11', _message, _message, _sender),
]);
// Read only up to each notified message, so newer ones stay unread.
expect(container.read(readStateProvider).contexts, {
'general': 1000,
threadContextKey(_root): 2000,
'solo': 3000,
'left': 1000,
});
expect(drafts.textFor(composeDraftKey('general')), isNull);
final elsewhere = prefs.getString(_elsewhereDrafts);
expect(elsewhere, contains('"key":"general:$_message"'));
expect(elsewhere, contains('"text":"reply 4"'));
expect(elsewhere, contains('"key":"nested:$_message"'));
expect(
elsewhere,
contains('"key":"own","channel_id":"own","text":"reply 13"'),
);
expect(
drafts.textFor(composeDraftKey('broken', threadHeadId: _message)),
'reply 5',
);
expect(
drafts.textFor(composeDraftKey('outside', threadHeadId: _message)),
'earlier draft\n\nreply 6',
);
expect(
drafts.textFor(composeDraftKey('lost', threadHeadId: _message)),
isNull,
);
expect(
drafts.textFor(composeDraftKey('general', threadHeadId: _message)),
'reply 14',
);
Future<void> signalReply(Map<String, String> reply) async {
waiting.add(reply);
await messenger.handlePlatformMessage(
_push.name,
_push.codec.encodeMethodCall(
const MethodCall('notificationReplyAvailable'),
),
(_) {},
);
await pumpEventQueue();
}
// A reply arriving while the app runs is sent when iOS signals it.
await signalReply(_reply('7', channelId: 'solo'));
expect(outcomes['7'], 'sent');
// When the relay can't be reached in time, nothing is sent and the
// text is kept.
session.reachable = false;
await signalReply(_reply('8', channelId: 'solo'));
expect(outcomes['8'], 'draft in null');
expect(sender.calls.last.$2, 'reply 7');
expect(drafts.textFor(composeDraftKey('solo')), 'reply 8');
// A community switch while the relay connects stops the reply before
// it reaches the other community's relay, and keeps the text in the
// reply's own community.
session.reachable = true;
session.onConnect = () async {
active = _elsewhere;
container.invalidate(activeCommunityProvider);
await container.read(activeCommunityProvider.future);
};
// The other community's channels say nothing about this one, so the
// notification's Solo flag places the draft.
await signalReply(_reply('9', channelId: 'solo', inSolo: true));
expect(outcomes['9'], 'draft in null');
expect(sender.calls.last.$2, 'reply 7');
expect(
prefs.getString(_selectedDrafts),
contains('"text":"reply 8\\n\\nreply 9"'),
);
expect(prefs.getString(_elsewhereDrafts), isNot(contains('reply 9')));
},
);
test('a reply publishes only while its outcome can still be reported', () {
fakeAsync((async) {
SharedPreferences.setMockInitialValues({});
late SharedPreferences prefs;
SharedPreferences.getInstance().then((value) => prefs = value);
async.flushMicrotasks();
final gate = RelayRateLimitGate(now: async.getClock(DateTime(2026)).now);
final session = _Session(rateLimitGate: gate);
final relay = _Relay(session);
var members = Completer<List<ChannelMember>>();
final send = SendMessage(
signedEventRelay: SignedEventRelay(
session: session,
nsec: nostr.Keys.generate().nsec,
),
fetchMembers: (_) => members.future,
readUserCache: () => const {},
addLocalMessage: (_, _) {},
completeLocalMessage: (_, _) {},
removeLocalMessage: (_, _) {},
);
final container = ProviderContainer(
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
authProvider.overrideWith(_Auth.new),
communityListProvider.overrideWith(_Communities.new),
activeCommunityProvider.overrideWith((ref) async => _community),
channelsProvider.overrideWith(() => _Channels([_channel('general')])),
channelMembersProvider.overrideWith(
(ref, channelId) async => [_member(_sender)],
),
sendMessageProvider.overrideWithValue(send),
readStateProvider.overrideWith(_ReadState.new),
relaySessionProvider.overrideWith(() => session),
],
);
addTearDown(container.dispose);
final ref = container.read(_refProvider);
container.read(relaySessionProvider);
session.debugAttachSocketForTest(relay);
final drafts = container.read(composeDraftsProvider.notifier);
NotificationReplyResult? result;
void reply(String id) {
result = null;
sendNotificationReply(
ref,
BuzzNotificationReply.fromArguments(_reply(id))!,
).then((value) => result = value);
}
// The message send's own member lookup finishes too late to hear the
// relay's OK in time, so the reply is never published and its text is
// kept where it would have gone.
reply('1');
async.elapse(const Duration(seconds: 25));
expect(result?.outcome, NotificationReplyOutcome.notSent);
expect(
drafts.textFor(composeDraftKey('general', threadHeadId: _message)),
'reply 1',
);
members.complete([_member(_sender)]);
async.flushMicrotasks();
expect(relay.events, isEmpty);
String? draft() =>
drafts.textFor(composeDraftKey('general', threadHeadId: _message));
// Held by a rate limit past its deadline, a reply is reported not sent,
// and never goes out once the limit lifts.
members = Completer()..complete([_member(_sender)]);
gate.activate(60);
reply('2');
async.elapse(const Duration(seconds: 25));
expect(result?.outcome, NotificationReplyOutcome.notSent);
expect(draft(), 'reply 1\n\nreply 2');
async.elapse(const Duration(seconds: 60));
expect(relay.events, isEmpty);
// A reply the relay refuses wasn't sent, so its text is kept.
relay.answer = (id) => ['OK', id, false, 'blocked: not allowed'];
reply('3');
async.elapse(const Duration(seconds: 25));
expect(relay.events.single['content'], 'reply 3');
expect(result?.outcome, NotificationReplyOutcome.notSent);
expect(draft(), 'reply 1\n\nreply 2\n\nreply 3');
// A published reply whose OK never comes may have gone out, so it
// leaves no draft to resend.
relay.answer = (_) => null;
reply('4');
async.elapse(const Duration(seconds: 25));
expect(relay.events.last['content'], 'reply 4');
expect(result?.outcome, NotificationReplyOutcome.unknown);
expect(draft(), 'reply 1\n\nreply 2\n\nreply 3');
});
});
}
final _refProvider = Provider<Ref>((ref) => ref);
class _Auth extends AuthNotifier {
@override
Future<AuthState> build() async =>
AuthState(status: AuthStatus.authenticated, community: _community);
}
class _Channels extends ChannelsNotifier {
_Channels(this._channels);
final List<Channel> _channels;
@override
Future<List<Channel>> build() async => _channels;
}
class _ReadState extends ReadStateNotifier {
@override
ReadStateState build() => const ReadStateState(
isReady: true,
pubkey: 'me',
contexts: {},
version: 1,
);
@override
void markContextRead(
String contextId,
int unixTimestamp, {
bool clearForcedMessages = false,
bool explicit = false,
}) {
state = state.copyWithContext(contextId, unixTimestamp);
}
}
class _Communities extends CommunityListNotifier {
@override
Future<List<Community>> build() async => [_community, _elsewhere];
}
class _Session extends RelaySessionNotifier {
_Session({super.rateLimitGate});
var reachable = true;
Future<void> Function()? onConnect;
@override
SessionState build() =>
const SessionState(status: SessionStatus.disconnected);
@override
Future<bool> connectForBackgroundWork({required Duration timeout}) async {
await onConnect?.call();
return reachable;
}
}
class _RecordingSender implements SendMessage {
_RecordingSender({required this.onSend});
final void Function(String text) onSend;
final calls = <(String, String, String?, String?, String?)>[];
@override
Future<void> call({
required String channelId,
required String content,
String? parentEventId,
String? rootEventId,
List<String>? mentionPubkeys,
Channel? channel,
List<List<String>> mediaTags = const [],
FutureOr<void> Function(NostrEvent)? beforePublish,
void Function()? validateDelivery,
Future<bool> Function()? mayPublish,
}) async {
if (mayPublish != null && !await mayPublish()) {
throw StateError('no longer wanted');
}
calls.add((
channelId,
content,
parentEventId,
rootEventId,
mentionPubkeys?.join(','),
));
onSend(content);
if (channelId == 'lost') throw TimeoutException('no OK');
}
}
/// A connected relay socket that answers each EVENT with [answer]'s reply,
/// or not at all when it gives null.
class _Relay extends RelaySocket {
_Relay(this._session)
: super(
wsUrl: 'ws://localhost',
nsec: null,
onMessage: (_) {},
onConnected: () {},
onDisconnected: (_) {},
);
final RelaySessionNotifier _session;
final events = <Map<String, dynamic>>[];
List<dynamic>? Function(String id) answer = (id) => ['OK', id, true, ''];
@override
SocketState get state => SocketState.connected;
@override
void send(List<dynamic> payload) {
if (payload case ['EVENT', final Map<String, dynamic> event]) {
events.add(event);
final reply = answer(event['id'] as String);
if (reply != null) _session.debugHandleSocketMessageForTest(reply);
}
}
@override
Future<void> disconnect() async {}
@override
void dispose() {}
}