Skip to content

Commit 14037e3

Browse files
dr0uleauSol
andauthored
fix(mobile): show progress after pairing QR scans (#9009)
## Summary After a pairing QR scan, the app shows connection progress until code entry or a connection error. A WebSocket handshake that remains pending stops after ten seconds. Cancel is available until storage starts. It resets pairing and restores the entry form. Pasted codes remain available after an error or cancellation. Back navigation stays blocked during identity transfer and storage. Cancel on Send identity to desktop returns to Settings, so another attempt starts through the authorization step. ### Related issue Related to [#9001](#9001), which covers organization sign-in after identity transfer. This change covers the earlier connection stage. ### Testing A simulator build with PR #9001 completes identity transfer and saves the community. The community chooser still blocks organization sign-in. [PR #9028](#9028) covers that step. Physical iPhone testing and successful real-relay checks in light and dark mode remain pending. On a build with this change, scan a desktop pairing QR code. Check that progress appears, Cancel returns to the entry form, and a fresh attempt reaches code entry. Paste a pairing code, cancel during connection, and check that the form keeps the code. Test a failed connection and retry. In Settings, open Send identity to desktop, start a recovery connection, and cancel. Check that Settings appears and another attempt asks for authorization. Camera recognition speed and the reported intermittent phone network failure remain unverified. Generated with Codex --------- Signed-off-by: Daniel Rouleau <[email protected]> Co-authored-by: Sol <c35e7cc95f6fdbfb7e861dccc02bdca567b6d6b20f16b5115c3c6ae082bbeede@buzz.block.builderlab.xyz>
1 parent 069cb9a commit 14037e3

8 files changed

Lines changed: 302 additions & 8 deletions

File tree

‎mobile/lib/features/pairing/pairing_page.dart‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,12 +432,46 @@ class PairingPage extends HookConsumerWidget {
432432

433433
final appSurface = PopScope(
434434
key: const Key('pairing-pop-scope'),
435+
canPop: !showLoading,
435436
onPopInvokedWithResult: (didPop, _) {
436437
if (didPop && pairingState.status != PairingStatus.success) {
437438
ref.read(pairingProvider.notifier).reset();
438439
}
439440
},
440-
child: pairingScaffold,
441+
child: Stack(
442+
fit: StackFit.expand,
443+
children: [
444+
ExcludeFocus(
445+
excluding: pairingState.status == PairingStatus.connecting,
446+
child: Offstage(
447+
offstage: pairingState.status == PairingStatus.connecting,
448+
child: pairingScaffold,
449+
),
450+
),
451+
if (pairingState.status == PairingStatus.connecting)
452+
const CommunityLoadingSurface(
453+
key: Key('pairing-community-loading'),
454+
),
455+
if (pairingState.status == PairingStatus.connecting)
456+
SafeArea(
457+
child: Align(
458+
alignment: Alignment.topLeft,
459+
child: Material(
460+
color: Colors.transparent,
461+
child: TextButton(
462+
onPressed: () {
463+
ref.read(pairingProvider.notifier).reset();
464+
if (identityRecoveryOnly) {
465+
Navigator.of(context).pop();
466+
}
467+
},
468+
child: const Text('Cancel'),
469+
),
470+
),
471+
),
472+
),
473+
],
474+
),
441475
);
442476

443477
if (!fallbackScannerVisible.value) {

‎mobile/lib/features/pairing/pairing_provider.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ class PairingNotifier extends Notifier<PairingState> {
887887
await validator(relayUrl: community.relayUrl, nsec: community.nsec);
888888

889889
if (generation != _pairingGeneration) return;
890+
state = const PairingState(status: PairingStatus.storing);
890891
await ref
891892
.read(authProvider.notifier)
892893
.authenticateWithCommunity(

‎mobile/lib/features/pairing/pairing_socket.dart‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ class PairingSocket {
6666
}
6767
final channel = _channelFactory(Uri.parse(_wsUrl));
6868
_channel = channel;
69-
await Future.any([channel.ready, _closed.future]);
69+
try {
70+
await Future.any([
71+
channel.ready,
72+
_closed.future,
73+
]).timeout(const Duration(seconds: 10));
74+
} catch (_) {
75+
dispose();
76+
rethrow;
77+
}
7078
if (_closed.isCompleted) {
7179
throw const PairingAuthException('Pairing connection cancelled');
7280
}

‎mobile/lib/onboarding_app_entry.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ class OnboardingAppEntry extends HookConsumerWidget {
151151

152152
final handlingBack = useRef(false);
153153
void back() {
154+
final pairingStatus = ref.read(pairingProvider).status;
155+
if (pairingStatus == PairingStatus.transferring ||
156+
pairingStatus == PairingStatus.storing ||
157+
pairingStatus == PairingStatus.success) {
158+
return;
159+
}
154160
// Nested page and entry PopScopes receive the same system Back event.
155161
if (handlingBack.value) return;
156162
handlingBack.value = true;

‎mobile/test/features/pairing/pairing_page_test.dart‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ void main() {
486486
expect(find.text('Connect'), findsNothing);
487487
});
488488

489-
testWidgets('pairing actions are disabled when connecting', (tester) async {
489+
testWidgets('connection progress replaces pairing actions', (tester) async {
490490
await tester.pumpWidget(
491491
WidgetHelpers.testable(
492492
overrides: [
@@ -497,13 +497,44 @@ void main() {
497497
);
498498
await tester.pump();
499499

500-
final scanButton = tester.widget<FilledButton>(find.byType(FilledButton));
501-
final pairingCodeButton = tester.widget<TextButton>(
502-
find.widgetWithText(TextButton, 'Use pairing code'),
500+
expect(
501+
find.byKey(const Key('pairing-community-loading')),
502+
findsOneWidget,
503503
);
504+
expect(find.text('Scan a QR code'), findsNothing);
505+
expect(find.text('Use pairing code'), findsNothing);
506+
});
504507

505-
expect(scanButton.onPressed, isNull);
506-
expect(pairingCodeButton.onPressed, isNull);
508+
testWidgets('connection Cancel closes the identity recovery route', (
509+
tester,
510+
) async {
511+
final navigator = GlobalKey<NavigatorState>();
512+
await tester.pumpWidget(
513+
ProviderScope(
514+
overrides: [
515+
pairingProvider.overrideWith(() => _ConnectingPairingNotifier()),
516+
],
517+
child: MaterialApp(
518+
navigatorKey: navigator,
519+
theme: AppTheme.light(),
520+
home: const Scaffold(body: Text('Settings')),
521+
),
522+
),
523+
);
524+
navigator.currentState!.push(
525+
MaterialPageRoute<void>(
526+
builder: (_) => const PairingPage(
527+
addingCommunity: true,
528+
identityRecoveryOnly: true,
529+
),
530+
),
531+
);
532+
await tester.pump();
533+
await tester.pump(const Duration(milliseconds: 400));
534+
await tester.tap(find.widgetWithText(TextButton, 'Cancel'));
535+
await tester.pumpAndSettle();
536+
expect(find.byType(PairingPage), findsNothing);
537+
expect(find.text('Settings'), findsOneWidget);
507538
});
508539

509540
testWidgets('recovery entry rejects ordinary nostrpair codes', (

‎mobile/test/features/pairing/pairing_provider_test.dart‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import '../../helpers/controllable_pairing_socket.dart';
22
import 'package:buzz/features/pairing/pairing_page.dart';
3+
import 'package:buzz/onboarding_app_entry.dart';
34
import 'package:buzz/shared/theme/theme.dart';
45
import 'package:flutter/material.dart';
56
import 'package:buzz/shared/community/paired_community_landing.dart';
@@ -110,6 +111,65 @@ void main() {
110111
},
111112
);
112113

114+
for (final entry in ['pairing', 'add', 'inline']) {
115+
testWidgets('legacy storage blocks Back from $entry', (tester) async {
116+
final auth = FakeAuthNotifier()
117+
..entered = Completer<void>()
118+
..authentication = Completer<void>();
119+
final notifier = PairingNotifier(
120+
credentialValidator: ({required relayUrl, required nsec}) async {},
121+
);
122+
container = ProviderContainer(
123+
overrides: [
124+
pairingProvider.overrideWith(() => notifier),
125+
authProvider.overrideWith(() => auth),
126+
],
127+
);
128+
var exits = 0;
129+
final navigator = GlobalKey<NavigatorState>();
130+
await tester.pumpWidget(
131+
UncontrolledProviderScope(
132+
container: container,
133+
child: MaterialApp(
134+
theme: AppTheme.light(),
135+
navigatorKey: navigator,
136+
home: const Scaffold(body: Text('Previous page')),
137+
),
138+
),
139+
);
140+
navigator.currentState!.push(
141+
MaterialPageRoute<void>(
142+
builder: (_) => entry != 'pairing'
143+
? OnboardingAppEntry(
144+
addingCommunity: true,
145+
restoreAccount: false,
146+
onExit: entry == 'inline' ? () => exits++ : null,
147+
)
148+
: const PairingPage(),
149+
),
150+
);
151+
await tester.pumpAndSettle();
152+
final pending = notifier.pair(
153+
_encodePairingCode(nsec: nostr.Keys.generate().nsec),
154+
);
155+
await auth.entered!.future;
156+
await tester.pump();
157+
expect(container.read(pairingProvider).status, PairingStatus.storing);
158+
expect(find.text('Cancel'), findsNothing);
159+
expect(await navigator.currentState!.maybePop(), isTrue);
160+
await tester.pump();
161+
await tester.binding.handlePopRoute();
162+
await tester.pump();
163+
expect(find.byType(PairingPage), findsOneWidget);
164+
expect(exits, 0);
165+
expect(container.read(pairingProvider).status, PairingStatus.storing);
166+
auth.authentication!.complete();
167+
await pending;
168+
expect(container.read(pairingProvider).status, PairingStatus.success);
169+
await tester.pumpWidget(const SizedBox.shrink());
170+
});
171+
}
172+
113173
test(
114174
'organization relays are admitted by their session, not pre-validated',
115175
() async {

‎mobile/test/features/pairing/pairing_qr_scanner_test.dart‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
import 'dart:io';
2+
3+
import 'package:buzz/features/pairing/pairing_provider.dart';
4+
5+
import '../../helpers/controllable_pairing_socket.dart';
6+
17
import 'dart:async';
28

39
import 'package:buzz/features/pairing/pairing_qr_scanner.dart';
410
import 'package:buzz/features/pairing/pairing_page.dart';
11+
512
import '../../helpers/widget_helpers.dart';
13+
614
import 'package:flutter/foundation.dart';
715
import 'package:flutter/material.dart';
816
import 'package:flutter/services.dart';
@@ -29,6 +37,115 @@ void main() {
2937
MobileScannerPlatform.instance = defaultScannerPlatform;
3038
});
3139

40+
for (final (paste, cancel) in [
41+
(false, false),
42+
(true, false),
43+
(false, true),
44+
(true, true),
45+
]) {
46+
testWidgets('pairing entry paste=$paste cancel=$cancel permits retry', (
47+
tester,
48+
) async {
49+
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
50+
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
51+
.setMockMethodCallHandler(
52+
_qrScannerPlatformChannel,
53+
(call) async =>
54+
call.method == 'usesDynamicIslandQrScannerPortal' ? true : null,
55+
);
56+
final readiness = Completer<void>();
57+
final notifier = PairingNotifier(
58+
socketFactory:
59+
({
60+
required wsUrl,
61+
required ephemeralPrivkey,
62+
required onMessage,
63+
required onDisconnected,
64+
}) => _PendingPairingSocket(
65+
readiness.future,
66+
ephemeralPrivkey: ephemeralPrivkey,
67+
onMessage: onMessage,
68+
onDisconnected: onDisconnected,
69+
),
70+
);
71+
await tester.pumpWidget(
72+
WidgetHelpers.testable(
73+
disableAnimations: true,
74+
overrides: [pairingProvider.overrideWith(() => notifier)],
75+
child: PairingPage(
76+
addingCommunity: paste,
77+
existingUser: paste,
78+
connectionLanding: true,
79+
showConnectionOptions: false,
80+
),
81+
),
82+
);
83+
const code =
84+
'nostrpair://79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'
85+
'?secret=abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789'
86+
'&relay=wss%3A%2F%2Fpairing.example&v=1';
87+
if (paste) {
88+
await tester.enterText(
89+
find.byKey(const Key('community-pairing-input')),
90+
code,
91+
);
92+
await tester.tap(find.text('Connect'));
93+
} else {
94+
await tester.tap(find.text('Scan a QR code'));
95+
await tester.pump();
96+
await tester.pump();
97+
fakeScannerPlatform._barcodes.add(
98+
BarcodeCapture(barcodes: [Barcode(rawValue: code)]),
99+
);
100+
}
101+
await tester.pump();
102+
await tester.pump();
103+
await tester.pump();
104+
expect(
105+
find.byKey(const Key('pairing-community-loading')),
106+
findsOneWidget,
107+
);
108+
expect(find.text('Scan a QR code'), findsNothing);
109+
if (cancel) {
110+
await tester.tap(find.text('Cancel'));
111+
await tester.pump();
112+
expect(
113+
find.byKey(const Key('pairing-community-loading')),
114+
findsNothing,
115+
);
116+
}
117+
readiness.completeError(const SocketException('Network is unreachable'));
118+
await tester.pump();
119+
await tester.pump();
120+
expect(
121+
find.textContaining('Could not reach the pairing relay'),
122+
cancel ? findsNothing : findsOneWidget,
123+
);
124+
if (paste) {
125+
expect(
126+
tester
127+
.widget<TextField>(
128+
find.byKey(const Key('community-pairing-input')),
129+
)
130+
.controller!
131+
.text,
132+
code,
133+
);
134+
expect(
135+
tester
136+
.widget<FilledButton>(
137+
find.widgetWithText(FilledButton, 'Connect'),
138+
)
139+
.onPressed,
140+
isNotNull,
141+
);
142+
} else {
143+
expect(find.text('Scan a QR code'), findsOneWidget);
144+
}
145+
debugDefaultTargetPlatformOverride = null;
146+
});
147+
}
148+
32149
group('DynamicIslandQrScannerGeometry', () {
33150
const viewport = Size(393, 852);
34151
const geometry = DynamicIslandQrScannerGeometry(
@@ -582,3 +699,18 @@ class _FakeMobileScannerPlatform extends MobileScannerPlatform {
582699
await _barcodes.close();
583700
}
584701
}
702+
703+
class _PendingPairingSocket extends ControllablePairingSocket {
704+
_PendingPairingSocket(
705+
this.readiness, {
706+
required super.ephemeralPrivkey,
707+
required super.onMessage,
708+
required super.onDisconnected,
709+
});
710+
final Future<void> readiness;
711+
@override
712+
Future<void> connect() async {
713+
await readiness;
714+
await super.connect();
715+
}
716+
}

‎mobile/test/features/pairing/pairing_socket_test.dart‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ void main() {
4343
});
4444
}
4545

46+
testWidgets('a stalled upgrade stops within ten seconds', (tester) async {
47+
final readiness = Completer<void>();
48+
final channel = _ControlledWebSocketChannel(readiness: readiness.future);
49+
final socket = _socket('ws://unused', channelFactory: (_) => channel);
50+
Object? failure;
51+
final connection = socket.connect().then<void>(
52+
(_) {},
53+
onError: (Object error) {
54+
failure = error;
55+
},
56+
);
57+
try {
58+
await tester.pump(const Duration(seconds: 11));
59+
expect(failure, isA<TimeoutException>());
60+
expect(socket.isConnected, isFalse);
61+
} finally {
62+
socket.dispose();
63+
readiness.complete();
64+
await connection;
65+
}
66+
});
67+
4668
test('connects when the pairing relay sends no AUTH challenge', () async {
4769
final server = await _TestRelay.start((_) {});
4870
addTearDown(server.close);

0 commit comments

Comments
 (0)