diff --git a/app/lib/application/bootstrap.dart b/app/lib/application/bootstrap.dart index 85accfc4..15ad0070 100644 --- a/app/lib/application/bootstrap.dart +++ b/app/lib/application/bootstrap.dart @@ -4,17 +4,49 @@ import 'package:flutter/widgets.dart'; import 'app_url_strategy.dart'; import 'pipelines_app.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 endpoint for this launch and starts the normal composition. -void runPipelinesApp({Uri? base, String configured = configuredServerUrl}) { - useAppUrlStrategy(); - final server = ServerEndpoint.resolve( +/// Resolves the launch endpoint, loading the last saved native endpoint only +/// when no build-time endpoint was supplied. +Future resolveLaunchServer({ + Uri? base, + String configured = configuredServerUrl, + bool web = kIsWeb, + SettingsStore? settingsStore, +}) async { + WidgetsFlutterBinding.ensureInitialized(); + var endpoint = configured; + if (!web && endpoint.trim().isEmpty) { + try { + endpoint = + (await (settingsStore ?? const SharedPreferencesSettingsStore()) + .load()) + .serverUrl; + } catch (_) { + endpoint = ''; + } + } + return ServerEndpoint.resolve( base: base ?? Uri.base, - web: kIsWeb, + web: web, + configured: endpoint, + ); +} + +/// Resolves the endpoint for this launch and starts the normal composition. +Future runPipelinesApp({ + Uri? base, + String configured = configuredServerUrl, + SettingsStore? settingsStore, +}) async { + useAppUrlStrategy(); + final server = await resolveLaunchServer( + base: base, configured: configured, + settingsStore: settingsStore, ); runApp(PipelinesApp(server: server)); } diff --git a/app/test/application/bootstrap_test.dart b/app/test/application/bootstrap_test.dart new file mode 100644 index 00000000..68b1b714 --- /dev/null +++ b/app/test/application/bootstrap_test.dart @@ -0,0 +1,86 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/application/bootstrap.dart'; +import 'package:pipelines_app/features/settings/settings_store.dart'; + +class _TestSettingsStore implements SettingsStore { + _TestSettingsStore(this.value, {this.fail = false}); + + final String value; + final bool fail; + int loads = 0; + + @override + Future load() async { + loads++; + if (fail) throw StateError('unavailable'); + return SettingsValues(serverUrl: value); + } + + @override + Future saveServerUrl(String value) async => true; + + @override + Future saveWorkspace(String value) async => true; +} + +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); + + 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); + }, + ); + + test( + 'blank or failed settings load leaves native configuration required', + () async { + for (final store in [ + _TestSettingsStore(''), + _TestSettingsStore('', fail: true), + ]) { + final server = await resolveLaunchServer( + base: Uri.parse(nativeBase), + configured: '', + web: false, + settingsStore: store, + ); + expect(server.requiresConfiguration, isTrue); + expect(server.uri, isNull); + } + }, + ); + + test( + 'web infers same-origin endpoint without loading native settings', + () async { + final store = _TestSettingsStore('wss://saved.example.test'); + final server = await resolveLaunchServer( + base: Uri.parse('https://web.example.test/runs'), + configured: '', + web: true, + settingsStore: store, + ); + expect(server.uri, Uri.parse('wss://web.example.test/connect')); + expect(store.loads, 0); + }, + ); +}