209 lines
7.5 KiB
Dart
209 lines
7.5 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:uuid/uuid.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);
|
|
});
|
|
});
|
|
}
|