test: 增加全链路多设备同步与药瓶生命周期自动化测试套件
This commit is contained in:
@@ -0,0 +1,209 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:aero_track/models/bottle.dart';
|
||||
import 'package:aero_track/models/dose_record.dart';
|
||||
import 'package:aero_track/models/canister.dart';
|
||||
import 'package:aero_track/services/storage_service.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('Multi-Device Collaborative Sync Simulation Tests', () {
|
||||
test('1. 设备 A 开新瓶:生成新 Bottle 实体与 60 喷满瓶初始状态', () async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
final storageA = await StorageService.init();
|
||||
|
||||
final flixA1 = storageA.getActiveFlixotideBottle()!;
|
||||
expect(flixA1.sequenceNumber, 1);
|
||||
expect(storageA.getPrimaryFlixotide()!.remainingDoses, 60);
|
||||
|
||||
// 设备 A 开新瓶 (启用第 2 瓶)
|
||||
final flixA2 = await storageA.openNewBottle(flixA1.canisterId, deviceName: '妈妈手机');
|
||||
expect(flixA2.sequenceNumber, 2);
|
||||
expect(flixA2.isActive, true);
|
||||
expect(storageA.getPrimaryFlixotide()!.remainingDoses, 60);
|
||||
|
||||
// 验证第 1 瓶已归档
|
||||
final allBottles = storageA.getBottles().where((b) => b.canisterId == flixA1.canisterId).toList();
|
||||
expect(allBottles.length, 2);
|
||||
expect(allBottles.firstWhere((b) => b.sequenceNumber == 1).isActive, false);
|
||||
});
|
||||
|
||||
test('2. 设备 B (旧缓存/未换瓶) 同步后自动切换至第 2 瓶并展示满瓶 60 喷', () async {
|
||||
// 模拟设备 B 处于旧状态 (只知道第 1 瓶且剩余 40 喷)
|
||||
final oldBottle1 = Bottle(
|
||||
id: 'flix_b1_initial',
|
||||
canisterId: 'flixotide_primary',
|
||||
name: '辅舒酮 (丙酸氟替卡松)',
|
||||
sequenceNumber: 1,
|
||||
totalDoses: 60,
|
||||
openedAt: DateTime.parse('2020-01-01T00:00:00Z'),
|
||||
isActive: true,
|
||||
updatedAt: DateTime.parse('2020-01-01T00:00:00Z'),
|
||||
);
|
||||
|
||||
SharedPreferences.setMockInitialValues({
|
||||
'aerotrack_bottles': [oldBottle1.toJson()],
|
||||
'aerotrack_canisters': [
|
||||
Canister(
|
||||
id: 'flixotide_primary',
|
||||
name: '辅舒酮 (丙酸氟替卡松)',
|
||||
totalDoses: 60,
|
||||
remainingDoses: 40,
|
||||
openedAt: DateTime.parse('2020-01-01T00:00:00Z'),
|
||||
).toJson()
|
||||
],
|
||||
});
|
||||
|
||||
final storageB = await StorageService.init();
|
||||
expect(storageB.getActiveFlixotideBottle()!.sequenceNumber, 1);
|
||||
|
||||
// 模拟从服务端拉到设备 A 创建的第 2 瓶
|
||||
final remoteBottle1Archived = Bottle(
|
||||
id: 'flix_b1_initial',
|
||||
canisterId: 'flixotide_primary',
|
||||
name: '辅舒酮',
|
||||
sequenceNumber: 1,
|
||||
totalDoses: 60,
|
||||
openedAt: DateTime.parse('2020-01-01T00:00:00Z'),
|
||||
finishedAt: DateTime.parse('2026-10-28T05:00:00Z'),
|
||||
isActive: false,
|
||||
updatedAt: DateTime.parse('2026-10-28T05:00:00Z'),
|
||||
);
|
||||
final remoteBottle2Active = Bottle(
|
||||
id: 'flix_b2_new',
|
||||
canisterId: 'flixotide_primary',
|
||||
name: '辅舒酮',
|
||||
sequenceNumber: 2,
|
||||
totalDoses: 60,
|
||||
openedAt: DateTime.parse('2026-10-28T05:00:00Z'),
|
||||
isActive: true,
|
||||
updatedAt: DateTime.parse('2026-10-28T05:00:00Z'),
|
||||
);
|
||||
|
||||
// 设备 B 合并 Bottles
|
||||
final localBottles = storageB.getBottles();
|
||||
final bottleMap = {for (var b in localBottles) b.id: b};
|
||||
for (var rb in [remoteBottle1Archived, remoteBottle2Active]) {
|
||||
if (!bottleMap.containsKey(rb.id)) {
|
||||
localBottles.add(rb);
|
||||
} else {
|
||||
final idx = localBottles.indexWhere((b) => b.id == rb.id);
|
||||
localBottles[idx] = rb;
|
||||
}
|
||||
}
|
||||
await storageB.saveBottles(localBottles);
|
||||
await storageB.recalculateCanisterDoses();
|
||||
|
||||
// 验证设备 B 自动更新活跃瓶为第 2 瓶,且余量瞬间变为满瓶 60 喷
|
||||
final activeB = storageB.getActiveFlixotideBottle()!;
|
||||
expect(activeB.sequenceNumber, 2);
|
||||
expect(activeB.id, 'flix_b2_new');
|
||||
expect(storageB.getPrimaryFlixotide()!.remainingDoses, 60);
|
||||
});
|
||||
|
||||
test('3. 设备 B 在第 2 瓶上雾化用药,扣减精准归属于第 2 瓶', () async {
|
||||
final bottle2 = Bottle(
|
||||
id: 'flix_b2_new',
|
||||
canisterId: 'flixotide_primary',
|
||||
name: '辅舒酮',
|
||||
sequenceNumber: 2,
|
||||
totalDoses: 60,
|
||||
openedAt: DateTime.parse('2026-10-28T05:00:00Z'),
|
||||
isActive: true,
|
||||
);
|
||||
|
||||
SharedPreferences.setMockInitialValues({
|
||||
'aerotrack_bottles': [bottle2.toJson()],
|
||||
'aerotrack_canisters': [
|
||||
Canister(
|
||||
id: 'flixotide_primary',
|
||||
name: '辅舒酮',
|
||||
totalDoses: 60,
|
||||
remainingDoses: 60,
|
||||
openedAt: DateTime.parse('2026-10-28T05:00:00Z'),
|
||||
).toJson()
|
||||
],
|
||||
});
|
||||
|
||||
final storage = await StorageService.init();
|
||||
|
||||
// 在第 2 瓶上吸入 2 喷
|
||||
await storage.addRecord(DoseRecord(
|
||||
id: 'dose_1',
|
||||
startTime: DateTime.parse('2026-10-28T05:10:00Z'),
|
||||
endTime: DateTime.parse('2026-10-28T05:11:00Z'),
|
||||
flixotidePuffs: 2,
|
||||
flixotideBottleId: bottle2.id,
|
||||
));
|
||||
|
||||
expect(storage.getPrimaryFlixotide()!.remainingDoses, 58);
|
||||
});
|
||||
|
||||
test('4. 软删除墓碑在多端同步传播,删除后各端自动准确恢复喷数', () async {
|
||||
final bottle2 = Bottle(
|
||||
id: 'flix_b2_new',
|
||||
canisterId: 'flixotide_primary',
|
||||
name: '辅舒酮',
|
||||
sequenceNumber: 2,
|
||||
totalDoses: 60,
|
||||
openedAt: DateTime.parse('2026-10-28T05:00:00Z'),
|
||||
isActive: true,
|
||||
);
|
||||
|
||||
// 设备 A 录入并随后软删除了记录
|
||||
final deletedDoseRecord = DoseRecord(
|
||||
id: 'dose_mistake',
|
||||
startTime: DateTime.parse('2026-10-28T05:10:00Z'),
|
||||
endTime: DateTime.parse('2026-10-28T05:11:00Z'),
|
||||
flixotidePuffs: 2,
|
||||
flixotideBottleId: bottle2.id,
|
||||
isDeleted: true,
|
||||
updatedAt: DateTime.parse('2026-10-28T05:15:00Z'),
|
||||
);
|
||||
|
||||
// 模拟设备 B 正在同步拉取该记录
|
||||
SharedPreferences.setMockInitialValues({
|
||||
'aerotrack_bottles': [bottle2.toJson()],
|
||||
'aerotrack_records': [deletedDoseRecord.toJson()],
|
||||
});
|
||||
|
||||
final storageB = await StorageService.init();
|
||||
|
||||
// 验证虽然有历史流水,但因被标记为 isDeleted,不扣减当瓶喷数,余量保持 60 喷
|
||||
expect(storageB.getPrimaryFlixotide()!.remainingDoses, 60);
|
||||
expect(storageB.getRecords().length, 0); // 用户界面不呈现已删除记录
|
||||
expect(storageB.getAllRecords(includeDeleted: true).length, 1); // 同步底层完好保留墓碑
|
||||
});
|
||||
|
||||
test('5. 跨时区与时钟偏差鲁棒性测试', () async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
final storage = await StorageService.init();
|
||||
final activeBottle = storage.getActiveFlixotideBottle()!;
|
||||
|
||||
// 模拟东八区 (CST +08:00) 与西七区 (PST -07:00) 产生的记录
|
||||
final cstTime = DateTime.parse('2026-10-28T08:00:00+08:00').toUtc();
|
||||
final pstTime = DateTime.parse('2026-10-27T17:00:00-07:00').toUtc();
|
||||
|
||||
await storage.addRecord(DoseRecord(
|
||||
id: const Uuid().v4(),
|
||||
startTime: cstTime,
|
||||
endTime: cstTime,
|
||||
flixotidePuffs: 2,
|
||||
flixotideBottleId: activeBottle.id,
|
||||
));
|
||||
|
||||
await storage.addRecord(DoseRecord(
|
||||
id: const Uuid().v4(),
|
||||
startTime: pstTime,
|
||||
endTime: pstTime,
|
||||
flixotidePuffs: 2,
|
||||
flixotideBottleId: activeBottle.id,
|
||||
));
|
||||
|
||||
// 60 - 2 - 2 = 56 喷,绝对确定,绝不被本地时区字符串带偏
|
||||
expect(storage.getPrimaryFlixotide()!.remainingDoses, 56);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user