feat: 增加全家庭智能药箱功能,支持扫码识别(追溯码/GS1/条形码)、出厂有效期与开封保质期双轨追踪及局域网多设备同步
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
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';
|
||||
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:aero_track/models/medicine_item.dart';
|
||||
import 'package:aero_track/services/medicine_parser_service.dart';
|
||||
import 'package:aero_track/services/storage_service.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('MedicineItem & Expiry Logic Tests', () {
|
||||
test('Unopened medicine within validity is safe', () {
|
||||
final now = DateTime.now();
|
||||
final med = MedicineItem(
|
||||
id: const Uuid().v4(),
|
||||
name: '美林 (布洛芬混悬液)',
|
||||
category: MedicineCategory.oralLiquid,
|
||||
expiryDate: now.add(const Duration(days: 365)),
|
||||
shelfLifeDaysAfterOpening: 60,
|
||||
isOpened: false,
|
||||
updatedAt: now.toUtc(),
|
||||
);
|
||||
|
||||
expect(med.healthStatus, MedicineHealthStatus.safe);
|
||||
expect(med.healthStatusLabel, '未开封');
|
||||
expect(med.daysUntilExpiry, inInclusiveRange(364, 366));
|
||||
});
|
||||
|
||||
test('Medicine expiring within 30 days triggers expiringSoon', () {
|
||||
final now = DateTime.now();
|
||||
final med = MedicineItem(
|
||||
id: const Uuid().v4(),
|
||||
name: '感冒冲剂',
|
||||
category: MedicineCategory.tablets,
|
||||
expiryDate: now.add(const Duration(days: 15)),
|
||||
isOpened: false,
|
||||
updatedAt: now.toUtc(),
|
||||
);
|
||||
|
||||
expect(med.healthStatus, MedicineHealthStatus.expiringSoon);
|
||||
expect(med.healthStatusLabel, contains('即将到期 (15天)'));
|
||||
});
|
||||
|
||||
test('Medicine past expiryDate triggers expired', () {
|
||||
final now = DateTime.now();
|
||||
final med = MedicineItem(
|
||||
id: const Uuid().v4(),
|
||||
name: '头孢克肟颗粒',
|
||||
category: MedicineCategory.oralLiquid,
|
||||
expiryDate: now.subtract(const Duration(days: 2)),
|
||||
isOpened: false,
|
||||
updatedAt: now.toUtc(),
|
||||
);
|
||||
|
||||
expect(med.healthStatus, MedicineHealthStatus.expired);
|
||||
expect(med.healthStatusLabel, '❌ 出厂已过期');
|
||||
});
|
||||
|
||||
test('Opened eye drops exceeding 28 days triggers overdueInUse even if factory expiry is 2 years away', () {
|
||||
final now = DateTime.now();
|
||||
final med = MedicineItem(
|
||||
id: const Uuid().v4(),
|
||||
name: '托百士滴眼液',
|
||||
category: MedicineCategory.eyeDrops,
|
||||
expiryDate: now.add(const Duration(days: 500)), // 出厂保质期还很长
|
||||
shelfLifeDaysAfterOpening: 28, // 开封只能放 28 天
|
||||
isOpened: true,
|
||||
openedAt: now.subtract(const Duration(days: 35)), // 已经开了 35 天
|
||||
updatedAt: now.toUtc(),
|
||||
);
|
||||
|
||||
expect(med.healthStatus, MedicineHealthStatus.overdueInUse);
|
||||
expect(med.healthStatusLabel, '⚠️ 开封已超期');
|
||||
expect(med.daysSinceOpened, 35);
|
||||
expect(med.daysUntilInUseExpiry, -7);
|
||||
});
|
||||
|
||||
test('Opened medicine within safe days triggers safe in-use', () {
|
||||
final now = DateTime.now();
|
||||
final med = MedicineItem(
|
||||
id: const Uuid().v4(),
|
||||
name: '辅舒酮吸入剂',
|
||||
category: MedicineCategory.inhaler,
|
||||
expiryDate: now.add(const Duration(days: 400)),
|
||||
shelfLifeDaysAfterOpening: 60,
|
||||
isOpened: true,
|
||||
openedAt: now.subtract(const Duration(days: 10)), // 开了 10 天
|
||||
updatedAt: now.toUtc(),
|
||||
);
|
||||
|
||||
expect(med.healthStatus, MedicineHealthStatus.safe);
|
||||
expect(med.healthStatusLabel, '在用正常');
|
||||
expect(med.daysSinceOpened, 10);
|
||||
expect(med.daysUntilInUseExpiry, 50);
|
||||
});
|
||||
});
|
||||
|
||||
group('MedicineParserService Scanned Barcode Tests', () {
|
||||
test('Parse GS1 DataMatrix with (17)EXP, (10)BATCH and (11)MFG', () async {
|
||||
// (01)06921147600010(17)280531(10)LOT987(11)250501
|
||||
const sampleGs1 = '(01)06921147600010(17)280531(10)LOT987(11)250501';
|
||||
final info = await MedicineParserService.parseRawBarcode(sampleGs1);
|
||||
|
||||
expect(info.parseSource, 'gs1_datamatrix');
|
||||
expect(info.batchNumber, 'LOT987');
|
||||
expect(info.expiryDate, DateTime(2028, 5, 31));
|
||||
expect(info.productionDate, DateTime(2025, 5, 1));
|
||||
});
|
||||
|
||||
test('Parse 69 EAN-13 catalog medicine (Meilin)', () async {
|
||||
const meilinBarcode = '6925567800018';
|
||||
final info = await MedicineParserService.parseRawBarcode(meilinBarcode);
|
||||
|
||||
expect(info.parseSource, 'ean13_catalog');
|
||||
expect(info.name, contains('美林'));
|
||||
expect(info.category, MedicineCategory.oralLiquid);
|
||||
expect(info.shelfLifeDaysAfterOpening, 60);
|
||||
expect(info.targetUser, '宝宝');
|
||||
});
|
||||
|
||||
test('Parse 69 EAN-13 catalog medicine (Flixotide)', () async {
|
||||
const flixBarcode = '6921147600010';
|
||||
final info = await MedicineParserService.parseRawBarcode(flixBarcode);
|
||||
|
||||
expect(info.parseSource, 'ean13_catalog');
|
||||
expect(info.name, contains('辅舒酮'));
|
||||
expect(info.category, MedicineCategory.inhaler);
|
||||
expect(info.spec, contains('60喷'));
|
||||
});
|
||||
|
||||
test('Detect 20-digit Chinese traceability code', () async {
|
||||
const traceCode = '81888880123456789012';
|
||||
final info = await MedicineParserService.parseRawBarcode(traceCode);
|
||||
|
||||
expect(info.parseSource, 'traceability_code');
|
||||
expect(info.barcode, traceCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('StorageService Family Medicine CRUD Tests', () {
|
||||
setUp(() {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
});
|
||||
|
||||
test('Initial seeding initializes default common medicines', () async {
|
||||
final storage = await StorageService.init();
|
||||
final meds = storage.getMedicines();
|
||||
|
||||
expect(meds.isNotEmpty, true);
|
||||
expect(meds.any((m) => m.name.contains('辅舒酮')), true);
|
||||
expect(meds.any((m) => m.name.contains('美林')), true);
|
||||
});
|
||||
|
||||
test('Open medicine marks it as opened and sets openedAt timestamp', () async {
|
||||
final storage = await StorageService.init();
|
||||
final meds = storage.getMedicines();
|
||||
final unopened = meds.firstWhere((m) => !m.isOpened);
|
||||
|
||||
await storage.openMedicine(unopened.id, deviceName: '测试手机');
|
||||
|
||||
final updatedMeds = storage.getMedicines();
|
||||
final updatedItem = updatedMeds.firstWhere((m) => m.id == unopened.id);
|
||||
|
||||
expect(updatedItem.isOpened, true);
|
||||
expect(updatedItem.openedAt, isNotNull);
|
||||
expect(updatedItem.openedByDevice, '测试手机');
|
||||
});
|
||||
|
||||
test('Soft delete medicine removes it from active list and retains tombstone', () async {
|
||||
final storage = await StorageService.init();
|
||||
final medsBefore = storage.getMedicines();
|
||||
final idToDelete = medsBefore.first.id;
|
||||
|
||||
await storage.deleteMedicine(idToDelete);
|
||||
|
||||
final medsAfter = storage.getMedicines();
|
||||
expect(medsAfter.any((m) => m.id == idToDelete), false);
|
||||
|
||||
final allMedsWithDeleted = storage.getMedicines(includeDeleted: true);
|
||||
final deleted = allMedsWithDeleted.firstWhere((m) => m.id == idToDelete);
|
||||
expect(deleted.isDeleted, true);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user