fix(settings): preserve server editor drafts until save succeeds

This commit is contained in:
2026-10-24 10:52:01 +01:00
parent 6609b52b1c
commit 25437b3181
6 changed files with 414 additions and 27 deletions
@@ -0,0 +1,177 @@
import 'dart:async';
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';
class _ControlledStore extends MemoryServerCatalogStore {
_ControlledStore({super.catalog});
final writes = <ServerCatalog>[];
Completer<bool>? pending;
@override
Future<bool> save(ServerCatalog catalog) async {
writes.add(catalog);
if (pending != null && !await pending!.future) return false;
return super.save(catalog);
}
}
Future<void> _openEditor(
WidgetTester tester,
ServerConnections connections,
bool cupertino, {
bool edit = false,
}) async {
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(edit ? 'Edit Home' : 'Add server'));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('server-profile-name')),
'My home',
);
await tester.enterText(
find.byKey(const Key('server-profile-addresses')),
'wss://home.test/connect\nws://192.168.1.2:8083/connect',
);
await tester.ensureVisible(find.text('Save server'));
await tester.pumpAndSettle();
}
void main() {
for (final cupertino in [false, true]) {
testWidgets('Back cancels an unsaved draft ($cupertino)', (tester) async {
final store = _ControlledStore();
final connections = ServerConnections(
store: store,
onSelected: (_) async => fail('Cancel must not select a server'),
);
addTearDown(connections.dispose);
await _openEditor(tester, connections, cupertino);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.byKey(const Key('server-profile-name')), findsNothing);
expect(store.writes, isEmpty);
expect(connections.catalog.servers, isEmpty);
});
testWidgets(
'failed edit keeps draft and blocks duplicate save ($cupertino)',
(tester) async {
final original = SavedServer.validated(
id: 'home',
name: 'Home',
endpoints: ['wss://home.test/connect'],
);
final store = _ControlledStore(
catalog: ServerCatalog(servers: [original], selectedId: original.id),
)..pending = Completer<bool>();
final activations = <SavedServer?>[];
final connections = ServerConnections(
store: store,
onSelected: (server) async => activations.add(server),
);
addTearDown(connections.dispose);
await connections.load();
await _openEditor(tester, connections, cupertino, edit: true);
await tester.tap(find.text('Save server'));
await tester.pump();
expect(find.text('Saving server…'), findsOneWidget);
final button = find.widgetWithText(
cupertino ? CupertinoButton : FilledButton,
'Saving server…',
);
expect(
cupertino
? tester.widget<CupertinoButton>(button).onPressed
: tester.widget<FilledButton>(button).onPressed,
isNull,
);
await tester.tap(find.text('Saving server…'));
await tester.binding.handlePopRoute();
await tester.pump();
expect(find.byKey(const Key('server-profile-name')), findsOneWidget);
expect(store.writes, hasLength(1));
expect(connections.catalog.active?.name, 'Home');
expect(activations, isEmpty);
store.pending!.complete(false);
await tester.pumpAndSettle();
expect(find.textContaining('could not be saved'), findsOneWidget);
final name = tester.widget<EditableText>(
find.descendant(
of: find.byKey(const Key('server-profile-name')),
matching: find.byType(EditableText),
),
);
final addresses = tester.widget<EditableText>(
find.descendant(
of: find.byKey(const Key('server-profile-addresses')),
matching: find.byType(EditableText),
),
);
expect(name.controller.text, 'My home');
expect(addresses.controller.text, contains('ws://192.168.1.2:8083'));
expect(connections.catalog.active?.name, 'Home');
store.pending = null;
await tester.ensureVisible(find.text('Save server'));
await tester.tap(find.text('Save server'));
await tester.pumpAndSettle();
expect(find.byKey(const Key('server-profile-name')), findsNothing);
expect(connections.catalog.active?.name, 'My home');
expect(store.catalog?.active?.endpoints, hasLength(2));
expect(store.writes, hasLength(2));
expect(activations.single?.id, original.id);
expect(tester.takeException(), isNull);
},
);
testWidgets(
'activation failure retry does not duplicate new profile ($cupertino)',
(tester) async {
final store = _ControlledStore();
var attempts = 0;
final connections = ServerConnections(
store: store,
onSelected: (_) async {
if (++attempts == 1) throw StateError('Connection unavailable');
},
);
addTearDown(connections.dispose);
await connections.load();
await _openEditor(tester, connections, cupertino);
await tester.tap(find.text('Save server'));
await tester.pumpAndSettle();
expect(find.textContaining('Connection unavailable'), findsOneWidget);
expect(find.byKey(const Key('server-profile-name')), findsOneWidget);
expect(store.catalog?.servers, hasLength(1));
final savedId = store.catalog!.servers.single.id;
await tester.ensureVisible(find.text('Save server'));
await tester.tap(find.text('Save server'));
await tester.pumpAndSettle();
expect(attempts, 2);
expect(store.catalog?.servers, hasLength(1));
expect(store.catalog?.servers.single.id, savedId);
expect(find.byKey(const Key('server-profile-name')), findsNothing);
expect(tester.takeException(), isNull);
},
);
}
}