feat(core): 设备号改用系统标识,重装不再变成新游客
- Android 读 SSAID、iOS 读 IDFV,均免权限,由原生通道提供,不引插件 - 取不到时回退本地 UUIDv4,替代原来 32 位随机 + 时间戳的拼接
This commit is contained in:
1 parent
935de13343
commit
1b19513efb
5 files changed
+165
-12
No files matched your search
@@ -0,0 +1,65 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:fruitshorts/core/data/services/auth_session_store.dart';
|
||||
import 'package:fruitshorts/core/data/services/device_id_provider.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';
|
||||
|
||||
/// 可控的系统设备标识来源。
|
||||
class _FakeDeviceIdProvider implements DeviceIdProvider {
|
||||
_FakeDeviceIdProvider(this.value);
|
||||
|
||||
final String? value;
|
||||
int calls = 0;
|
||||
|
||||
@override
|
||||
Future<String?> systemDeviceId() async {
|
||||
calls++;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
setUp(() {
|
||||
SharedPreferencesAsyncPlatform.instance =
|
||||
InMemorySharedPreferencesAsync.empty();
|
||||
});
|
||||
|
||||
test('should_prefer_system_device_id_over_local_value', () async {
|
||||
final provider = _FakeDeviceIdProvider('a-9774d56d682e549c');
|
||||
final store = AuthSessionStore(deviceIdProvider: provider);
|
||||
|
||||
expect(await store.deviceId(), 'a-9774d56d682e549c');
|
||||
});
|
||||
|
||||
test('should_fall_back_to_stable_local_uuid_when_system_id_missing', () async {
|
||||
final store = AuthSessionStore(
|
||||
deviceIdProvider: _FakeDeviceIdProvider(null),
|
||||
);
|
||||
|
||||
final first = await store.deviceId();
|
||||
final second = await store.deviceId();
|
||||
|
||||
// 同一次运行内固定,且符合 UUIDv4 形态
|
||||
expect(first, second);
|
||||
expect(first, startsWith('d-'));
|
||||
expect(
|
||||
RegExp(
|
||||
r'^d-[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$',
|
||||
).hasMatch(first),
|
||||
isTrue,
|
||||
reason: '本地兜底必须是 UUIDv4,避免 32 位随机带来的碰撞',
|
||||
);
|
||||
});
|
||||
|
||||
test('should_cache_system_id_within_one_session', () async {
|
||||
final provider = _FakeDeviceIdProvider('i-E621E1F8-C36C-495A-93FC-0C247A3E6E5F');
|
||||
final store = AuthSessionStore(deviceIdProvider: provider);
|
||||
|
||||
await store.deviceId();
|
||||
await store.deviceId();
|
||||
|
||||
expect(provider.calls, 1);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user