Files
buzz/mobile/test/helpers/hosted_communities_fixture.dart
John Matthew Tennant 33eaa73c83 fix(mobile): let users sign out after leaving all communities (#8993)
## Summary
Fixes a dead-end in signed-in mobile onboarding: after leaving every
community, Back kept returning to the same Add a community screen
without a way to end the account session.

Show “Signed in as [email]” on that screen. Back asks “Sign out of
BuilderLab?” with the email; Cancel keeps the session, and Sign out
returns to the welcome screen while retaining saved Buzz identities.
Nested Back and Add community from an existing session keep their
navigation behavior.

### Related issue
None found in the existing issue and PR search for onboarding sign-out.

### Testing
The new widget journey failed before the change and passes through the
real onboarding widgets, account client, storage and flow with external
HTTP/browser boundaries mocked. It covers nested Back, Cancel, system
Back, durable session removal, retained identity/backup, and starting
email sign-in again. The existing hosted onboarding journey also covers
accounts without an email.

On iPhone 18 Pro simulator (iOS 27), rendered the production onboarding
screen and native confirmation using a disposable account fixture. Live
browser account selection has not been verified; this clears the app
session, not website cookies.

To reproduce on an auth-enabled mobile build: sign in, leave all
communities, check the displayed email, tap Back, and choose Cancel. Tap
Back again and choose Sign out. The yellow welcome screen should appear,
with email sign-in available.

## Screenshots

Before, from the original reported phone screen:

<img
src="https://raw.githubusercontent.com/block/buzz/07440b3b10c3f48a6fd19c923f2ee5ba66d574f0/pr-8993--before.png"
alt="Before: account state not shown" width="300">

After:

Signed-in landing and native sign-out confirmation, using a disposable
account:

<img
src="https://raw.githubusercontent.com/block/buzz/a4e18c85bc3a19d971b08ff595ba6b081c67e20f/pr-8993--landing.png"
alt="Signed-in landing" width="300"> <img
src="https://raw.githubusercontent.com/block/buzz/a4e18c85bc3a19d971b08ff595ba6b081c67e20f/pr-8993--confirmation.png"
alt="Sign-out confirmation" width="300">

---------

Signed-off-by: Aaron <f425b86c3b780d43f8d9cf1802429d04dd985bf92eac44fca9468b5ad3887c5e@buzz.block.builderlab.xyz>
Signed-off-by: John Tennant <jtennant@squareup.com>
Co-authored-by: Aaron <f425b86c3b780d43f8d9cf1802429d04dd985bf92eac44fca9468b5ad3887c5e@buzz.block.builderlab.xyz>
2026-11-03 18:25:54 +00:00

201 lines
5.9 KiB
Dart

import 'dart:convert';
import 'dart:async';
import 'package:buzz/shared/deeplink/deep_link.dart';
import 'package:buzz/shared/hosted/account_identity_store.dart';
import 'package:buzz/shared/hosted/browser_auth.dart';
import 'package:buzz/shared/hosted/builderlab_account.dart';
import 'package:buzz/shared/hosted/hosted_flow.dart';
import 'package:buzz/shared/hosted/hosted_identity_service.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:nostr/nostr.dart' as nostr;
class DiscoveryBrowser extends BrowserAuth {
final callbacks = StreamController<BrowserAuthCode>.broadcast();
Map<String, String>? startedIntent;
@override
Future<void> start(Map<String, String> intent) async {
startedIntent = intent;
}
@override
Stream<BrowserAuthCode> get codes => callbacks.stream;
@override
Future<void> dispose() async {
await callbacks.close();
await super.dispose();
}
@override
Future<void> initialize() async {}
@override
Future<void> clearIntent() async {}
@override
Future<void> cancel() async {}
}
class WidgetDiscoveryAccount extends BuilderlabAccountClient {
WidgetDiscoveryAccount(this.fixture);
final HostedCommunitiesFixture fixture;
@override
BuilderlabAccount? get account => fixture.expired
? null
: BuilderlabAccount(
subject: fixture.subject,
expiresAt: DateTime.now().add(const Duration(hours: 1)),
);
@override
Future<BuilderlabAccount?> restore() async => account;
@override
Future<void> rememberCommunity(
String relay, {
required String expectedSubject,
}) async {
if (account?.subject != expectedSubject) throw AccountFailure.denied;
fixture.remembered.add(relay);
}
@override
Future<Map<String, dynamic>> post(
String path,
Map<String, dynamic> body, {
String? expectedSubject,
}) async {
fixture.requests.add(path);
if (path.endsWith('/current')) {
if (fixture.rejectCurrent) throw AccountFailure.denied;
return {
'identity': {'pubkey_hex': fixture.bound},
};
}
if (path.endsWith('/known-communities/list')) {
return {'communities': fixture.rows};
}
throw AccountFailure.unavailable;
}
}
class HostedCommunitiesFixture {
final keys = nostr.Keys('1'.padLeft(64, '0'));
AccountIdentityStore get store => AccountIdentityStore(subject: subject);
final browser = DiscoveryBrowser();
late BuilderlabAccountClient account;
late final HostedFlow flow;
final admitted = <String>[];
String? openedCommunity;
Future<void> Function()? beforeOpen;
final remembered = <String>[];
final requests = <String>[];
List<Map<String, dynamic>> rows = [];
String? bound;
bool rejectList = false;
bool rejectCurrent = false;
bool expired = false;
String subject = 'owner';
Future<void> Function(String)? beforeAdmission;
Future<void> Function()? beforeList;
Uri? opened;
Map<String, dynamic> row(String name) => {
'community_url': 'wss://$name.communities.buzz.xyz',
};
Future<void> initialize({bool backup = true, bool widget = false}) async {
bound = keys.public;
final expiry = DateTime.now()
.toUtc()
.add(const Duration(hours: 1))
.toIso8601String();
account = BuilderlabAccountClient(
httpClient: MockClient((request) async {
final path = request.url.path;
requests.add(path);
if (path.endsWith('/exchange')) {
return http.Response(
jsonEncode({'session_credential': 'fixture', 'expires_at': expiry}),
200,
);
}
if (path.endsWith('/me')) {
if (expired) return http.Response('{}', 401);
return http.Response(
jsonEncode({
'subject': subject,
'email': 'owner@example.com',
'expires_at': expiry,
}),
200,
);
}
if (path.endsWith('/current')) {
return http.Response(
jsonEncode({
'identity': {'pubkey_hex': bound},
}),
200,
);
}
if (path.endsWith('/known-communities/list')) {
await beforeList?.call();
if (rejectList) return http.Response('{}', 503);
return http.Response(jsonEncode({'communities': rows}), 200);
}
if (path.endsWith('/known-communities/add')) {
final body = jsonDecode(request.body) as Map<String, dynamic>;
return http.Response(
jsonEncode({
'community': {'community_url': body['community_url']},
}),
200,
);
}
return http.Response('{}', 503);
}),
);
if (widget) {
account.dispose();
account = WidgetDiscoveryAccount(this);
} else {
await account.exchange(code: 'code', verifier: 'a' * 43);
}
await store.create(keys.nsec);
if (backup) {
await store.confirmBackup(
BackupReceipt(
backend: BuilderlabAccountClient.origin.toString(),
subject: 'owner',
pubkey: keys.public,
backupId: 'fixture-backup',
),
);
}
flow = HostedFlow(
account: account,
browser: browser,
store: store,
identity: HostedIdentityService(account: account, store: store),
openWeb: (uri) async {
opened = uri;
return true;
},
openLandingCommunity: (link, subject, pubkey, current) async {
await beforeOpen?.call();
if (current()) openedCommunity = link.relayUrl;
},
enterCommunity: (link, subject, pubkey, current) async {
// Record the relay host: names are display labels.
final host = Uri.parse((link as AddCommunityDeepLink).relayUrl).host;
await beforeAdmission?.call(host);
if (current()) admitted.add(host);
},
);
}
Future<void> dispose() async {
flow.dispose();
await browser.dispose();
account.dispose();
}
}