test: 增加全链路多设备同步与药瓶生命周期自动化测试套件

This commit is contained in:
shen
2026-10-27 22:58:46 -07:00
parent 89ab13ecf5
commit ba32e29682
4 changed files with 712 additions and 2 deletions
+210
View File
@@ -0,0 +1,210 @@
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/canister.dart';
import 'package:aero_track/models/dose_record.dart';
import 'package:aero_track/services/storage_service.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUp(() {
SharedPreferences.setMockInitialValues({});
});
group('StorageService & Bottle Lifecycle Tests', () {
test('1. 初始化时自动创建第 1 瓶 (Bottle #1) 且初始余量满喷', () async {
final storage = await StorageService.init();
final bottles = storage.getBottles();
expect(bottles.length, 2);
final flixBottle = storage.getActiveFlixotideBottle();
expect(flixBottle, isNotNull);
expect(flixBottle!.sequenceNumber, 1);
expect(flixBottle.totalDoses, 60);
expect(flixBottle.isActive, true);
final ventBottle = storage.getActiveVentolinBottle();
expect(ventBottle, isNotNull);
expect(ventBottle!.sequenceNumber, 1);
expect(ventBottle.totalDoses, 200);
expect(ventBottle.isActive, true);
final canisters = storage.getCanisters();
final flixCan = canisters.firstWhere((c) => c.isSteroid);
final ventCan = canisters.firstWhere((c) => !c.isSteroid);
expect(flixCan.remainingDoses, 60);
expect(ventCan.remainingDoses, 200);
});
test('2. 正常雾化记录自动扣减对应药瓶喷数并保持一致', () async {
final storage = await StorageService.init();
final flixBottle = storage.getActiveFlixotideBottle()!;
final ventBottle = storage.getActiveVentolinBottle()!;
final record = DoseRecord(
id: const Uuid().v4(),
startTime: DateTime.now().toUtc(),
endTime: DateTime.now().toUtc(),
flixotidePuffs: 2,
ventolinPuffs: 2,
flixotideBottleId: flixBottle.id,
ventolinBottleId: ventBottle.id,
);
await storage.addRecord(record);
final canisters = storage.getCanisters();
final flixCan = canisters.firstWhere((c) => c.isSteroid);
final ventCan = canisters.firstWhere((c) => !c.isSteroid);
expect(flixCan.remainingDoses, 58); // 60 - 2
expect(ventCan.remainingDoses, 198); // 200 - 2
});
test('3. 开新瓶 (openNewBottle) 归档旧瓶,激活第 2 瓶并重置为满喷', () async {
final storage = await StorageService.init();
final bottle1 = storage.getActiveFlixotideBottle()!;
// 第 1 瓶用掉 4 喷
await storage.addRecord(DoseRecord(
id: const Uuid().v4(),
startTime: DateTime.now().toUtc(),
endTime: DateTime.now().toUtc(),
flixotidePuffs: 4,
flixotideBottleId: bottle1.id,
));
expect(storage.getPrimaryFlixotide()!.remainingDoses, 56);
// 换新药瓶 (开新瓶)
final bottle2 = await storage.openNewBottle(bottle1.canisterId, deviceName: '妈妈手机');
expect(bottle2.sequenceNumber, 2);
expect(bottle2.isActive, true);
expect(bottle2.totalDoses, 60);
// 验证第 1 瓶已被归档
final allBottles = storage.getBottles().where((b) => b.canisterId == bottle1.canisterId).toList();
expect(allBottles.length, 2);
final archivedBottle1 = allBottles.firstWhere((b) => b.sequenceNumber == 1);
expect(archivedBottle1.isActive, false);
expect(archivedBottle1.finishedAt, isNotNull);
// 验证当前活跃药瓶切换为第 2 瓶,且余量对齐为满瓶 60 喷
final currentActive = storage.getActiveFlixotideBottle()!;
expect(currentActive.id, bottle2.id);
expect(storage.getPrimaryFlixotide()!.remainingDoses, 60);
});
test('4. 开新瓶后新产生的雾化仅扣减新瓶,旧瓶归档数据不被污染', () async {
final storage = await StorageService.init();
final bottle1 = storage.getActiveFlixotideBottle()!;
// 第 1 瓶用掉 60 喷 (用尽)
await storage.addRecord(DoseRecord(
id: const Uuid().v4(),
startTime: DateTime.now().toUtc().subtract(const Duration(days: 1)),
endTime: DateTime.now().toUtc().subtract(const Duration(days: 1)),
flixotidePuffs: 60,
flixotideBottleId: bottle1.id,
));
// 开第 2 瓶
final bottle2 = await storage.openNewBottle(bottle1.canisterId, deviceName: '测试设备');
expect(storage.getPrimaryFlixotide()!.remainingDoses, 60);
// 第 2 瓶吸入 2 喷
await storage.addRecord(DoseRecord(
id: const Uuid().v4(),
startTime: DateTime.now().toUtc(),
endTime: DateTime.now().toUtc(),
flixotidePuffs: 2,
flixotideBottleId: bottle2.id,
));
// 第 2 瓶余量应为 58 喷
expect(storage.getPrimaryFlixotide()!.remainingDoses, 58);
});
test('5. 软删除墓碑测试:误操作删除记录后喷数自动恢复', () async {
final storage = await StorageService.init();
final flixBottle = storage.getActiveFlixotideBottle()!;
final recordId = const Uuid().v4();
await storage.addRecord(DoseRecord(
id: recordId,
startTime: DateTime.now().toUtc(),
endTime: DateTime.now().toUtc(),
flixotidePuffs: 2,
flixotideBottleId: flixBottle.id,
));
expect(storage.getPrimaryFlixotide()!.remainingDoses, 58);
// 删除该记录
await storage.deleteRecord(recordId);
// 验证用户可见列表已移除该记录
expect(storage.getRecords().length, 0);
// 验证全量记录中标记了 isDeleted = true
final allRaw = storage.getAllRecords(includeDeleted: true);
expect(allRaw.length, 1);
expect(allRaw.first.isDeleted, true);
// 验证药瓶余量已恢复至 60 喷
expect(storage.getPrimaryFlixotide()!.remainingDoses, 60);
});
test('6. 历史数据平滑升级:未绑定 bottleId 的旧数据自动归属到第 1 瓶', () async {
// 预先写入未绑定 bottleId 的旧版数据
final legacyRecord = DoseRecord(
id: 'legacy_rec_1',
startTime: DateTime.parse('2026-10-27T02:00:00Z'),
endTime: DateTime.parse('2026-10-27T02:01:00Z'),
flixotidePuffs: 2,
ventolinPuffs: 1,
);
final prefs = await SharedPreferences.getInstance();
await prefs.setStringList('aerotrack_records', [legacyRecord.toJson()]);
// 初始化 StorageService
final storage = await StorageService.init();
// 验证平滑迁移:自动创建第 1 瓶,且历史记录关联到了第 1 瓶
final records = storage.getRecords();
expect(records.length, 1);
expect(records.first.flixotideBottleId, 'flix_b1_initial');
expect(records.first.ventolinBottleId, 'vent_b1_initial');
// 验证余量正确扣减了历史数据
expect(storage.getPrimaryFlixotide()!.remainingDoses, 58); // 60 - 2
expect(storage.getPrimaryVentolin()!.remainingDoses, 199); // 200 - 1
});
test('7. 试喷 (Prime) 扣除正确关联药瓶并扣减 1 喷', () async {
final storage = await StorageService.init();
final flixBottle = storage.getActiveFlixotideBottle()!;
final primeRecord = DoseRecord(
id: const Uuid().v4(),
startTime: DateTime.now().toUtc(),
endTime: DateTime.now().toUtc(),
flixotidePuffs: 1,
ventolinPuffs: 0,
flixotideBottleId: flixBottle.id,
isPrime: true,
note: '辅舒酮试喷',
);
await storage.addRecord(primeRecord);
expect(storage.getPrimaryFlixotide()!.remainingDoses, 59);
expect(storage.getRecords().first.isPrime, true);
});
});
}
+209
View File
@@ -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);
});
});
}