11import 'package:buzz/features/channels/channel_management_provider.dart' ;
22import 'package:buzz/shared/custom_emoji/custom_emoji_render.dart' ;
33import 'package:buzz/shared/emoji/native_emoji_glyph.dart' ;
4- import 'package:buzz/shared/relay/media_image.dart' ;
54import 'package:http/http.dart' as http;
65import 'package:http/testing.dart' ;
76import 'package:buzz/shared/emoji/emoji_avatar.dart' ;
@@ -12,7 +11,13 @@ import 'package:buzz/features/channels/channel_identity_names_provider.dart';
1211import 'package:buzz/shared/identity_names/identity_names.dart' ;
1312import 'package:buzz/features/channels/message_long_press_region.dart' ;
1413import 'package:buzz/features/channels/reaction_row.dart' ;
14+ import 'package:buzz/shared/hosted/builderlab_account.dart' ;
15+ import 'package:buzz/shared/hosted/hosted_relay_auth.dart' ;
1516import 'package:buzz/shared/hosted/native_media_headers.dart' ;
17+ import 'package:buzz/shared/relay/relay.dart' ;
18+ import 'package:flutter_secure_storage/flutter_secure_storage.dart' ;
19+ import 'package:nostr/nostr.dart' as nostr;
20+ import 'dart:convert' ;
1621import 'package:buzz/features/channels/timeline_message.dart' ;
1722import 'package:buzz/shared/emoji/emoji_burst.dart' ;
1823import 'package:buzz/shared/emoji/emoji_data.dart' ;
@@ -389,6 +394,156 @@ void main() {
389394 },
390395 );
391396
397+ testWidgets (
398+ 'native reaction sheet refreshes an assertion inside its refresh margin' ,
399+ (tester) async {
400+ debugDefaultTargetPlatformOverride = TargetPlatform .iOS;
401+ addTearDown (() => debugDefaultTargetPlatformOverride = null );
402+ FlutterSecureStorage .setMockInitialValues ({});
403+ final keys = nostr.Keys .generate ();
404+ final organization = Uri .parse ('https://org.example' );
405+ final config = RelayConfig (
406+ baseUrl: 'https://relay.example' ,
407+ nsec: keys.nsec,
408+ );
409+ final expiry = DateTime .now ().toUtc ().add (const Duration (days: 1 ));
410+ await const FlutterSecureStorage ().write (
411+ key: 'buzz_hosted_session_org_example_v1' ,
412+ value: jsonEncode ({
413+ 'origin' : organization.toString (),
414+ 'credential' : 'secret' ,
415+ 'subject' : 'member' ,
416+ 'expires_at' : expiry.toIso8601String (),
417+ }),
418+ );
419+ var clock = DateTime .utc (2026 , 11 , 4 , 12 );
420+ var issued = 0 ;
421+ final auth = HostedRelayAuth (
422+ BuilderlabAccountClient (),
423+ organization: BuilderlabAccountClient (
424+ accountOrigin: organization,
425+ httpClient: MockClient ((request) async {
426+ return switch (request.url.path) {
427+ '/api/goose/v1/auth/me' => http.Response (
428+ jsonEncode ({
429+ 'subject' : 'member' ,
430+ 'expires_at' : expiry.toIso8601String (),
431+ }),
432+ 200 ,
433+ ),
434+ '/api/goose/v1/buzz/nostr-identities/current' => http.Response (
435+ jsonEncode ({
436+ 'identity' : {'pubkey_hex' : keys.public},
437+ }),
438+ 200 ,
439+ ),
440+ '/api/goose/v1/buzz/identity/assertions' => http.Response (
441+ jsonEncode ({
442+ 'assertion' : 'token-${++issued }' ,
443+ 'nostr_pubkey' : keys.public,
444+ 'expires_at' : clock.millisecondsSinceEpoch ~ / 1000 + 120 ,
445+ }),
446+ 200 ,
447+ ),
448+ _ => http.Response ('{}' , 404 ),
449+ };
450+ }),
451+ ),
452+ organizationRelay: (relay) => relay.host == 'relay.example' ,
453+ now: () => clock,
454+ );
455+ addTearDown (auth.dispose);
456+ // The live socket's assertion, issued when the session connected.
457+ expect (await auth.headers (config), {
458+ 'Nostr-Federated-Identity' : 'Bearer token-1' ,
459+ });
460+ final payloads = < Map <Object ?, Object ?>> [];
461+ final completion = Completer <Map <String , Object ?>>();
462+ tester.binding.defaultBinaryMessenger.setMockMethodCallHandler (
463+ NativeMessagePresentation .channel,
464+ (call) async {
465+ if (call.method == 'reactions' ) {
466+ payloads.add (call.arguments as Map <Object ?, Object ?>);
467+ return completion.future;
468+ }
469+ return < String , Object ? > {};
470+ },
471+ );
472+ addTearDown (
473+ () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler (
474+ NativeMessagePresentation .channel,
475+ null ,
476+ ),
477+ );
478+ await tester.pumpWidget (
479+ WidgetHelpers .testable (
480+ overrides: [
481+ emojiDatasetOrEmptyProvider.overrideWithValue (_dataset),
482+ relayConfigProvider.overrideWith (
483+ () => _FixedRelayConfigNotifier (config),
484+ ),
485+ hostedRelayAuthProvider.overrideWithValue (auth),
486+ userCacheProvider.overrideWith (
487+ () => _FakeUserCacheNotifier ({
488+ _alice: const UserProfile (
489+ pubkey: _alice,
490+ displayName: 'Honey' ,
491+ avatarUrl: _aliceAvatarUrl,
492+ ),
493+ }),
494+ ),
495+ channelIdentityNamesProvider ('channel' ).overrideWith (
496+ (ref) => IdentityNameSources (
497+ profiles: ref.watch (userCacheProvider),
498+ ).scope ([_alice]),
499+ ),
500+ ],
501+ child: ReactionRow (
502+ messageId: _messageId,
503+ channelId: 'channel' ,
504+ reactions: [
505+ TimelineReaction (
506+ emoji: _fire,
507+ count: 1 ,
508+ reactedByCurrentUser: false ,
509+ userPubkeys: [_alice],
510+ emojiUrl: _customEmojiUrl,
511+ ),
512+ ],
513+ onToggle: (_) {},
514+ ),
515+ ),
516+ );
517+ await tester.pumpAndSettle ();
518+ // Rendering the row may itself fetch media through the hosted client,
519+ // so enter the margin only now (120s lifetime, 30s margin): the socket
520+ // is still valid, but the synchronous cache reads as empty.
521+ final issuedBeforeSheet = issued;
522+ clock = clock.add (const Duration (seconds: 100 ));
523+ expect (auth.cachedHeaders (config), isNull);
524+ await tester.longPress (
525+ find.byKey (const ValueKey ('reaction-pill-$_fire ' )),
526+ );
527+ await tester.pumpAndSettle ();
528+
529+ expect (issued, issuedBeforeSheet + 1 );
530+ final payload = payloads.single;
531+ final avatar = (payload['profiles' ] as Map )[_alice] as Map ;
532+ expect (
533+ (avatar['headers' ] as Map )['Nostr-Federated-Identity' ],
534+ 'Bearer token-$issued ' ,
535+ );
536+ final reaction = (payload['reactions' ] as List ).single as Map ;
537+ expect (
538+ (reaction['headers' ] as Map )['Nostr-Federated-Identity' ],
539+ 'Bearer token-$issued ' ,
540+ );
541+ completion.complete (< String , Object ? > {});
542+ await tester.pumpAndSettle ();
543+ debugDefaultTargetPlatformOverride = null ;
544+ },
545+ );
546+
392547 testWidgets (
393548 'native hydration sends bounded diffs and preserves emoji avatars' ,
394549 (tester) async {
@@ -927,3 +1082,12 @@ void main() {
9271082 });
9281083 });
9291084}
1085+
1086+ class _FixedRelayConfigNotifier extends RelayConfigNotifier {
1087+ _FixedRelayConfigNotifier (this ._config);
1088+
1089+ final RelayConfig _config;
1090+
1091+ @override
1092+ RelayConfig build () => _config;
1093+ }
0 commit comments