Persist exact tab locations including Settings under Pipelines

This commit is contained in:
2026-11-08 01:04:00 +00:00
parent d6006bb1f8
commit 151d99fbb6
10 changed files with 778 additions and 25 deletions
+16
View File
@@ -36,6 +36,22 @@ Future<AppRouterDelegate> pumpRouter(
}
void main() {
test(
'restoring Settings subpages from another tab preserves their parent',
() async {
final router = delegate(AppRoute.chat);
addTearDown(router.dispose);
router.goTo(AppRoute.template('release'));
expect(router.shellRoute, AppRoute.settings);
router.closeTemplates();
expect(router.route, AppRoute.settings);
router.goTo(AppRoute.chat);
await router.setNewRoutePath(AppRoute.schedules);
expect(router.shellRoute, AppRoute.settings);
router.closeSchedules();
expect(router.route, AppRoute.settings);
},
);
TestWidgetsFlutterBinding.ensureInitialized();
test('with nothing pushed the router leaves Back to the platform', () async {
for (final route in [
@@ -0,0 +1,202 @@
import 'dart:async';
import 'package:flutter/material.dart';
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/pipelines_app.dart';
import 'package:pipelines_app/application/tab_locations_store.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import 'package:pipelines_app/features/settings/settings_screen.dart';
import 'package:pipelines_app/features/pipelines/screen.dart';
import 'package:pipelines_app/shell/destination.dart';
import '../bar_action.dart';
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
import '../features/pipelines/fixtures.dart';
import 'pipelines_app_test.dart' show webServer;
import 'pipelines_replies.dart';
import 'tab_locations_test.dart' show MemoryTabLocationsStore;
Future<({PlatformRouteInformationProvider provider, FakeChannel channel})>
mount(
WidgetTester tester, {
String route = '/pipelines',
bool cupertino = false,
TabLocationsStore? store,
}) async {
tester.view.physicalSize = const Size(560, 1000);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse(route)),
);
addTearDown(provider.dispose);
addTearDown(client.dispose);
await tester.pumpWidget(
PipelinesApp(
server: webServer(),
client: client,
cupertino: cupertino,
serverId: 'navigation-test',
routeInformationProvider: provider,
tabLocationsStore: store,
),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));
return (provider: provider, channel: channel);
}
Future<void> tab(WidgetTester tester, String name) async {
await tester.tap(find.byKey(ValueKey('shell.destination.$name')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));
}
void main() {
setUp(() => SharedPreferences.setMockInitialValues({}));
for (final cupertino in [false, true]) {
testWidgets(
'Settings is retained inside Pipelines across tab switches ($cupertino)',
(tester) async {
final app = await mount(tester, cupertino: cupertino);
await replyPipelines(tester, app.channel, PipelineFixture());
await tester.pumpAndSettle();
await tapBarAction(
tester,
find.byKey(const Key('pipelines-open-settings')),
'Settings',
);
await tester.pumpAndSettle();
expect(app.provider.value.uri.path, '/settings');
final settingsState = tester.state(find.byType(SettingsScreen));
await tab(tester, 'chat');
expect(app.provider.value.uri.path, '/chat');
await tab(tester, 'pipelines');
expect(app.provider.value.uri.path, '/settings');
expect(tester.state(find.byType(SettingsScreen)), same(settingsState));
await tab(tester, 'pipelines');
expect(
app.provider.value.uri.path,
'/settings',
reason: 'tab retap is not Back',
);
await tester.tap(find.byKey(const ValueKey('shell.back')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));
expect(
app.provider.value.uri.path,
'/pipelines',
reason: 'explicit Back still exits Settings',
);
expect(tester.takeException(), isNull);
},
);
}
testWidgets(
'per-tab locations survive app recreation; an explicit deep link wins',
(tester) async {
final first = await mount(tester, route: '/settings');
await tester.pumpAndSettle();
expect(first.provider.value.uri.path, '/settings');
await tester.pumpWidget(const SizedBox.shrink());
await tester.pump();
final restored = await mount(tester, route: '/');
await tester.pumpAndSettle();
expect(restored.provider.value.uri.path, '/settings');
expect(find.byType(SettingsScreen), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
await tester.pump();
final linked = await mount(tester, route: '/runs/8/stage/plan?visit=2');
expect(
linked.provider.value.uri.toString(),
'/runs/8/stage/plan?visit=2',
);
await tab(tester, 'chat');
await tab(tester, 'runs');
expect(
linked.provider.value.uri.toString(),
'/runs/8/stage/plan?visit=2',
);
await tab(tester, 'pipelines');
expect(linked.provider.value.uri.path, '/settings');
expect(tester.takeException(), isNull);
},
);
testWidgets(
'a delayed read merges locations without redirecting a user move',
(tester) async {
final store = MemoryTabLocationsStore()..read = Completer();
final app = await mount(tester, route: '/', store: store);
await tab(tester, 'chat');
expect(store.writes, isEmpty);
store.read!.complete(
const TabLocationsSnapshot(
active: AppDestination.pipelines,
routes: {AppDestination.pipelines: AppRoute.settings},
),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));
expect(app.provider.value.uri.path, '/chat');
await tab(tester, 'pipelines');
expect(app.provider.value.uri.path, '/settings');
expect(tester.takeException(), isNull);
},
);
testWidgets('an explicit root URL does not restore a different active tab', (
tester,
) async {
final store = MemoryTabLocationsStore()
..value = TabLocationsSnapshot(
active: AppDestination.runs,
routes: {
AppDestination.runs: AppRoute.run(9),
AppDestination.pipelines: AppRoute.settings,
},
);
final app = await mount(tester, route: '/pipelines', store: store);
expect(app.provider.value.uri.path, '/pipelines');
await tab(tester, 'runs');
expect(app.provider.value.uri.path, '/runs/9');
await tab(tester, 'pipelines');
expect(app.provider.value.uri.path, '/pipelines');
expect(tester.takeException(), isNull);
});
testWidgets('Settings does not clear the mounted Pipelines detail', (
tester,
) async {
final app = await mount(tester);
tester.view.physicalSize = const Size(1100, 1000);
await tester.pump();
final fixture = PipelineFixture();
await replyPipelines(tester, app.channel, fixture);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('pipeline-row-1')));
await tester.pump();
await replyPipelineDetail(tester, app.channel, fixture);
await tester.pumpAndSettle();
expect(app.provider.value.uri.path, '/pipelines/1');
final before = tester.widget<PipelinesScreen>(find.byType(PipelinesScreen));
expect(before.initialPipelineId, 1);
final pipelineState = tester.state(find.byType(PipelinesScreen));
await tapBarAction(
tester,
find.byKey(const Key('pipelines-open-settings')),
'Settings',
);
await tester.pumpAndSettle();
expect(app.provider.value.uri.path, '/settings');
final hidden = find.byType(PipelinesScreen, skipOffstage: false);
expect(tester.widget<PipelinesScreen>(hidden).initialPipelineId, 1);
expect(tester.state(hidden), same(pipelineState));
await tab(tester, 'chat');
await tab(tester, 'pipelines');
expect(app.provider.value.uri.path, '/settings');
expect(tester.widget<PipelinesScreen>(hidden).initialPipelineId, 1);
expect(tester.takeException(), isNull);
});
}
@@ -0,0 +1,250 @@
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);
}
}
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);
},
);
}