This commit is contained in:
macbook_max
2026-10-15 20:36:59 +08:00
commit 6bf949949c
7923 changed files with 1369582 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
// 搜索历史本地存储单测:去重置顶 / 上限 10 条 / 删除 / 清空
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:video_app/core/storage/search_history_store.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
Future<SearchHistoryStore> setup([Map<String, Object> initial = const {}]) async {
SharedPreferences.setMockInitialValues(initial);
final prefs = await SharedPreferences.getInstance();
SearchHistoryStore.initForTest(prefs);
return SearchHistoryStore.I;
}
test('add 追加并最新在前', () async {
final store = await setup();
await store.add('日落');
await store.add('美食');
expect(store.history, ['美食', '日落']);
});
test('重复关键词去重置顶', () async {
final store = await setup();
await store.add('日落');
await store.add('美食');
await store.add('日落');
expect(store.history, ['日落', '美食']);
});
test('超出 10 条截断最旧记录', () async {
final store = await setup();
for (var i = 1; i <= 12; i++) {
await store.add('词$i');
}
expect(store.history, hasLength(10));
expect(store.history.first, '词12');
expect(store.history.last, '词3');
});
test('空白关键词不入库', () async {
final store = await setup();
await store.add(' ');
expect(store.history, isEmpty);
});
test('remove 删除单条,clear 清空', () async {
final store = await setup();
await store.add('日落');
await store.add('美食');
await store.remove('日落');
expect(store.history, ['美食']);
await store.clear();
expect(store.history, isEmpty);
});
}