import 'dart:async'; import 'dart:convert'; 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/features/walkthrough/walkthrough_store.dart'; import 'package:pipelines_app/shell/destination.dart'; import 'package:pipelines_app/shell/shell_navigation_bar.dart'; import 'package:pipelines_app/ui/adaptive/floating_destination_bar.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', 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( androidSupported: false, server: webServer(), client: client, 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() { // The first-run walkthrough would sit over the Pipelines tab and absorb the // bar taps these tests make; a device that has seen it has no overlay. setUp( () => SharedPreferences.setMockInitialValues({walkthroughSeenKey: true}), ); testWidgets('Settings keeps its state across Pipelines, Runs and back', ( tester, ) async { final app = await mount(tester); await replyPipelines(tester, app.channel, PipelineFixture()); await tester.pumpAndSettle(); await tab(tester, 'settings'); expect(app.provider.value.uri.path, '/settings'); // A destination opened from its nav item: no Back. expect(find.byKey(const ValueKey('shell.back')), findsNothing); final settingsState = tester.state(find.byType(SettingsScreen)); await tab(tester, 'runs'); expect(app.provider.value.uri.path, '/runs'); expect(find.byType(SettingsScreen), findsNothing); await tab(tester, 'settings'); 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, '/pipelines', reason: 'the Pipelines item leaves Settings for its list', ); expect(find.byKey(const ValueKey('shell.back')), findsNothing); expect(tester.takeException(), isNull); }); testWidgets('re-selecting Pipelines does not show Settings', (tester) async { final app = await mount(tester); await replyPipelines(tester, app.channel, PipelineFixture()); await tester.pumpAndSettle(); await tab(tester, 'settings'); expect(app.provider.value.uri.path, '/settings'); await tab(tester, 'pipelines'); expect(app.provider.value.uri.path, '/pipelines'); expect(find.byType(SettingsScreen), findsNothing); await tab(tester, 'pipelines'); expect(app.provider.value.uri.path, '/pipelines'); expect(find.byType(SettingsScreen), findsNothing); expect(tester.takeException(), isNull); }); testWidgets('on /settings only Settings is selected in the bar and rail', ( tester, ) async { await mount(tester, route: '/settings'); await tester.pumpAndSettle(); // A phone: the bar's last destination is Settings. expect( tester .widget( find.byKey(const ValueKey('shell.navigation-bar')), ) .selectedIndex, shellBarDestinations.indexOf(AppDestination.settings), ); // A window beside the list-detail minimum: the floating tab bar lights no // tab, Pipelines included. tester.view.physicalSize = const Size(650, 1000); await tester.pumpAndSettle(); expect( tester .widget(find.byType(FloatingDestinationBar)) .currentIndex, -1, ); // A wide window: the rail selects no tab and Settings alone is selected. tester.view.physicalSize = const Size(1100, 1000); await tester.pumpAndSettle(); expect( tester .widget( find.byKey(const ValueKey('shell.navigation-rail')), ) .selectedIndex, isNull, ); expect(find.byType(SettingsScreen), findsOneWidget); expect(tester.takeException(), isNull); }); testWidgets('selecting Settings after a Settings sub-page opens /settings', ( tester, ) async { final app = await mount(tester); await replyPipelines(tester, app.channel, PipelineFixture()); await tester.pumpAndSettle(); // Schedules is a Settings sub-page, opened from the Pipelines bar. await tester.tap(find.byKey(const Key('pipelines-open-schedules'))); await tester.pumpAndSettle(); expect(app.provider.value.uri.path, '/schedules'); // Leave the sub-page for Runs, as a pushed URL would, then select Settings. await tester.binding.handlePushRoute('/runs'); await tester.pumpAndSettle(); expect(app.provider.value.uri.path, '/runs'); await tab(tester, 'settings'); expect(app.provider.value.uri.path, '/settings'); expect(find.byType(SettingsScreen), findsOneWidget); expect(tester.takeException(), isNull); }); testWidgets('reload on /settings restores Settings', (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); 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, 'settings'); 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.settings, routes: {AppDestination.settings: AppRoute.settings}, ), ); await tester.pump(); await tester.pump(const Duration(milliseconds: 500)); expect(app.provider.value.uri.path, '/chat'); await tab(tester, 'settings'); 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.settings: 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(); final firstList = pipelineRequest(app.channel, 'list_pipelines')!['id']; final firstRuns = pipelineRequest(app.channel, 'list_runs')!['id']; await tester.tap(find.byKey(const Key('pipeline-row-1'))); await tester.pump(); // The wide layout re-reads the list and the runs; their replies come // before the detail's, as the transport serializes requests. final reread = await waitForLaterRequest( tester, app.channel, 'list_pipelines', firstList as String, ); app.channel.incoming.add( jsonEncode({ 'id': reread['id'], 'type': 'pipelines', 'pipelines': fixture.summaries, }), ); await tester.pump(); final rerunRuns = await waitForLaterRequest( tester, app.channel, 'list_runs', firstRuns as String, ); app.channel.incoming.add( jsonEncode({'id': rerunRuns['id'], 'type': 'runs', 'runs': fixture.runs}), ); await tester.pump(); await waitForRequest(tester, app.channel, 'chat_sessions'); final sessions = pipelineRequest(app.channel, 'chat_sessions')!; app.channel.incoming.add( jsonEncode({ 'id': sessions['id'], 'type': 'chat_sessions', 'chat_sessions': [], }), ); 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 tab(tester, 'settings'); 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, '/pipelines/1'); expect(tester.widget(hidden).initialPipelineId, 1); await tab(tester, 'settings'); expect(app.provider.value.uri.path, '/settings'); // Settings has no Back; the Pipelines item returns to the open pipeline. expect(find.byKey(const ValueKey('shell.back')), findsNothing); await tab(tester, 'pipelines'); 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); }); testWidgets('ordinary root with query restores its tab', (tester) async { final store = MemoryTabLocationsStore() ..value = const TabLocationsSnapshot( active: AppDestination.settings, routes: {AppDestination.settings: AppRoute.settings}, ); final app = await mount(tester, route: '/?preview=1', store: store); await tester.pumpAndSettle(); expect(app.provider.value.uri.path, '/settings'); expect(find.byType(SettingsScreen), findsOneWidget); expect(tester.takeException(), isNull); }); }