424 lines
20 KiB
JavaScript
424 lines
20 KiB
JavaScript
'use strict';
|
||
/**
|
||
* batchQuickRecheck 接口完整真实流程测试
|
||
* 测试时间: 2026-06-13
|
||
* 测试仓库: 100000
|
||
* 测试货主: 1000000
|
||
* 目标: 验证 batchQuickRecheck 接口的 7 个测试用例
|
||
*/
|
||
|
||
const mysql = require('mysql2/promise');
|
||
const DB = { host: '127.0.0.1', port: 3307, user: 'wms', password: 'dspxmRwRdRbWtbm7', database: 'wms' };
|
||
const BASE_URL = 'http://127.0.0.1:6060';
|
||
const TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJsaXVmdW1pbmdyZW4iLCJyZWFsX25hbWUiOiLliJjku5jmmI7ku4EiLCJwbGF0Zm9ybV9jb2RlIjoid2ViIiwiY3VycmVudF9yb2xlX2lkIjo0LCJjdXJyZW50X3N0b3JlX2lkIjoxMDAwMDAsImlhdCI6MTc4MTM2MDk3MiwiZXhwIjoxNzgxMzc1MzcyfQ.u0svJXHuk8RdlM8ObhDMlRj07gWQzS8Y_41dPcBDtc0';
|
||
const TABLE = 'wms_order_delivery_100000';
|
||
|
||
// ========== 工具函数 ==========
|
||
|
||
let conn = null;
|
||
|
||
async function getConn() {
|
||
if (!conn) conn = await mysql.createConnection(DB);
|
||
return conn;
|
||
}
|
||
|
||
async function closeConn() {
|
||
if (conn) { await conn.end(); conn = null; }
|
||
}
|
||
|
||
async function getOrderSnapshot(ids) {
|
||
const c = await getConn();
|
||
if (!ids.length) return {};
|
||
const [ rows ] = await c.query(
|
||
`SELECT id, delivery_order_code, pre_delivery_order_code,
|
||
pick_status, recheck_status, storage_status, post_status, status, intercept_status
|
||
FROM ${TABLE} WHERE id IN (?)`, [ ids ]
|
||
);
|
||
const map = {};
|
||
rows.forEach(r => { map[r.id] = r; });
|
||
return map;
|
||
}
|
||
|
||
async function getStorageSnapshot(ids) {
|
||
const c = await getConn();
|
||
if (!ids.length) return {};
|
||
const [ rows ] = await c.query(
|
||
'SELECT id, quantity, locked_quantity FROM wms_storage WHERE id IN (?) AND warehouse_id = 100000', [ ids ]
|
||
);
|
||
const map = {};
|
||
rows.forEach(r => { map[r.id] = r; });
|
||
return map;
|
||
}
|
||
|
||
async function callApi(ids) {
|
||
const ts = Math.floor(Date.now() / 1000);
|
||
const url = `${BASE_URL}/system?method=order.recheckOrder.batchQuickRecheck&v=1.0×tamp=${ts}`;
|
||
const headers = {
|
||
platform: 'web',
|
||
project: 'wms',
|
||
'Content-Type': 'application/json; charset=utf-8',
|
||
Authorization: `Bearer ${TOKEN}`,
|
||
};
|
||
const body = JSON.stringify({ ids });
|
||
|
||
const response = await fetch(url, { method: 'POST', headers, body });
|
||
const text = await response.text();
|
||
let json;
|
||
try { json = JSON.parse(text); } catch { json = { raw: text }; }
|
||
return { status: response.status, body: json };
|
||
}
|
||
|
||
// ========== 测试用例 ==========
|
||
|
||
async function runTestCase(name, ids, expectedDesc, preSetup, postVerify, postCleanup) {
|
||
console.log(`\n${'='.repeat(60)}`);
|
||
console.log(`【${name}】`);
|
||
console.log(` 输入IDs: [${ids.join(', ')}]`);
|
||
console.log(` 预期: ${expectedDesc}`);
|
||
|
||
let beforeOrder = {},
|
||
beforeStorage = {},
|
||
afterOrder = {},
|
||
afterStorage = {};
|
||
let passed = false;
|
||
let failReason = '';
|
||
|
||
try {
|
||
// 前置准备
|
||
if (preSetup) {
|
||
console.log(' [前置准备] 执行中...');
|
||
await preSetup();
|
||
console.log(' [前置准备] 完成');
|
||
}
|
||
|
||
// 测试前快照
|
||
beforeOrder = await getOrderSnapshot(ids);
|
||
console.log(' 测试前订单状态:');
|
||
for (const [ id, snap ] of Object.entries(beforeOrder)) {
|
||
console.log(` ID ${id}: storage="${snap.storage_status}" post="${snap.post_status}" status="${snap.status}" intercept="${snap.intercept_status}" pick="${snap.pick_status}" recheck="${snap.recheck_status}" code="${snap.delivery_order_code || snap.pre_delivery_order_code || ''}"`);
|
||
}
|
||
|
||
// 调用 API
|
||
const result = await callApi(ids);
|
||
console.log(` HTTP Status: ${result.status}`);
|
||
const data = result.body && (result.body.data !== undefined ? result.body.data : result.body);
|
||
console.log(` 返回结果: ${JSON.stringify(data, null, 2)}`);
|
||
|
||
// 测试后快照
|
||
afterOrder = await getOrderSnapshot(ids);
|
||
console.log(' 测试后订单状态:');
|
||
for (const [ id, snap ] of Object.entries(afterOrder)) {
|
||
const before = beforeOrder[id] || {};
|
||
const changes = [];
|
||
if (snap.pick_status !== before.pick_status) changes.push(`pick: ${before.pick_status}→${snap.pick_status}`);
|
||
if (snap.recheck_status !== before.recheck_status) changes.push(`recheck: ${before.recheck_status}→${snap.recheck_status}`);
|
||
if (snap.storage_status !== before.storage_status) changes.push(`storage: ${before.storage_status}→${snap.storage_status}`);
|
||
const changeStr = changes.length ? ` [变更: ${changes.join(', ')}]` : ' [无变更]';
|
||
console.log(` ID ${id}: pick="${snap.pick_status}" recheck="${snap.recheck_status}" storage="${snap.storage_status}"${changeStr}`);
|
||
}
|
||
|
||
// 自定义验证
|
||
if (postVerify) {
|
||
const verifyResult = await postVerify(result, beforeOrder, afterOrder);
|
||
if (verifyResult === true) {
|
||
passed = true;
|
||
} else {
|
||
passed = false;
|
||
failReason = typeof verifyResult === 'string' ? verifyResult : '自定义验证失败';
|
||
}
|
||
} else {
|
||
// 默认验证:HTTP 200
|
||
passed = result.status === 200;
|
||
if (!passed) failReason = `HTTP ${result.status}`;
|
||
}
|
||
|
||
return { name, passed, failReason, result: data, beforeOrder, afterOrder, ids };
|
||
} catch (e) {
|
||
console.log(` 异常: ${e.message}`);
|
||
return { name, passed: false, failReason: e.message, ids };
|
||
} finally {
|
||
// 后置清理
|
||
if (postCleanup) {
|
||
try {
|
||
console.log(' [后置清理] 执行中...');
|
||
await postCleanup();
|
||
console.log(' [后置清理] 完成');
|
||
} catch (cleanupErr) {
|
||
console.log(` [后置清理] 失败: ${cleanupErr.message}`);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// ========== 主测试流程 ==========
|
||
|
||
async function main() {
|
||
console.log('='.repeat(60));
|
||
console.log('batchQuickRecheck 接口完整真实流程测试');
|
||
console.log(`测试时间: ${new Date().toISOString()}`);
|
||
console.log(`目标表: ${TABLE}`);
|
||
console.log(`Base URL: ${BASE_URL}`);
|
||
console.log('测试仓库ID: 100000 | 测试货主ID: 1000000');
|
||
console.log('='.repeat(60));
|
||
|
||
const results = [];
|
||
|
||
// ── 全局前置准备:恢复 120946 的库存锁定数量 ──
|
||
console.log('\n── 全局前置:恢复 120946 关联库存 locked_quantity ──');
|
||
const c = await getConn();
|
||
// 记录 storage 184 原始状态
|
||
const [ origStorage ] = await c.query('SELECT id, quantity, locked_quantity FROM wms_storage WHERE id = 184 AND warehouse_id = 100000');
|
||
console.log(` storage 184 原始: quantity=${origStorage[0].quantity}, locked=${origStorage[0].locked_quantity}`);
|
||
// 恢复 locked_quantity 以便测试成功扣减
|
||
if (origStorage[0].locked_quantity < 1) {
|
||
await c.query('UPDATE wms_storage SET locked_quantity = locked_quantity + 1, updated_at = NOW() WHERE id = 184 AND warehouse_id = 100000');
|
||
console.log(' storage 184 locked_quantity 已 +1(恢复用于测试)');
|
||
}
|
||
// 恢复 wms_picking 状态
|
||
await c.query("UPDATE wms_picking SET status = 'pending', updated_at = NOW() WHERE id = 87425 AND warehouse_id = 100000");
|
||
console.log(' wms_picking 87425 status 恢复为 pending');
|
||
// 恢复 wave_pick_task_order
|
||
await c.query('UPDATE wms_wave_pick_task_order SET pick_status = 0, picked_time = NULL, update_time = NOW() WHERE id = 6057 AND warehouse_id = 100000');
|
||
console.log(' wave_pick_task_order 6057 pick_status 恢复为 0');
|
||
// 恢复 120946 的 pick_status 和 recheck_status
|
||
await c.query("UPDATE wms_order_delivery_100000 SET pick_status = 'none', recheck_status = 'none', updated_at = NOW() WHERE id = 120946 AND warehouse_id = 100000");
|
||
console.log(' 订单 120946 pick_status/recheck_status 已恢复');
|
||
|
||
// ── 用例 7: 空列表(不修改数据,最先测) ──
|
||
results.push(await runTestCase(
|
||
'用例7: 空列表-应返回错误',
|
||
[],
|
||
'返回错误 "订单ID列表不能为空"',
|
||
null,
|
||
async result => {
|
||
// batchQuickRecheck 返回 500 而非 400(内部 throw(400) 被框架包装为 500)
|
||
// 检查是否包含错误消息
|
||
const msg = result.body && (result.body.message || '');
|
||
const dataMsg = result.body && result.body.data && result.body.data.message || '';
|
||
if (msg.includes('订单ID列表不能为空') || dataMsg.includes('订单ID列表不能为空')) return true;
|
||
return `期望包含"订单ID列表不能为空",实际: ${JSON.stringify(result.body)}`;
|
||
}
|
||
));
|
||
|
||
// ── 用例 8: 不存在的订单ID ──
|
||
results.push(await runTestCase(
|
||
'用例8: 不存在的订单ID',
|
||
[ 999999999 ],
|
||
'全部 failed, reason="未找到对应订单"',
|
||
null,
|
||
async result => {
|
||
const data = result.body && (result.body.data !== undefined ? result.body.data : result.body);
|
||
if (!data || typeof data !== 'object') return `返回数据格式异常: ${JSON.stringify(result.body)}`;
|
||
if (data.success_count === 0 && data.failed_count >= 1) {
|
||
const firstFail = data.failed && data.failed[0];
|
||
if (firstFail && firstFail.reason === '未找到对应订单') return true;
|
||
return `期望 reason="未找到对应订单",实际: ${JSON.stringify(firstFail)}`;
|
||
}
|
||
return `期望 success_count=0, failed_count>=1,实际: ${JSON.stringify(data)}`;
|
||
}
|
||
));
|
||
|
||
// ── 用例 5: 失败-未同步(120858: status='pedding') ──
|
||
results.push(await runTestCase(
|
||
'用例5: 失败-未同步',
|
||
[ 120858, 120857 ],
|
||
'全部 failed, reason="没有同步"',
|
||
null,
|
||
async result => {
|
||
const data = result.body && (result.body.data !== undefined ? result.body.data : result.body);
|
||
if (!data || data.success_count !== 0) return `期望 success_count=0,实际: ${JSON.stringify(data)}`;
|
||
const allNoSync = (data.failed || []).every(f => f.reason === '没有同步');
|
||
if (allNoSync) return true;
|
||
return `期望所有 failed reason="没有同步",实际: ${JSON.stringify(data.failed)}`;
|
||
}
|
||
));
|
||
|
||
// ── 用例 4: 失败-无快递(120944: post_status='none') ──
|
||
results.push(await runTestCase(
|
||
'用例4: 失败-无快递',
|
||
[ 120944, 120943 ],
|
||
'全部 failed, reason="没有分配快递"',
|
||
null,
|
||
async result => {
|
||
const data = result.body && (result.body.data !== undefined ? result.body.data : result.body);
|
||
if (!data || data.success_count !== 0) return `期望 success_count=0,实际: ${JSON.stringify(data)}`;
|
||
const allNoPost = (data.failed || []).every(f => f.reason === '没有分配快递');
|
||
if (allNoPost) return true;
|
||
return `期望所有 failed reason="没有分配快递",实际: ${JSON.stringify(data.failed)}`;
|
||
}
|
||
));
|
||
|
||
// ── 用例 3: 失败-未分配储位(120947: storage_status='none') ──
|
||
results.push(await runTestCase(
|
||
'用例3: 失败-未分配储位',
|
||
[ 120947, 120939 ],
|
||
'全部 failed, reason="未分配储位"',
|
||
null,
|
||
async result => {
|
||
const data = result.body && (result.body.data !== undefined ? result.body.data : result.body);
|
||
if (!data || data.success_count !== 0) return `期望 success_count=0,实际: ${JSON.stringify(data)}`;
|
||
const allNoStorage = (data.failed || []).every(f => f.reason === '未分配储位');
|
||
if (allNoStorage) return true;
|
||
return `期望所有 failed reason="未分配储位",实际: ${JSON.stringify(data.failed)}`;
|
||
}
|
||
));
|
||
|
||
// ── 用例 2: 失败-已拦截(120945: intercept_status='intercepted_at_warehouse') ──
|
||
results.push(await runTestCase(
|
||
'用例2: 失败-已拦截',
|
||
[ 120945 ],
|
||
'全部 failed, reason="已拦截"',
|
||
null,
|
||
async result => {
|
||
const data = result.body && (result.body.data !== undefined ? result.body.data : result.body);
|
||
if (!data || data.success_count !== 0) return `期望 success_count=0,实际: ${JSON.stringify(data)}`;
|
||
const allIntercepted = (data.failed || []).every(f => f.reason === '已拦截');
|
||
if (allIntercepted) return true;
|
||
return `期望所有 failed reason="已拦截",实际: ${JSON.stringify(data.failed)}`;
|
||
}
|
||
));
|
||
|
||
// ── 用例 1: 成功-含未拣货(120946: storage='fin', post='fin', status='created', pick='none') ──
|
||
// 前置: 确保 120946 的 picking 记录和库存已恢复
|
||
let case1Cleanup = null;
|
||
results.push(await runTestCase(
|
||
'用例1: 成功-含未拣货',
|
||
[ 120946 ],
|
||
'success_count >= 1, 订单 recheck_status=fin, pick_status=fin',
|
||
async () => {
|
||
// 再次确认前置条件
|
||
const c2 = await getConn();
|
||
const [ st ] = await c2.query('SELECT locked_quantity FROM wms_storage WHERE id = 184 AND warehouse_id = 100000');
|
||
console.log(` [前置确认] storage 184 locked_quantity=${st[0].locked_quantity}`);
|
||
const [ pk ] = await c2.query('SELECT status FROM wms_picking WHERE id = 87425 AND warehouse_id = 100000');
|
||
console.log(` [前置确认] picking 87425 status=${pk[0].status}`);
|
||
},
|
||
async result => {
|
||
const data = result.body && (result.body.data !== undefined ? result.body.data : result.body);
|
||
if (!data || data.success_count < 1) return `期望 success_count >= 1,实际: ${JSON.stringify(data)}`;
|
||
|
||
// 验证 120946 的状态
|
||
const c3 = await getConn();
|
||
const [ order ] = await c3.query(
|
||
'SELECT pick_status, recheck_status FROM wms_order_delivery_100000 WHERE id = 120946 AND warehouse_id = 100000'
|
||
);
|
||
console.log(` [验证] 120946 pick_status="${order[0].pick_status}" recheck_status="${order[0].recheck_status}"`);
|
||
if (order[0].pick_status !== 'fin') return `期望 pick_status='fin',实际="${order[0].pick_status}"`;
|
||
if (order[0].recheck_status !== 'fin') return `期望 recheck_status='fin',实际="${order[0].recheck_status}"`;
|
||
|
||
// 验证库存已扣减
|
||
const [ stAfter ] = await c3.query('SELECT locked_quantity, quantity FROM wms_storage WHERE id = 184 AND warehouse_id = 100000');
|
||
console.log(` [验证] storage 184 扣减后: locked=${stAfter[0].locked_quantity}, quantity=${stAfter[0].quantity}`);
|
||
|
||
// 记录清理函数
|
||
case1Cleanup = async () => {
|
||
const cc = await getConn();
|
||
// 恢复库存
|
||
await cc.query('UPDATE wms_storage SET locked_quantity = locked_quantity + 1, quantity = quantity + 1, updated_at = NOW() WHERE id = 184 AND warehouse_id = 100000');
|
||
// 恢复 picking
|
||
await cc.query("UPDATE wms_picking SET status = 'pending', updated_at = NOW() WHERE id = 87425 AND warehouse_id = 100000");
|
||
// 恢复 wave_pick_task_order
|
||
await cc.query('UPDATE wms_wave_pick_task_order SET pick_status = 0, picked_time = NULL, update_time = NOW() WHERE id = 6057 AND warehouse_id = 100000');
|
||
// 恢复订单状态
|
||
await cc.query("UPDATE wms_order_delivery_100000 SET pick_status = 'none', recheck_status = 'none', updated_at = NOW() WHERE id = 120946 AND warehouse_id = 100000");
|
||
console.log(' [用例1恢复] 120946 相关数据已恢复');
|
||
};
|
||
return true;
|
||
},
|
||
case1Cleanup ? async () => { await case1Cleanup(); } : null
|
||
));
|
||
|
||
// ── 用例 6: 部分失败-混合(有效+无效) ──
|
||
// 需要先确保 120946 恢复到可测试状态
|
||
results.push(await runTestCase(
|
||
'用例6: 部分失败-混合',
|
||
[ 120946, 120947, 999999999 ],
|
||
'success_count >= 1 且 failed_count >= 2',
|
||
async () => {
|
||
// 恢复 120946 的前置条件
|
||
const c4 = await getConn();
|
||
const [ st ] = await c4.query('SELECT locked_quantity FROM wms_storage WHERE id = 184 AND warehouse_id = 100000');
|
||
if (st[0].locked_quantity < 1) {
|
||
await c4.query('UPDATE wms_storage SET locked_quantity = locked_quantity + 1, quantity = quantity + 1, updated_at = NOW() WHERE id = 184 AND warehouse_id = 100000');
|
||
console.log(' [前置] storage 184 locked_quantity 已恢复 +1');
|
||
}
|
||
await c4.query("UPDATE wms_picking SET status = 'pending', updated_at = NOW() WHERE id = 87425 AND warehouse_id = 100000");
|
||
await c4.query('UPDATE wms_wave_pick_task_order SET pick_status = 0, picked_time = NULL, update_time = NOW() WHERE id = 6057 AND warehouse_id = 100000');
|
||
await c4.query("UPDATE wms_order_delivery_100000 SET pick_status = 'none', recheck_status = 'none', updated_at = NOW() WHERE id = 120946 AND warehouse_id = 100000");
|
||
console.log(' [前置] 120946 数据已恢复');
|
||
},
|
||
async result => {
|
||
const data = result.body && (result.body.data !== undefined ? result.body.data : result.body);
|
||
if (!data) return `返回数据为空: ${JSON.stringify(result.body)}`;
|
||
if (data.success_count >= 1 && data.failed_count >= 2) {
|
||
const hasUnfound = (data.failed || []).some(f => f.reason === '未找到对应订单');
|
||
const hasNoStorage = (data.failed || []).some(f => f.reason === '未分配储位');
|
||
console.log(` [验证] 包含未找到: ${hasUnfound}, 包含未分配储位: ${hasNoStorage}`);
|
||
return true;
|
||
}
|
||
return `期望 success_count>=1 且 failed_count>=2,实际 success_count=${data.success_count}, failed_count=${data.failed_count}`;
|
||
},
|
||
async () => {
|
||
// 恢复 120946
|
||
const c5 = await getConn();
|
||
const [ st ] = await c5.query('SELECT locked_quantity, quantity FROM wms_storage WHERE id = 184 AND warehouse_id = 100000');
|
||
// 如果 locked_quantity 减少了(测试成功扣减了),恢复
|
||
await c5.query("UPDATE wms_picking SET status = 'pending', updated_at = NOW() WHERE id = 87425 AND warehouse_id = 100000");
|
||
await c5.query('UPDATE wms_wave_pick_task_order SET pick_status = 0, picked_time = NULL, update_time = NOW() WHERE id = 6057 AND warehouse_id = 100000');
|
||
await c5.query("UPDATE wms_order_delivery_100000 SET pick_status = 'none', recheck_status = 'none', updated_at = NOW() WHERE id = 120946 AND warehouse_id = 100000");
|
||
// 检查库存是否需要恢复
|
||
const [ st2 ] = await c5.query('SELECT locked_quantity FROM wms_storage WHERE id = 184 AND warehouse_id = 100000');
|
||
console.log(` [用例6恢复] 120946 数据已恢复,storage 184 locked=${st2[0].locked_quantity}`);
|
||
}
|
||
));
|
||
|
||
// ── 全局恢复:将 storage 184 恢复到测试前状态 ──
|
||
console.log('\n── 全局恢复:storage 184 ──');
|
||
// 检查当前状态,恢复到原始值
|
||
const [ finalSt ] = await c.query('SELECT quantity, locked_quantity FROM wms_storage WHERE id = 184 AND warehouse_id = 100000');
|
||
console.log(` storage 184 最终: quantity=${finalSt[0].quantity}, locked=${finalSt[0].locked_quantity}`);
|
||
console.log(` storage 184 原始: quantity=${origStorage[0].quantity}, locked=${origStorage[0].locked_quantity}`);
|
||
|
||
// 恢复 picking 87425 到 pending
|
||
await c.query("UPDATE wms_picking SET status = 'pending', updated_at = NOW() WHERE id = 87425 AND warehouse_id = 100000");
|
||
// 恢复 wave_pick_task_order 6057 到 pick_status=0
|
||
await c.query('UPDATE wms_wave_pick_task_order SET pick_status = 0, picked_time = NULL, update_time = NOW() WHERE id = 6057 AND warehouse_id = 100000');
|
||
// 恢复订单 120946
|
||
await c.query("UPDATE wms_order_delivery_100000 SET pick_status = 'none', recheck_status = 'none', updated_at = NOW() WHERE id = 120946 AND warehouse_id = 100000");
|
||
console.log(' 全局恢复完成');
|
||
|
||
// ── 汇总 ──
|
||
console.log(`\n${'='.repeat(60)}`);
|
||
console.log('测试汇总');
|
||
console.log('='.repeat(60));
|
||
const passCount = results.filter(r => r.passed).length;
|
||
const failCount = results.filter(r => !r.passed).length;
|
||
console.log(`总用例数: ${results.length}`);
|
||
console.log(`通过: ${passCount}`);
|
||
console.log(`失败: ${failCount}`);
|
||
console.log('');
|
||
|
||
for (const r of results) {
|
||
const status = r.passed ? 'PASS' : 'FAIL';
|
||
console.log(` [${status}] ${r.name}`);
|
||
if (r.result && r.result.success_count !== undefined) {
|
||
console.log(` success_count=${r.result.success_count}, failed_count=${r.result.failed_count}`);
|
||
}
|
||
if (!r.passed && r.failReason) {
|
||
console.log(` 失败原因: ${r.failReason}`);
|
||
}
|
||
}
|
||
|
||
await closeConn();
|
||
return { results, passCount, failCount };
|
||
}
|
||
|
||
main().then(({ results, passCount, failCount }) => {
|
||
console.log('\n测试完成。');
|
||
// 返回给调用者
|
||
process.exit(failCount > 0 ? 1 : 0);
|
||
}).catch(e => {
|
||
console.error('测试脚本异常:', e.message);
|
||
process.exit(1);
|
||
});
|