317 lines
13 KiB
JavaScript
317 lines
13 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',
|
||
};
|
||
|
||
const FALLBACK_ROLE_IDS = [ 4, 6, 30 ];
|
||
|
||
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('stock.warehouse.list', {
|
||
pagination: { page: 1, pageSize: 1 },
|
||
where: {},
|
||
}, currentToken, EXTRA_HEADERS);
|
||
|
||
if (listRes.status === 200 && listRes.body?.flag === 'success' && listRes.body?.code === 200) {
|
||
return {
|
||
token: currentToken,
|
||
roleId,
|
||
};
|
||
}
|
||
}
|
||
|
||
throw new Error('未找到可访问仓库商品列表的仓库角色');
|
||
}
|
||
|
||
/**
|
||
* 从仓库商品列表中获取一个有效的 stock_id
|
||
* @param {string} token 传入的token必须具有访问仓库商品列表的权限
|
||
*/
|
||
async function getValidStockId(token) {
|
||
const listRes = await invokeSystemMethod('stock.warehouse.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 list = listRes.body?.data?.list;
|
||
assert.ok(Array.isArray(list) && list.length > 0, '仓库中没有商品数据,无法执行测试');
|
||
|
||
return list[0].id;
|
||
}
|
||
|
||
describe('test/app/controller/system.stock.warehouse.getStockDetail.test.js', () => {
|
||
let warehouseToken;
|
||
let validStockId;
|
||
|
||
before(async () => {
|
||
const { token } = await getAuthToken();
|
||
const switched = await switchToWarehouseRole(token);
|
||
warehouseToken = switched.token;
|
||
validStockId = await getValidStockId(warehouseToken);
|
||
});
|
||
|
||
it('should return stockItem, storageList and pagination for a valid stock_id', async () => {
|
||
const res = await invokeSystemMethod('stock.warehouse.getStockDetail', {
|
||
stock_id: validStockId,
|
||
}, warehouseToken, EXTRA_HEADERS);
|
||
|
||
assert.equal(res.status, 200);
|
||
assert.equal(res.body.flag, 'success');
|
||
assert.equal(res.body.code, 200);
|
||
|
||
const data = res.body.data;
|
||
assert.ok(data, '响应缺少 data');
|
||
|
||
// stockItem 结构校验
|
||
assert.ok(data.stockItem, '缺少 stockItem');
|
||
assert.ok('id' in data.stockItem, 'stockItem 缺少 id');
|
||
assert.ok('item_code' in data.stockItem, 'stockItem 缺少 item_code');
|
||
assert.ok('item_name' in data.stockItem, 'stockItem 缺少 item_name');
|
||
assert.ok('quantity' in data.stockItem, 'stockItem 缺少 quantity');
|
||
assert.ok('locked_quantity' in data.stockItem, 'stockItem 缺少 locked_quantity');
|
||
|
||
// storageList 结构校验
|
||
assert.ok(Array.isArray(data.storageList), 'storageList 应为数组');
|
||
|
||
// pagination 结构校验
|
||
assert.ok(data.pagination, '缺少 pagination');
|
||
assert.ok('page' in data.pagination, 'pagination 缺少 page');
|
||
assert.ok('pageSize' in data.pagination, 'pagination 缺少 pageSize');
|
||
assert.ok('total' in data.pagination, 'pagination 缺少 total');
|
||
});
|
||
|
||
it('should respect pagination parameters', async () => {
|
||
const res = await invokeSystemMethod('stock.warehouse.getStockDetail', {
|
||
stock_id: validStockId,
|
||
pagination: { page: 1, pageSize: 2 },
|
||
}, warehouseToken, EXTRA_HEADERS);
|
||
|
||
assert.equal(res.status, 200);
|
||
assert.equal(res.body.flag, 'success');
|
||
assert.equal(res.body.code, 200);
|
||
|
||
const data = res.body.data;
|
||
assert.ok(data, '响应缺少 data');
|
||
assert.ok(Array.isArray(data.storageList), 'storageList 应为数组');
|
||
assert.ok(data.storageList.length <= 2, 'storageList 数量应不超过 pageSize');
|
||
assert.equal(data.pagination.page, 1);
|
||
assert.equal(data.pagination.pageSize, 2);
|
||
});
|
||
|
||
it('should return error when stock_id is empty', async () => {
|
||
const res = await invokeSystemMethod('stock.warehouse.getStockDetail', {}, warehouseToken, EXTRA_HEADERS);
|
||
|
||
assert.equal(res.status, 200);
|
||
assert.equal(res.body.flag, 'success');
|
||
assert.equal(res.body.code, 0, '缺少 stock_id 应返回 code=0');
|
||
assert.ok(res.body.message, '缺少 stock_id 应返回错误消息');
|
||
});
|
||
|
||
it('should return error when stock_id does not exist', async () => {
|
||
const res = await invokeSystemMethod('stock.warehouse.getStockDetail', {
|
||
stock_id: 999999999,
|
||
}, warehouseToken, EXTRA_HEADERS);
|
||
|
||
assert.equal(res.status, 200);
|
||
assert.equal(res.body.flag, 'success');
|
||
assert.equal(res.body.code, 0, '不存在的 stock_id 应返回 code=0');
|
||
assert.ok(res.body.message, '不存在的 stock_id 应返回错误消息');
|
||
});
|
||
|
||
it('should return empty storageList when stock exists but has no storage records', async () => {
|
||
// 使用有效 stock_id 查询,如果该商品恰好有库存,则仅验证结构
|
||
// 若无库存记录,storageList 应为空数组
|
||
const res = await invokeSystemMethod('stock.warehouse.getStockDetail', {
|
||
stock_id: validStockId,
|
||
pagination: { page: 9999, pageSize: 20 },
|
||
}, warehouseToken, EXTRA_HEADERS);
|
||
|
||
assert.equal(res.status, 200);
|
||
assert.equal(res.body.flag, 'success');
|
||
assert.equal(res.body.code, 200);
|
||
|
||
const data = res.body.data;
|
||
assert.ok(data, '响应缺少 data');
|
||
assert.ok(data.stockItem, '缺少 stockItem');
|
||
assert.ok(Array.isArray(data.storageList), 'storageList 应为数组');
|
||
assert.equal(data.storageList.length, 0, '超出范围的分页应返回空 storageList');
|
||
});
|
||
|
||
// ---- group_by_batch 相关用例 ----
|
||
|
||
const BATCH_FIELDS = [ 'batch_no', 'production_date', 'expiry_date', 'inbound_time', 'inbound_order_no', 'supplier_batch_no' ];
|
||
|
||
it('should merge storageList by space_id when group_by_batch is not provided (default)', async () => {
|
||
// 先用 group_by_batch=true 获取原始明细
|
||
const rawRes = await invokeSystemMethod('stock.warehouse.getStockDetail', {
|
||
stock_id: validStockId,
|
||
group_by_batch: true,
|
||
pagination: { page: 1, pageSize: 200 },
|
||
}, warehouseToken, EXTRA_HEADERS);
|
||
|
||
assert.equal(rawRes.status, 200);
|
||
assert.equal(rawRes.body.code, 200);
|
||
const rawList = rawRes.body.data.storageList;
|
||
|
||
// 默认不传 group_by_batch
|
||
const mergedRes = await invokeSystemMethod('stock.warehouse.getStockDetail', {
|
||
stock_id: validStockId,
|
||
pagination: { page: 1, pageSize: 200 },
|
||
}, warehouseToken, EXTRA_HEADERS);
|
||
|
||
assert.equal(mergedRes.status, 200);
|
||
assert.equal(mergedRes.body.flag, 'success');
|
||
assert.equal(mergedRes.body.code, 200);
|
||
|
||
const mergedList = mergedRes.body.data.storageList;
|
||
assert.ok(Array.isArray(mergedList), 'storageList 应为数组');
|
||
|
||
// 按 space_id 分组计算期望的合并数
|
||
const spaceIds = new Set(rawList.map(r => r.space_id));
|
||
assert.equal(mergedList.length, spaceIds.size, '合并后记录数应等于不同 space_id 数量');
|
||
|
||
// 验证批次字段全部置 null
|
||
for (const row of mergedList) {
|
||
for (const field of BATCH_FIELDS) {
|
||
assert.equal(row[field], null, `合并模式下 ${field} 应为 null`);
|
||
}
|
||
}
|
||
|
||
// 验证 quantity 累加正确:raw 按 space_id 分组求和 vs merged
|
||
for (const row of mergedList) {
|
||
const rawForSpace = rawList.filter(r => r.space_id === row.space_id);
|
||
const expectedQty = rawForSpace.reduce((sum, r) => sum + Number(r.quantity), 0);
|
||
const expectedLocked = rawForSpace.reduce((sum, r) => sum + Number(r.locked_quantity), 0);
|
||
assert.equal(Number(row.quantity), expectedQty, `space_id=${row.space_id} quantity 累加不正确`);
|
||
assert.equal(Number(row.locked_quantity), expectedLocked, `space_id=${row.space_id} locked_quantity 累加不正确`);
|
||
assert.equal(Number(row.available_quantity), expectedQty - expectedLocked, `space_id=${row.space_id} available_quantity 计算不正确`);
|
||
}
|
||
});
|
||
|
||
it('should merge storageList by space_id when group_by_batch=false (explicit)', async () => {
|
||
// 显式传 false
|
||
const explicitRes = await invokeSystemMethod('stock.warehouse.getStockDetail', {
|
||
stock_id: validStockId,
|
||
group_by_batch: false,
|
||
pagination: { page: 1, pageSize: 200 },
|
||
}, warehouseToken, EXTRA_HEADERS);
|
||
|
||
assert.equal(explicitRes.status, 200);
|
||
assert.equal(explicitRes.body.flag, 'success');
|
||
assert.equal(explicitRes.body.code, 200);
|
||
|
||
const explicitList = explicitRes.body.data.storageList;
|
||
|
||
// 默认不传 group_by_batch
|
||
const defaultRes = await invokeSystemMethod('stock.warehouse.getStockDetail', {
|
||
stock_id: validStockId,
|
||
pagination: { page: 1, pageSize: 200 },
|
||
}, warehouseToken, EXTRA_HEADERS);
|
||
|
||
assert.equal(defaultRes.status, 200);
|
||
assert.equal(defaultRes.body.code, 200);
|
||
|
||
const defaultList = defaultRes.body.data.storageList;
|
||
|
||
// 显式 false 与默认行为一致
|
||
assert.equal(explicitList.length, defaultList.length, '显式 false 与默认不传应返回相同数量');
|
||
|
||
// 验证批次字段全部置 null
|
||
for (const row of explicitList) {
|
||
for (const field of BATCH_FIELDS) {
|
||
assert.equal(row[field], null, `显式 false 模式下 ${field} 应为 null`);
|
||
}
|
||
}
|
||
|
||
// quantity 应一致
|
||
for (let i = 0; i < explicitList.length; i++) {
|
||
assert.equal(Number(explicitList[i].quantity), Number(defaultList[i].quantity), `第 ${i} 条 quantity 不一致`);
|
||
assert.equal(Number(explicitList[i].locked_quantity), Number(defaultList[i].locked_quantity), `第 ${i} 条 locked_quantity 不一致`);
|
||
}
|
||
});
|
||
|
||
it('should return independent records per batch when group_by_batch=true', async () => {
|
||
const res = await invokeSystemMethod('stock.warehouse.getStockDetail', {
|
||
stock_id: validStockId,
|
||
group_by_batch: true,
|
||
pagination: { page: 1, pageSize: 200 },
|
||
}, warehouseToken, EXTRA_HEADERS);
|
||
|
||
assert.equal(res.status, 200);
|
||
assert.equal(res.body.flag, 'success');
|
||
assert.equal(res.body.code, 200);
|
||
|
||
const data = res.body.data;
|
||
assert.ok(Array.isArray(data.storageList), 'storageList 应为数组');
|
||
|
||
// 验证结构完整:每条记录包含批次字段
|
||
for (const row of data.storageList) {
|
||
assert.ok('batch_no' in row, '区分批次模式下应包含 batch_no');
|
||
assert.ok('production_date' in row, '区分批次模式下应包含 production_date');
|
||
assert.ok('expiry_date' in row, '区分批次模式下应包含 expiry_date');
|
||
assert.ok('inbound_time' in row, '区分批次模式下应包含 inbound_time');
|
||
assert.ok('inbound_order_no' in row, '区分批次模式下应包含 inbound_order_no');
|
||
assert.ok('supplier_batch_no' in row, '区分批次模式下应包含 supplier_batch_no');
|
||
assert.ok('quantity' in row, '区分批次模式下应包含 quantity');
|
||
assert.ok('locked_quantity' in row, '区分批次模式下应包含 locked_quantity');
|
||
assert.ok('available_quantity' in row, '区分批次模式下应包含 available_quantity');
|
||
}
|
||
|
||
// 与合并模式对比:group_by_batch=true 的记录数应 >= 合并后记录数
|
||
const mergedRes = await invokeSystemMethod('stock.warehouse.getStockDetail', {
|
||
stock_id: validStockId,
|
||
pagination: { page: 1, pageSize: 200 },
|
||
}, warehouseToken, EXTRA_HEADERS);
|
||
|
||
assert.equal(mergedRes.status, 200);
|
||
assert.equal(mergedRes.body.code, 200);
|
||
|
||
const mergedCount = mergedRes.body.data.storageList.length;
|
||
assert.ok(data.storageList.length >= mergedCount, 'group_by_batch=true 记录数应 >= 合并后记录数');
|
||
|
||
// pagination 结构校验
|
||
assert.ok(data.pagination, '缺少 pagination');
|
||
assert.ok('page' in data.pagination, 'pagination 缺少 page');
|
||
assert.ok('pageSize' in data.pagination, 'pagination 缺少 pageSize');
|
||
assert.ok('total' in data.pagination, 'pagination 缺少 total');
|
||
});
|
||
});
|