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 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(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(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(hidden).initialPipelineId, 1); await tester.tap(find.byKey(const Key('shell.back'))); await tester.pumpAndSettle(); expect(app.provider.value.uri.path, '/pipelines/1'); expect(tester.state(find.byType(PipelinesScreen)), same(pipelineState)); expect( tester .widget(find.byType(PipelinesScreen)) .initialPipelineId, 1, ); expect(tester.takeException(), isNull); }); for (final cupertino in [false, true]) { testWidgets('ordinary root with query restores its tab ($cupertino)', ( tester, ) async { final store = MemoryTabLocationsStore() ..value = const TabLocationsSnapshot( routes: {AppDestination.pipelines: AppRoute.settings}, ); final app = await mount( tester, route: '/?appearance=cupertino', cupertino: cupertino, store: store, ); await tester.pumpAndSettle(); expect(app.provider.value.uri.path, '/settings'); expect(find.byType(SettingsScreen), findsOneWidget); expect(tester.takeException(), isNull); }); } }