Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ class _SmoothAutocompleteTextFieldState
return _SearchResults.empty();
}

final DateTime start = DateTime.now();

if (_suggestions[search] != null) {
return _suggestions[search]!;
Expand All @@ -222,19 +221,16 @@ class _SmoothAutocompleteTextFieldState
_suggestions[search] = _SearchResults(
await widget.manager!.getSuggestions(search),
);
} catch (_) {}
} catch (_) {
_setLoading(false);
return _SearchResults.empty();
}
Comment thread
Sherley-Sonali marked this conversation as resolved.

if (_suggestions[search]?.isEmpty ?? true && search == _searchInput) {
_setLoading(false);
}

if (_searchInput != search &&
start.difference(DateTime.now()).inSeconds > 5) {
// Ignore this request, it's too long and this is not even the current search
return _SearchResults.empty();
} else {
return _suggestions[search] ?? _SearchResults.empty();
}
return _suggestions[search] ?? _SearchResults.empty();
Comment thread
Sherley-Sonali marked this conversation as resolved.
}
}

Expand Down
74 changes: 74 additions & 0 deletions packages/smooth_app/test/pages/autocomplete_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name/intent (“returns most recent cached result when out of order”) isn’t being asserted: both requests use the same delay and the expectations only check isNotEmpty. This can pass even if out-of-order/stale-result handling is broken. Either rename the test to what it actually verifies, or make it deterministic by forcing out-of-order completion (different delays) and asserting the manager returns/keeps the latest-query result.

Copilot uses AI. Check for mistakes.

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,
);
});
});
}
Loading