Add immutable server catalog and versioned preference store

This commit is contained in:
2026-10-24 02:23:49 +01:00
parent 54f74ade33
commit 289a971b73
4 changed files with 515 additions and 0 deletions
@@ -0,0 +1,136 @@
import 'dart:convert';
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:shared_preferences/shared_preferences.dart';
ServerCatalog fixture() => ServerCatalog(
servers: [
SavedServer.validated(
id: 'home',
name: 'Home',
endpoints: ['ws://home.test', 'wss://backup.test'],
),
],
selectedId: 'home',
);
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
const store = SharedPreferencesServerCatalogStore();
setUp(() => SharedPreferences.setMockInitialValues({}));
test('empty storage gives empty catalog and never writes on load', () async {
expect((await store.load()).servers, isEmpty);
final preferences = await SharedPreferences.getInstance();
expect(preferences.getKeys(), isEmpty);
});
test('one versioned preference roundtrips all catalog data', () async {
final catalog = fixture();
expect(await store.save(catalog), isTrue);
final preferences = await SharedPreferences.getInstance();
expect(preferences.getKeys(), {serverCatalogKey});
final json = jsonDecode(preferences.getString(serverCatalogKey)!);
expect(json['version'], 1);
expect(json['servers'], catalog.toJson()['servers']);
expect((await store.load()).toJson(), catalog.toJson());
});
test('legacy migration is stable, selected and preserves old keys', () async {
const legacyKey = 'pipelines.next.serverUrl.v1';
const legacy = ' ws://legacy.test/ ';
SharedPreferences.setMockInitialValues({
legacyKey: legacy,
'pipelines.next.workspace.v1': '/keep/workspace',
});
final migrated = await store.load(legacyEndpoint: legacy);
expect(migrated.selectedId, 'legacy-server');
expect(migrated.active!.name, 'My server');
expect(migrated.active!.endpoints, ['ws://legacy.test/connect']);
final preferences = await SharedPreferences.getInstance();
expect(preferences.containsKey(serverCatalogKey), isFalse);
expect(await store.save(migrated), isTrue);
expect(preferences.getString(legacyKey), legacy);
expect(
preferences.getString('pipelines.next.workspace.v1'),
'/keep/workspace',
);
expect(
(await store.load(legacyEndpoint: 'invalid')).toJson(),
migrated.toJson(),
);
});
test('existing empty catalog takes priority over legacy migration', () async {
await store.save(ServerCatalog());
expect(
(await store.load(legacyEndpoint: 'ws://old.test')).servers,
isEmpty,
);
});
test('corrupt, invalid and future data surface without mutation', () async {
for (final raw in <Object>[
'{broken',
'',
7,
'[]',
'{"version":2,"servers":[]}',
'{"version":1,"servers":[],"selectedId":"missing"}',
'{"version":1,"servers":[{"id":"a","name":"A","endpoints":[]}]}',
]) {
SharedPreferences.setMockInitialValues({serverCatalogKey: raw});
await expectLater(
store.load(legacyEndpoint: 'ws://old.test'),
throwsFormatException,
);
final preferences = await SharedPreferences.getInstance();
expect(preferences.get(serverCatalogKey), raw);
expect(preferences.getKeys(), {serverCatalogKey});
}
});
test('invalid legacy endpoint reports error without writing', () async {
await expectLater(
store.load(legacyEndpoint: 'https://old.test'),
throwsFormatException,
);
final preferences = await SharedPreferences.getInstance();
expect(preferences.getKeys(), isEmpty);
});
test(
'memory store roundtrip and failed writes preserve prior data',
() async {
final original = fixture();
final memory = MemoryServerCatalogStore(catalog: original);
memory.failSaves = true;
expect(await memory.save(ServerCatalog()), isFalse);
expect((await memory.load()).toJson(), original.toJson());
memory.failSaves = false;
final changed = original.remove('home');
expect(await memory.save(changed), isTrue);
expect(
(await memory.load(legacyEndpoint: 'ws://ignored.test')).servers,
isEmpty,
);
expect(original.active!.id, 'home');
},
);
test('memory store follows the same read-only migration contract', () async {
final memory = MemoryServerCatalogStore();
expect((await memory.load()).servers, isEmpty);
final migrated = await memory.load(legacyEndpoint: 'wss://old.test');
expect(migrated.active!.id, 'legacy-server');
memory.failSaves = true;
expect(await memory.save(migrated), isFalse);
expect((await memory.load()).servers, isEmpty);
memory.failSaves = false;
expect(await memory.save(migrated), isTrue);
expect((await memory.load()).toJson(), migrated.toJson());
});
}
@@ -0,0 +1,157 @@
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/servers/server_catalog.dart';
SavedServer server(String id) => SavedServer.validated(
id: id,
name: 'Server $id',
endpoints: ['wss://$id.test'],
);
void main() {
test('normalizes, deduplicates and retains primary/backup order', () {
final value = SavedServer.validated(
id: ' home ',
name: ' Home ',
endpoints: [
' WS://HOME.test/ ',
'wss://backup.test',
'ws://home.test/connect',
],
);
expect(value.id, 'home');
expect(value.name, 'Home');
expect(value.endpoints, [
'ws://home.test/connect',
'wss://backup.test/connect',
]);
});
test('rejects blank identity, empty endpoints and invalid URLs', () {
for (final id in ['', ' ']) {
expect(
() => SavedServer.validated(id: id, name: 'Name', endpoints: []),
throwsFormatException,
);
}
expect(
() => SavedServer.validated(id: 'a', name: ' ', endpoints: ['ws://a']),
throwsFormatException,
);
expect(
() => SavedServer.validated(id: 'a', name: 'A', endpoints: []),
throwsFormatException,
);
for (final url in [
'',
'https://a.test',
'ws://user:secret@a.test',
'ws://a.test/#fragment',
'ws://',
]) {
expect(
() => SavedServer.validated(id: 'a', name: 'A', endpoints: [url]),
throwsFormatException,
);
}
});
test('roundtrips catalog JSON with multiple ordered endpoints', () {
final first = SavedServer.validated(
id: 'a',
name: 'A',
endpoints: ['ws://primary.test', 'wss://backup.test'],
);
final catalog = ServerCatalog(
servers: [first, server('b')],
selectedId: 'a',
);
final decoded = ServerCatalog.fromJson(
jsonDecode(jsonEncode(catalog.toJson())) as Map<String, dynamic>,
);
expect(decoded.toJson(), catalog.toJson());
expect(decoded.active!.endpoints, first.endpoints);
expect(ServerCatalog().active, isNull);
});
test('rejects duplicate IDs and cross-profile normalized addresses', () {
final a = server('a');
expect(() => ServerCatalog(servers: [a, a]), throwsFormatException);
final alias = SavedServer.validated(
id: 'alias',
name: 'Alias',
endpoints: ['WSS://A.TEST/'],
);
expect(() => ServerCatalog(servers: [a, alias]), throwsFormatException);
expect(
() => ServerCatalog(servers: [a]).upsert(alias),
throwsFormatException,
);
});
test('selection is explicit; upsert/remove never switch servers', () {
final a = server('a');
final b = server('b');
final original = ServerCatalog(servers: [a, b]);
expect(original.upsert(server('c')).active, isNull);
final selected = original.select('a');
final replacement = SavedServer.validated(
id: 'a',
name: 'Renamed',
endpoints: ['ws://new.test', 'wss://new-backup.test'],
);
final updated = selected.upsert(replacement);
expect(updated.active, same(replacement));
expect(updated.servers.map((entry) => entry.id), ['a', 'b']);
expect(updated.remove('b').active, same(replacement));
expect(updated.remove('a').active, isNull);
expect(updated.remove('a').servers, [b]);
expect(updated.remove('missing').toJson(), updated.toJson());
expect(() => original.select('missing'), throwsFormatException);
expect(selected.active, same(a));
expect(original.active, isNull);
});
test('copies inputs and exposes no mutable collection aliases', () {
final endpoints = ['ws://a.test'];
final a = SavedServer.validated(id: 'a', name: 'A', endpoints: endpoints);
endpoints.add('ws://injected.test');
final servers = [a];
final catalog = ServerCatalog(servers: servers, selectedId: 'a');
servers.clear();
expect(catalog.servers, [a]);
expect(a.endpoints, ['ws://a.test/connect']);
expect(() => a.endpoints.clear(), throwsUnsupportedError);
expect(() => catalog.servers.clear(), throwsUnsupportedError);
(a.toJson()['endpoints'] as List).clear();
(catalog.toJson()['servers'] as List).clear();
expect(catalog.active!.endpoints, ['ws://a.test/connect']);
});
test('malformed JSON cannot bypass catalog validation', () {
for (final json in <Map<String, dynamic>>[
{},
{'servers': 'wrong'},
{
'servers': [null],
},
{'servers': [], 'selectedId': 'missing'},
{'servers': [], 'selectedId': 1},
{
'servers': [server('a').toJson(), server('a').toJson()],
},
{
'servers': [
{
'id': 'a',
'name': 'A',
'endpoints': [1],
},
],
},
]) {
expect(() => ServerCatalog.fromJson(json), throwsFormatException);
}
});
}