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) => AppRouterDelegate( builder: (context, route) => const SizedBox.shrink(), cupertino: false, initial: initial, ); Future pumpRouter( WidgetTester tester, AppRoute initial, ) async { final router = AppRouterDelegate( builder: (_, route) => Scaffold(body: Text(route.path)), templatesBuilder: (_) => const Scaffold(body: Text('Templates page')), cupertino: false, initial: initial, ); addTearDown(router.dispose); final provider = PlatformRouteInformationProvider( initialRouteInformation: RouteInformation(uri: Uri.parse(initial.path)), ); addTearDown(provider.dispose); await tester.pumpWidget( MaterialApp.router( routerDelegate: router, routeInformationParser: const AppRouteParser(), routeInformationProvider: provider, ), ); await tester.pumpAndSettle(); return router; } 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 [ AppRoute.pipelines, AppRoute.chat, AppRoute.chatSession('s1'), AppRoute.run.stage(77, 'plan', visit: 0), AppRoute.pipelineStage(4, 'plan', version: 2), ]) { final router = delegate(route); expect(await router.popRoute(), isFalse, reason: route.path); expect(router.route, route, reason: route.path); router.dispose(); } }); for (final root in [ AppRoute.pipelines, AppRoute.run(7), AppRoute.chat, AppRoute.settings, ]) { testWidgets('${root.path}: Back pops a dialog, then belongs to the ' 'platform with no exit prompt or step-up', (tester) async { final router = await pumpRouter(tester, root); final navigator = tester.state(find.byType(Navigator)); expect( find.byWidgetPredicate((widget) => widget is PopScope), findsNothing, ); 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(), isFalse); await tester.pumpAndSettle(); expect(find.text('Press back again to close'), findsNothing); expect(router.route, root); await tester.pumpWidget(const SizedBox.shrink()); }); } testWidgets('Templates is a page above Settings and Back pops it', ( tester, ) async { final router = await pumpRouter(tester, AppRoute.settings); router.openTemplates(); await tester.pumpAndSettle(); expect(find.text('Templates page'), findsOneWidget); expect(await router.popRoute(), isTrue); await tester.pumpAndSettle(); expect(router.route, AppRoute.settings); expect(router.templatesOpen, isFalse); await tester.pumpWidget(const SizedBox.shrink()); }); 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'); }); test('the native chat notification link opens its chat', () async { const parser = AppRouteParser(); final chat = await parser.parseRouteInformation( RouteInformation(uri: Uri.parse('pipelines://chat/ses_abc123')), ); expect(chat, AppRoute.chatSession('ses_abc123')); expect(chat.chatSessionId, 'ses_abc123'); }); }