Files
buzz/mobile/test/features/channels/send_message_provider_test.dart
Tom Brow 3742076434 fix(mobile): mitigate message-post delay with optimistic rendering (#3037)
### What changed?

Adds optimistic rendering for newly locally posted messages in channels
and threads.
- Messages appear immediately
- Relay echoes and history are deduplicated by event ID
- Rejected or timed-out publishes erase the optimistic rendering.

The implementation covers reconnect and hydration races, channel-window
and legacy WebSocket paths, thread-local overlays, and rapid concurrent
sends.

### Why?

This is a valuable partial mitigation for
[BOT-1449](https://linear.app/squareup/issue/BOT-1449/buzz-mobile-posted-messages-dont-appear-until-leavingre-entering-the):
senders no longer depend on receiving a relay echo before seeing their
own post.

It does not address the likely primary cause of stale channels. Mobile
currently does not recover live subscriptions after a rate-limited relay
`CLOSED`; that recovery is being handled separately.

### How is it tested?

Full mobile suite: 676 passed, 1 skipped.

Added regression coverage for optimistic insertion, authoritative
deduplication, rollback, reconnect and hydration, thread replies, rapid
and equal-time sends, never-echoed successful sends, and legacy
WebSocket retirement.

---------

Signed-off-by: npub102wg7q285p64ch2fjvstmf2ntn2sz3c4u5hmwatalc76mhsuauysftjtfj <7a9c8f0147a0755c5d499320bda5535cd5014715e52fb7757dfe3dadde1cef09@buzz.block.builderlab.xyz>
Co-authored-by: npub102wg7q285p64ch2fjvstmf2ntn2sz3c4u5hmwatalc76mhsuauysftjtfj <7a9c8f0147a0755c5d499320bda5535cd5014715e52fb7757dfe3dadde1cef09@buzz.block.builderlab.xyz>
2026-07-27 07:16:33 -04:00

97 lines
3.1 KiB
Dart

import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:nostr/nostr.dart' as nostr;
import 'package:buzz/features/channels/send_message_provider.dart';
import 'package:buzz/shared/relay/relay.dart';
void main() {
test(
'adds the signed message locally before relay acknowledgement',
() async {
final session = _PendingPublishRelaySession();
final localMessages = <NostrEvent>[];
final removedIds = <String>[];
final completedIds = <String>[];
final send = SendMessage(
signedEventRelay: SignedEventRelay(
session: session,
nsec: nostr.Keys.generate().nsec,
),
fetchMembers: (_) async => const [],
readUserCache: () => const {},
addLocalMessage: (_, event) => localMessages.add(event),
completeLocalMessage: (_, eventId) => completedIds.add(eventId),
removeLocalMessage: (_, eventId) => removedIds.add(eventId),
);
final result = send(channelId: _channelId, content: 'hello');
await session.published;
expect(localMessages, hasLength(1));
expect(localMessages.single.id, session.event.id);
expect(localMessages.single.content, 'hello');
expect(localMessages.single.channelId, _channelId);
expect(removedIds, isEmpty);
session.accept();
await result;
expect(completedIds, [localMessages.single.id]);
expect(removedIds, isEmpty);
},
);
test('rolls back the signed local message when publish fails', () async {
final session = _PendingPublishRelaySession();
final localMessages = <NostrEvent>[];
final completedIds = <String>[];
final removedIds = <String>[];
final send = SendMessage(
signedEventRelay: SignedEventRelay(
session: session,
nsec: nostr.Keys.generate().nsec,
),
fetchMembers: (_) async => const [],
readUserCache: () => const {},
addLocalMessage: (_, event) => localMessages.add(event),
completeLocalMessage: (_, eventId) => completedIds.add(eventId),
removeLocalMessage: (_, eventId) => removedIds.add(eventId),
);
final result = send(channelId: _channelId, content: 'hello');
await session.published;
session.reject();
await expectLater(result, throwsException);
expect(completedIds, isEmpty);
expect(removedIds, [localMessages.single.id]);
});
}
const _channelId = '11111111-1111-4111-8111-111111111111';
class _PendingPublishRelaySession extends RelaySessionNotifier {
final Completer<NostrEvent> _result = Completer<NostrEvent>();
final Completer<void> _published = Completer<void>();
late NostrEvent event;
Future<void> get published => _published.future;
@override
SessionState build() => const SessionState(status: SessionStatus.connected);
@override
Future<NostrEvent> publish(
NostrEvent event, {
Duration timeout = const Duration(seconds: 8),
}) {
this.event = event;
_published.complete();
return _result.future;
}
void accept() => _result.complete(event);
void reject() => _result.completeError(Exception('relay rejected event'));
}