-
-
Notifications
You must be signed in to change notification settings - Fork 448
fix: remove broken stale request check and handle network errors in autocomplete #7441
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
base: develop
Are you sure you want to change the base?
Changes from 3 commits
daf43e7
3611e24
2cc94a8
09b4200
018aa19
afdf76b
9e09e0a
9e84865
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,66 @@ | ||||||
| import 'package:flutter_test/flutter_test.dart'; | ||||||
| import 'package:openfoodfacts/openfoodfacts.dart'; | ||||||
|
|
||||||
| /// Mock autocompleter that simulates slow/failing network | ||||||
| class _MockAutocompleter implements Autocompleter { | ||||||
| _MockAutocompleter({this.delay = Duration.zero, this.shouldFail = false}); | ||||||
|
|
||||||
| final Duration delay; | ||||||
| final bool shouldFail; | ||||||
| int callCount = 0; | ||||||
|
|
||||||
| @override | ||||||
| Future<List<String>> getSuggestions(String input) async { | ||||||
| callCount++; | ||||||
| await Future<void>.delayed(delay); | ||||||
| if (shouldFail) { | ||||||
| throw Exception('Network error'); | ||||||
| } | ||||||
| return <String>['$input-result1', '$input-result2']; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| void main() { | ||||||
| group('AutocompleteManager', () { | ||||||
| test('cache hit returns immediately without server call', () async { | ||||||
| final _MockAutocompleter mock = _MockAutocompleter(); | ||||||
| final AutocompleteManager manager = AutocompleteManager(mock); | ||||||
|
|
||||||
| // First call hits server | ||||||
| final List<String> first = await manager.getSuggestions('bo'); | ||||||
| expect(first, contains('bo-result1')); | ||||||
| expect(mock.callCount, 1); | ||||||
|
|
||||||
| // Second call should hit cache — no new server call | ||||||
| final List<String> second = await manager.getSuggestions('bo'); | ||||||
| expect(second, contains('bo-result1')); | ||||||
| expect(mock.callCount, 1); // still 1 — cache served it | ||||||
| }); | ||||||
|
|
||||||
| test('returns most recent cached result when out of order', () async { | ||||||
| final _MockAutocompleter slowMock = _MockAutocompleter( | ||||||
| delay: const Duration(milliseconds: 100), | ||||||
| ); | ||||||
| final AutocompleteManager manager = AutocompleteManager(slowMock); | ||||||
|
|
||||||
| // Fire both requests simultaneously | ||||||
| final Future<List<String>> boFuture = manager.getSuggestions('bo'); | ||||||
| final Future<List<String>> botFuture = manager.getSuggestions('bot'); | ||||||
|
|
||||||
| final List<String> boResult = await boFuture; | ||||||
| final List<String> botResult = await botFuture; | ||||||
|
|
||||||
| // Both should have cached their own results | ||||||
| expect(boResult, isNotEmpty); | ||||||
| expect(botResult, isNotEmpty); | ||||||
| }); | ||||||
|
Comment on lines
+40
to
+56
|
||||||
|
|
||||||
| test('network failure throws exception', () async { | ||||||
| final _MockAutocompleter failMock = _MockAutocompleter(shouldFail: true); | ||||||
| final AutocompleteManager manager = AutocompleteManager(failMock); | ||||||
|
|
||||||
| // Should throw — smooth-app catch block must handle this | ||||||
| expect(() => manager.getSuggestions('bo'), throwsException); | ||||||
|
||||||
| expect(() => manager.getSuggestions('bo'), throwsException); | |
| await expectLater(manager.getSuggestions('bo'), throwsException); |
Uh oh!
There was an error while loading. Please reload this page.