import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/application/app_route.dart'; import 'package:pipelines_app/application/app_router.dart'; AppRouterDelegate delegate(AppRoute initial, {void Function()? onChat}) => AppRouterDelegate( builder: (context, route) => const SizedBox.shrink(), onPopToRuns: () {}, onPopToChat: onChat ?? () {}, cupertino: false, initial: initial, ); void main() { TestWidgetsFlutterBinding.ensureInitialized(); test('system Back leaves one chat session for the chat list', () async { var left = 0; final router = delegate(AppRoute.chatSession('s1'), onChat: () => left++); addTearDown(router.dispose); expect(await router.popRoute(), isTrue); expect(left, 1); }); test('system Back steps up one level at a time', () async { var runs = 0; final router = AppRouterDelegate( builder: (context, route) => const SizedBox.shrink(), onPopToRuns: () => runs++, onPopToChat: () {}, cupertino: false, initial: AppRoute.run.stage(77, 'plan', visit: 0), ); addTearDown(router.dispose); // A stage returns to its run, then the run to the Runs list. expect(await router.popRoute(), isTrue); expect(router.route, AppRoute.run(77)); expect(await router.popRoute(), isTrue); expect(runs, 1); // A pipeline stage returns to its pipeline, then to the list, and the // list arms exit, then leaves Back to the platform. router.goTo(AppRoute.pipelineStage(4, 'plan', version: 2)); expect(await router.popRoute(), isTrue); expect(router.route, AppRoute.pipeline(4, version: 2)); expect(await router.popRoute(), isTrue); expect(router.route, AppRoute.pipelines); expect(await router.popRoute(), isTrue); expect(await router.popRoute(), isFalse); }); test( 'Android list Back arms exit before handing it to the platform', () async { final router = delegate(AppRoute.chat); addTearDown(router.dispose); expect(await router.popRoute(), isTrue); expect(await router.popRoute(), isFalse); }, ); for (final root in [ AppRoute.pipelines, AppRoute.runs, AppRoute.runsForPipeline(3), AppRoute.chat, AppRoute.settings, ]) { testWidgets('${root.path} root exit expires and navigation clears it', ( tester, ) async { final router = AppRouterDelegate( builder: (_, route) => Scaffold(body: Text(route.path)), onPopToRuns: () {}, onPopToChat: () {}, cupertino: false, initial: root, ); addTearDown(router.dispose); await tester.pumpWidget( MaterialApp.router( routerDelegate: router, routeInformationParser: const AppRouteParser(), routeInformationProvider: PlatformRouteInformationProvider( initialRouteInformation: RouteInformation( uri: Uri.parse(root.path), ), ), ), ); await tester.pumpAndSettle(); expect(await router.popRoute(), isTrue); await tester.pumpAndSettle(); expect(find.text('Press back again to close'), findsOneWidget); expect(await router.popRoute(), isFalse); await tester.pump(const Duration(seconds: 2)); await tester.pumpAndSettle(); expect(await router.popRoute(), isTrue); await tester.pump(); router.goTo(AppRoute.pipeline(7)); await tester.pumpAndSettle(); expect(await router.popRoute(), isTrue); await tester.pumpAndSettle(); expect(router.route, AppRoute.pipelines); expect(find.text('Press back again to close'), findsNothing); expect(await router.popRoute(), isTrue); await tester.pumpAndSettle(); expect(find.text('Press back again to close'), findsOneWidget); // Imperative dialogs also clear an already armed root exit. final navigator = tester.state(find.byType(Navigator)); showDialog( context: navigator.overlay!.context, builder: (_) => const AlertDialog(title: Text('Root dialog')), ); await tester.pumpAndSettle(); expect(await router.popRoute(), isTrue); await tester.pumpAndSettle(); expect(find.text('Root dialog'), findsNothing); expect(await router.popRoute(), isTrue); await tester.pumpAndSettle(); expect(await router.popRoute(), isFalse); // Switching tabs or applying a platform/browser route re-arms exit too. router.goTo(AppRoute.settings); await tester.pumpAndSettle(); expect(await router.popRoute(), isTrue); await tester.pumpAndSettle(); await router.setNewRoutePath(AppRoute.chat); await tester.pumpAndSettle(); expect(await router.popRoute(), isTrue); await tester.pump(const Duration(seconds: 3)); await tester.pumpWidget(const SizedBox.shrink()); }); } test('iOS root Back is not intercepted', () async { debugDefaultTargetPlatformOverride = TargetPlatform.iOS; addTearDown(() => debugDefaultTargetPlatformOverride = null); final router = delegate(AppRoute.pipelines); addTearDown(router.dispose); expect(await router.popRoute(), isFalse); expect(await router.popRoute(), isFalse); }); test('Templates opens above Settings and Back restores the route', () async { final router = delegate(AppRoute.settings); addTearDown(router.dispose); router.openTemplates(); expect(router.route, AppRoute.templates); expect(router.templatesOpen, isTrue); expect(await router.popRoute(), isTrue); expect(router.route, AppRoute.settings); expect(router.templatesOpen, isFalse); }); test('a /templates boot link still closes back to Settings', () async { final router = delegate(AppRoute.templates); addTearDown(router.dispose); router.closeTemplates(); expect(router.route, AppRoute.settings); }); test('the parser keeps a query and restores it as a query', () async { const parser = AppRouteParser(); final route = await parser.parseRouteInformation( RouteInformation(uri: Uri.parse('/runs/77/stage/plan?visit=1')), ); expect(route, AppRoute.run.stage(77, 'plan', visit: 1)); final restored = parser.restoreRouteInformation(route).uri; expect(restored.path, '/runs/77/stage/plan'); expect(restored.query, 'visit=1'); expect(restored.toString(), '/runs/77/stage/plan?visit=1'); // A reload or Back/Forward feeds the restored location straight back in. expect(AppRoute.parse(restored.toString()), route); }); test('every query route survives a restore and reparse', () { const parser = AppRouteParser(); for (final route in [ AppRoute.runsForPipeline(7), AppRoute.run.decide(3), AppRoute.run.stage(3, 'plan', visit: 0), AppRoute.run.stage(5, 'build ✓', visit: 4), ]) { final uri = parser.restoreRouteInformation(route).uri; expect(uri.toString(), route.path, reason: route.path); expect(uri.path, isNot(contains('%3F')), reason: route.path); expect(AppRoute.parse(uri.toString()), route, reason: route.path); } }); test('a web launch keeps the path and its query', () { final visit = routeInformationForLocation( Uri.parse('https://example.test/runs/7/stage/plan?visit=2'), ); expect(visit.uri.toString(), '/runs/7/stage/plan?visit=2'); expect(visit.uri.path, '/runs/7/stage/plan'); expect(visit.uri.query, 'visit=2'); final decide = routeInformationForLocation( Uri.parse('https://example.test/runs/9?action=decide'), ); expect(decide.uri.toString(), '/runs/9?action=decide'); expect( routeInformationForLocation( Uri.parse('https://example.test/'), ).uri.toString(), '/', ); }); test('the native notification link opens its run', () async { const parser = AppRouteParser(); final run = await parser.parseRouteInformation( RouteInformation(uri: Uri.parse('pipelines://run/42')), ); expect(run, AppRoute.run(42)); final decide = await parser.parseRouteInformation( RouteInformation(uri: Uri.parse('pipelines://run/42?action=decide')), ); expect(decide.path, '/runs/42?action=decide'); // Another scheme or host is not a run link. expect(routeLocation(Uri.parse('pipelines://other/42')), '/42'); }); }