test(bridge): inject the lookup cache persist debounce

The lookup cache test waited out the 500 ms production debounce four
times (2.7 s). A @visibleForTesting setter lets the test use a 10 ms
debounce while keeping the same 50 to 250 ms margin after the timer
fires for the background encode. The production default stays 500 ms
and the assertions are unchanged.
This commit is contained in:
zarzet committed 2026-10-19 05:10:50 +07:00
1 parent 202923fbd9
commit c7cccd4edb
2 files changed
+20 -5

No files matched your search

+10 -1
View File
@@ -145,7 +145,9 @@ class PlatformBridge {
static const _bridgeCacheMaxBytes = 8 * 1024 * 1024;
static const _persistentCacheMaxEntryBytes = 1024 * 1024;
static const _persistentCacheMaxBytes = 2 * 1024 * 1024;
static const _lookupCachePersistDebounce = Duration(milliseconds: 500);
static const _defaultLookupCachePersistDebounce = Duration(milliseconds: 500);
static Duration _lookupCachePersistDebounce =
_defaultLookupCachePersistDebounce;
static const _metadataPersistentCacheKey = 'bridge_metadata_lookup_cache_v1';
static const _downloadProgressEvents = EventChannel(
'com.zarz.spotiflac/download_progress_stream',
@@ -574,6 +576,13 @@ class PlatformBridge {
}
}
/// Shortens the lookup cache persist debounce so tests need not wait the
/// production delay. Null restores the default.
@visibleForTesting
static set lookupCachePersistDebounceForTesting(Duration? value) {
_lookupCachePersistDebounce = value ?? _defaultLookupCachePersistDebounce;
}
static void _scheduleLookupCachePersist(
Map<String, _BridgeCacheEntry<Map<String, dynamic>>> cache,
String prefsKey,
+10 -4
View File
@@ -13,16 +13,22 @@ void main() {
final messenger =
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
// The waits below keep the original margin after the debounce fires (50 to
// 250 ms for the background encode) while skipping the 500 ms default.
const debounce = Duration(milliseconds: 10);
setUp(() async {
SharedPreferences.setMockInitialValues({});
messenger.setMockMethodCallHandler(channel, (_) async => null);
await PlatformBridge.clearTrackCache();
PlatformBridge.lookupCachePersistDebounceForTesting = debounce;
});
tearDown(() async {
messenger.setMockMethodCallHandler(channel, (_) async => null);
await PlatformBridge.clearTrackCache();
messenger.setMockMethodCallHandler(channel, null);
PlatformBridge.lookupCachePersistDebounceForTesting = null;
});
Map<String, dynamic> collection([int count = 2]) => {
@@ -57,7 +63,7 @@ void main() {
expect(results.last['text'], isNot('changed'));
expect((await load())['text'], isNot('changed'));
expect(calls, 2);
await Future<void>.delayed(const Duration(milliseconds: 550));
await Future<void>.delayed(debounce + const Duration(milliseconds: 50));
final prefs = await SharedPreferences.getInstance();
final stored = prefs.getString('bridge_metadata_lookup_cache_v1');
expect(stored == null || jsonDecode(stored) is Map, true);
@@ -95,7 +101,7 @@ void main() {
});
await PlatformBridge.getProviderMetadata('example', 'album', 'large');
await PlatformBridge.getProviderMetadata('example', 'track', 'small');
await Future<void>.delayed(const Duration(milliseconds: 650));
await Future<void>.delayed(debounce + const Duration(milliseconds: 150));
final prefs = await SharedPreferences.getInstance();
final stored = prefs.getString('bridge_metadata_lookup_cache_v1')!;
expect(utf8.encode(stored).length, lessThanOrEqualTo(2 * 1024 * 1024));
@@ -120,7 +126,7 @@ void main() {
for (final id in ['escaped', 'multibyte', 'small']) {
await PlatformBridge.getProviderMetadata('example', 'album', id);
}
await Future<void>.delayed(const Duration(milliseconds: 750));
await Future<void>.delayed(debounce + const Duration(milliseconds: 250));
final prefs = await SharedPreferences.getInstance();
final stored =
jsonDecode(prefs.getString('bridge_metadata_lookup_cache_v1')!)
@@ -137,7 +143,7 @@ void main() {
for (var index = 0; index < 3; index++) {
await PlatformBridge.getProviderMetadata('example', 'album', '$index');
}
await Future<void>.delayed(const Duration(milliseconds: 750));
await Future<void>.delayed(debounce + const Duration(milliseconds: 250));
final prefs = await SharedPreferences.getInstance();
final raw = prefs.getString('bridge_metadata_lookup_cache_v1')!;
expect(utf8.encode(raw).length, lessThanOrEqualTo(2 * 1024 * 1024));