305 lines
10 KiB
JavaScript
305 lines
10 KiB
JavaScript
/**
|
|
* 维修类型名称显示功能测试
|
|
*
|
|
* 测试目标:
|
|
* 1. 验证报修单详情中维修类型显示名称而不是ID
|
|
* 2. 验证工单分派对话框中维修类型名称正确显示
|
|
* 3. 验证getCategoryNameById函数的正确性
|
|
*/
|
|
|
|
const axios = require('axios');
|
|
const colors = require('colors');
|
|
|
|
// 配置
|
|
const config = {
|
|
baseURL: 'http://localhost/dev-api',
|
|
timeout: 10000,
|
|
clientId: 'e5cd7e4891bf95d1d19206ce24a7b32e',
|
|
};
|
|
|
|
/**
|
|
* 获取Token(登录)
|
|
*/
|
|
async function getToken() {
|
|
try {
|
|
const { getToken } = require('./quick-token-getter');
|
|
return await getToken('admin', 'admin123');
|
|
} catch (error) {
|
|
console.error('获取Token失败:', error.message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 测试维修类型树形数据获取
|
|
*/
|
|
async function testCategoryTree(token) {
|
|
console.log('\\n🌳 测试维修类型树形数据获取'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
try {
|
|
const response = await axios.get(`${config.baseURL}/pm/repairCategory/tree`, {
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
clientid: config.clientId,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
if (response.data.code === 200) {
|
|
const categories = response.data.data || [];
|
|
console.log('✅ 维修类型树形数据获取成功'.green);
|
|
console.log(`📊 树形数据结构包含 ${categories.length} 个一级分类`.cyan);
|
|
|
|
// 递归统计所有节点
|
|
function countNodes(nodes) {
|
|
let count = 0;
|
|
for (const node of nodes) {
|
|
count++;
|
|
if (node.children && node.children.length > 0) {
|
|
count += countNodes(node.children);
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
const totalNodes = countNodes(categories);
|
|
console.log(`📈 总共包含 ${totalNodes} 个维修类型节点`.cyan);
|
|
|
|
// 显示前几个分类示例
|
|
if (categories.length > 0) {
|
|
console.log('\\n📋 维修类型示例:'.yellow.bold);
|
|
categories.slice(0, 3).forEach((category, index) => {
|
|
console.log(
|
|
` ${index + 1}. ID: ${category.id}, 名称: ${
|
|
category.categoryName || '未命名'
|
|
}`,
|
|
);
|
|
if (category.children && category.children.length > 0) {
|
|
category.children.slice(0, 2).forEach((child, childIndex) => {
|
|
console.log(
|
|
` ├─ ${childIndex + 1}. ID: ${child.id}, 名称: ${
|
|
child.categoryName || '未命名'
|
|
}`,
|
|
);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
return { success: true, data: categories };
|
|
} else {
|
|
console.log('❌ 维修类型树形数据获取失败'.red);
|
|
console.log(`错误信息: ${response.data.msg}`.red);
|
|
return { success: false, error: response.data.msg };
|
|
}
|
|
} catch (error) {
|
|
console.log('❌ 维修类型树形数据获取异常'.red);
|
|
console.log(`异常信息: ${error.message}`.red);
|
|
return { success: false, error: error.message };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 测试报修工单数据获取
|
|
*/
|
|
async function testRepairOrderList(token) {
|
|
console.log('\\n📋 测试报修工单数据获取'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
try {
|
|
const response = await axios.get(`${config.baseURL}/pm/repairOrder/list`, {
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
clientid: config.clientId,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
params: {
|
|
pageNum: 1,
|
|
pageSize: 5,
|
|
},
|
|
});
|
|
|
|
if (response.data.code === 200) {
|
|
const orders = response.data.rows || [];
|
|
console.log('✅ 报修工单数据获取成功'.green);
|
|
console.log(`📊 共获取 ${orders.length} 条工单记录`.cyan);
|
|
|
|
if (orders.length > 0) {
|
|
console.log('\\n📋 工单维修类型信息:'.yellow.bold);
|
|
orders.forEach((order, index) => {
|
|
console.log(` ${index + 1}. 工单号: ${order.orderNo}`);
|
|
console.log(` 标题: ${order.title}`);
|
|
console.log(` 维修类型ID: ${order.categoryId || '未设置'}`);
|
|
console.log(` 维修类型名称: ${order.categoryName || '未获取'}`);
|
|
console.log(
|
|
` ${
|
|
order.categoryName
|
|
? '✅ 有名称'
|
|
: '❌ 仅有ID,需要通过getCategoryNameById函数获取名称'
|
|
}`.cyan,
|
|
);
|
|
console.log('');
|
|
});
|
|
}
|
|
|
|
return { success: true, data: orders };
|
|
} else {
|
|
console.log('❌ 报修工单数据获取失败'.red);
|
|
console.log(`错误信息: ${response.data.msg}`.red);
|
|
return { success: false, error: response.data.msg };
|
|
}
|
|
} catch (error) {
|
|
console.log('❌ 报修工单数据获取异常'.red);
|
|
console.log(`异常信息: ${error.message}`.red);
|
|
return { success: false, error: error.message };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 模拟getCategoryNameById函数测试
|
|
*/
|
|
function testGetCategoryNameById(categories, testCategoryIds) {
|
|
console.log('\\n🔍 测试getCategoryNameById函数'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
// 递归查找分类名称函数
|
|
function findCategoryName(categories, targetId) {
|
|
for (const category of categories) {
|
|
if (String(category.id) === String(targetId)) {
|
|
return category.categoryName || '未命名分类';
|
|
}
|
|
if (category.children && category.children.length > 0) {
|
|
const found = findCategoryName(category.children, targetId);
|
|
if (found) {
|
|
return found;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
console.log('🧪 执行分类名称查找测试:'.yellow.bold);
|
|
|
|
testCategoryIds.forEach((categoryId) => {
|
|
const foundName = findCategoryName(categories, categoryId);
|
|
const result = foundName || `未知类型(${categoryId})`;
|
|
|
|
console.log(` 查找ID: ${categoryId} → 结果: ${result} ${foundName ? '✅' : '❌'}`.cyan);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 综合测试结果评估
|
|
*/
|
|
function evaluateTestResults(categoryResult, orderResult, categoryIds) {
|
|
console.log('\\n📊 测试结果评估'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
let score = 0;
|
|
let total = 3;
|
|
|
|
console.log('🔍 评估项目:'.yellow.bold);
|
|
|
|
// 1. 维修类型数据获取
|
|
if (categoryResult.success) {
|
|
console.log(' ✅ 维修类型树形数据获取正常 (+1分)'.green);
|
|
score++;
|
|
} else {
|
|
console.log(' ❌ 维修类型树形数据获取失败 (+0分)'.red);
|
|
}
|
|
|
|
// 2. 报修工单数据获取
|
|
if (orderResult.success) {
|
|
console.log(' ✅ 报修工单数据获取正常 (+1分)'.green);
|
|
score++;
|
|
} else {
|
|
console.log(' ❌ 报修工单数据获取失败 (+0分)'.red);
|
|
}
|
|
|
|
// 3. 类型名称映射
|
|
if (categoryResult.success && categoryResult.data.length > 0) {
|
|
console.log(' ✅ 维修类型名称映射功能可用 (+1分)'.green);
|
|
score++;
|
|
} else {
|
|
console.log(' ❌ 维修类型名称映射功能不可用 (+0分)'.red);
|
|
}
|
|
|
|
console.log('');
|
|
console.log(`📈 最终评分: ${score}/${total} (${Math.round((score / total) * 100)}%)`.cyan.bold);
|
|
|
|
if (score === total) {
|
|
console.log('🎉 所有功能测试通过,维修类型名称显示功能正常!'.green.bold);
|
|
} else if (score >= 2) {
|
|
console.log('⚠️ 主要功能正常,部分功能需要检查'.yellow.bold);
|
|
} else {
|
|
console.log('❌ 多项功能异常,需要排查问题'.red.bold);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 主测试函数
|
|
*/
|
|
async function runTests() {
|
|
console.log('🧪 维修类型名称显示功能测试'.bold.blue);
|
|
console.log('='.repeat(60).blue);
|
|
console.log(`⏰ 测试时间: ${new Date().toLocaleString()}`);
|
|
console.log(`🔗 测试环境: ${config.baseURL}`);
|
|
|
|
// 1. 获取Token
|
|
console.log('\\n🔐 第一步:获取访问Token'.blue.bold);
|
|
const token = await getToken();
|
|
if (!token) {
|
|
console.log('❌ 无法获取Token,测试终止'.red.bold);
|
|
return;
|
|
}
|
|
console.log('✅ Token获取成功'.green);
|
|
|
|
// 2. 测试维修类型数据
|
|
const categoryResult = await testCategoryTree(token);
|
|
|
|
// 3. 测试报修工单数据
|
|
const orderResult = await testRepairOrderList(token);
|
|
|
|
// 4. 测试分类名称查找函数
|
|
if (categoryResult.success) {
|
|
const testIds = [];
|
|
// 从分类数据中提取一些ID进行测试
|
|
if (categoryResult.data.length > 0) {
|
|
testIds.push(categoryResult.data[0].id);
|
|
if (categoryResult.data[0].children && categoryResult.data[0].children.length > 0) {
|
|
testIds.push(categoryResult.data[0].children[0].id);
|
|
}
|
|
}
|
|
testIds.push('999'); // 测试不存在的ID
|
|
|
|
testGetCategoryNameById(categoryResult.data, testIds);
|
|
}
|
|
|
|
// 5. 综合评估
|
|
evaluateTestResults(categoryResult, orderResult);
|
|
|
|
console.log('\\n📝 测试建议:'.yellow.bold);
|
|
console.log(
|
|
' 1. 确保前端组件中使用getCategoryNameById(categoryId)替换直接显示categoryId'.cyan,
|
|
);
|
|
console.log(' 2. 验证报修单详情对话框中维修类型正确显示'.cyan);
|
|
console.log(' 3. 验证工单分派对话框中维修类型名称正确显示'.cyan);
|
|
console.log(' 4. 测试新增报修时维修类型树形选择器功能'.cyan);
|
|
|
|
console.log('\\n✅ 测试完成!'.green.bold);
|
|
}
|
|
|
|
// 执行测试
|
|
if (require.main === module) {
|
|
runTests().catch((error) => {
|
|
console.error('测试执行失败:', error);
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
runTests,
|
|
testCategoryTree,
|
|
testRepairOrderList,
|
|
};
|