'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('未找到可访问收货单的仓库角色'); } async function getBindableStoreInfo(token) { const res = await invokeSystemMethod('storeWarehouse.store.list', { page: 1, page_size: 20, }, token, EXTRA_HEADERS); assert.equal(res.status, 200); assert.equal(res.body.flag, 'success'); assert.equal(res.body.code, 200); const list = Array.isArray(res.body?.data?.list) ? res.body.data.list : []; const candidate = list.find(item => Number(item?.counterparty_id || item?.store_id || 0) > 0); if (!candidate) { return null; } return { store_id: Number(candidate.counterparty_id || candidate.store_id || 0), store_name: String(candidate.store_name || candidate.counterparty_name || ''), }; } async function getBindableSystemProducts(token) { const res = await invokeSystemMethod('stock.warehouse.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); const list = Array.isArray(payload?.list) ? payload.list : []; return list .filter(item => Number(item?.id || 0) > 0) .map(item => ({ id: Number(item.id), product_name: String(item.item_name || item.name || item.product_name || ''), })); } 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)); assert.ok(Array.isArray(detailPayload.receiving.details), 'receiving.details 应为数组'); const detailWithImages = detailPayload.receiving.details.find(item => Array.isArray(item.images) && item.images.length > 0); if (detailWithImages) { const firstImage = detailWithImages.images[0]; assert.equal(typeof firstImage, 'object', '明细图片应返回对象结构'); assert.equal(typeof firstImage.url, 'string', '明细图片应返回实际链接'); assert.ok(firstImage.url.length > 0, '明细图片链接不能为空'); assert.ok(!Object.prototype.hasOwnProperty.call(firstImage, 'business_type'), '不应返回 FileReference 原始字段'); } }); it('should bind store to receiving and reject rebinding when owner already exists', async function() { const { token } = await getAuthToken(); const switched = await switchToWarehouseRole(token); const bindableStore = await getBindableStoreInfo(switched.token); let createdId = 0; if (!bindableStore || bindableStore.store_id <= 0) { this.skip(); } try { const createRes = await invokeSystemMethod('storage.receiving.create', { delivery_method: 'truck', license_plate: `ITEST-BIND-${Date.now().toString().slice(-6)}`, loading_method: 'with_pallet', remark: 'bind-store-regression', 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, '创建收货单失败,无法执行货主绑定回归'); const bindRes = await invokeSystemMethod('storage.receiving.bindStore', { id: createdId, store_id: bindableStore.store_id, }, switched.token, EXTRA_HEADERS); assert.equal(bindRes.status, 200); assert.equal(bindRes.body.flag, 'success'); assert.equal(bindRes.body.code, 200); assert.equal(Number(bindRes.body?.data?.store_id || 0), Number(bindableStore.store_id)); const repeatRes = await invokeSystemMethod('storage.receiving.bindStore', { id: createdId, store_id: bindableStore.store_id, }, switched.token, EXTRA_HEADERS); assert.equal(repeatRes.status, 200); assert.equal(repeatRes.body.flag, 'success'); assert.equal(Number(repeatRes.body.code), 400); assert.match(String(repeatRes.body.message || ''), /已绑定|重复绑定|已有货主/); } finally { if (createdId > 0) { await invokeSystemMethod('storage.receiving.batchCancelReceiving', { ids: [ createdId ], }, switched.token, EXTRA_HEADERS); } } }); it('should reject bindStore when receiving is cancelled', async function() { const { token } = await getAuthToken(); const switched = await switchToWarehouseRole(token); const bindableStore = await getBindableStoreInfo(switched.token); let createdId = 0; if (!bindableStore || bindableStore.store_id <= 0) { this.skip(); } try { const createRes = await invokeSystemMethod('storage.receiving.create', { delivery_method: 'truck', license_plate: `ICB-${Date.now().toString().slice(-6)}`, loading_method: 'with_pallet', remark: 'bind-store-cancelled-regression', 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, '创建收货单失败,无法执行取消状态绑定回归'); 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.ok([ 0, 200 ].includes(Number(cancelRes.body.code)), `取消收货单返回异常: ${JSON.stringify(cancelRes.body)}`); const bindRes = await invokeSystemMethod('storage.receiving.bindStore', { id: createdId, store_id: bindableStore.store_id, }, switched.token, EXTRA_HEADERS); assert.equal(bindRes.status, 200); assert.equal(bindRes.body.flag, 'success'); assert.equal(Number(bindRes.body.code), 400); assert.match(String(bindRes.body.message || ''), /取消|作废|不允许绑定/); } finally { if (createdId > 0) { await invokeSystemMethod('storage.receiving.batchCancelReceiving', { ids: [ createdId ], }, switched.token, EXTRA_HEADERS); } } }); it('should bind and rebind system product for a receiving detail', async function() { const { token } = await getAuthToken(); const switched = await switchToWarehouseRole(token); const systemProducts = await getBindableSystemProducts(switched.token); let createdId = 0; if (!Array.isArray(systemProducts) || systemProducts.length === 0) { this.skip(); } try { const createRes = await invokeSystemMethod('storage.receiving.create', { delivery_method: 'truck', license_plate: `IDB-${Date.now().toString().slice(-6)}`, loading_method: 'with_pallet', remark: 'bind-detail-product-regression', 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, '创建收货单失败,无法执行明细绑定商品回归'); const addDetailRes = await invokeSystemMethod('storage.receiving.addDetail', { receiving_id: createdId, }, switched.token, EXTRA_HEADERS); assert.equal(addDetailRes.status, 200); assert.equal(addDetailRes.body.flag, 'success'); assert.equal(Number(addDetailRes.body.code), 200); const detailId = Number(addDetailRes.body?.data?.data?.selectId || addDetailRes.body?.data?.selectId || 0); assert.ok(detailId > 0, '新增收货单明细后未返回有效 detail_id'); const firstProduct = systemProducts[0]; const bindRes = await invokeSystemMethod('storage.receiving.bindDetailProduct', { detail_id: detailId, system_product_id: firstProduct.id, }, switched.token, EXTRA_HEADERS); assert.equal(bindRes.status, 200); assert.equal(bindRes.body.flag, 'success'); assert.equal(Number(bindRes.body.code), 200); assert.equal(Number(bindRes.body?.data?.detail_id || 0), detailId); assert.equal(Number(bindRes.body?.data?.system_product_id || 0), Number(firstProduct.id)); assert.ok(String(bindRes.body?.data?.product_name || '').length > 0, '绑定后应回填商品名称'); const secondProduct = systemProducts.find(item => Number(item.id) !== Number(firstProduct.id)) || firstProduct; const rebindRes = await invokeSystemMethod('storage.receiving.bindDetailProduct', { detail_id: detailId, system_product_id: secondProduct.id, }, switched.token, EXTRA_HEADERS); assert.equal(rebindRes.status, 200); assert.equal(rebindRes.body.flag, 'success'); assert.equal(Number(rebindRes.body.code), 200); assert.equal(Number(rebindRes.body?.data?.detail_id || 0), detailId); assert.equal(Number(rebindRes.body?.data?.system_product_id || 0), Number(secondProduct.id)); assert.ok(String(rebindRes.body?.data?.product_name || '').length > 0, '改绑后应返回商品名称'); } finally { if (createdId > 0) { await invokeSystemMethod('storage.receiving.batchCancelReceiving', { ids: [ createdId ], }, switched.token, EXTRA_HEADERS); } } }); 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 generate unique receiving numbers under concurrent create requests', async () => { const { token } = await getAuthToken(); const switched = await switchToWarehouseRole(token); const suffix = Date.now().toString().slice(-6); const createdIds = []; try { const requests = [ 1, 2 ].map(index => invokeSystemMethod('storage.receiving.create', { delivery_method: 'truck', license_plate: `ITEST-C${index}-${suffix}`, loading_method: 'with_pallet', remark: `integration-concurrent-${index}`, total_boxes: 0, total_pallets: 0, }, switched.token, EXTRA_HEADERS)); const responses = await Promise.all(requests); responses.forEach((res, index) => { assert.equal(res.status, 200, `第 ${index + 1} 个并发请求 HTTP 状态异常`); assert.equal(res.body.flag, 'success', `第 ${index + 1} 个并发请求应成功: ${JSON.stringify(res.body)}`); assert.equal(res.body.code, 200, `第 ${index + 1} 个并发请求 code 应为 200`); }); const ids = responses.map(res => Number(res.body.id || res.body?.data?.id || 0)); const receivingNumbers = responses.map(res => String(res.body.receiving_number || res.body?.data?.receiving_number || '')); ids.forEach(id => { assert.ok(id > 0, '并发创建成功但未返回有效 id'); createdIds.push(id); }); receivingNumbers.forEach(number => { assert.ok(number.startsWith('R'), `收货单号格式异常: ${number}`); }); assert.equal(new Set(receivingNumbers).size, receivingNumbers.length, '并发创建时 receiving_number 应保持唯一'); } finally { if (createdIds.length > 0) { await invokeSystemMethod('storage.receiving.batchCancelReceiving', { ids: createdIds, }, switched.token, EXTRA_HEADERS); } } }); it('should include same-day records when created_at uses daterange filter', async () => { const { token } = await getAuthToken(); const switched = await switchToWarehouseRole(token); let createdId = 0; try { const uniquePlate = `ITEST-DATE-${Date.now().toString().slice(-6)}`; const createRes = await invokeSystemMethod('storage.receiving.create', { delivery_method: 'truck', license_plate: uniquePlate, loading_method: 'with_pallet', remark: 'date-range-regression', 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, '创建收货单失败,无法执行日期范围回归'); const now = new Date(); const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime(); const listRes = await invokeSystemMethod('storage.receiving.getReceivingList', { pagination: { page: 1, pageSize: 20, }, where: { created_at: { operator: 'between', value: [ startOfDay, startOfDay ], type: 'daterange', }, }, }, 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 ids = Array.isArray(payload.list) ? payload.list.map(item => Number(item && item.id)) : []; assert.ok(ids.includes(createdId), '同日创建的收货单应命中 created_at daterange 查询'); } 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 || ''), /不存在|无权访问|状态不允许/); }); });