'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 ]; 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; } function normalizeDetailPayload(data) { if (data && data.receiving) return data; if (data && data.data && data.data.receiving) return data.data; return null; } async function switchToWarehouseRole(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 warehouseRoles = employees.filter(item => item && item.role_type === 'warehouse' && Number(item.id)); const candidateRoleIds = [ ...new Set([ ...warehouseRoles.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 listRes = await invokeSystemMethod('storage.receiving.getReceivingList', { pagination: { page: 1, pageSize: 10 }, where: {}, }, currentToken, EXTRA_HEADERS); if (listRes.status === 200 && listRes.body?.flag === 'success' && listRes.body?.code === 200) { return { token: currentToken, roleId, }; } } throw new Error('未找到可访问收货单的仓库角色'); } describe('test/app/controller/system.storage.receiving.test.js', () => { it('should query receiving list', async () => { const { token } = await getAuthToken(); const switched = await switchToWarehouseRole(token); const res = await invokeSystemMethod('storage.receiving.getReceivingList', { 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 query receiving detail when list has records', async () => { const { token } = await getAuthToken(); const switched = await switchToWarehouseRole(token); const listRes = await invokeSystemMethod('storage.receiving.getReceivingList', { pagination: { page: 1, pageSize: 10, }, where: {}, }, switched.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); assert.ok(payload, '返回结构中缺少收货单列表 payload'); const first = Array.isArray(payload.list) && payload.list.length > 0 ? payload.list[0] : null; if (!first || !first.id) { return; } const detailRes = await invokeSystemMethod('storage.receiving.getReceivingDetail', { id: Number(first.id), pagination: { page: 1, pageSize: 10, }, }, switched.token, EXTRA_HEADERS); assert.equal(detailRes.status, 200); assert.equal(detailRes.body.flag, 'success'); assert.equal(detailRes.body.code, 200); const detailPayload = normalizeDetailPayload(detailRes.body.data); assert.ok(detailPayload, '返回结构中缺少收货单详情 payload'); assert.ok(detailPayload.receiving, 'receiving 字段缺失'); assert.equal(Number(detailPayload.receiving.id), Number(first.id)); }); it('should create and then batch cancel receiving order', async () => { const { token } = await getAuthToken(); const switched = await switchToWarehouseRole(token); let createdId = null; try { const uniquePlate = `ITEST-${Date.now().toString().slice(-6)}`; const createRes = await invokeSystemMethod('storage.receiving.create', { delivery_method: 'truck', license_plate: uniquePlate, loading_method: 'with_pallet', remark: 'integration-test', total_boxes: 0, total_pallets: 0, }, switched.token, EXTRA_HEADERS); assert.equal(createRes.status, 200); assert.equal(createRes.body.flag, 'success'); assert.equal(createRes.body.code, 200); createdId = Number(createRes.body.id || createRes.body?.data?.id || 0); assert.ok(createdId > 0, `创建成功但未返回有效 id: ${JSON.stringify(createRes.body)}`); const cancelRes = await invokeSystemMethod('storage.receiving.batchCancelReceiving', { ids: [ createdId ], }, switched.token, EXTRA_HEADERS); assert.equal(cancelRes.status, 200); assert.equal(cancelRes.body.flag, 'success'); assert.equal(cancelRes.body.code, 200); assert.match(String(cancelRes.body.message || ''), /成功/); } finally { if (createdId > 0) { await invokeSystemMethod('storage.receiving.batchCancelReceiving', { ids: [ createdId ], }, switched.token, EXTRA_HEADERS); } } }); it('should reject getReceivingDetail with invalid id', async () => { const { token } = await getAuthToken(); const switched = await switchToWarehouseRole(token); const res = await invokeSystemMethod('storage.receiving.getReceivingDetail', { id: -1, pagination: { page: 1, pageSize: 10, }, }, switched.token, EXTRA_HEADERS); assert.equal(res.status, 200); assert.equal(res.body.flag, 'success'); assert.equal(res.body.code, 0); assert.match(String(res.body.message || ''), /无记录|无权访问|不存在/); }); it('should reject create with invalid loading_method enum', async () => { const { token } = await getAuthToken(); const switched = await switchToWarehouseRole(token); const res = await invokeSystemMethod('storage.receiving.create', { delivery_method: 'truck', license_plate: `ITEST-ERR-${Date.now().toString().slice(-6)}`, loading_method: 'manual', remark: 'invalid-enum-test', total_boxes: 0, total_pallets: 0, }, switched.token, EXTRA_HEADERS); assert.equal(res.status, 200); assert.equal(res.body.flag, 'success'); assert.equal(res.body.code, 0); assert.match(String(res.body.message || ''), /创建失败|Data truncated|loading_method/); }); it('should reject batchCancelReceiving with nonexistent ids', async () => { const { token } = await getAuthToken(); const switched = await switchToWarehouseRole(token); const res = await invokeSystemMethod('storage.receiving.batchCancelReceiving', { ids: [ 999999999 ], }, switched.token, EXTRA_HEADERS); assert.equal(res.status, 200); assert.equal(res.body.flag, 'success'); assert.equal(res.body.code, 0); assert.match(String(res.body.message || ''), /不存在|无权访问|状态不允许/); }); });