为什么:上一批归档只覆盖了「已跟踪文件的修改」,原型阶段新增的文件(app_config、theme、 home_feed/play_ticket 模型、catalog_api、测试目录等)还是未入库状态 —— 而已入库的代码会 import 它们, 留在工作区会导致 HEAD 处的代码编译不过。一并归档,让 HEAD 自洽。 纯归档,无行为改动。
136 lines
4.0 KiB
Dart
136 lines
4.0 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:nontonki/core/data/models/engagement_snapshot.dart';
|
|
import 'package:nontonki/core/data/repositories/engagement_repository.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
|
|
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
|
|
|
|
/// Mirrors the private key used by [SharedPreferencesEngagementRepository].
|
|
const _key = 'engagement.v1.demo-local-v1';
|
|
|
|
void main() {
|
|
late SharedPreferencesAsync preferences;
|
|
|
|
setUp(() {
|
|
SharedPreferencesAsyncPlatform.instance =
|
|
InMemorySharedPreferencesAsync.empty();
|
|
preferences = SharedPreferencesAsync();
|
|
});
|
|
|
|
SharedPreferencesEngagementRepository repository() =>
|
|
SharedPreferencesEngagementRepository(preferences: preferences);
|
|
|
|
Future<EngagementSnapshot> loadRaw(String raw) async {
|
|
await preferences.setString(_key, raw);
|
|
return repository().load();
|
|
}
|
|
|
|
test('a first launch reads an empty snapshot', () async {
|
|
final snapshot = await repository().load();
|
|
|
|
expect(snapshot.savedDramaIds, isEmpty);
|
|
expect(snapshot.likedDramaIds, isEmpty);
|
|
expect(snapshot.commentsByDrama, isEmpty);
|
|
});
|
|
|
|
test('a snapshot survives a save and reload round trip', () async {
|
|
await repository().save(
|
|
const EngagementSnapshot(
|
|
savedDramaIds: {'signal', 'city'},
|
|
likedDramaIds: {'city'},
|
|
commentsByDrama: {
|
|
'signal': ['first note', 'second note'],
|
|
},
|
|
),
|
|
);
|
|
|
|
final loaded = await repository().load();
|
|
|
|
expect(loaded.savedDramaIds, {'signal', 'city'});
|
|
expect(loaded.likedDramaIds, {'city'});
|
|
expect(loaded.commentsByDrama, {
|
|
'signal': ['first note', 'second note'],
|
|
});
|
|
});
|
|
|
|
test('an empty snapshot round trips as empty collections', () async {
|
|
await repository().save(const EngagementSnapshot());
|
|
|
|
final loaded = await repository().load();
|
|
|
|
expect(loaded.savedDramaIds, isEmpty);
|
|
expect(loaded.likedDramaIds, isEmpty);
|
|
expect(loaded.commentsByDrama, isEmpty);
|
|
});
|
|
|
|
test('corrupt or non-object payloads fall back to an empty snapshot', () async {
|
|
for (final raw in <String>[
|
|
'',
|
|
'not json at all',
|
|
'[]',
|
|
'"a bare string"',
|
|
'42',
|
|
'null',
|
|
]) {
|
|
final snapshot = await loadRaw(raw);
|
|
|
|
expect(snapshot.savedDramaIds, isEmpty, reason: raw);
|
|
expect(snapshot.likedDramaIds, isEmpty, reason: raw);
|
|
expect(snapshot.commentsByDrama, isEmpty, reason: raw);
|
|
}
|
|
});
|
|
|
|
test('wrongly typed fields are dropped instead of throwing', () async {
|
|
final snapshot = await loadRaw(
|
|
jsonEncode({
|
|
'saved': 'signal',
|
|
'liked': {'city': true},
|
|
'comments': {
|
|
'signal': 'a plain string',
|
|
'city': ['kept', 7, null, true],
|
|
'town': <Object>[],
|
|
},
|
|
}),
|
|
);
|
|
|
|
expect(snapshot.savedDramaIds, isEmpty);
|
|
expect(snapshot.likedDramaIds, isEmpty);
|
|
expect(snapshot.commentsByDrama, {
|
|
'city': ['kept'],
|
|
'town': <String>[],
|
|
});
|
|
});
|
|
|
|
test('a payload missing keys falls back per field', () async {
|
|
final snapshot = await loadRaw(jsonEncode({'saved': ['signal']}));
|
|
|
|
expect(snapshot.savedDramaIds, {'signal'});
|
|
expect(snapshot.likedDramaIds, isEmpty);
|
|
expect(snapshot.commentsByDrama, isEmpty);
|
|
});
|
|
|
|
test('save writes only json primitives', () async {
|
|
await repository().save(
|
|
const EngagementSnapshot(
|
|
savedDramaIds: {'signal'},
|
|
likedDramaIds: {'city'},
|
|
commentsByDrama: {
|
|
'signal': ['note'],
|
|
},
|
|
),
|
|
);
|
|
|
|
final raw = await preferences.getString(_key);
|
|
expect(raw, isNotNull);
|
|
|
|
final decoded = jsonDecode(raw!) as Map<String, dynamic>;
|
|
expect(decoded['saved'], ['signal']);
|
|
expect(decoded['liked'], ['city']);
|
|
expect(decoded['comments'], {
|
|
'signal': ['note'],
|
|
});
|
|
});
|
|
}
|