liufumingren dc4894cb6d 1
2026-05-25 17:08:56 +08:00

168 lines
4.0 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const ShopsMethodService = require('../../../../app/service/systemMethods/shops');
function createHarness() {
const calls = {
list: [],
add: [],
del: [],
edit: [],
getAuthorizeSessionKey: [],
prepareWhereContext: [],
};
const ctx = {
app: {
config: {
jwt: {},
},
Sequelize: {},
},
user: {
store_id: 100000,
},
service: {
systems: {
shops: {
getListWhereSpecialFieldMap() {
return {
id: { field: 'id' },
};
},
async list(payload) {
calls.list.push(payload);
return {
list: [],
pagination: {
page: 1,
pageSize: 20,
total: 0,
totalPages: 0,
hasNext: false,
hasPrevious: false,
},
};
},
async add(payload) {
calls.add.push(payload);
return { id: 1 };
},
async del(payload) {
calls.del.push(payload);
return { deleted: true, id: 1 };
},
async edit(payload) {
calls.edit.push(payload);
return { updated: true, id: 1 };
},
async getAuthorizeSessionKey(payload) {
calls.getAuthorizeSessionKey.push(payload);
return {
shop_id: 1,
session_key: 'session-key',
authorize_url: 'https://oauth.example.com/state',
};
},
},
},
},
};
const service = new ShopsMethodService(ctx);
service.prepareWhereContext = function prepareWhereContext(data, options) {
calls.prepareWhereContext.push({ data, options });
return { where: data.where || {} };
};
return {
service,
calls,
};
}
describe('test/app/service/systemMethods/shops.test.js', () => {
it('shops.list 应注入 where 解析并返回统一结构', async () => {
const { service, calls } = createHarness();
const result = await service.list({
page: 1,
pageSize: 20,
where: {
id: {
operator: 'eq',
value: 1,
},
},
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(calls.prepareWhereContext.length, 1);
assert.equal(calls.list.length, 1);
});
it('shops.add 应解构并透传 custom_name/platform_code/shop_name', async () => {
const { service, calls } = createHarness();
const result = await service.add({
custom_name: '自定义店铺A',
platformCode: 'pdd',
shop_name: '店铺A',
});
assert.equal(result.flag, 'success');
assert.deepEqual(calls.add[0], {
custom_name: '自定义店铺A',
platform_code: 'pdd',
shop_name: '店铺A',
});
});
it('shops.del 应兼容 shop_id 并映射为 id', async () => {
const { service, calls } = createHarness();
const result = await service.del({
shop_id: 22,
});
assert.equal(result.flag, 'success');
assert.deepEqual(calls.del[0], {
id: 22,
});
});
it('shops.edit 只透传存在的可编辑字段', async () => {
const { service, calls } = createHarness();
const result = await service.edit({
id: 5,
customName: '新名称',
platform_code: 'CAINIAO',
});
assert.equal(result.flag, 'success');
assert.deepEqual(calls.edit[0], {
id: 5,
custom_name: '新名称',
platform_code: 'CAINIAO',
});
});
it('shop.GetAuthorizeSessionKey 应兼容 callback_url 与 shopId', async () => {
const { service, calls } = createHarness();
const result = await service.GetAuthorizeSessionKey({
shopId: 7,
callback_url: 'https://callback.example.com',
});
assert.equal(result.flag, 'success');
assert.deepEqual(calls.getAuthorizeSessionKey[0], {
id: 7,
callbackurl: 'https://callback.example.com',
});
});
});