147 lines
4.5 KiB
Dart
147 lines
4.5 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
// ignore_for_file: depend_on_referenced_packages
|
|
|
|
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
|
|
|
import 'package:pj_supermarket_cloud/common/index.dart';
|
|
import 'package:pj_supermarket_cloud/common/services/database/database_migration.dart';
|
|
|
|
void main() {
|
|
sqfliteFfiInit();
|
|
|
|
setUpAll(() {
|
|
databaseFactory = databaseFactoryFfi;
|
|
});
|
|
|
|
test('MQTT 打印消息允许线上预约缺少 androidOrderCode', () {
|
|
final message = MqttInvoiceMsg.fromJson(const {
|
|
'action': 'PRINT',
|
|
'historyId': 9001,
|
|
'orderId': 8001,
|
|
'androidOrderCode': null,
|
|
'sourceHardId': 101,
|
|
'targetHardId': 202,
|
|
'printType': '2',
|
|
'printNo': 3,
|
|
'retryVersion': 4,
|
|
'pic': 'https://example.test/voucher.png',
|
|
});
|
|
|
|
expect(message.androidOrderCode, isNull);
|
|
expect(message.historyId, '9001');
|
|
expect(message.orderId, '8001');
|
|
expect(message.sourceHardId, '101');
|
|
expect(message.targetHardId, '202');
|
|
expect(message.printType, '2');
|
|
expect(message.printNo, 3);
|
|
expect(message.retryVersion, 4);
|
|
expect(message.toJson(), containsPair('androidOrderCode', null));
|
|
});
|
|
|
|
test('打印任务最多尝试三次且不安排第四次出纸', () {
|
|
expect(
|
|
SyncPrintTool.attemptDelays,
|
|
const [
|
|
Duration.zero,
|
|
Duration(seconds: 5),
|
|
Duration(seconds: 30),
|
|
],
|
|
);
|
|
});
|
|
|
|
test('同版本重复任务被拒绝,只有失败任务的更高版本可重试', () {
|
|
final task = PrintHistoryEntity(
|
|
orderId: 'local-order',
|
|
serviceOrderId: '8001',
|
|
servicePrintId: '9001',
|
|
invoiceUrl: 'https://example.test/voucher.png',
|
|
retryVersion: 2,
|
|
status: PrintStatus.ERROR,
|
|
createTime: DateTime(2026, 9, 4),
|
|
);
|
|
|
|
expect(SyncPrintTool.canApplyTaskVersion(task, 2), isFalse);
|
|
expect(SyncPrintTool.canApplyTaskVersion(task, 1), isFalse);
|
|
expect(SyncPrintTool.canApplyTaskVersion(task, 3), isTrue);
|
|
|
|
task.status = PrintStatus.SUCCESS;
|
|
expect(SyncPrintTool.canApplyTaskVersion(task, 3), isFalse);
|
|
task.status = PrintStatus.SYNC;
|
|
expect(SyncPrintTool.canApplyTaskVersion(task, 3), isFalse);
|
|
});
|
|
|
|
test('12到13迁移保留每个服务端任务最新记录并建立唯一索引', () async {
|
|
final database =
|
|
await databaseFactoryFfi.openDatabase(inMemoryDatabasePath);
|
|
addTearDown(database.close);
|
|
await database.execute('''
|
|
CREATE TABLE print_history (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
orderId TEXT NOT NULL,
|
|
serviceOrderId TEXT NOT NULL,
|
|
servicePrintId TEXT NOT NULL,
|
|
invoiceUrl TEXT NOT NULL,
|
|
status TEXT NOT NULL,
|
|
printTime TEXT,
|
|
syncTime TEXT,
|
|
createTime TEXT NOT NULL
|
|
)
|
|
''');
|
|
await database.insert('print_history', {
|
|
'orderId': 'old-1',
|
|
'serviceOrderId': '8001',
|
|
'servicePrintId': '9001',
|
|
'invoiceUrl': 'old.png',
|
|
'status': 'ERROR',
|
|
'createTime': '2026-09-04T08:00:00.000',
|
|
});
|
|
await database.insert('print_history', {
|
|
'orderId': 'old-2',
|
|
'serviceOrderId': '8001',
|
|
'servicePrintId': '9001',
|
|
'invoiceUrl': 'new.png',
|
|
'status': 'SUCCESS',
|
|
'createTime': '2026-09-04T08:01:00.000',
|
|
});
|
|
|
|
await DatabaseMigration.migration12To13.migrate(database);
|
|
|
|
final rows = await database.query('print_history');
|
|
final columns = await database.rawQuery('PRAGMA table_info(print_history)');
|
|
final indexes = await database.rawQuery('PRAGMA index_list(print_history)');
|
|
expect(rows, hasLength(1));
|
|
expect(rows.single['orderId'], 'old-2');
|
|
expect(rows.single['printType'], '1');
|
|
expect(rows.single['printNo'], 1);
|
|
expect(rows.single['retryVersion'], 0);
|
|
expect(rows.single['attemptCount'], 0);
|
|
expect(rows.single['resultPending'], 0);
|
|
expect(
|
|
columns.map((column) => column['name']),
|
|
containsAll(const {
|
|
'printType',
|
|
'printNo',
|
|
'retryVersion',
|
|
'attemptCount',
|
|
'lastError',
|
|
'resultPending',
|
|
}),
|
|
);
|
|
expect(
|
|
indexes.map((index) => index['name']),
|
|
contains('index_print_history_servicePrintId'),
|
|
);
|
|
await expectLater(
|
|
database.insert('print_history', {
|
|
'orderId': 'duplicate',
|
|
'serviceOrderId': '8001',
|
|
'servicePrintId': '9001',
|
|
'invoiceUrl': 'duplicate.png',
|
|
'status': 'NONE',
|
|
'createTime': '2026-09-04T08:02:00.000',
|
|
}),
|
|
throwsA(isA<DatabaseException>()),
|
|
);
|
|
});
|
|
}
|