import 'package:flutter_test/flutter_test.dart'; import 'package:social_app/data/cache/cache_policy.dart'; import 'package:social_app/data/cache/cache_scope.dart'; import 'package:social_app/data/cache/cached_repository.dart'; import 'package:social_app/data/cache/cache_store.dart'; class _IntCachedRepository extends CachedRepository { int loadCount = 0; _IntCachedRepository({required super.store}) : super( policy: const CachePolicy( softTtl: Duration(hours: 1), hardTtl: Duration(hours: 2), minRefreshInterval: Duration(minutes: 10), ), ); Future fetch({bool forceRefresh = false}) { return getOrLoad( key: 'test:number', forceRefresh: forceRefresh, loadFromRemote: () async { loadCount += 1; return loadCount; }, ); } } void main() { group('CachedRepository', () { setUp(() { CacheScope.configureProvider(() => 'user:test'); }); tearDown(() { CacheScope.resetProvider(); }); test('reads from cache after first load', () async { final repo = _IntCachedRepository( store: HybridCacheStore( memory: MemoryCacheStore(), persistent: PersistentCacheStore(), ), ); final first = await repo.fetch(); final second = await repo.fetch(); expect(first, 1); expect(second, 1); expect(repo.loadCount, 1); }); test('force refresh bypasses cached value', () async { final repo = _IntCachedRepository( store: HybridCacheStore( memory: MemoryCacheStore(), persistent: PersistentCacheStore(), ), ); await repo.fetch(); final refreshed = await repo.fetch(forceRefresh: true); expect(refreshed, 2); expect(repo.loadCount, 2); }); test('falls back to remote load when scope is unavailable', () async { CacheScope.resetProvider(); final repo = _IntCachedRepository( store: HybridCacheStore( memory: MemoryCacheStore(), persistent: PersistentCacheStore(), ), ); final first = await repo.fetch(); final second = await repo.fetch(); expect(first, 1); expect(second, 2); expect(repo.loadCount, 2); }); }); }