WMS_MAIN/test/app/controller/system.storage.inventory.list.test.js
liufumingren 61c530c0cc 1
2026-05-14 04:00:07 +08:00

142 lines
5.3 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('storage.inventory.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('未找到可访问库存列表的仓库角色');
}
describe('test/app/controller/system.storage.inventory.list.test.js', () => {
let warehouseToken;
before(async function() {
const { token } = await getAuthToken();
try {
const switched = await switchToWarehouseRole(token);
warehouseToken = switched.token;
} catch (error) {
this.skip();
}
});
it('should return merged inventory list by default', async () => {
const res = await invokeSystemMethod('storage.inventory.list', {
pagination: { page: 1, pageSize: 200 },
where: {},
}, 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.list), 'list 应为数组');
assert.ok(data.pagination, '缺少 pagination');
const hasMergedRow = data.list.some(row => row && row.batch_count !== undefined);
assert.ok(hasMergedRow, '默认模式应返回聚合后的库存记录');
const firstRow = data.list[0];
assert.ok(firstRow?.stock, '默认模式应包含 stock 子对象');
assert.ok(Array.isArray(firstRow.stock.barcodes), 'stock.barcodes 应为数组');
assert.ok(Array.isArray(firstRow.stock.images), 'stock.images 应为数组');
});
it('should return batch-level inventory records when group_by_batch=true', async () => {
const mergedRes = await invokeSystemMethod('storage.inventory.list', {
pagination: { page: 1, pageSize: 200 },
where: {},
}, 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.list;
const detailRes = await invokeSystemMethod('storage.inventory.list', {
pagination: { page: 1, pageSize: 200 },
group_by_batch: true,
where: {},
}, warehouseToken, EXTRA_HEADERS);
assert.equal(detailRes.status, 200);
assert.equal(detailRes.body.flag, 'success');
assert.equal(detailRes.body.code, 200);
const detailList = detailRes.body.data.list;
assert.ok(Array.isArray(detailList), 'group_by_batch=true 时 list 应为数组');
assert.ok(detailList.length >= mergedList.length, 'group_by_batch=true 时记录数不应少于合并模式');
for (const row of detailList) {
assert.ok('id' in row, '明细模式应包含 id');
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('expiry_remaining_days' in row, '明细模式应包含 expiry_remaining_days');
assert.ok('inventory_warning_level' in row, '明细模式应包含 inventory_warning_level');
assert.ok('inventory_warning_color' in row, '明细模式应包含 inventory_warning_color');
assert.ok('inventory_warning_range' in row, '明细模式应包含 inventory_warning_range');
assert.ok('inventory_warning_tip_text' in row, '明细模式应包含 inventory_warning_tip_text');
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(Array.isArray(row.stock?.barcodes), 'stock.barcodes 应为数组');
assert.ok(Array.isArray(row.stock?.images), 'stock.images 应为数组');
}
});
});