'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 buildProductForm 归属口径', () => { const service = createService(); it('我的店铺:SelectShopId=ShopId,不带商家与平台过滤以外的字段', () => { const form = service.buildProductForm('', 1, 500, { shopId: '12345897', platformType: 'TouTiao' }); assert.strictEqual(form.get('SelectShopId'), '12345897'); assert.strictEqual(form.get('SelectAgentId'), ''); assert.strictEqual(form.get('SelectPlatformType'), 'TouTiao'); }); it('平台商家:SelectAgentId=FxUserId,不带店铺', () => { const form = service.buildProductForm('953516', 1, 500, {}); assert.strictEqual(form.get('SelectAgentId'), '953516'); assert.strictEqual(form.get('SelectShopId'), ''); assert.strictEqual(form.get('SelectPlatformType'), ''); }); }); describe('printPlatform/dgj 我的店铺缺少 ShopId', () => { const service = createService(); const shop = { external_source: 'my_shop', external_data: { FxUserId: 1227061, PlatformType: 'TouTiao' } }; it('listOrders 明确报错,不退回按账号 FxUserId 拉取', async () => { await assert.rejects(() => service.listOrders(shop, {}), /缺少平台店铺ID/); }); it('listProducts 明确报错,不退回按账号 FxUserId 拉取', async () => { await assert.rejects(() => service.listProducts(shop), /缺少平台店铺ID/); }); }); describe('printPlatform/dgj 我的店铺订单过滤', () => { it('按 LogicOrder.ShopId 过滤(Contract=in),不带商家条件;时间窗上界补 .999', async () => { const service = createService(); let captured = null; let requestUrl = ''; service.request = async (url, options) => { requestUrl = url; captured = new URLSearchParams(options.body); return { status: 200, json: { Success: true, Data: { Rows: [], Total: 0 } }, text: '', setCookies: [] }; }; await service.fetchOrderPage('fxdd.dgjapp.com', { traceId: 'trace', dbname: 'y/2wUDWCZOk=', startTime: new Date('2026-11-01T00:00:00+08:00'), endTime: new Date('2026-11-08T23:59:59+08:00'), pageIndex: 1, erpState: 'waitsellersend', shopId: '12345897', }); const filters = []; for (let index = 0; index < 5; index += 1) { const name = captured.get(`Filters[${index}][Name]`); if (!name) break; filters.push(`${name}:${captured.get(`Filters[${index}][Contract]`)}:${captured.get(`Filters[${index}][Value]`)}`); } assert.deepStrictEqual(filters, [ 'CreateTime:between:2026-11-01 00:00:00', 'ErpState:=:waitsellersend', 'ShopId:in:12345897' ]); assert.strictEqual(captured.get('Filters[2][TableAlias]'), 'o'); assert.strictEqual(captured.get('Filters[2][TableName]'), 'LogicOrder'); assert.strictEqual(captured.get('Filters[0][ExtValue]'), '2026-11-08 23:59:59.999'); assert.ok(requestUrl.includes('/NewOrder/List')); }); it('同时传 shopId 与 merchantFxUserId 时以店铺为准(不串商家口径)', async () => { const service = createService(); let captured = null; service.request = async (url, options) => { captured = new URLSearchParams(options.body); return { status: 200, json: { Success: true, Data: { Rows: [], Total: 0 } }, text: '', setCookies: [] }; }; await service.fetchOrderPage('fxdd.dgjapp.com', { traceId: 'trace', dbname: '', pageIndex: 1, shopId: '12345897', merchantFxUserId: '953516', }); const names = []; for (let index = 0; index < 5; index += 1) { const name = captured.get(`Filters[${index}][Name]`); if (!name) break; names.push(name); } assert.ok(names.includes('ShopId')); assert.ok(!names.includes('UpFxUserId')); }); }); 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'); }); });