'use strict'; const { assert } = require('egg-mock/bootstrap'); const DgjService = require('../../../app/lib/printPlatform/dgj'); /** * 构造一个只用于纯函数校验的店管家服务实例(不会发请求) * @return {DgjService} 服务实例 */ function createService() { return new DgjService({ platform: { code: 'dgj', name: '店管家', url: 'fxali.dgjapp.com' }, account: { token: 'token', cookie: 'cookie' }, }); } describe('printPlatform/dgj listShopSources', () => { it('默认来源为平台商家,且包含我的店铺', () => { const sources = createService().listShopSources(); assert.deepStrictEqual(sources.map(item => item.code), [ 'agent', 'my_shop' ]); assert.strictEqual(sources[0].name, '平台商家'); }); }); describe('printPlatform/dgj normalizeMyShop', () => { const service = createService(); it('授权成功:启用、平台编码映射到系统预设', () => { const item = service.normalizeMyShop({ ShopId: 12345897, ShopNameRemark: 'Moocola个人护理旗舰店', NickName: 'Moocola', PlatformType: 'TouTiao', PlatformTypeName: '抖店', Status: 1, IsExpire: false, IsAuthExpired: false, AuthTime: '2026-12-07 21:23:16', }); assert.strictEqual(item.source, 'my_shop'); assert.strictEqual(item.external_id, '12345897'); assert.strictEqual(item.name, 'Moocola个人护理旗舰店'); assert.strictEqual(item.status, 1); assert.strictEqual(item.status_label, '正常'); assert.strictEqual(item.platform_code, 'DYXD'); assert.strictEqual(item.platform_name, '抖店'); assert.strictEqual(item.platform_type, 'TouTiao'); assert.strictEqual(item.auth_expired, false); assert.strictEqual(item.service_expired, false); assert.strictEqual(item.auth_time, '2026-12-07 21:23:16'); }); it('授权过期(Status=3):禁用并给出「授权过期」', () => { const item = service.normalizeMyShop({ ShopId: 1, ShopNameRemark: '过期店', PlatformType: 'KuaiShou', Status: 3, IsAuthExpired: true }); assert.strictEqual(item.status, 0); assert.strictEqual(item.status_label, '授权过期'); assert.strictEqual(item.auth_expired, true); assert.strictEqual(item.platform_code, 'KS'); }); it('服务到期但授权仍有效:保持启用,文案为「服务到期」', () => { const item = service.normalizeMyShop({ ShopId: 2, ShopNameRemark: '临期店', PlatformType: 'XiaoHongShu', Status: 1, IsExpire: true }); assert.strictEqual(item.status, 1); assert.strictEqual(item.status_label, '服务到期'); assert.strictEqual(item.service_expired, true); assert.strictEqual(item.platform_code, 'XHS'); }); it('线下单映射到线下,名称缺失时兜底为空串', () => { const item = service.normalizeMyShop({ ShopId: 3, PlatformType: 'Virtual', Status: 1 }); assert.strictEqual(item.platform_code, 'OFFLINE'); assert.strictEqual(item.name, ''); }); }); describe('printPlatform/dgj resolveShopScope', () => { const service = createService(); it('我的店铺:按 ShopId 定位,域名由 PlatformType 推导', () => { const scope = service.resolveShopScope({ code: 'dgj-1-my_shop-12345897', external_source: 'my_shop', external_data: { ShopId: 12345897, PlatformType: 'TouTiao' }, }); assert.deepStrictEqual(scope, { source: 'my_shop', externalId: '12345897', shopId: '12345897', platformType: 'TouTiao', hosts: [ 'fxdd.dgjapp.com' ], }); }); it('平台商家:按商家 Id 定位,域名由分区声明推导', () => { const scope = service.resolveShopScope({ code: 'dgj-1-7251148', external_source: 'agent', external_data: { Id: 7251148, FxUserId: 953516, DbAreas: [{ DbName: 'alibaba' }] }, }); assert.strictEqual(scope.source, 'agent'); assert.strictEqual(scope.externalId, '7251148'); assert.deepStrictEqual(scope.hosts, [ 'fxali.dgjapp.com' ]); }); it('来源缺省时按平台商家处理(兼容老数据)', () => { const scope = service.resolveShopScope({ code: 'dgj-1-100', external_data: { Id: 100 } }); assert.strictEqual(scope.source, 'agent'); assert.strictEqual(scope.externalId, '100'); }); }); describe('printPlatform/dgj 我的店铺暂不支持拉单/拉商品', () => { const service = createService(); const shop = { external_source: 'my_shop', external_data: { ShopId: 12345897, FxUserId: 1227061, PlatformType: 'TouTiao' } }; it('listOrders 明确拒绝,避免按账号 FxUserId 拉到别人的订单', async () => { await assert.rejects(() => service.listOrders(shop, {}), /暂未支持拉取订单/); }); it('listProducts 明确拒绝,避免按账号 FxUserId 拉到别人的商品', async () => { await assert.rejects(() => service.listProducts(shop), /暂未支持同步商品/); }); }); describe('printPlatform/dgj resolveMyShopHost', () => { const service = createService(); it('按平台类型映射到订单/商品接口域名', () => { assert.strictEqual(service.resolveMyShopHost('TouTiao'), 'fxdd.dgjapp.com'); assert.strictEqual(service.resolveMyShopHost('Pinduoduo'), 'fxpdd.dgjapp.com'); assert.strictEqual(service.resolveMyShopHost('Jingdong'), 'fxjd.dgjapp.com'); assert.strictEqual(service.resolveMyShopHost('Taobao'), 'fxali.dgjapp.com'); assert.strictEqual(service.resolveMyShopHost('KuaiShou'), 'fxali.dgjapp.com'); assert.strictEqual(service.resolveMyShopHost('WxVideo'), 'fxali.dgjapp.com'); assert.strictEqual(service.resolveMyShopHost('XiaoHongShu'), 'fxali.dgjapp.com'); assert.strictEqual(service.resolveMyShopHost('Virtual'), 'fxali.dgjapp.com'); assert.strictEqual(service.resolveMyShopHost(''), 'fxali.dgjapp.com'); }); });