Files
buzz/mobile/test/features/channels/quick_send_test.dart
Tom Brow a303d7d5b5 fix(mobile): send Siri Bestie messages without confirmations (#9779)
Saying "Message my Bestie" to Siri (or running it from the Action button
or Shortcuts) now sends the dictated message straight away. Siri no
longer asks "Send … to <Bestie>?" before sending or shows a "Sent."
dialog afterward.

The Bestie's display name was looked up only to word that confirmation,
so that lookup is gone too: the Flutter side no longer preloads the
Bestie's profile or returns its name, and the bridge's `confirm`
callback is removed.

### Regressions

- A misheard message, or a Bestie changed on another device that this
one hasn't synced yet, now sends without a chance to cancel. We accept
that: the point of the shortcut is a hands-free send, and the message
still appears in the Bestie DM, where it can be deleted.
- This includes runs while the phone is locked and Shortcuts automations
that supply the message: both send immediately, by design.
- The community-switch guard stays: a Bestie looked up in one account or
community still can't be sent to from another.
- Nothing outside the Siri intent changes.

### Validation

- The app code at this head (the last commit only touches a test) is
built into a Buzz Dev development build installed on an iPhone 16e. The
Siri flow itself has not yet been run on that build.
- Updated
[quick_send_test.dart](https://github.com/block/buzz/blob/tomb/siri-no-confirm/mobile/test/features/channels/quick_send_test.dart)
for the `bestie` reply without a name.
- No before/after captures: the change is in Siri's own UI, which can't
be driven or recorded in the simulator.

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz>
2026-11-20 06:07:17 +00:00

359 lines
11 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:buzz/shared/drafts/compose_drafts_provider.dart';
import 'package:buzz/features/channels/channel.dart';
import 'package:buzz/features/channels/channels_provider.dart';
import 'package:buzz/features/channels/quick_send_host.dart';
import 'package:buzz/shared/me/default_agent.dart';
import 'package:buzz/shared/relay/relay.dart';
import 'package:buzz/shared/theme/theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:nostr/nostr.dart' as nostr;
import 'package:shared_preferences/shared_preferences.dart';
class _EmptyChannels extends ChannelsNotifier {
@override
Future<List<Channel>> build() async => [];
}
class _Lifecycle extends AppLifecycleNotifier {
@override
AppLifecycleState build() => AppLifecycleState.resumed;
void background() => state = AppLifecycleState.paused;
}
class _Config extends RelayConfigNotifier {
_Config(this.config);
final RelayConfig config;
@override
RelayConfig build() => config;
}
/// A relay that is not connected until the test says so, accepts every
/// publish and serves back the DM its open command finds or creates.
class _Relay extends RelaySessionNotifier {
_Relay(this.owner);
final nostr.Keys owner;
final published = <NostrEvent>[];
/// Rejects the next message the way an over-quota relay does.
var rateLimitNextMessage = false;
/// Loses the next message's answer, so it may or may not have landed.
var timeOutNextMessage = false;
/// Drops the socket just before the next message goes out.
var dropBeforeNextMessage = false;
final _stored = <NostrEvent>[];
static const dmId = 'bestie-dm';
@override
SessionState build() =>
const SessionState(status: SessionStatus.disconnected);
void connect() => state = const SessionState(status: SessionStatus.connected);
/// Background pauses started; the real session closes the socket after one.
var pauses = 0;
@override
void onAppPaused() => pauses++;
@override
void onAppResumed() => connect();
NostrEvent _sign(int kind, List<List<String>> tags) => NostrEvent.fromJson(
nostr.Event.from(
kind: kind,
content: '',
secretKey: owner.secret,
tags: tags,
).toMap(),
);
@override
Future<List<NostrEvent>> queryRelay(
List<NostrFilter> filters, {
Duration timeout = const Duration(seconds: 8),
}) async => const [];
@override
Future<List<NostrEvent>> fetchHistory(
NostrFilter filter, {
Duration timeout = const Duration(seconds: 8),
}) async {
final ds = filter.tags['#d'];
return _stored.reversed
.where(
(e) =>
filter.kinds.contains(e.kind) &&
(ds == null || ds.contains(e.getTagValue('d'))),
)
.take(filter.limit)
.toList();
}
@override
Future<void Function()> subscribeWithStatus(
NostrFilter filter,
void Function(NostrEvent) onEvent, {
void Function(String message)? onClosed,
required void Function(RelaySubscriptionStatus status) onStatusChanged,
bool resumeSince = true,
}) async => () {};
@override
Future<NostrEvent> publish(
NostrEvent event, {
Duration timeout = const Duration(seconds: 8),
Future<bool> Function()? mayPublish,
}) async {
if (event.kind == 9 && rateLimitNextMessage) {
rateLimitNextMessage = false;
throw Exception('rate-limited: quota exceeded; retry in 0s');
}
if (event.kind == 9 && dropBeforeNextMessage) {
dropBeforeNextMessage = false;
throw StateError('Relay session is not connected');
}
if (event.kind == 9 && timeOutNextMessage) {
timeOutNextMessage = false;
throw TimeoutException('no OK');
}
published.add(event);
if (event.kind != 41010) return event;
// Opening is find-or-create: the relay answers with the one DM.
if (!_stored.any((e) => e.getTagValue('d') == dmId)) {
final peer = event.getTagValue('p')!;
_stored.add(
_sign(39000, [
['d', dmId],
['name', 'DM'],
['t', 'dm'],
['private'],
['hidden'],
['p', owner.public],
['p', peer],
]),
);
_stored.add(
_sign(39002, [
['d', dmId],
['p', owner.public, '', 'member'],
['p', peer, '', 'bot'],
]),
);
}
return NostrEvent(
id: event.id,
pubkey: event.pubkey,
createdAt: event.createdAt,
kind: event.kind,
tags: event.tags,
content: 'response:${jsonEncode({'channel_id': dmId, 'created': true})}',
sig: event.sig,
);
}
}
void main() {
testWidgets('Siri quick send posts in the Bestie DM', (tester) async {
const channel = MethodChannel('buzz/quick_send');
final platformCalls = <String>[];
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (
call,
) async {
platformCalls.add(call.method);
return null;
});
addTearDown(
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
channel,
null,
),
);
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
final owner = nostr.Keys.generate(), bestie = nostr.Keys.generate();
final relay = _Relay(owner);
String? chosenBestie;
await tester.pumpWidget(
ProviderScope(
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
relayConfigProvider.overrideWith(
() => _Config(
RelayConfig(baseUrl: 'https://relay.test', nsec: owner.nsec),
),
),
relaySessionProvider.overrideWith(() => relay),
defaultAgentProvider.overrideWith(
(ref) => Stream.value(chosenBestie),
),
channelsProvider.overrideWith(_EmptyChannels.new),
appLifecycleProvider.overrideWith(_Lifecycle.new),
],
child: MaterialApp(
theme: AppTheme.light(),
home: const QuickSendHost(),
),
),
);
await tester.pumpAndSettle();
expect(platformCalls, ['ready'], reason: 'Siri can send once Home is up');
// Siri calls the host the way QuickSend.swift does and gets its reply.
Future<(Object?, PlatformException?)> siri(
String method, [
Object? arguments,
]) async {
Object? result;
PlatformException? failure;
var replied = false;
tester.binding.defaultBinaryMessenger.handlePlatformMessage(
channel.name,
channel.codec.encodeMethodCall(MethodCall(method, arguments)),
(reply) {
try {
result = channel.codec.decodeEnvelope(reply!);
} on PlatformException catch (e) {
failure = e;
}
replied = true;
},
);
for (var i = 0; i < 100 && !replied; i++) {
await tester.pump(const Duration(milliseconds: 50));
}
expect(replied, isTrue, reason: '$method never answered');
return (result, failure);
}
final container = ProviderScope.containerOf(
tester.element(find.byType(QuickSendHost)),
);
container.read(relaySessionProvider);
relay.connect();
await tester.pump();
final (_, noBestie) = await siri('bestie');
expect(noBestie?.code, 'failed');
expect(noBestie?.message, 'Choose a Bestie in Buzz first.');
chosenBestie = bestie.public;
container.invalidate(defaultAgentProvider);
await tester.pump();
final (named, _) = await siri('bestie');
final scope = (named! as Map)['scope'];
expect(named, {'pubkey': bestie.public, 'scope': scope});
// A Bestie looked up before a community switch can't send here.
final (_, switched) = await siri('send', {
'text': 'Remind me to call Sam',
'bestie': bestie.public,
'scope': 'https://other.test $scope',
});
expect(switched?.message, contains('switched communities'));
final (_, blank) = await siri('send', {
'text': ' \n ',
'bestie': bestie.public,
'scope': scope,
});
expect(blank?.code, 'failed');
expect(blank?.message, 'Say a message.');
expect(relay.published, isEmpty);
// Buzz sat in the background long enough for its socket to close.
(container.read(appLifecycleProvider.notifier) as _Lifecycle).background();
relay.state = const SessionState(status: SessionStatus.disconnected);
final pausesBefore = relay.pauses;
// Its catch-up used the write quota, so the first try is turned away.
relay.rateLimitNextMessage = true;
await tester.pumpAndSettle();
final (_, failure) = await siri('send', {
'text': ' Remind me to call Sam ',
'bestie': bestie.public,
'scope': scope,
});
expect(failure, isNull);
expect(
relay.pauses,
pausesBefore + 1,
reason: 'The send hands the session back to its background pause',
);
final open = relay.published.singleWhere((e) => e.kind == 41010);
expect(open.pubkey, owner.public);
expect(open.tags, [
['p', bestie.public],
]);
final first = relay.published.singleWhere((e) => e.kind == 9);
expect(first.content, 'Remind me to call Sam');
expect(first.pubkey, owner.public);
expect(first.getTagValue('h'), _Relay.dmId);
expect(first.tags, contains(equals(['p', bestie.public])));
// A sent message leaves the DM's draft as it was.
final drafts = container.read(composeDraftsProvider.notifier);
drafts.save(
key: _Relay.dmId,
channelId: _Relay.dmId,
text: 'Half a @Sam',
mentionKeys: {'Sam': 'sam-pubkey'},
);
// A second quick send goes to the same DM.
final (_, again) = await siri('send', {
'text': 'And buy milk',
'bestie': bestie.public,
'scope': scope,
});
expect(again, isNull);
final messages = relay.published.where((e) => e.kind == 9).toList();
expect(messages.map((e) => e.content), [
'Remind me to call Sam',
'And buy milk',
]);
expect(messages.map((e) => e.getTagValue('h')).toSet(), {_Relay.dmId});
expect(drafts.textFor(_Relay.dmId), 'Half a @Sam');
// When Buzz can't tell whether a send landed, the text stays in the DM's
// draft, after the person's own, and Siri says to check before resending.
relay.timeOutNextMessage = true;
final (_, unsure) = await siri('send', {
'text': 'Call back',
'bestie': bestie.public,
'scope': scope,
});
expect(
unsure?.message,
"Buzz isn't sure that sent. Check your Bestie DM before trying again.",
);
final kept = drafts.draftFor(_Relay.dmId)!;
expect(kept.text, 'Half a @Sam\n\nCall back');
expect(kept.mentionKeys, {'Sam': 'sam-pubkey'});
// A socket that drops just before the message goes out keeps it too.
relay.dropBeforeNextMessage = true;
final (_, dropped) = await siri('send', {
'text': 'Then Sam',
'bestie': bestie.public,
'scope': scope,
});
expect(dropped?.message, contains('Check your Bestie DM'));
expect(drafts.textFor(_Relay.dmId), 'Half a @Sam\n\nCall back\n\nThen Sam');
// Leaving Home, for example on sign-out, takes the handler with it.
await tester.pumpWidget(const SizedBox());
expect(platformCalls, ['ready', 'unready']);
// Let the sent messages' local animations finish.
await tester.pump(const Duration(seconds: 10));
});
}