Bind legacy server identity and preserve pending run migrations
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/pipelines/pending_run_store.dart';
|
||||
import 'package:pipelines_app/features/pipelines/run_pipeline_intent.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../../core/servers/failing_preferences.dart';
|
||||
|
||||
const intent = RunPipelineIntent(
|
||||
pipelineId: 7,
|
||||
version: 3,
|
||||
prompt: 'unknown admission',
|
||||
workspace: '/work',
|
||||
notifyMode: RunNotifyMode.notify,
|
||||
requestKey: 'original-operation-id',
|
||||
);
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
const legacy = 'ws://original.test/connect';
|
||||
final oldKey = pendingRunStorageKey(legacy, 7, 3);
|
||||
final stableKey = pendingRunStorageKey('profile:legacy-server', 7, 3);
|
||||
const store = SharedPreferencesPendingRunStore(
|
||||
'profile:legacy-server',
|
||||
legacyEndpoints: [legacy],
|
||||
);
|
||||
setUp(() => SharedPreferences.setMockInitialValues({}));
|
||||
|
||||
test(
|
||||
'restores unknown admission under profile with identical operation id',
|
||||
() async {
|
||||
final preferences = await SharedPreferences.getInstance();
|
||||
final raw = jsonEncode(intent.toJson());
|
||||
await preferences.setString(oldKey, raw);
|
||||
expect((await store.read(7, 3))!.requestKey, intent.requestKey);
|
||||
expect(preferences.getString(stableKey), raw);
|
||||
expect(preferences.getString(oldKey), raw);
|
||||
expect(await store.read(7, 4), isNull);
|
||||
},
|
||||
);
|
||||
|
||||
test('stable record wins, including corrupt stable data', () async {
|
||||
final preferences = await SharedPreferences.getInstance();
|
||||
await preferences.setString(oldKey, jsonEncode(intent.toJson()));
|
||||
await preferences.setString(stableKey, 'corrupt');
|
||||
expect(await store.read(7, 3), isNull);
|
||||
expect(preferences.getString(stableKey), 'corrupt');
|
||||
await store.write(intent);
|
||||
expect((await store.read(7, 3))!.requestKey, intent.requestKey);
|
||||
});
|
||||
|
||||
test(
|
||||
'skips nonmatching fallback and never invents an operation id',
|
||||
() async {
|
||||
final preferences = await SharedPreferences.getInstance();
|
||||
await preferences.setString(
|
||||
oldKey,
|
||||
jsonEncode({...intent.toJson(), 'pipelineId': 8}),
|
||||
);
|
||||
expect(await store.read(7, 3), isNull);
|
||||
await preferences.setString(
|
||||
oldKey,
|
||||
jsonEncode({...intent.toJson(), 'requestKey': ''}),
|
||||
);
|
||||
expect(await store.read(7, 3), isNull);
|
||||
expect(preferences.containsKey(stableKey), isFalse);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'successful remove clears only stable and known legacy copies',
|
||||
() async {
|
||||
final preferences = await SharedPreferences.getInstance();
|
||||
final raw = jsonEncode(intent.toJson());
|
||||
final other = pendingRunStorageKey('ws://other.test', 7, 3);
|
||||
await preferences.setString(oldKey, raw);
|
||||
await preferences.setString(other, raw);
|
||||
await store.read(7, 3);
|
||||
await store.remove(7, 3);
|
||||
expect(preferences.containsKey(oldKey), isFalse);
|
||||
expect(preferences.containsKey(stableKey), isFalse);
|
||||
expect(preferences.getString(other), raw);
|
||||
expect(await store.read(7, 3), isNull);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'copy failure returns original operation and preserves source on disk',
|
||||
() async {
|
||||
for (final throws in [false, true]) {
|
||||
final disk = FailingPreferences()..install();
|
||||
final preferences = await SharedPreferences.getInstance();
|
||||
final raw = jsonEncode(intent.toJson());
|
||||
await preferences.setString(oldKey, raw);
|
||||
disk.failWriteKey = stableKey;
|
||||
disk.throwWrites = throws;
|
||||
expect((await store.read(7, 3))!.requestKey, intent.requestKey);
|
||||
await preferences.reload();
|
||||
expect(preferences.getString(oldKey), raw);
|
||||
expect(preferences.containsKey(stableKey), isFalse);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
test('partial cleanup failure keeps stable recovery copy', () async {
|
||||
final disk = FailingPreferences()..install();
|
||||
final preferences = await SharedPreferences.getInstance();
|
||||
final raw = jsonEncode(intent.toJson());
|
||||
await preferences.setString(oldKey, raw);
|
||||
await store.read(7, 3);
|
||||
disk.failRemoveKey = oldKey;
|
||||
await store.remove(7, 3);
|
||||
await preferences.reload();
|
||||
expect(preferences.getString(stableKey), raw);
|
||||
expect(preferences.getString(oldKey), raw);
|
||||
disk.failRemoveKey = null;
|
||||
await store.remove(7, 3);
|
||||
await preferences.reload();
|
||||
expect(await store.read(7, 3), isNull);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user