// 观看去重存储单测:mark/contains 与跨日 key 惰性清理 import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:video_app/core/storage/watch_dedup_store.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); Future setupPrefs(Map initial) async { SharedPreferences.setMockInitialValues(initial); final prefs = await SharedPreferences.getInstance(); WatchDedupStore.initForTest(prefs); return prefs; } group('WatchDedupStore', () { test('markWatched 后 hasWatched 为 true,未标记为 false', () async { await setupPrefs(const {}); final store = WatchDedupStore.I; expect(store.hasWatched(101), isFalse); await store.markWatched(101); expect(store.hasWatched(101), isTrue); expect(store.hasWatched(102), isFalse); }); test('重复标记不产生重复记录', () async { final prefs = await setupPrefs(const {}); final store = WatchDedupStore.I; await store.markWatched(101); await store.markWatched(101); final key = prefs.getKeys().firstWhere((k) => k.startsWith('watch_')); expect(prefs.getStringList(key), ['101']); }); test('complete 与 watch 相互独立', () async { await setupPrefs(const {}); final store = WatchDedupStore.I; await store.markWatched(101); expect(store.hasCompleted(101), isFalse); await store.markCompleted(101); expect(store.hasCompleted(101), isTrue); }); test('访问时惰性清理非今日 key', () async { final prefs = await setupPrefs(const { 'watch_20200101': ['1', '2'], 'complete_20200101': ['3'], 'other_key': 'keep', }); final store = WatchDedupStore.I; // 触发清理 store.hasWatched(999); store.hasCompleted(999); expect(prefs.getKeys().contains('watch_20200101'), isFalse); expect(prefs.getKeys().contains('complete_20200101'), isFalse); // 无关 key 不受影响 expect(prefs.getString('other_key'), 'keep'); }); test('陈旧数据不会命中今日去重', () async { await setupPrefs(const { 'watch_20200101': ['101'], }); final store = WatchDedupStore.I; expect(store.hasWatched(101), isFalse); }); }); }