2026-04-17 19:21:57 +08:00

285 lines
8.2 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const { Op } = require('sequelize');
const SettingExpressService = require('../../../../../app/service/systems/setting/express');
function buildRecord(row, calls) {
return {
...row,
async update(updates) {
calls.updatePayload = {
id: Number(this.id),
updates: {
...updates,
},
};
Object.assign(this, updates);
return this;
},
get() {
return { ...this };
},
};
}
function createHarness(options = {}) {
const calls = {
systemExpressListCalls: 0,
findAndCountAllCalls: 0,
findOneCalls: [],
createPayload: null,
updatePayload: null,
};
const systemExpressList = options.systemExpressList || [
{ code: 'SF', name: '顺丰' },
{ code: 'YTO', name: '圆通' },
];
const settingRows = options.settingRows || [
{ id: 11, warehouse_id: 100000, code: 'SF-001', name: '顺丰设置', express_code: 'SF' },
{ id: 12, warehouse_id: 100000, code: 'YTO-001', name: '圆通设置', express_code: 'YTO' },
];
const ctx = {
user: {
store_id: options.storeId || 100000,
},
where: options.where || {},
whereParsed: options.whereParsed || null,
app: {
Sequelize: {
Op,
},
},
model: {
ExpressSetting: {
async findAndCountAll(query) {
calls.findAndCountAllCalls += 1;
calls.findAndCountAllQuery = query;
return {
rows: options.rows || settingRows,
count: options.count !== undefined ? options.count : (options.rows || settingRows).length,
};
},
async findOne(query) {
calls.findOneCalls.push(query);
const where = query.where || {};
const excludedId = where.id && where.id[Op.ne] ? Number(where.id[Op.ne]) : null;
const found = (options.settingRows || settingRows).find(row => {
const sameWarehouse = Number(row.warehouse_id) === Number(where.warehouse_id);
const isExcluded = excludedId && Number(row.id) === excludedId;
if (Object.prototype.hasOwnProperty.call(where, 'id') && !Object.prototype.hasOwnProperty.call(where, 'code')) {
return sameWarehouse && Number(row.id) === Number(where.id) && !isExcluded;
}
if (Object.prototype.hasOwnProperty.call(where, 'code')) {
const sameCode = String(row.code || '') === String(where.code || '');
return sameWarehouse && sameCode && !isExcluded;
}
return sameWarehouse && !isExcluded;
});
return found ? buildRecord({ ...found }, calls) : null;
},
async create(payload) {
calls.createPayload = {
...payload,
};
const created = buildRecord({
id: options.createdId || 99,
...payload,
}, calls);
return created;
},
},
},
service: {
systems: {
express: {
async getSystemExpressList() {
calls.systemExpressListCalls += 1;
return systemExpressList;
},
},
},
},
logger: {
info() {},
warn() {},
error() {},
},
};
return {
ctx,
calls,
};
}
describe('test/app/service/systems/setting/express.test.js', () => {
it('should list mapped express settings with pagination and system express names', async () => {
const { ctx, calls } = createHarness({
where: {
quickWhere: '设置',
},
whereParsed: {
dynamicConditions: [],
rawConditionsByField: {
quickWhere: {
value: '设置',
},
},
},
rows: [
{ id: 12, warehouse_id: 100000, code: 'YTO-001', name: '圆通设置', express_code: 'YTO' },
],
count: 1,
});
const service = new SettingExpressService(ctx);
const result = await service.list({
page: 2,
page_size: 1,
where: {
quickWhere: '设置',
},
});
assert.equal(calls.findAndCountAllCalls, 1);
assert.equal(calls.systemExpressListCalls, 1);
assert.equal(calls.findAndCountAllQuery.offset, 1);
assert.equal(calls.findAndCountAllQuery.limit, 1);
assert.equal(calls.findAndCountAllQuery.where[Op.and][0].warehouse_id, 100000);
assert.equal(result.code, 200);
assert.equal(result.data.total, 1);
assert.equal(result.data.page, 2);
assert.equal(result.data.page_size, 1);
assert.deepEqual(result.data.list, [
{
id: 12,
warehouse_id: 100000,
code: 'YTO-001',
name: '圆通设置',
express_code: 'YTO',
express_name: '圆通',
express_label: '圆通',
},
]);
});
it('should prefer pagination object over legacy page fields', async () => {
const { ctx, calls } = createHarness({
whereParsed: {
dynamicConditions: [],
rawConditionsByField: {},
},
});
const service = new SettingExpressService(ctx);
const result = await service.list({
pagination: {
page: 3,
pageSize: 2,
},
page: 1,
page_size: 10,
where: {},
});
assert.equal(calls.findAndCountAllCalls, 1);
assert.equal(calls.findAndCountAllQuery.offset, 4);
assert.equal(calls.findAndCountAllQuery.limit, 2);
assert.equal(result.data.page, 3);
assert.equal(result.data.page_size, 2);
});
it('should load system express base list only', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressService(ctx);
const result = await service.listBase();
assert.equal(calls.systemExpressListCalls, 1);
assert.deepEqual(result, {
code: 200,
data: [
{ code: 'SF', name: '顺丰' },
{ code: 'YTO', name: '圆通' },
],
});
});
it('should mark code as occupied and exclude current record when checking uniqueness', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressService(ctx);
const occupied = await service.checkCodeUnique({
code: 'SF-001',
});
const available = await service.checkCodeUnique({
id: 11,
code: 'SF-001',
});
assert.equal(calls.findOneCalls.length, 2);
assert.equal(occupied.data.isUnique, false);
assert.equal(occupied.data.message, '快递编码已被当前仓库使用');
assert.equal(available.data.isUnique, true);
});
it('should create a mapping after validating system express and code uniqueness', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressService(ctx);
const result = await service.create({
code: 'jd-001',
name: '京东设置',
express_code: 'SF',
});
assert.equal(calls.systemExpressListCalls, 1);
assert.equal(calls.findOneCalls.length, 1);
assert.equal(calls.createPayload.warehouse_id, 100000);
assert.equal(calls.createPayload.code, 'JD-001'.toUpperCase());
assert.equal(calls.createPayload.express_code, 'SF');
assert.equal(result.data.code, 'JD-001'.toUpperCase());
assert.equal(result.data.express_name, '顺丰');
});
it('should reject create when system express code does not exist', async () => {
const { ctx } = createHarness();
const service = new SettingExpressService(ctx);
await assert.rejects(async () => {
await service.create({
code: 'SF-NEW',
name: '顺丰新增',
express_code: 'NOT_EXISTS',
});
}, /系统快递编码不存在/);
});
it('should update a mapping and allow the same code on the current record', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressService(ctx);
const result = await service.update({
id: 11,
code: 'SF-001',
name: '顺丰新设置',
express_code: 'SF',
});
assert.equal(calls.findOneCalls.length >= 1, true);
assert.equal(calls.updatePayload.id, 11);
assert.equal(calls.updatePayload.updates.code, 'SF-001');
assert.equal(calls.updatePayload.updates.name, '顺丰新设置');
assert.equal(calls.updatePayload.updates.express_code, 'SF');
assert.equal(result.data.code, 'SF-001');
assert.equal(result.data.name, '顺丰新设置');
});
});