'use strict'; const assert = require('node:assert/strict'); const { getAuthToken, invokeSystemMethod, } = require('../../support/system_method_client'); const EXTRA_HEADERS = { platform: 'web', project: 'all', }; const FALLBACK_ROLE_IDS = [ 4, 6, 30 ]; const TEST_WAREHOUSE_ID = 100000; function normalizeListPayload(data) { if (data && Array.isArray(data.list)) return data; if (data && data.data && Array.isArray(data.data.list)) return data.data; return null; } async function switchToWarehouseRoleWithDeliveryData(token) { let currentToken = token; const infoRes = await invokeSystemMethod('user.info', {}, currentToken, EXTRA_HEADERS); assert.equal(infoRes.status, 200); assert.equal(infoRes.body.flag, 'success'); assert.equal(infoRes.body.code, 200); const employees = Array.isArray(infoRes.body?.data?.employees) ? infoRes.body.data.employees : []; const preferredWarehouseRoles = employees.filter(item => item && item.role_type === 'warehouse' && Number(item.id) && Number(item.store_id) === TEST_WAREHOUSE_ID ); const otherWarehouseRoles = employees.filter(item => item && item.role_type === 'warehouse' && Number(item.id) && Number(item.store_id) !== TEST_WAREHOUSE_ID ); const candidateRoleIds = [ ...new Set([ ...preferredWarehouseRoles.map(item => Number(item.id)), ...otherWarehouseRoles.map(item => Number(item.id)), ...FALLBACK_ROLE_IDS, ].filter(Boolean)) ]; assert.ok(candidateRoleIds.length > 0, '当前账号没有可尝试的仓库角色'); for (const roleId of candidateRoleIds) { const changeRes = await invokeSystemMethod('user.changeRole', { roleId, }, currentToken, EXTRA_HEADERS); if (changeRes.status !== 200 || changeRes.body?.flag !== 'success' || changeRes.body?.code !== 200) { continue; } currentToken = changeRes.body?.data?.token || currentToken; const statsRes = await invokeSystemMethod('order.stats', { where: {}, }, currentToken, EXTRA_HEADERS); if (statsRes.status !== 200 || statsRes.body?.flag !== 'success' || statsRes.body?.code !== 200) { continue; } const total = Number(statsRes.body?.data?.total || 0); if (total > 0) { return { token: currentToken, roleId, total, }; } } throw new Error('未找到有订单数据的仓库角色'); } async function getFirstDelivery(token) { const res = await invokeSystemMethod('storage.stockout.list', { pagination: { page: 1, pageSize: 10, }, where: {}, }, token, EXTRA_HEADERS); assert.equal(res.status, 200); assert.equal(res.body.flag, 'success'); assert.equal(res.body.code, 200); const payload = normalizeListPayload(res.body.data); assert.ok(payload, '返回结构中缺少出库单列表 payload'); assert.ok(Array.isArray(payload.list), '出库单 list 不是数组'); assert.ok(payload.pagination, '出库单 pagination 缺失'); const first = payload.list[0]; assert.ok(first, '出库单列表为空,无法继续验证 toOrder/listItems'); return first; } async function getEntryContextForRemovedMethods(token) { const listRes = await invokeSystemMethod('storage.entry.list', { pagination: { page: 1, pageSize: 1, }, where: {}, }, token, EXTRA_HEADERS); assert.equal(listRes.status, 200); assert.equal(listRes.body.flag, 'success'); assert.equal(listRes.body.code, 200); const payload = normalizeListPayload(listRes.body.data); const entry = payload && Array.isArray(payload.list) ? payload.list[0] : null; if (!entry || !entry.id) { return { orderId: 1, detailId: 1, itemCode: `SKU-${Date.now().toString().slice(-6)}`, }; } const getRes = await invokeSystemMethod('storage.entry.get', { id: Number(entry.id), }, token, EXTRA_HEADERS); assert.equal(getRes.status, 200); assert.equal(getRes.body.flag, 'success'); assert.equal(getRes.body.code, 200); const entryOrder = getRes.body?.data?.entryOrder || {}; const firstItem = Array.isArray(entryOrder?.items) ? entryOrder.items[0] : null; return { orderId: Number(entry.id), detailId: Number(firstItem?.id || 1), itemCode: String(firstItem?.item_code || firstItem?.stock?.item_code || `SKU-${Date.now().toString().slice(-6)}`), }; } describe('test/app/controller/system.storage.delivery.module.test.js', function() { this.timeout(120000); it('should return delivery list via storage.stockout.list', async () => { const { token } = await getAuthToken(); const switched = await switchToWarehouseRoleWithDeliveryData(token); const res = await invokeSystemMethod('storage.stockout.list', { pagination: { page: 1, pageSize: 10, }, where: {}, }, switched.token, EXTRA_HEADERS); assert.equal(res.status, 200); assert.equal(res.body.flag, 'success'); assert.equal(res.body.code, 200); const payload = normalizeListPayload(res.body.data); assert.ok(payload, '返回结构中缺少出库单列表 payload'); assert.ok(Array.isArray(payload.list), '出库单 list 不是数组'); assert.ok(payload.pagination, '出库单 pagination 缺失'); assert.equal(typeof payload.pagination.total, 'number', 'pagination.total 不是 number'); }); it('should return placeholder failure via storage.stockout.toOrder', async () => { const { token } = await getAuthToken(); const switched = await switchToWarehouseRoleWithDeliveryData(token); const delivery = await getFirstDelivery(switched.token); const res = await invokeSystemMethod('storage.stockout.toOrder', { id: Number(delivery.id), delivery_order_code: delivery.delivery_order_code, warehouse_id: TEST_WAREHOUSE_ID, }, switched.token, EXTRA_HEADERS); console.log('[storage.stockout.toOrder]', JSON.stringify({ flag: res.body?.flag, code: res.body?.code, message: res.body?.message, placeholder: res.body?.placeholder, })); assert.equal(res.status, 200); assert.notEqual(res.body.flag, 'success', 'toOrder 不应再返回 success'); assert.equal(Number(res.body.code), 405); assert.match(String(res.body.message || ''), /占位接口|暂未开放/); assert.equal(res.body.flag, 'failure'); assert.equal(res.body.placeholder, true); }); it('should return item list via storage.stockout.listItems', async () => { const { token } = await getAuthToken(); const switched = await switchToWarehouseRoleWithDeliveryData(token); const delivery = await getFirstDelivery(switched.token); const res = await invokeSystemMethod('storage.stockout.listItems', { id: Number(delivery.id), delivery_order_code: delivery.delivery_order_code, warehouse_id: TEST_WAREHOUSE_ID, }, switched.token, EXTRA_HEADERS); assert.equal(res.status, 200); assert.equal(res.body.flag, 'success'); assert.equal(res.body.code, 200); assert.ok(res.body.data && typeof res.body.data === 'object', '缺少 data 对象'); assert.ok(Array.isArray(res.body.data.items), 'data.items 不是数组'); }); it('should not expose removed storage.entry detail methods', async () => { const { token } = await getAuthToken(); const switched = await switchToWarehouseRoleWithDeliveryData(token); const entryContext = await getEntryContextForRemovedMethods(switched.token); const removedMethods = [ { method: 'storage.entry.addDetail', body: { order_id: entryContext.orderId, count: 1, item_code: entryContext.itemCode, inventory_type: 'ZP', }, }, { method: 'storage.entry.deleteDetail', body: { id: entryContext.detailId, }, }, { method: 'storage.entry.detailChange', body: { id: entryContext.detailId, count: 2, item_code: entryContext.itemCode, }, }, ]; for (const { method, body } of removedMethods) { const res = await invokeSystemMethod(method, body, switched.token, EXTRA_HEADERS); const isLegacySuccess = res.status === 200 && res.body?.flag === 'success' && Number(res.body?.code) === 200; assert.ok(!isLegacySuccess, `${method} 仍然返回 success 200: ${JSON.stringify(res.body)}`); } }); });