为什么:红线第 8 条门禁立即生效,要求 Flutter 测试命名合规;此前 155 条用例名是英文自然语言 或中文描述,合规数 0/155,属存量欠账,按"先补齐再上新功能"在本批处理。 - 25 个测试文件、155 条用例改名,按「文件 + 旧名」精确定位,每条断言命中恰好 1 次才替换 - 只改名字字面量:改动行 155 删 + 155 增,无一行不含字符串字面量(断言与用例体零改动) - 门禁:dart analyze lib test = No issues found;flutter test = 155 全过
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('should_read_empty_snapshot_when_first_launch', () async {
|
|
final snapshot = await repository().load();
|
|
|
|
expect(snapshot.savedDramaIds, isEmpty);
|
|
expect(snapshot.likedDramaIds, isEmpty);
|
|
expect(snapshot.commentsByDrama, isEmpty);
|
|
});
|
|
|
|
test('should_survive_save_and_reload_round_trip_when_snapshot_is_stored', () 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('should_round_trip_as_empty_collections_when_snapshot_is_empty', () async {
|
|
await repository().save(const EngagementSnapshot());
|
|
|
|
final loaded = await repository().load();
|
|
|
|
expect(loaded.savedDramaIds, isEmpty);
|
|
expect(loaded.likedDramaIds, isEmpty);
|
|
expect(loaded.commentsByDrama, isEmpty);
|
|
});
|
|
|
|
test('should_fall_back_to_empty_snapshot_when_payload_is_corrupt_or_not_an_object', () 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('should_drop_mistyped_fields_when_payload_has_wrong_types', () 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('should_fall_back_per_field_when_payload_keys_are_missing', () async {
|
|
final snapshot = await loadRaw(jsonEncode({'saved': ['signal']}));
|
|
|
|
expect(snapshot.savedDramaIds, {'signal'});
|
|
expect(snapshot.likedDramaIds, isEmpty);
|
|
expect(snapshot.commentsByDrama, isEmpty);
|
|
});
|
|
|
|
test('should_write_only_json_primitives_when_saving', () 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'],
|
|
});
|
|
});
|
|
}
|