751 lines
28 KiB
JavaScript
751 lines
28 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const {
|
|
getAuthToken,
|
|
invokeSystemMethod,
|
|
} = require('../../support/system_method_client');
|
|
|
|
const EXTRA_HEADERS = {
|
|
platform: 'web',
|
|
project: 'all',
|
|
};
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 切换到固定测试仓角色,并验证该角色可以访问收货单列表。
|
|
*
|
|
* @param {string} token 当前登录令牌
|
|
* @return {Promise<{token: string, roleId: number}>} 切换后的令牌与角色 ID
|
|
*/
|
|
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)
|
|
&& Number(item.store_id) === 100000);
|
|
|
|
const candidateRoleIds = [ ...new Set(warehouseRoles.map(item => Number(item.id)).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?.store_id || item?.counterparty_id || 0) === 1000000
|
|
&& Number(item?.warehouse_id || 0) === 100000);
|
|
|
|
if (!candidate) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
store_id: 1000000,
|
|
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 ].sort((left, right) => Number(left?.id || 0) - Number(right?.id || 0))[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;
|
|
|
|
assert.ok(bindableStore, '未找到固定测试仓/测试货主对应关系');
|
|
|
|
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;
|
|
|
|
assert.ok(bindableStore, '未找到固定测试仓/测试货主对应关系');
|
|
|
|
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;
|
|
|
|
assert.ok(Array.isArray(systemProducts) && systemProducts.length > 0, '未找到可绑定的系统商品');
|
|
|
|
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 detailChangeRes = await invokeSystemMethod('storage.receiving.detailChange', {
|
|
id: detailId,
|
|
receiving_id: createdId,
|
|
product_name: '审核前绑定测试商品',
|
|
barcode: `IDR-BC-${Date.now().toString().slice(-6)}`,
|
|
actual_quantity: 1,
|
|
box_quantity: 1,
|
|
pallet_quantity: 0,
|
|
}, switched.token, EXTRA_HEADERS);
|
|
assert.equal(detailChangeRes.status, 200);
|
|
assert.equal(detailChangeRes.body.flag, 'success');
|
|
|
|
const sortedProducts = [ ...systemProducts ].sort((left, right) => Number(left.id) - Number(right.id));
|
|
const firstProduct = sortedProducts[0];
|
|
const secondProduct = sortedProducts[1];
|
|
|
|
assert.ok(firstProduct, '未找到第一个可绑定商品');
|
|
assert.ok(secondProduct, '未找到第二个可绑定商品');
|
|
|
|
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 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');
|
|
} 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 || ''), /不存在|无权访问|状态不允许/);
|
|
});
|
|
|
|
// ==================== storage.receiving.get ====================
|
|
|
|
it('should get receiving detail with details and images via storage.receiving.get', async function() {
|
|
const { token } = await getAuthToken();
|
|
const switched = await switchToWarehouseRole(token);
|
|
let createdId = 0;
|
|
|
|
try {
|
|
// 1. 创建收货单
|
|
const createRes = await invokeSystemMethod('storage.receiving.create', {
|
|
delivery_method: 'truck',
|
|
license_plate: `IGET-${Date.now().toString().slice(-6)}`,
|
|
loading_method: 'with_pallet',
|
|
remark: 'get-detail-regression',
|
|
total_boxes: 1,
|
|
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, '创建收货单失败');
|
|
|
|
// 2. 添加明细
|
|
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);
|
|
|
|
// 3. 调用 get 接口
|
|
const getRes = await invokeSystemMethod('storage.receiving.get', {
|
|
id: createdId,
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(getRes.status, 200);
|
|
assert.equal(getRes.body.flag, 'success');
|
|
assert.equal(Number(getRes.body.code), 200);
|
|
|
|
const data = getRes.body.data || {};
|
|
assert.ok(data.id || data.receiving_number, 'get 返回应包含收货单主表信息');
|
|
assert.equal(Number(data.id), createdId, '返回的收货单ID应与请求一致');
|
|
assert.ok(Array.isArray(data.details), 'get 返回应包含 details 数组');
|
|
assert.ok(data.details.length > 0, '应至少包含刚添加的明细');
|
|
assert.ok(Array.isArray(data.images), 'get 返回应包含 images 数组');
|
|
|
|
// 4. 验证明细结构
|
|
const detail = data.details[0];
|
|
assert.ok(detail.id, '明细应包含 id');
|
|
assert.ok(Array.isArray(detail.images), '明细应包含 images 数组');
|
|
} finally {
|
|
if (createdId > 0) {
|
|
await invokeSystemMethod('storage.receiving.batchCancelReceiving', {
|
|
ids: [ createdId ],
|
|
}, switched.token, EXTRA_HEADERS);
|
|
}
|
|
}
|
|
});
|
|
|
|
it('should return code 0 when getting non-existent receiving via storage.receiving.get', async () => {
|
|
const { token } = await getAuthToken();
|
|
const switched = await switchToWarehouseRole(token);
|
|
|
|
const res = await invokeSystemMethod('storage.receiving.get', {
|
|
id: 999999999,
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.body.flag, 'success');
|
|
assert.equal(Number(res.body.code), 0);
|
|
assert.match(String(res.body.message || ''), /无记录|无权访问|不存在/);
|
|
});
|
|
|
|
it('should return details sorted by id DESC via storage.receiving.get', async function() {
|
|
const { token } = await getAuthToken();
|
|
const switched = await switchToWarehouseRole(token);
|
|
let createdId = 0;
|
|
|
|
try {
|
|
const createRes = await invokeSystemMethod('storage.receiving.create', {
|
|
delivery_method: 'express',
|
|
tracking_number: `IGET-SORT-${Date.now().toString().slice(-6)}`,
|
|
loading_method: 'with_pallet',
|
|
remark: 'get-sort-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);
|
|
|
|
// 添加两条明细
|
|
await invokeSystemMethod('storage.receiving.addDetail', {
|
|
receiving_id: createdId,
|
|
}, switched.token, EXTRA_HEADERS);
|
|
await invokeSystemMethod('storage.receiving.addDetail', {
|
|
receiving_id: createdId,
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
const getRes = await invokeSystemMethod('storage.receiving.get', {
|
|
id: createdId,
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(getRes.status, 200);
|
|
assert.equal(getRes.body.flag, 'success');
|
|
assert.equal(Number(getRes.body.code), 200);
|
|
|
|
const details = getRes.body.data?.details || [];
|
|
assert.ok(details.length >= 2, '应至少有 2 条明细');
|
|
|
|
// 验证按 id DESC 排序
|
|
for (let i = 1; i < details.length; i++) {
|
|
assert.ok(Number(details[i - 1].id) >= Number(details[i].id), '明细应按 id DESC 排序');
|
|
}
|
|
} finally {
|
|
if (createdId > 0) {
|
|
await invokeSystemMethod('storage.receiving.batchCancelReceiving', {
|
|
ids: [ createdId ],
|
|
}, switched.token, EXTRA_HEADERS);
|
|
}
|
|
}
|
|
});
|
|
|
|
it('should include location space info in details via storage.receiving.get', async function() {
|
|
const { token } = await getAuthToken();
|
|
const switched = await switchToWarehouseRole(token);
|
|
|
|
// 使用已有数据,查列表取第一条
|
|
const listRes = await invokeSystemMethod('storage.receiving.getReceivingList', {
|
|
pagination: { page: 1, pageSize: 5 },
|
|
where: {},
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(listRes.status, 200);
|
|
|
|
const payload = normalizeListPayload(listRes.body.data);
|
|
const first = Array.isArray(payload?.list) && payload.list.length > 0
|
|
? [ ...payload.list ].sort((left, right) => Number(left?.id || 0) - Number(right?.id || 0))[0]
|
|
: null;
|
|
|
|
assert.ok(first && first.id, '未找到可用于查询详情的收货单');
|
|
|
|
const getRes = await invokeSystemMethod('storage.receiving.get', {
|
|
id: Number(first.id),
|
|
}, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(getRes.status, 200);
|
|
assert.equal(getRes.body.flag, 'success');
|
|
assert.equal(Number(getRes.body.code), 200);
|
|
|
|
const data = getRes.body.data || {};
|
|
assert.ok(Array.isArray(data.details), 'details 应为数组');
|
|
|
|
// 如果有明细且有库位关联,验证 receiving_space 结构
|
|
const detailWithSpace = data.details.find(d => d.receiving_space && d.receiving_space.space_code);
|
|
if (detailWithSpace) {
|
|
assert.equal(typeof detailWithSpace.receiving_space.space_code, 'string', 'space_code 应为字符串');
|
|
}
|
|
|
|
// 如果有明细图片,验证结构
|
|
const detailWithImg = data.details.find(d => Array.isArray(d.images) && d.images.length > 0);
|
|
if (detailWithImg) {
|
|
const img = detailWithImg.images[0];
|
|
assert.equal(typeof img.url, 'string', '图片应有 url');
|
|
assert.ok(img.url.length > 0, 'url 不能为空');
|
|
assert.ok(img.id !== undefined, '图片应有 id');
|
|
}
|
|
});
|
|
});
|