WMS_MAIN/test/app/service/systems/shops.test.js
liufumingren 99367e5043 1
2026-06-06 05:15:46 +08:00

327 lines
8.7 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const ShopsService = require('../../../../app/service/systems/shops');
function createFindOneResult(row) {
if (!row) {
return null;
}
return {
...row,
get() {
return { ...row };
},
async save() {
return this;
},
async update(payload) {
Object.assign(this, payload);
return this;
},
};
}
function createHarness(options = {}) {
const calls = {
shopFindAndCountAll: null,
shopFindOneCalls: [],
shopCreatePayload: null,
expressInterfaceFindOneCalls: [],
printPlatformFindOneCalls: [],
};
const shopRows = Array.isArray(options.shopRows) ? options.shopRows : [];
const printPlatforms = Array.isArray(options.printPlatforms)
? options.printPlatforms
: [{ platform_code: 'PDD', platform_name: '拼多多', auth_base_url: 'https://oauth.pdd.com?state=', deleted: 0, status: 'ACTIVE' }];
const ctx = {
app: {
Sequelize: {
Op: {
and: Symbol('and'),
or: Symbol('or'),
like: Symbol('like'),
notLike: Symbol('notLike'),
ne: Symbol('ne'),
in: Symbol('in'),
},
},
},
user: {
store_id: 100000,
},
whereParsed: options.whereParsed || {
dynamicConditions: [],
rawConditionsByField: {},
},
async curl() {
if (typeof options.curl === 'function') {
return await options.curl(...arguments);
}
return {
data: {
code: 1,
msg: 'mock failed',
},
};
},
model: {
Shop: {
async findAndCountAll(query) {
calls.shopFindAndCountAll = query;
return {
count: shopRows.length,
rows: shopRows.map(row => createFindOneResult(row)),
};
},
async findOne(query) {
calls.shopFindOneCalls.push(query);
if (typeof options.shopFindOne === 'function') {
return await options.shopFindOne(query);
}
const where = query && query.where ? query.where : {};
const id = Number(where.id || 0);
const row = shopRows.find(item => Number(item.id) === id) || null;
return createFindOneResult(row);
},
async create(payload) {
calls.shopCreatePayload = payload;
return createFindOneResult({ id: 9001, ...payload });
},
},
PrintPlatform: {
async findOne(query) {
calls.printPlatformFindOneCalls.push(query);
const code = String(query && query.where && query.where.platform_code || '').toUpperCase();
const row = printPlatforms.find(item => String(item.platform_code || '').toUpperCase() === code) || null;
return row ? createFindOneResult(row) : null;
},
},
ExpressInterface: {
async findOne(query) {
calls.expressInterfaceFindOneCalls.push(query);
if (typeof options.expressInterfaceFindOne === 'function') {
return await options.expressInterfaceFindOne(query);
}
return null;
},
},
},
};
const service = new ShopsService(ctx);
return {
service,
calls,
ctx,
};
}
describe('test/app/service/systems/shops.test.js', () => {
it('list 应带上仓库与软删除过滤并返回分页', async () => {
const { service, calls } = createHarness({
shopRows: [
{
id: 1,
warehouse_id: 100000,
platform_code: 'PDD',
shop_name: '店铺A',
custom_name: '自定义A',
status: 1,
auth_expire_at: '2026-06-01 00:00:00',
created_at: '2026-05-01 00:00:00',
updated_at: '2026-05-02 00:00:00',
printPlatform: {
platform_code: 'PDD',
platform_name: '拼多多',
},
},
],
});
const result = await service.list({
page: 1,
pageSize: 20,
});
assert.equal(Array.isArray(result.list), true);
assert.equal(result.list.length, 1);
assert.equal(result.pagination.total, 1);
assert.equal(result.list[0].auth_expire_at, '2026-06-01 00:00:00');
assert.equal(Boolean(calls.shopFindAndCountAll.where), true);
});
it('add 应校验平台并创建 status=0 店铺', async () => {
const { service, calls } = createHarness({
shopRows: [],
});
const result = await service.add({
custom_name: '测试店铺',
platform_code: 'pdd',
});
assert.equal(result.id, 9001);
assert.equal(calls.shopCreatePayload.platform_code, 'PDD');
assert.equal(calls.shopCreatePayload.custom_name, '测试店铺');
assert.equal(calls.shopCreatePayload.status, 0);
});
it('add 传入 shop_name 时也应忽略并生成待授权店铺名', async () => {
const { service, calls } = createHarness({
shopFindOne: async query => {
const where = query && query.where ? query.where : {};
if (where.shop_name === '前端传入店铺名') {
return createFindOneResult({ id: 88 });
}
return null;
},
});
const result = await service.add({
custom_name: '02',
platform_code: 'PDD',
shop_name: '前端传入店铺名',
});
assert.equal(result.id, 9001);
assert.equal(calls.shopCreatePayload.custom_name, '02');
assert.notEqual(calls.shopCreatePayload.shop_name, '前端传入店铺名');
assert.equal(calls.shopCreatePayload.shop_name.startsWith('PENDING_'), true);
});
it('add 未传 shop_name 时,首次占位名冲突应重试并成功', async () => {
let findOneCallCount = 0;
const { service, calls } = createHarness({
shopFindOne: async () => {
findOneCallCount += 1;
if (findOneCallCount === 1) {
return createFindOneResult({ id: 101 });
}
return null;
},
});
const generatedCodes = [ 'RETRY1', 'RETRY2' ];
service.generateAuthorizeRandomCode = () => generatedCodes.shift() || 'RETRY_FALLBACK';
const result = await service.add({
custom_name: '重试店铺',
platform_code: 'PDD',
});
assert.equal(result.id, 9001);
assert.equal(calls.shopCreatePayload.shop_name, 'PENDING_RETRY2');
});
it('add 连续冲突超过重试上限时应返回占位名冲突提示', async () => {
const { service } = createHarness({
shopFindOne: async () => {
return createFindOneResult({ id: 102 });
},
});
service.generateAuthorizeRandomCode = () => 'ALWAYS_CONFLICT';
await assert.rejects(async () => {
await service.add({
custom_name: '冲突店铺',
platform_code: 'PDD',
});
}, error => {
assert.equal(error && error.message, '系统生成占位名冲突,请重试');
assert.equal(error && error.code, 409);
return true;
});
});
it('del 遇到快递接口引用时应拒绝删除', async () => {
const { service } = createHarness({
shopRows: [
{
id: 5,
warehouse_id: 100000,
platform_code: 'PDD',
shop_name: '店铺X',
custom_name: '店铺X',
status: 1,
},
],
expressInterfaceFindOne: async () => ({ id: 1 }),
});
await assert.rejects(async () => {
await service.del({ id: 5 });
}, /店铺存在快递接口引用/);
});
it('edit 仅修改 custom_name/platform_code', async () => {
const { service } = createHarness({
shopRows: [
{
id: 7,
warehouse_id: 100000,
platform_code: 'PDD',
shop_name: '店铺Y',
custom_name: '旧名',
status: 1,
},
],
});
const result = await service.edit({
id: 7,
custom_name: '新名',
platform_code: 'PDD',
});
assert.equal(result.updated, true);
assert.equal(result.id, 7);
});
it('getAuthorizeSessionKey 请求授权接口失败时应抛出业务错误', async () => {
const { service } = createHarness({
shopRows: [
{
id: 9,
warehouse_id: 100000,
platform_code: 'DY',
shop_name: '店铺Z',
custom_name: '店铺Z',
status: 1,
},
],
printPlatforms: [
{
platform_code: 'DY',
platform_name: '抖音',
auth_base_url: '',
deleted: 0,
status: 'ACTIVE',
},
],
curl: async () => ({
data: {
code: 1,
msg: '接口异常',
},
}),
});
await assert.rejects(async () => {
await service.getAuthorizeSessionKey({
id: 9,
callbackurl: 'https://callback.example.com',
});
}, /获取授权失败:接口异常/);
});
});