2026-03-29 20:26:30 +08:00
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2026-03-31 18:26:36 +08:00
|
|
|
import 'package:social_app/data/cache/cache_scope.dart';
|
2026-03-29 20:26:30 +08:00
|
|
|
import 'package:social_app/data/cache/cache_store.dart';
|
|
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
|
|
|
|
|
|
group('HybridCacheStore', () {
|
|
|
|
|
test('falls back to persistent and backfills memory', () async {
|
|
|
|
|
SharedPreferences.setMockInitialValues(<String, Object>{});
|
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
|
final persistent = PersistentCacheStore(prefs: prefs);
|
|
|
|
|
await persistent.write<String>('k', 'v');
|
|
|
|
|
|
|
|
|
|
final hybrid = HybridCacheStore(
|
|
|
|
|
memory: MemoryCacheStore(),
|
|
|
|
|
persistent: persistent,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final firstRead = await hybrid.read<String>('k');
|
|
|
|
|
expect(firstRead, 'v');
|
|
|
|
|
|
|
|
|
|
await persistent.remove('k');
|
|
|
|
|
final secondRead = await hybrid.read<String>('k');
|
|
|
|
|
expect(secondRead, 'v');
|
|
|
|
|
});
|
2026-03-31 18:26:36 +08:00
|
|
|
|
|
|
|
|
test('cache invalidator no-ops without configured scope', () async {
|
|
|
|
|
CacheScope.resetProvider();
|
|
|
|
|
final invalidator = CacheInvalidator(
|
|
|
|
|
store: HybridCacheStore(
|
|
|
|
|
memory: MemoryCacheStore(),
|
|
|
|
|
persistent: PersistentCacheStore(),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(
|
|
|
|
|
() => invalidator.invalidate('calendar:day:2026-03-31'),
|
|
|
|
|
returnsNormally,
|
|
|
|
|
);
|
|
|
|
|
});
|
2026-03-29 20:26:30 +08:00
|
|
|
});
|
|
|
|
|
}
|