370 lines
13 KiB
Dart
370 lines
13 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:pipelines_app/application/app_route.dart';
|
|
import 'package:pipelines_app/application/tab_locations.dart';
|
|
import 'package:pipelines_app/application/tab_locations_store.dart';
|
|
import 'package:pipelines_app/shell/destination.dart';
|
|
|
|
class MemoryTabLocationsStore implements TabLocationsStore {
|
|
TabLocationsSnapshot value = const TabLocationsSnapshot();
|
|
Completer<TabLocationsSnapshot>? read;
|
|
final writes = <TabLocationsSnapshot>[];
|
|
Completer<bool>? write;
|
|
@override
|
|
Future<TabLocationsSnapshot> load() async =>
|
|
read == null ? value : read!.future;
|
|
@override
|
|
Future<bool> save(TabLocationsSnapshot snapshot) async {
|
|
writes.add(snapshot);
|
|
if (write case final pending?) {
|
|
write = null;
|
|
await pending.future;
|
|
}
|
|
value = snapshot;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
class UnreadableTabLocationsStore extends MemoryTabLocationsStore {
|
|
@override
|
|
Future<TabLocationsSnapshot> load() async => throw StateError('unavailable');
|
|
}
|
|
|
|
class FailingTabLocationsStore extends MemoryTabLocationsStore {
|
|
bool fails = true;
|
|
@override
|
|
Future<bool> save(TabLocationsSnapshot snapshot) async {
|
|
if (fails) {
|
|
fails = false;
|
|
throw StateError('temporary write failure');
|
|
}
|
|
return super.save(snapshot);
|
|
}
|
|
}
|
|
|
|
class GatedPreferencesTabLocationsStore
|
|
extends SharedPreferencesTabLocationsStore {
|
|
GatedPreferencesTabLocationsStore({required super.scope});
|
|
final gate = Completer<void>();
|
|
final started = Completer<void>();
|
|
@override
|
|
Future<bool> save(TabLocationsSnapshot snapshot) async {
|
|
if (!started.isCompleted) started.complete();
|
|
await gate.future;
|
|
return super.save(snapshot);
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
test('Settings and nested pages retain Pipelines ownership', () async {
|
|
final store = MemoryTabLocationsStore();
|
|
final tabs = TabLocations(store);
|
|
tabs.record(AppRoute.settings);
|
|
tabs.record(AppRoute.chat);
|
|
await tabs.restore(restoreActive: false);
|
|
await tabs.settled;
|
|
expect(tabs.routes[AppDestination.pipelines], AppRoute.settings);
|
|
expect(tabs.routes[AppDestination.chat], AppRoute.chat);
|
|
expect(store.value.routes[AppDestination.pipelines], AppRoute.settings);
|
|
tabs.record(AppRoute.template('release'));
|
|
await tabs.settled;
|
|
expect(
|
|
store.value.routes[AppDestination.pipelines],
|
|
AppRoute.template('release'),
|
|
);
|
|
});
|
|
test('canonical details survive serialization, decisions do not replay', () {
|
|
final snapshot = TabLocationsSnapshot.decode(
|
|
jsonEncode({
|
|
'version': 1,
|
|
'active': 'runs',
|
|
'routes': {
|
|
'pipelines': '/pipelines/7/3/stage/build',
|
|
'runs': '/runs/8/stage/review?visit=2',
|
|
'chat': '/chat/session-2',
|
|
'search': '/search',
|
|
},
|
|
}),
|
|
);
|
|
expect(
|
|
snapshot.routes[AppDestination.runs],
|
|
AppRoute.run.stage(8, 'review', visit: 2),
|
|
);
|
|
expect(
|
|
snapshot.routes[AppDestination.pipelines],
|
|
AppRoute.pipelineStage(7, 'build', version: 3),
|
|
);
|
|
expect(
|
|
TabLocationsSnapshot.decode(
|
|
jsonEncode({
|
|
'version': 1,
|
|
'routes': {'runs': '/runs/8?action=decide'},
|
|
}),
|
|
).routes[AppDestination.runs],
|
|
AppRoute.run(8),
|
|
);
|
|
});
|
|
test('malformed, cross-owner and external stored routes are ignored', () {
|
|
for (final value in ['no json', '[]', '{"version":2,"routes":{}}']) {
|
|
expect(TabLocationsSnapshot.decode(value).routes, isEmpty);
|
|
}
|
|
final saved = TabLocationsSnapshot.decode(
|
|
jsonEncode({
|
|
'version': 1,
|
|
'routes': {
|
|
'runs': '/settings',
|
|
'pipelines': 'https://example.com/settings',
|
|
'chat': '/chat/invalid/id',
|
|
'settings': '/settings',
|
|
'search': '/unknown',
|
|
},
|
|
}),
|
|
);
|
|
expect(saved.routes, isEmpty);
|
|
});
|
|
test(
|
|
'delayed restore merges untouched tabs but never overwrites user navigation',
|
|
() async {
|
|
final store = MemoryTabLocationsStore()..read = Completer();
|
|
final tabs = TabLocations(store)
|
|
..record(AppRoute.pipelines, visited: false);
|
|
final restoring = tabs.restore(restoreActive: true);
|
|
tabs.record(AppRoute.run(12));
|
|
expect(
|
|
store.writes,
|
|
isEmpty,
|
|
reason: 'never erase storage before reading it',
|
|
);
|
|
store.read!.complete(
|
|
TabLocationsSnapshot(
|
|
active: AppDestination.pipelines,
|
|
routes: {
|
|
AppDestination.pipelines: AppRoute.settings,
|
|
AppDestination.runs: AppRoute.run(5),
|
|
},
|
|
),
|
|
);
|
|
expect(await restoring, isNull);
|
|
await tabs.settled;
|
|
expect(tabs.routes[AppDestination.runs], AppRoute.run(12));
|
|
expect(tabs.routes[AppDestination.pipelines], AppRoute.settings);
|
|
expect(store.value.active, AppDestination.runs);
|
|
},
|
|
);
|
|
test(
|
|
'ordinary boot restores active location; explicit boot wins its slot',
|
|
() async {
|
|
final store = MemoryTabLocationsStore()
|
|
..value = const TabLocationsSnapshot(
|
|
active: AppDestination.pipelines,
|
|
routes: {AppDestination.pipelines: AppRoute.settings},
|
|
);
|
|
final ordinary = TabLocations(store)
|
|
..record(AppRoute.pipelines, visited: false);
|
|
expect(await ordinary.restore(restoreActive: true), AppRoute.settings);
|
|
await ordinary.settled;
|
|
final explicit = TabLocations(store)
|
|
..record(AppRoute.pipeline(3, version: 2));
|
|
expect(await explicit.restore(restoreActive: false), isNull);
|
|
expect(
|
|
explicit.routes[AppDestination.pipelines],
|
|
AppRoute.pipeline(3, version: 2),
|
|
);
|
|
await explicit.settled;
|
|
},
|
|
);
|
|
test(
|
|
'writes serialize and last move wins even with a slow first write',
|
|
() async {
|
|
final first = Completer<bool>();
|
|
final store = MemoryTabLocationsStore()..write = first;
|
|
final tabs = TabLocations(store)..record(AppRoute.settings);
|
|
await tabs.restore(restoreActive: false);
|
|
await Future<void>.delayed(Duration.zero);
|
|
tabs.record(AppRoute.chat);
|
|
tabs.record(AppRoute.run(9));
|
|
expect(store.writes, hasLength(1));
|
|
first.complete(true);
|
|
await tabs.settled;
|
|
expect(store.value.active, AppDestination.runs);
|
|
expect(store.value.routes[AppDestination.pipelines], AppRoute.settings);
|
|
expect(store.value.routes[AppDestination.runs], AppRoute.run(9));
|
|
},
|
|
);
|
|
test(
|
|
'SharedPreferences survives recreation and isolates logical servers',
|
|
() async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
const a = SharedPreferencesTabLocationsStore(scope: 'server-a');
|
|
const b = SharedPreferencesTabLocationsStore(scope: 'server-b');
|
|
expect(
|
|
await a.save(
|
|
const TabLocationsSnapshot(
|
|
routes: {AppDestination.pipelines: AppRoute.settings},
|
|
),
|
|
),
|
|
isTrue,
|
|
);
|
|
expect(
|
|
(await const SharedPreferencesTabLocationsStore(
|
|
scope: 'server-a',
|
|
).load()).routes[AppDestination.pipelines],
|
|
AppRoute.settings,
|
|
);
|
|
expect((await b.load()).routes, isEmpty);
|
|
},
|
|
);
|
|
test(
|
|
'unreadable history is never replaced by a partial new snapshot',
|
|
() async {
|
|
final store = UnreadableTabLocationsStore();
|
|
final tabs = TabLocations(store)..record(AppRoute.settings);
|
|
expect(await tabs.restore(restoreActive: false), isNull);
|
|
tabs.record(AppRoute.chat);
|
|
await tabs.settled;
|
|
expect(tabs.persistenceFailed, isTrue);
|
|
expect(store.writes, isEmpty);
|
|
expect(tabs.routes[AppDestination.pipelines], AppRoute.settings);
|
|
},
|
|
);
|
|
test('a failed write does not prevent a later navigation save', () async {
|
|
final store = FailingTabLocationsStore();
|
|
final tabs = TabLocations(store)..record(AppRoute.settings);
|
|
await tabs.restore(restoreActive: false);
|
|
await tabs.settled;
|
|
expect(tabs.persistenceFailed, isTrue);
|
|
tabs.record(AppRoute.chat);
|
|
await tabs.settled;
|
|
expect(tabs.persistenceFailed, isFalse);
|
|
expect(store.value.active, AppDestination.chat);
|
|
});
|
|
test(
|
|
'a disposed app never writes a late storage read back over newer history',
|
|
() async {
|
|
final store = MemoryTabLocationsStore()..read = Completer();
|
|
final tabs = TabLocations(store)
|
|
..record(AppRoute.pipelines, visited: false);
|
|
final restoring = tabs.restore(restoreActive: true);
|
|
tabs.dispose();
|
|
store.read!.complete(
|
|
const TabLocationsSnapshot(
|
|
routes: {AppDestination.pipelines: AppRoute.settings},
|
|
),
|
|
);
|
|
expect(await restoring, isNull);
|
|
await tabs.settled;
|
|
expect(store.writes, isEmpty);
|
|
},
|
|
);
|
|
test(
|
|
'accepted old writes finish before a replacement load and newer move',
|
|
() async {
|
|
final gate = Completer<bool>();
|
|
final store = MemoryTabLocationsStore()..write = gate;
|
|
final old = TabLocations(store)..record(AppRoute.settings);
|
|
await old.restore(restoreActive: false);
|
|
await Future<void>.delayed(Duration.zero);
|
|
expect(store.writes, hasLength(1));
|
|
old.record(AppRoute.chat); // queued behind the blocked Settings write
|
|
old.dispose();
|
|
final replacement = TabLocations(store)..record(AppRoute.run(12));
|
|
var readFinished = false;
|
|
final restoring = replacement.restore(restoreActive: false).then((value) {
|
|
readFinished = true;
|
|
return value;
|
|
});
|
|
await Future<void>.delayed(Duration.zero);
|
|
expect(readFinished, isFalse, reason: 'wait for the old in-flight write');
|
|
gate.complete(true);
|
|
await restoring;
|
|
await old.settled;
|
|
await replacement.settled;
|
|
expect(store.value.active, AppDestination.runs);
|
|
expect(store.value.routes[AppDestination.runs], AppRoute.run(12));
|
|
expect(store.value.routes[AppDestination.pipelines], AppRoute.settings);
|
|
expect(
|
|
store.value.routes[AppDestination.chat],
|
|
AppRoute.chat,
|
|
reason: 'the accepted queued Chat move is durable before replacement',
|
|
);
|
|
expect(store.writes, hasLength(3));
|
|
expect(store.writes[1].active, AppDestination.chat);
|
|
expect(store.writes.last.active, AppDestination.runs);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'different preference store instances share ordering for the same key',
|
|
() async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final oldStore = GatedPreferencesTabLocationsStore(scope: 'same-server');
|
|
final old = TabLocations(oldStore)..record(AppRoute.settings);
|
|
await old.restore(restoreActive: false);
|
|
await oldStore.started.future;
|
|
old.record(AppRoute.chat);
|
|
old.dispose();
|
|
const newStore = SharedPreferencesTabLocationsStore(scope: 'same-server');
|
|
final replacement = TabLocations(newStore)..record(AppRoute.run(18));
|
|
var readFinished = false;
|
|
final restoring = replacement.restore(restoreActive: false).then((value) {
|
|
readFinished = true;
|
|
return value;
|
|
});
|
|
await Future<void>.delayed(Duration.zero);
|
|
expect(readFinished, isFalse);
|
|
oldStore.gate.complete();
|
|
await restoring;
|
|
await old.settled;
|
|
await replacement.settled;
|
|
final saved = await newStore.load();
|
|
expect(saved.active, AppDestination.runs);
|
|
expect(saved.routes[AppDestination.runs], AppRoute.run(18));
|
|
expect(saved.routes[AppDestination.pipelines], AppRoute.settings);
|
|
expect(saved.routes[AppDestination.chat], AppRoute.chat);
|
|
},
|
|
);
|
|
test(
|
|
'recreating without a new choice restores the last accepted queued move',
|
|
() async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final oldStore = GatedPreferencesTabLocationsStore(
|
|
scope: 'ordinary-restart',
|
|
);
|
|
final old = TabLocations(oldStore)..record(AppRoute.settings);
|
|
await old.restore(restoreActive: false);
|
|
await oldStore.started.future;
|
|
old.record(AppRoute.chatSession('last-open-chat'));
|
|
old.dispose();
|
|
old.record(AppRoute.run(999)); // disposal rejects new, unaccepted moves
|
|
const newStore = SharedPreferencesTabLocationsStore(
|
|
scope: 'ordinary-restart',
|
|
);
|
|
final replacement = TabLocations(newStore)
|
|
..record(AppRoute.pipelines, visited: false);
|
|
var readFinished = false;
|
|
final restoring = replacement.restore(restoreActive: true).then((value) {
|
|
readFinished = true;
|
|
return value;
|
|
});
|
|
await Future<void>.delayed(Duration.zero);
|
|
expect(readFinished, isFalse);
|
|
oldStore.gate.complete();
|
|
expect(await restoring, AppRoute.chatSession('last-open-chat'));
|
|
await old.settled;
|
|
await replacement.settled;
|
|
final saved = await newStore.load();
|
|
expect(saved.active, AppDestination.chat);
|
|
expect(
|
|
saved.routes[AppDestination.chat],
|
|
AppRoute.chatSession('last-open-chat'),
|
|
);
|
|
expect(saved.routes[AppDestination.runs], isNull);
|
|
expect(saved.routes[AppDestination.pipelines], AppRoute.settings);
|
|
},
|
|
);
|
|
}
|