-
Notifications
You must be signed in to change notification settings - Fork 28
fix: do not let a cold-start deep link become the router's location #673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
21Mill
wants to merge
3
commits into
MostroP2P:main
Choose a base branch
from
21Mill:fix/deep-link-cold-start-crash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /// Whether the URI uses a scheme the app resolves itself, such as `mostro:` | ||
| bool isCustomSchemeUri(Uri uri) => | ||
| uri.scheme.isNotEmpty && uri.scheme != 'http' && uri.scheme != 'https'; | ||
|
|
||
| /// [isCustomSchemeUri] for an unparsed location; unparseable input is | ||
| /// treated as not custom. | ||
| bool isCustomSchemeLocation(String location) { | ||
| final uri = Uri.tryParse(location); | ||
| return uri != null && isCustomSchemeUri(uri); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:go_router/go_router.dart'; | ||
| import 'package:mostro_mobile/core/app_routes.dart'; | ||
| import 'package:mostro_mobile/shared/providers/storage_providers.dart'; | ||
| import 'package:shared_preferences/shared_preferences.dart'; | ||
| import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart'; | ||
| import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart'; | ||
|
|
||
| const _mostroLink = | ||
| 'mostro:8927bb1d-da68-491e-b0e2-db0ed548d52c?relays=wss://relay.mostro.network'; | ||
|
|
||
| /// Holds a single router for the test, so a rebuild cannot make another one. | ||
| class _RouterHost extends ConsumerStatefulWidget { | ||
| const _RouterHost(); | ||
|
|
||
| @override | ||
| ConsumerState<_RouterHost> createState() => _RouterHostState(); | ||
| } | ||
|
|
||
| class _RouterHostState extends ConsumerState<_RouterHost> { | ||
| late final GoRouter router = createRouter(ref); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) => const SizedBox.shrink(); | ||
| } | ||
|
|
||
| /// Builds the app's real router inside a scope that can resolve it, and hands | ||
| /// it back without mounting any screen. | ||
| Future<GoRouter> buildRouter(WidgetTester tester) async { | ||
| await tester.pumpWidget( | ||
| ProviderScope( | ||
| overrides: [ | ||
| sharedPreferencesProvider.overrideWithValue(SharedPreferencesAsync()), | ||
| ], | ||
| child: const _RouterHost(), | ||
| ), | ||
| ); | ||
| final router = | ||
| tester.state<_RouterHostState>(find.byType(_RouterHost)).router; | ||
| addTearDown(router.dispose); | ||
| return router; | ||
| } | ||
|
|
||
| void main() { | ||
| setUp(() { | ||
| SharedPreferencesAsyncPlatform.instance = | ||
| InMemorySharedPreferencesAsync.empty(); | ||
| }); | ||
|
|
||
| group('createRouter initial location', () { | ||
| // Regression test for #670: go_router preferred the cold-start deep link | ||
| // over initialLocation and asserted while matching it. | ||
| testWidgets('ignores a custom scheme handed over by the platform', | ||
| (tester) async { | ||
| tester.binding.platformDispatcher.defaultRouteNameTestValue = _mostroLink; | ||
| addTearDown( | ||
| tester.binding.platformDispatcher.clearDefaultRouteNameTestValue); | ||
|
|
||
| final router = await buildRouter(tester); | ||
|
|
||
| expect( | ||
| router.routeInformationProvider.value.uri.toString(), | ||
| '/', | ||
| ); | ||
| expect(tester.takeException(), isNull); | ||
| }); | ||
|
|
||
| testWidgets('starts at the root on an ordinary launch', (tester) async { | ||
| tester.binding.platformDispatcher.defaultRouteNameTestValue = '/'; | ||
| addTearDown( | ||
| tester.binding.platformDispatcher.clearDefaultRouteNameTestValue); | ||
|
|
||
| final router = await buildRouter(tester); | ||
|
|
||
| expect(router.routeInformationProvider.value.uri.toString(), '/'); | ||
| expect(tester.takeException(), isNull); | ||
| }); | ||
|
|
||
| // On web the platform default is a real location and must still win. | ||
| testWidgets('honours a real location handed over by the platform', | ||
| (tester) async { | ||
| tester.binding.platformDispatcher.defaultRouteNameTestValue = '/settings'; | ||
| addTearDown( | ||
| tester.binding.platformDispatcher.clearDefaultRouteNameTestValue); | ||
|
|
||
| final router = await buildRouter(tester); | ||
|
|
||
| expect( | ||
| router.routeInformationProvider.value.uri.toString(), | ||
| '/settings', | ||
| ); | ||
| expect(tester.takeException(), isNull); | ||
| }); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:mostro_mobile/core/deep_link_schemes.dart'; | ||
|
|
||
| void main() { | ||
| group('isCustomSchemeLocation', () { | ||
| test('claims mostro links', () { | ||
| expect( | ||
| isCustomSchemeLocation( | ||
| 'mostro:8927bb1d-da68-491e-b0e2-db0ed548d52c' | ||
| '?relays=wss://relay.mostro.network', | ||
| ), | ||
| isTrue, | ||
| ); | ||
| expect(isCustomSchemeLocation('mostro:'), isTrue); | ||
| }); | ||
|
|
||
| test('claims other non-web schemes', () { | ||
| expect( | ||
| isCustomSchemeLocation('lightning:lnbc1...'), | ||
| isTrue, | ||
| ); | ||
| }); | ||
|
|
||
| test('leaves app locations alone', () { | ||
| expect(isCustomSchemeLocation('/'), isFalse); | ||
| expect( | ||
| isCustomSchemeLocation('/take_sell/order-1'), | ||
| isFalse, | ||
| ); | ||
| expect( | ||
| isCustomSchemeLocation('/settings?tab=relays'), | ||
| isFalse, | ||
| ); | ||
| expect(isCustomSchemeLocation(''), isFalse); | ||
| }); | ||
|
|
||
| test('claims schemes that merely start like a web one', () { | ||
| expect( | ||
| isCustomSchemeLocation('httpfoo://example.com'), | ||
| isTrue, | ||
| ); | ||
| }); | ||
|
|
||
| test('leaves web locations alone', () { | ||
| expect( | ||
| isCustomSchemeLocation('https://mostro.network/x'), | ||
| isFalse, | ||
| ); | ||
| expect( | ||
| isCustomSchemeLocation('http://localhost:8080/'), | ||
| isFalse, | ||
| ); | ||
| // Uri normalises the scheme, so no case handling of our own is needed. | ||
| expect( | ||
| isCustomSchemeLocation('HTTPS://mostro.network/x'), | ||
| isFalse, | ||
| ); | ||
| }); | ||
|
|
||
| test('treats an unparseable location as an ordinary one', () { | ||
| // Nothing we could hand to the deep link handler either. | ||
| expect(isCustomSchemeLocation('::::'), isFalse); | ||
| }); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 Nitpick
createRouter(ref)runs inside aConsumer.builder, which may be invoked more than once — each pass constructs anotherGoRouterthat nobody disposes, and thelate routerassignment quietly depends on build ordering.A small
StatefulWidgetcreating the router ininitState, or a plainProviderContainer+Consumer-free call, would make this deterministic. Not blocking — the assertions themselves are good, and I confirmed the first test does fail against the unfixed router.