fix(sync): 修复喷数重复扣减与重装恢复,支持余量校准
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:aero_track/models/dose_record.dart';
|
||||
import 'package:aero_track/services/storage_service.dart';
|
||||
import 'package:aero_track/services/sync_service.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/testing.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
setUp(() => SharedPreferences.setMockInitialValues({}));
|
||||
|
||||
Future<SyncResult> syncAgainst(
|
||||
StorageService storage, {
|
||||
required Map<String, dynamic> ping,
|
||||
required Map<String, dynamic> Function() sync,
|
||||
}) {
|
||||
return http.runWithClient(
|
||||
() => SyncService(storage).performSync(customUrl: 'http://test.local'),
|
||||
() => MockClient((request) async {
|
||||
final data = request.url.path == '/api/v1/ping' ? ping : sync();
|
||||
return http.Response.bytes(utf8.encode(jsonEncode(data)), 200);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
test('恢复时以服务器实际药瓶为准并完整计算已用喷数', () async {
|
||||
final storage = await StorageService.init();
|
||||
final flix = storage.getActiveFlixotideBottle()!;
|
||||
final opened = DateTime.utc(2026, 10, 29);
|
||||
final result = await syncAgainst(
|
||||
storage,
|
||||
ping: {'sync_version': 2},
|
||||
sync: () => {
|
||||
'bottles': [
|
||||
{
|
||||
...flix.toMap(),
|
||||
'openedAt': opened.toIso8601String(),
|
||||
'updatedAt': '2020-01-01T00:00:00Z',
|
||||
},
|
||||
],
|
||||
'canisters': storage
|
||||
.getCanisters()
|
||||
.map((item) => item.toMap())
|
||||
.toList(),
|
||||
'records': [
|
||||
DoseRecord(
|
||||
id: 'prior-bottle',
|
||||
startTime: opened.subtract(const Duration(days: 1)),
|
||||
endTime: opened.subtract(const Duration(days: 1)),
|
||||
flixotidePuffs: 12,
|
||||
flixotideBottleId: flix.id,
|
||||
).toMap(),
|
||||
DoseRecord(
|
||||
id: 'current-bottle',
|
||||
startTime: opened,
|
||||
endTime: opened,
|
||||
flixotidePuffs: 4,
|
||||
flixotideBottleId: flix.id,
|
||||
).toMap(),
|
||||
],
|
||||
},
|
||||
);
|
||||
|
||||
expect(result.success, isTrue, reason: result.message);
|
||||
expect(storage.getRecords(), hasLength(2));
|
||||
expect(storage.getActiveFlixotideBottle()!.openedAt, opened);
|
||||
expect(storage.getPrimaryFlixotide()!.remainingDoses, 56);
|
||||
final reopened = await StorageService.init();
|
||||
expect(reopened.getPrimaryFlixotide()!.remainingDoses, 56);
|
||||
});
|
||||
|
||||
test('校准保留历史,只扣减校准后的用量', () async {
|
||||
final storage = await StorageService.init();
|
||||
final flix = storage.getActiveFlixotideBottle()!;
|
||||
final cutoff = DateTime.utc(2026, 11, 25);
|
||||
final result = await syncAgainst(
|
||||
storage,
|
||||
ping: {'sync_version': 2},
|
||||
sync: () => {
|
||||
'bottles': [
|
||||
{
|
||||
...flix.toMap(),
|
||||
'calibratedAt': cutoff.toIso8601String(),
|
||||
'calibratedRemainingDoses': 11,
|
||||
},
|
||||
],
|
||||
'records': [
|
||||
DoseRecord(
|
||||
id: 'old',
|
||||
startTime: cutoff.subtract(const Duration(minutes: 1)),
|
||||
endTime: cutoff,
|
||||
flixotidePuffs: 60,
|
||||
flixotideBottleId: flix.id,
|
||||
).toMap(),
|
||||
],
|
||||
},
|
||||
);
|
||||
expect(result.success, isTrue, reason: result.message);
|
||||
expect(storage.getPrimaryFlixotide()!.remainingDoses, 11);
|
||||
final after = cutoff.add(const Duration(minutes: 1));
|
||||
await storage.addRecord(
|
||||
DoseRecord(
|
||||
id: 'new',
|
||||
startTime: after,
|
||||
endTime: after,
|
||||
flixotidePuffs: 1,
|
||||
flixotideBottleId: flix.id,
|
||||
),
|
||||
);
|
||||
final reopened = await StorageService.init();
|
||||
expect(reopened.getRecords(), hasLength(2));
|
||||
expect(reopened.getPrimaryFlixotide()!.remainingDoses, 10);
|
||||
});
|
||||
|
||||
test('旧服务器提示升级且不上传本地初始化数据', () async {
|
||||
final storage = await StorageService.init();
|
||||
var syncRequests = 0;
|
||||
final result = await syncAgainst(
|
||||
storage,
|
||||
ping: {'status': 'ok'},
|
||||
sync: () {
|
||||
syncRequests++;
|
||||
return {};
|
||||
},
|
||||
);
|
||||
|
||||
expect(result.success, isFalse);
|
||||
expect(result.message, contains('服务器需要更新'));
|
||||
expect(syncRequests, 0);
|
||||
expect(await SyncService.getLastSyncTime(), isNull);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user