'use strict'; const assert = require('node:assert/strict'); const ExpressPlatformService = require('../../../../app/service/systems/express_platform'); /** * 构建拼多多服务最小测试桩。 * * @param {Object} [options={}] 测试配置。 * @param {Object} [options.contractConfig] 合约配置覆盖值。 * @return {{ service: Object, calls: Object }} 服务实例与调用记录。 */ function createPddHarness(options = {}) { const calls = { curl: 0, curlOptions: null, transaction: 0, orderExpressUpdate: 0, orderDeliveryUpdate: 0, storeWarehouseFindAll: 0, storeWarehouseUpdate: 0, printQuantityLogCreate: 0, operationLogCreateOperationLogs: 0, getContractConfigCalls: [], filterAutoSyncSnOrderIdsCalls: [], syncOrderIdsCalls: [], }; const ctx = { app: { config: { pdd: { client_id: 'test-client-id', client_secret: 'test-client-secret', }, }, Sequelize: { Op: { or: 'or', }, }, async setCacheAsync() { // no-op in tests }, }, model: { async transaction(callback) { calls.transaction += 1; const transaction = { LOCK: { UPDATE: 'UPDATE', }, }; return callback(transaction); }, OrderExpress: { async update() { calls.orderExpressUpdate += 1; return [ 1 ]; }, }, StoreWarehouse: { async findAll() { calls.storeWarehouseFindAll += 1; return []; }, async update() { calls.storeWarehouseUpdate += 1; return [ 1 ]; }, }, StoreWarehousePrintQuantityLog: { async create() { calls.printQuantityLogCreate += 1; return {}; }, }, }, service: { systems: { express: { async loadExpressList() { return []; }, async getWaybillAsyncOrderDeliveryModel() { return { async update() { calls.orderDeliveryUpdate += 1; return [ 1 ]; }, }; }, }, operationLog: { async createOperationLogs() { calls.operationLogCreateOperationLogs += 1; return []; }, }, storeWarehouse: { async getContractConfig(warehouseId, storeId) { calls.getContractConfigCalls.push({ warehouseId, storeId }); if (options.contractConfig !== undefined) { return options.contractConfig; } return { policy_config: { auto_sync_back: 1 } }; }, }, }, systemMethods: { order: { pendingOrder: { async filterAutoSyncSnOrderIds(request) { calls.filterAutoSyncSnOrderIdsCalls.push(request); const skipped = Array.isArray(options.snOrderIds) ? options.snOrderIds : []; const requestOrderIds = Array.isArray(request.orderIds) ? request.orderIds : []; return { orderIds: requestOrderIds.filter(orderId => !skipped.includes(orderId)), skippedSnOrderIds: requestOrderIds.filter(orderId => skipped.includes(orderId)), }; }, async syncOrderIds(request) { calls.syncOrderIdsCalls.push(request); return { flag: 'success' }; }, }, }, }, }, logger: { info() {}, warn() {}, error() {}, }, async curl(url, options) { calls.curl += 1; calls.curlOptions = { url, options, }; return { data: { pdd_waybill_get_response: { modules: [ { object_id: '100000_2001_3001', waybill_code: 'WB-2001', }, ], }, }, }; }, }; return { service: new ExpressPlatformService(ctx), calls, }; } /** * 构造拼多多接口入参。 * * @return {Object} 拼多多接口测试入参。 */ function buildPayload() { return { matchedPlatform: { template_url: 'https://example.com/template', }, listByWarehousePlatformCode: { id: 91, token: 'test-token', ext_data: { category: 'fragile', tags: [ 'cold', 'urgent' ], }, city_name: '上海市', province_name: '上海市', district_name: '浦东新区', town_name: '花木街道', address: '世纪大道100号', sender_tel: '13800000000', sender_name: '测试发件人', logistics_code: 'PDD-EXP', logistics_name: '拼多多快递', name: '拼多多接口', platform_code: 'PDD', express: { PDD: 'YTO', }, shop: { owner_id: 'owner-1', }, }, orders: [ { business_key: 3001, order_delivery: { id: 2001, warehouse_id: 100000, store_id: 5001, source_order_codes: 'SO-1', delivery_order_code: 'DO-1', orderItems: [ { item_name: '商品A', plan_qty: 2, }, ], city: '上海市', province: '上海市', area: '浦东新区', town: '花木街道', detail_address: '测试地址1号', mobile: '13900000000', tel: '021-00000000', name: '收件人A', }, }, ], }; } describe('test/app/service/systems/express_platform.pdd.ext_data.test.js', () => { it('should stringify object ext_data for logistics_services', async () => { const { service, calls } = createPddHarness(); const payload = buildPayload(); const result = await service.pdd(payload); assert.equal(calls.curl, 1); assert.ok(calls.curlOptions); const requestBody = calls.curlOptions.options.data; const waybillRequest = JSON.parse(requestBody.param_waybill_cloud_print_apply_new_request); const orderItem = waybillRequest.trade_order_info_dtos[0]; assert.equal(typeof orderItem.logistics_services, 'string'); assert.equal(orderItem.logistics_services, JSON.stringify(payload.listByWarehousePlatformCode.ext_data)); assert.equal(result.flag, true); assert.equal(result.code, 0); }); it('auto_sync_back 开启且无登录用户上下文时,应以显式 warehouseId 触发订单同步回传', async () => { const { service, calls } = createPddHarness(); await service.pdd(buildPayload()); assert.equal(calls.getContractConfigCalls.length, 1); assert.deepEqual(calls.getContractConfigCalls[0], { warehouseId: 100000, storeId: 5001 }); assert.equal(calls.filterAutoSyncSnOrderIdsCalls.length, 1); assert.equal(calls.filterAutoSyncSnOrderIdsCalls[0].source, 'pdd'); assert.equal(calls.filterAutoSyncSnOrderIdsCalls[0].warehouseId, 100000); assert.equal(calls.syncOrderIdsCalls.length, 1); assert.deepEqual(calls.syncOrderIdsCalls[0], { orderIds: [ 2001 ], warehouseId: 100000, }); }); it('auto_sync_back 开启时 SN 订单静默跳过,不触发同步回传', async () => { const { service, calls } = createPddHarness({ snOrderIds: [ 2001 ] }); await service.pdd(buildPayload()); assert.equal(calls.filterAutoSyncSnOrderIdsCalls.length, 1); assert.equal(calls.syncOrderIdsCalls.length, 0); }); it('auto_sync_back 关闭时不应触发订单同步回传', async () => { const { service, calls } = createPddHarness({ contractConfig: { policy_config: { auto_sync_back: 0 } }, }); await service.pdd(buildPayload()); assert.equal(calls.syncOrderIdsCalls.length, 0); }); });