feat(app): configure and safely switch multi-address servers

This commit is contained in:
2026-10-24 03:04:53 +01:00
parent 47cfb31e7d
commit b499da304d
38 changed files with 1764 additions and 65 deletions
@@ -4,6 +4,76 @@ import 'package:pipelines_app/core/session/server_session.dart';
import 'package:pipelines_app/features/onboarding/onboarding_flow.dart';
void main() {
testWidgets('sign-in uses a reachable backup within the selected server', (
tester,
) async {
final primary = Uri.parse('wss://tail.example/connect');
final backup = Uri.parse('ws://192.168.1.2:8083/connect');
final checked = <Uri>[];
final signedIn = <Uri>[];
final saved = <String>[];
await tester.pumpWidget(
MaterialApp(
home: OnboardingFlow(
initialServer: primary.toString(),
serverAddresses: [primary, backup],
check: (uri) async {
checked.add(uri);
return uri == primary
? SessionState.unreachable
: SessionState.signedOut;
},
signIn: (uri, _) async {
signedIn.add(uri);
return null;
},
saveServer: (url) async => saved.add(url),
onDone: (_) {},
),
),
);
await tester.tap(find.byKey(const Key('onboarding-next')));
await tester.pumpAndSettle();
expect(checked, [primary, backup]);
expect(saved, [backup.toString()]);
await tester.enterText(
find.byKey(const Key('onboarding-password')),
'fixture-password',
);
await tester.tap(find.byKey(const Key('onboarding-next')));
await tester.pump();
await tester.pump();
expect(signedIn, [backup]);
});
testWidgets(
'typing a different server does not try another profile backups',
(tester) async {
final primary = Uri.parse('wss://tail.example/connect');
final checked = <Uri>[];
await tester.pumpWidget(
MaterialApp(
home: OnboardingFlow(
initialServer: primary.toString(),
serverAddresses: [primary, Uri.parse('ws://192.168.1.2/connect')],
check: (uri) async {
checked.add(uri);
return SessionState.unreachable;
},
onDone: (_) {},
),
),
);
await tester.enterText(
find.byKey(const Key('onboarding-server')),
'wss://different.example/connect',
);
await tester.tap(find.byKey(const Key('onboarding-next')));
await tester.pumpAndSettle();
expect(checked, [Uri.parse('wss://different.example/connect')]);
},
);
testWidgets('server, then password; both kept, then the app opens', (
tester,
) async {
@@ -0,0 +1,192 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/servers/server_catalog.dart';
import 'package:pipelines_app/core/servers/server_catalog_store.dart';
import 'package:pipelines_app/features/settings/saved_servers_section.dart';
import 'package:pipelines_app/features/settings/server_connections.dart';
SavedServer profile(String id, List<String> urls) =>
SavedServer.validated(id: id, name: id, endpoints: urls);
void main() {
test(
'browser Back aligns active identity with the actual page; failed switch restores selection',
() async {
final home = profile('Home', ['wss://home.test/connect']);
final work = profile('Work', ['wss://work.test/connect']);
final store = MemoryServerCatalogStore(
catalog: ServerCatalog(servers: [home, work], selectedId: 'Work'),
);
final controller = ServerConnections(
store: store,
onSelected: (_) async => throw StateError('Navigation failed'),
);
addTearDown(controller.dispose);
await controller.load();
controller.alignBrowserOrigin(Uri.parse('wss://home.test/connect'));
expect(controller.catalog.active?.id, 'Home');
expect(await controller.select('Work'), isFalse);
expect(controller.catalog.active?.id, 'Home');
expect(store.catalog?.active?.id, 'Home');
controller.alignBrowserOrigin(
Uri.parse('wss://unconfigured.test/connect'),
);
expect(controller.catalog.active, isNull);
},
);
test(
'selection and address edits persist before activation; failed saves keep selection',
() async {
final home = profile('Home', [
'wss://home.test/connect',
'ws://192.168.1.2:8083/connect',
]);
final work = profile('Work', ['wss://work.test/connect']);
final store = MemoryServerCatalogStore(
catalog: ServerCatalog(servers: [home, work], selectedId: home.id),
);
final activated = <String?>[];
final controller = ServerConnections(
store: store,
onSelected: (server) async {
expect(store.catalog?.selectedId, server?.id);
activated.add(server?.id);
},
);
addTearDown(controller.dispose);
await controller.load();
expect(await controller.select(work.id), isTrue);
expect(activated, ['Work']);
store.failSaves = true;
expect(await controller.select(home.id), isFalse);
expect(controller.catalog.active?.id, 'Work');
expect(activated, ['Work']);
expect(controller.error, contains('could not be saved'));
store.failSaves = false;
expect(await controller.remove('Work'), isTrue);
expect(controller.catalog.active, isNull);
expect(activated, ['Work', null]);
expect(controller.catalog.servers.single.id, 'Home');
},
);
test(
'first explicit add selects its server but adding another does not switch',
() async {
final calls = <String?>[];
final controller = ServerConnections(
store: MemoryServerCatalogStore(),
onSelected: (s) async => calls.add(s?.id),
);
addTearDown(controller.dispose);
await controller.load();
expect(
await controller.save(profile('Home', ['wss://home.test'])),
isTrue,
);
expect(controller.catalog.active?.id, 'Home');
expect(
await controller.save(profile('Work', ['wss://work.test'])),
isTrue,
);
expect(controller.catalog.active?.id, 'Home');
expect(calls, ['Home']);
},
);
for (final cupertino in [false, true]) {
testWidgets('add, edit and select ordered server aliases ($cupertino)', (
tester,
) async {
final calls = <String?>[];
final connections = ServerConnections(
store: MemoryServerCatalogStore(),
onSelected: (s) async => calls.add(s?.id),
);
addTearDown(connections.dispose);
await connections.load();
final body = SingleChildScrollView(
child: SavedServersSection(connections: connections),
);
await tester.pumpWidget(
cupertino
? CupertinoApp(
home: CupertinoPageScaffold(child: SafeArea(child: body)),
)
: MaterialApp(home: Scaffold(body: body)),
);
await tester.tap(find.text('Add server'));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('server-profile-name')),
'Home',
);
await tester.enterText(
find.byKey(const Key('server-profile-addresses')),
'wss://tail.example/connect\nws://192.168.1.2:8083/connect',
);
await tester.ensureVisible(find.text('Save server'));
await tester.pumpAndSettle();
await tester.tap(find.text('Save server'));
await tester.pumpAndSettle();
expect(connections.catalog.active?.name, 'Home');
expect(connections.catalog.active?.endpoints, [
'wss://tail.example/connect',
'ws://192.168.1.2:8083/connect',
]);
expect(
find.text('Backup 1: ws://192.168.1.2:8083/connect'),
findsOneWidget,
);
await tester.tap(find.text('Edit Home'));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('server-profile-name')),
'My home',
);
await tester.ensureVisible(find.text('Save server'));
await tester.pumpAndSettle();
await tester.tap(find.text('Save server'));
await tester.pumpAndSettle();
expect(connections.catalog.active?.name, 'My home');
expect(calls, hasLength(2));
expect(calls.first, calls.last);
expect(tester.takeException(), isNull);
});
testWidgets('invalid server addresses stay in editor ($cupertino)', (
tester,
) async {
final connections = ServerConnections(
store: MemoryServerCatalogStore(),
onSelected: (_) async {},
);
addTearDown(connections.dispose);
final body = SavedServersSection(connections: connections);
await tester.pumpWidget(
cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: body))
: MaterialApp(home: Scaffold(body: body)),
);
await tester.tap(find.text('Add server'));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('server-profile-name')),
'Home',
);
await tester.enterText(
find.byKey(const Key('server-profile-addresses')),
'not-a-url',
);
await tester.ensureVisible(find.text('Save server'));
await tester.pumpAndSettle();
await tester.tap(find.text('Save server'));
await tester.pumpAndSettle();
expect(find.byKey(const Key('server-profile-addresses')), findsOneWidget);
expect(connections.catalog.servers, isEmpty);
expect(find.textContaining('valid URL'), findsOneWidget);
});
}
}