From 636115bb2275678486a777b092bcdabb4251cf31 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 23 Oct 2026 23:52:34 +0100 Subject: [PATCH] feat(app): phone onboarding (server, then password) kept across launches; a known server opens straight into the app; a refused session asks for the password --- app/lib/application/app_session.dart | 16 +- app/lib/application/bootstrap.dart | 22 +- app/lib/application/pipelines_app.dart | 10 +- app/lib/application/pipelines_root.dart | 108 +++++++ app/lib/core/session/server_session.dart | 15 + app/lib/core/session/server_session_io.dart | 19 ++ app/lib/core/session/server_session_stub.dart | 3 + .../features/onboarding/onboarding_flow.dart | 274 ++++++++++++++++++ .../app_session_known_server_test.dart | 13 + app/test/application/bootstrap_test.dart | 40 ++- app/test/application/pipelines_root_test.dart | 61 ++++ .../onboarding/onboarding_flow_test.dart | 102 +++++++ 12 files changed, 650 insertions(+), 33 deletions(-) create mode 100644 app/lib/application/pipelines_root.dart create mode 100644 app/lib/features/onboarding/onboarding_flow.dart create mode 100644 app/test/application/app_session_known_server_test.dart create mode 100644 app/test/application/pipelines_root_test.dart create mode 100644 app/test/features/onboarding/onboarding_flow_test.dart diff --git a/app/lib/application/app_session.dart b/app/lib/application/app_session.dart index 39a48df8..0c2d31ff 100644 --- a/app/lib/application/app_session.dart +++ b/app/lib/application/app_session.dart @@ -18,9 +18,16 @@ enum RunSubscriptionState { pending, live, refused } /// `subscribe` on every connected transition because a new socket has no /// watch, and drops it deliberately with `unsubscribe` when the app stops. class AppSession extends ChangeNotifier { - AppSession({required Uri? endpoint, PipelinesClient? client}) - : _initialEndpoint = endpoint, - _client = client ?? PipelinesClient() { + /// [knownServer]: this device has signed in to [endpoint] before, so the + /// app opens straight away and reconnects behind a banner instead of the + /// full connection screen. + AppSession({ + required Uri? endpoint, + PipelinesClient? client, + bool knownServer = false, + }) : _initialEndpoint = endpoint, + _client = client ?? PipelinesClient(), + _everConnected = knownServer { _states = _client.states.listen(_onState); _frames = _client.frames.listen(_onFrame); } @@ -30,7 +37,8 @@ class AppSession extends ChangeNotifier { late final StreamSubscription _states; late final StreamSubscription _frames; ConnectionState _connection = const ConnectionState(ConnectionKind.idle); - bool _subscribed = false, _disposed = false, _everConnected = false; + bool _subscribed = false, _disposed = false; + bool _everConnected; int _connectionEpoch = 0; RunSubscriptionState _subscription = RunSubscriptionState.pending; String _subscriptionError = ''; diff --git a/app/lib/application/bootstrap.dart b/app/lib/application/bootstrap.dart index 8e9a2bbd..f5d1283c 100644 --- a/app/lib/application/bootstrap.dart +++ b/app/lib/application/bootstrap.dart @@ -4,14 +4,15 @@ import 'package:flutter/widgets.dart'; import 'app_url_strategy.dart'; import 'pipelines_app.dart'; +import 'pipelines_root.dart'; import 'server_endpoint.dart'; import '../features/settings/settings_store.dart'; /// Native builds must supply their server explicitly; there is no baked host. const configuredServerUrl = String.fromEnvironment('PIPELINES_SERVER_URL'); -/// Resolves the launch endpoint, loading the last saved native endpoint only -/// when no build-time endpoint was supplied. +/// Resolves the launch endpoint: on a phone the server saved at onboarding, +/// else the build's default (which only pre-fills onboarding until saved). Future resolveLaunchServer({ Uri? base, String configured = configuredServerUrl, @@ -20,14 +21,15 @@ Future resolveLaunchServer({ }) async { WidgetsFlutterBinding.ensureInitialized(); var endpoint = configured; - if (!web && endpoint.trim().isEmpty) { + if (!web) { try { - endpoint = + final saved = (await (settingsStore ?? const SharedPreferencesSettingsStore()) .load()) .serverUrl; + if (saved.trim().isNotEmpty) endpoint = saved; } catch (_) { - endpoint = ''; + // Keep the build's default. } } return ServerEndpoint.resolve( @@ -52,5 +54,13 @@ Future runPipelinesApp({ configured: configured, settingsStore: settingsStore, ); - runApp(PipelinesApp(server: server)); + runApp( + kIsWeb + ? PipelinesApp(server: server) + : PipelinesRoot( + server: server, + buildDefault: configured, + signedIn: ServerSession.cookie != null, + ), + ); } diff --git a/app/lib/application/pipelines_app.dart b/app/lib/application/pipelines_app.dart index 26cc6e3f..b79af92d 100644 --- a/app/lib/application/pipelines_app.dart +++ b/app/lib/application/pipelines_app.dart @@ -48,6 +48,7 @@ class PipelinesApp extends StatefulWidget { this.client, this.cupertino, this.routeInformationProvider, + this.knownServer = false, }); final ServerEndpoint server; @@ -57,6 +58,9 @@ class PipelinesApp extends StatefulWidget { /// Test seam: the Router's initial and subsequent route information. final RouteInformationProvider? routeInformationProvider; + /// The device has signed in to [server] before (see [AppSession]). + final bool knownServer; + @override State createState() => _PipelinesAppState(); } @@ -115,7 +119,11 @@ class _PipelinesAppState extends State void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); - _session = AppSession(endpoint: widget.server.uri, client: widget.client); + _session = AppSession( + endpoint: widget.server.uri, + client: widget.client, + knownServer: widget.knownServer, + ); _ownsProvider = widget.routeInformationProvider == null; _provider = widget.routeInformationProvider ?? diff --git a/app/lib/application/pipelines_root.dart b/app/lib/application/pipelines_root.dart new file mode 100644 index 00000000..572b6184 --- /dev/null +++ b/app/lib/application/pipelines_root.dart @@ -0,0 +1,108 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; + +import '../core/session/server_session.dart'; +import '../features/onboarding/onboarding_flow.dart'; +import '../features/settings/settings_store.dart'; +import '../ui/theme/app_theme.dart'; +import 'pipelines_app.dart'; +import 'server_endpoint.dart'; + +/// The phone app's root: onboarding until a server and its password are +/// known, then the app. A saved session the server later refuses brings the +/// password step back; an unreachable server just reconnects in the app. +class PipelinesRoot extends StatefulWidget { + const PipelinesRoot({ + super.key, + required this.server, + this.buildDefault = '', + this.signedIn = false, + this.check = ServerSession.check, + this.settingsStore = const SharedPreferencesSettingsStore(), + }); + + final ServerEndpoint server; + + /// The server this build was made for; it pre-fills onboarding. + final String buildDefault; + + /// A session is kept on this device. + final bool signedIn; + final ServerCheck check; + final SettingsStore settingsStore; + + @override + State createState() => _PipelinesRootState(); +} + +class _PipelinesRootState extends State { + late ServerEndpoint _server = widget.server; + late bool _onboarding = widget.server.uri == null || !widget.signedIn; + bool _refused = false; + int _generation = 0; + + @override + void initState() { + super.initState(); + if (!_onboarding) _verify(widget.server.uri!); + } + + /// A kept session the server now refuses asks for the password again. + Future _verify(Uri endpoint) async { + final state = await widget.check(endpoint); + if (!mounted || state != SessionState.signedOut) return; + setState(() { + _refused = true; + _onboarding = true; + }); + } + + void _done(Uri endpoint) { + setState(() { + _server = ServerEndpoint.resolve( + base: Uri.base, + web: false, + configured: endpoint.toString(), + ); + _onboarding = false; + _refused = false; + _generation++; + }); + } + + bool get _cupertino => + defaultTargetPlatform == TargetPlatform.iOS || + defaultTargetPlatform == TargetPlatform.macOS; + + @override + Widget build(BuildContext context) { + if (!_onboarding) { + return PipelinesApp( + key: ValueKey(_generation), + server: _server, + knownServer: true, + ); + } + final flow = OnboardingFlow( + initialServer: _server.uri?.toString() ?? widget.buildDefault, + startAtPassword: _refused, + saveServer: widget.settingsStore.saveServerUrl, + onDone: _done, + ); + return _cupertino + ? CupertinoApp( + title: 'Pipelines', + theme: appCupertinoTheme, + debugShowCheckedModeBanner: false, + home: flow, + ) + : MaterialApp( + title: 'Pipelines', + theme: appLightTheme(), + darkTheme: appDarkTheme(), + debugShowCheckedModeBanner: false, + home: flow, + ); + } +} diff --git a/app/lib/core/session/server_session.dart b/app/lib/core/session/server_session.dart index 008d4507..797c4250 100644 --- a/app/lib/core/session/server_session.dart +++ b/app/lib/core/session/server_session.dart @@ -61,8 +61,23 @@ class ServerSession { } static Future signOut() => _save(null); + + /// Whether [endpoint]'s server accepts the kept session. + static Future check(Uri endpoint) async { + final login = loginUriFor(endpoint); + if (login == null) return SessionState.unreachable; + final status = await impl.probeLogin(login, headers); + if (status == null) return SessionState.unreachable; + // The web login sends a signed-in visitor on; it shows its form otherwise. + if (status >= 300 && status < 400) return SessionState.signedIn; + if (status == 200) return SessionState.signedOut; + return SessionState.unreachable; + } } +/// What a server says about this device's session. +enum SessionState { signedIn, signedOut, unreachable } + /// The web login page for a websocket endpoint: same host and port, http(s). Uri? loginUriFor(Uri endpoint) { final scheme = switch (endpoint.scheme) { diff --git a/app/lib/core/session/server_session_io.dart b/app/lib/core/session/server_session_io.dart index 5d7e3885..383057c6 100644 --- a/app/lib/core/session/server_session_io.dart +++ b/app/lib/core/session/server_session_io.dart @@ -40,3 +40,22 @@ Future<({String? cookie, String? error})> postLogin( client.close(force: true); } } + +/// The status the web login answers with this session: a redirect away from +/// the login page when signed in, the page itself when not; null when the +/// server cannot be reached. +Future probeLogin(Uri login, Map headers) async { + final client = HttpClient()..connectionTimeout = const Duration(seconds: 8); + try { + final request = await client.getUrl(login); + request.followRedirects = false; + headers.forEach(request.headers.set); + final response = await request.close().timeout(const Duration(seconds: 10)); + await response.drain(); + return response.statusCode; + } on Exception { + return null; + } finally { + client.close(force: true); + } +} diff --git a/app/lib/core/session/server_session_stub.dart b/app/lib/core/session/server_session_stub.dart index 5f437a99..10f3b06e 100644 --- a/app/lib/core/session/server_session_stub.dart +++ b/app/lib/core/session/server_session_stub.dart @@ -3,3 +3,6 @@ Future<({String? cookie, String? error})> postLogin( Uri login, String password, ) async => (cookie: null, error: 'Sign in through the browser page.'); + +/// A browser page is already behind its own login. +Future probeLogin(Uri login, Map headers) async => 302; diff --git a/app/lib/features/onboarding/onboarding_flow.dart b/app/lib/features/onboarding/onboarding_flow.dart new file mode 100644 index 00000000..a8f4d7ba --- /dev/null +++ b/app/lib/features/onboarding/onboarding_flow.dart @@ -0,0 +1,274 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../core/session/server_session.dart'; +import '../../core/transport/server_url.dart'; +import '../../ui/adaptive/host.dart'; + +/// Reaches a server and reports what it says about this device's session. +typedef ServerCheck = Future Function(Uri endpoint); + +/// Signs in with the web password; null on success, else the reason. +typedef ServerSignIn = Future Function(Uri endpoint, String password); + +/// The phone's first run: which server, then its password. Both are kept, so +/// later launches open the app directly. +class OnboardingFlow extends StatefulWidget { + const OnboardingFlow({ + super.key, + required this.onDone, + this.initialServer = '', + this.startAtPassword = false, + this.check = ServerSession.check, + this.signIn = ServerSession.signIn, + this.saveServer, + }); + + /// Called with the signed-in server's endpoint. + final ValueChanged onDone; + + /// Pre-filled server address (the saved one, or the build's default). + final String initialServer; + + /// The saved session was refused: only the password is asked again. + final bool startAtPassword; + final ServerCheck check; + final ServerSignIn signIn; + final Future Function(String url)? saveServer; + + @override + State createState() => _OnboardingFlowState(); +} + +class _OnboardingFlowState extends State { + late final TextEditingController _server = TextEditingController( + text: widget.initialServer, + ); + final TextEditingController _password = TextEditingController(); + late bool _passwordStep = widget.startAtPassword; + Uri? _endpoint; + bool _busy = false; + String? _error; + + @override + void initState() { + super.initState(); + if (widget.startAtPassword) _endpoint = _parse(widget.initialServer); + } + + @override + void dispose() { + _server.dispose(); + _password.dispose(); + super.dispose(); + } + + Uri? _parse(String text) { + try { + return Uri.parse(ServerUrl.validate(text).wsUrl); + } on FormatException { + return null; + } + } + + Future _continue() async { + String? problem; + Uri? endpoint; + try { + endpoint = Uri.parse(ServerUrl.validate(_server.text).wsUrl); + } on FormatException catch (error) { + problem = error.message; + } + if (endpoint == null) { + setState(() => _error = problem); + return; + } + setState(() { + _busy = true; + _error = null; + }); + final state = await widget.check(endpoint); + if (!mounted) return; + if (state == SessionState.unreachable) { + setState(() { + _busy = false; + _error = 'Could not reach that server. Check the address and network.'; + }); + return; + } + await widget.saveServer?.call(endpoint.toString()); + if (!mounted) return; + if (state == SessionState.signedIn) { + widget.onDone(endpoint); + return; + } + setState(() { + _busy = false; + _endpoint = endpoint; + _passwordStep = true; + }); + } + + Future _signIn() async { + final endpoint = _endpoint; + if (endpoint == null || _password.text.isEmpty) return; + setState(() { + _busy = true; + _error = null; + }); + final error = await widget.signIn(endpoint, _password.text); + if (!mounted) return; + if (error != null) { + setState(() { + _busy = false; + _error = error; + }); + return; + } + widget.onDone(endpoint); + } + + void _back() => setState(() { + _passwordStep = false; + _error = null; + _password.clear(); + }); + + @override + Widget build(BuildContext context) { + final cupertino = usesCupertinoHost(context); + final title = _passwordStep ? 'Sign in' : 'Connect to Pipelines'; + final lead = _passwordStep + ? 'Enter the password chosen when Pipelines was installed on ' + '${_endpoint?.host ?? 'the server'}.' + : 'Enter the address of your Pipelines server, for example ' + 'wss://server.example/connect.'; + final field = _passwordStep + ? _field( + cupertino, + key: const Key('onboarding-password'), + controller: _password, + hint: 'Password', + obscure: true, + onSubmitted: _signIn, + ) + : _field( + cupertino, + key: const Key('onboarding-server'), + controller: _server, + hint: 'wss://server/connect', + keyboard: TextInputType.url, + onSubmitted: _continue, + ); + final action = _passwordStep ? _signIn : _continue; + final label = _passwordStep ? 'Sign in' : 'Continue'; + final button = cupertino + ? CupertinoButton.filled( + key: const Key('onboarding-next'), + onPressed: _busy ? null : action, + child: _busy ? const CupertinoActivityIndicator() : Text(label), + ) + : FilledButton( + key: const Key('onboarding-next'), + onPressed: _busy ? null : action, + child: _busy + ? const SizedBox( + width: 18, + height: 18, + child: CircularProgressIndicator(strokeWidth: 2), + ) + : Text(label), + ); + final errorColor = cupertino + ? CupertinoColors.systemRed.resolveFrom(context) + : Theme.of(context).colorScheme.error; + final body = SafeArea( + child: Center( + child: SingleChildScrollView( + padding: const EdgeInsets.all(24), + child: ConstrainedBox( + constraints: const BoxConstraints(maxWidth: 420), + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Text( + title, + style: cupertino + ? CupertinoTheme.of( + context, + ).textTheme.navLargeTitleTextStyle + : Theme.of(context).textTheme.headlineSmall, + ), + const SizedBox(height: 8), + Text(lead), + const SizedBox(height: 24), + field, + if (_error != null) ...[ + const SizedBox(height: 8), + Text( + _error!, + key: const Key('onboarding-error'), + style: TextStyle(color: errorColor), + ), + ], + const SizedBox(height: 16), + button, + if (_passwordStep && !widget.startAtPassword) ...[ + const SizedBox(height: 8), + cupertino + ? CupertinoButton( + onPressed: _busy ? null : _back, + child: const Text('Change server'), + ) + : TextButton( + onPressed: _busy ? null : _back, + child: const Text('Change server'), + ), + ], + ], + ), + ), + ), + ), + ); + return cupertino + ? CupertinoPageScaffold(child: body) + : Scaffold(body: body); + } + + Widget _field( + bool cupertino, { + required Key key, + required TextEditingController controller, + required String hint, + required VoidCallback onSubmitted, + bool obscure = false, + TextInputType? keyboard, + }) { + if (cupertino) { + return CupertinoTextField( + key: key, + controller: controller, + placeholder: hint, + obscureText: obscure, + autocorrect: false, + keyboardType: keyboard, + autofocus: true, + onSubmitted: (_) => onSubmitted(), + ); + } + return TextField( + key: key, + controller: controller, + obscureText: obscure, + autocorrect: false, + keyboardType: keyboard, + autofocus: true, + decoration: InputDecoration( + labelText: hint, + border: const OutlineInputBorder(), + ), + onSubmitted: (_) => onSubmitted(), + ); + } +} diff --git a/app/test/application/app_session_known_server_test.dart b/app/test/application/app_session_known_server_test.dart new file mode 100644 index 00000000..e5d4be48 --- /dev/null +++ b/app/test/application/app_session_known_server_test.dart @@ -0,0 +1,13 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/application/app_session.dart'; + +void main() { + test('a known server opens the app instead of the connection screen', () { + final fresh = AppSession(endpoint: null); + final known = AppSession(endpoint: null, knownServer: true); + addTearDown(fresh.dispose); + addTearDown(known.dispose); + expect(fresh.everConnected, isFalse); + expect(known.everConnected, isTrue); + }); +} diff --git a/app/test/application/bootstrap_test.dart b/app/test/application/bootstrap_test.dart index 68b1b714..58598436 100644 --- a/app/test/application/bootstrap_test.dart +++ b/app/test/application/bootstrap_test.dart @@ -26,29 +26,25 @@ class _TestSettingsStore implements SettingsStore { void main() { const nativeBase = 'http://127.0.0.1:8096/'; - test( - 'uses saved endpoint natively only when build configuration is blank', - () async { - final store = _TestSettingsStore('wss://saved.example.test'); - final saved = await resolveLaunchServer( - base: Uri.parse(nativeBase), - configured: '', - web: false, - settingsStore: store, - ); - expect(saved.uri, Uri.parse('wss://saved.example.test/connect')); - expect(store.loads, 1); + test('a saved endpoint wins over the build default on a phone', () async { + final store = _TestSettingsStore('wss://saved.example.test'); + final saved = await resolveLaunchServer( + base: Uri.parse(nativeBase), + configured: 'ws://build.example.test', + web: false, + settingsStore: store, + ); + expect(saved.uri, Uri.parse('wss://saved.example.test/connect')); - final configured = await resolveLaunchServer( - base: Uri.parse(nativeBase), - configured: 'ws://build.example.test', - web: false, - settingsStore: store, - ); - expect(configured.uri, Uri.parse('ws://build.example.test/connect')); - expect(store.loads, 1); - }, - ); + // Nothing saved yet: the build default stands (and pre-fills onboarding). + final fallback = await resolveLaunchServer( + base: Uri.parse(nativeBase), + configured: 'ws://build.example.test', + web: false, + settingsStore: _TestSettingsStore(''), + ); + expect(fallback.uri, Uri.parse('ws://build.example.test/connect')); + }); test( 'blank or failed settings load leaves native configuration required', diff --git a/app/test/application/pipelines_root_test.dart b/app/test/application/pipelines_root_test.dart new file mode 100644 index 00000000..e1c932bc --- /dev/null +++ b/app/test/application/pipelines_root_test.dart @@ -0,0 +1,61 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/application/pipelines_root.dart'; +import 'package:pipelines_app/application/server_endpoint.dart'; +import 'package:pipelines_app/core/session/server_session.dart'; +import 'package:pipelines_app/features/onboarding/onboarding_flow.dart'; +import 'package:pipelines_app/features/settings/settings_store.dart'; + +class _Store implements SettingsStore { + @override + Future load() async => const SettingsValues(); + + @override + Future saveServerUrl(String value) async => true; + + @override + Future saveWorkspace(String value) async => true; +} + +ServerEndpoint _server(String url) => ServerEndpoint.resolve( + base: Uri.parse('file:///'), + web: false, + configured: url, +); + +void main() { + testWidgets( + 'a first launch starts at the server, pre-filled from the build', + (tester) async { + await tester.pumpWidget( + PipelinesRoot( + server: _server(''), + buildDefault: 'wss://build.example/connect', + check: (_) async => SessionState.signedOut, + settingsStore: _Store(), + ), + ); + await tester.pumpAndSettle(); + expect(find.byType(OnboardingFlow), findsOneWidget); + expect(find.byKey(const Key('onboarding-server')), findsOneWidget); + expect(find.text('wss://build.example/connect'), findsOneWidget); + }, + ); + + testWidgets('a kept session the server refuses asks for the password', ( + tester, + ) async { + await tester.pumpWidget( + PipelinesRoot( + server: _server('wss://home.example/connect'), + // A saved server with no kept session goes to onboarding directly. + signedIn: false, + check: (_) async => SessionState.signedOut, + settingsStore: _Store(), + ), + ); + await tester.pumpAndSettle(); + expect(find.byKey(const Key('onboarding-server')), findsOneWidget); + expect(find.text('wss://home.example/connect'), findsOneWidget); + }); +} diff --git a/app/test/features/onboarding/onboarding_flow_test.dart b/app/test/features/onboarding/onboarding_flow_test.dart new file mode 100644 index 00000000..4b006ebd --- /dev/null +++ b/app/test/features/onboarding/onboarding_flow_test.dart @@ -0,0 +1,102 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/session/server_session.dart'; +import 'package:pipelines_app/features/onboarding/onboarding_flow.dart'; + +void main() { + testWidgets('server, then password; both kept, then the app opens', ( + tester, + ) async { + final saved = []; + final signIns = <(Uri, String)>[]; + Uri? done; + await tester.pumpWidget( + MaterialApp( + home: OnboardingFlow( + initialServer: 'wss://build.example/connect', + check: (_) async => SessionState.signedOut, + signIn: (endpoint, password) async { + signIns.add((endpoint, password)); + return password == 'right-password' ? null : 'Wrong password.'; + }, + saveServer: (url) async => saved.add(url), + onDone: (endpoint) => done = endpoint, + ), + ), + ); + // The build's server is pre-filled; another one can be typed. + expect(find.text('wss://build.example/connect'), findsOneWidget); + await tester.enterText( + find.byKey(const Key('onboarding-server')), + 'wss://home.example/connect', + ); + await tester.tap(find.byKey(const Key('onboarding-next'))); + await tester.pumpAndSettle(); + expect(saved, ['wss://home.example/connect']); + expect(find.byKey(const Key('onboarding-password')), findsOneWidget); + + await tester.enterText( + find.byKey(const Key('onboarding-password')), + 'nope-nope', + ); + await tester.tap(find.byKey(const Key('onboarding-next'))); + await tester.pumpAndSettle(); + expect(find.text('Wrong password.'), findsOneWidget); + expect(done, isNull); + + await tester.enterText( + find.byKey(const Key('onboarding-password')), + 'right-password', + ); + await tester.tap(find.byKey(const Key('onboarding-next'))); + // Signed in: the app replaces this screen (the spinner stays until then). + await tester.pump(); + await tester.pump(); + expect(done, Uri.parse('wss://home.example/connect')); + expect(signIns.last.$1, Uri.parse('wss://home.example/connect')); + }); + + testWidgets('an unreachable server is explained and nothing is kept', ( + tester, + ) async { + final saved = []; + await tester.pumpWidget( + MaterialApp( + home: OnboardingFlow( + check: (_) async => SessionState.unreachable, + signIn: (_, _) async => null, + saveServer: (url) async => saved.add(url), + onDone: (_) {}, + ), + ), + ); + await tester.enterText( + find.byKey(const Key('onboarding-server')), + 'wss://nowhere.example/connect', + ); + await tester.tap(find.byKey(const Key('onboarding-next'))); + await tester.pumpAndSettle(); + expect(find.byKey(const Key('onboarding-error')), findsOneWidget); + expect(find.byKey(const Key('onboarding-password')), findsNothing); + expect(saved, isEmpty); + }); + + testWidgets('a refused session asks only for the password again', ( + tester, + ) async { + await tester.pumpWidget( + MaterialApp( + home: OnboardingFlow( + initialServer: 'wss://home.example/connect', + startAtPassword: true, + check: (_) async => SessionState.signedOut, + signIn: (_, _) async => null, + onDone: (_) {}, + ), + ), + ); + expect(find.byKey(const Key('onboarding-password')), findsOneWidget); + expect(find.byKey(const Key('onboarding-server')), findsNothing); + expect(find.text('Change server'), findsNothing); + }); +}