You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
4.7 KiB
112 lines
4.7 KiB
|
3 months ago
|
// 测试客服在线状态检查逻辑
|
||
|
|
console.log('=== 测试客服在线状态检查逻辑 ===\n');
|
||
|
|
|
||
|
|
// 模拟onlineManagers Map和onlineStatusMap
|
||
|
|
const onlineManagers = new Map();
|
||
|
|
const onlineStatusMap = {};
|
||
|
|
|
||
|
|
// 模拟handleAuth函数中添加客服到onlineManagers的逻辑
|
||
|
|
function simulateAuth(managerId) {
|
||
|
|
console.log(`模拟客服认证: managerId=${managerId}`);
|
||
|
|
// 在handleAuth中是这样添加的:onlineManagers.set(managerId, ws);
|
||
|
|
const key = String(managerId); // 确保类型一致
|
||
|
|
onlineManagers.set(key, { connected: true });
|
||
|
|
console.log(` 已添加到onlineManagers: key=${key}`);
|
||
|
|
|
||
|
|
// 模拟更新数据库状态
|
||
|
|
onlineStatusMap[key] = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 复制isManagerOnline函数的实现
|
||
|
|
function isManagerOnline(id, managerId) {
|
||
|
|
console.log(`\n调用isManagerOnline: id=${id}, managerId=${managerId}`);
|
||
|
|
|
||
|
|
// 首先从内存中的onlineManagers Map中检查
|
||
|
|
const stringId = String(id);
|
||
|
|
const stringManagerId = String(managerId);
|
||
|
|
|
||
|
|
console.log(` 转换后的ID: stringId=${stringId}, stringManagerId=${stringManagerId}`);
|
||
|
|
console.log(` onlineManagers中的键:`, [...onlineManagers.keys()]);
|
||
|
|
|
||
|
|
// 检查id或managerId是否在onlineManagers中
|
||
|
|
for (const key of onlineManagers.keys()) {
|
||
|
|
console.log(` 比较key=${key} 与 stringId=${stringId} 或 stringManagerId=${stringManagerId}`);
|
||
|
|
if (key === stringId || key === stringManagerId) {
|
||
|
|
console.log(` ✅ 内存匹配成功: key=${key}`);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log(` ❌ 内存匹配失败`);
|
||
|
|
|
||
|
|
// 其次从数据库查询结果检查
|
||
|
|
const dbStatus = onlineStatusMap[stringId] || (stringManagerId && onlineStatusMap[stringManagerId]) || false;
|
||
|
|
console.log(` 数据库状态: id=${stringId} -> ${onlineStatusMap[stringId]}, managerId=${stringManagerId} -> ${onlineStatusMap[stringManagerId]}, 最终状态=${dbStatus}`);
|
||
|
|
return dbStatus;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 模拟/api/managers接口中的处理逻辑
|
||
|
|
function simulateApiResponse(personId, personManagerId) {
|
||
|
|
console.log(`\n=== 模拟API响应生成 ===`);
|
||
|
|
console.log(` 客服信息: id=${personId}, managerId=${personManagerId}`);
|
||
|
|
|
||
|
|
const online = isManagerOnline(personId, personManagerId);
|
||
|
|
|
||
|
|
console.log(` API返回: online=${online}`);
|
||
|
|
return online;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 测试场景1: 正常匹配 (managerId完全匹配)
|
||
|
|
console.log('\n🔍 测试场景1: managerId完全匹配');
|
||
|
|
simulateAuth(22);
|
||
|
|
const result1 = simulateApiResponse(1001, 22);
|
||
|
|
console.log(`\n结果1: ${result1 ? '✅ 在线' : '❌ 离线'} - 应该在线`);
|
||
|
|
|
||
|
|
// 测试场景2: ID格式不匹配 (数字vs字符串)
|
||
|
|
console.log('\n🔍 测试场景2: ID格式不匹配');
|
||
|
|
onlineManagers.clear();
|
||
|
|
// 清空onlineStatusMap而不是重新赋值
|
||
|
|
Object.keys(onlineStatusMap).forEach(key => delete onlineStatusMap[key]);
|
||
|
|
simulateAuth('22'); // 使用字符串格式
|
||
|
|
const result2 = simulateApiResponse(1001, 22); // 使用数字格式
|
||
|
|
console.log(`\n结果2: ${result2 ? '✅ 在线' : '❌ 离线'} - 应该在线`);
|
||
|
|
|
||
|
|
// 测试场景3: 测试不存在的ID
|
||
|
|
console.log('\n🔍 测试场景3: 测试不存在的ID');
|
||
|
|
const result3 = simulateApiResponse(999, 999);
|
||
|
|
console.log(`\n结果3: ${result3 ? '✅ 在线' : '❌ 离线'} - 应该离线`);
|
||
|
|
|
||
|
|
// 测试场景4: 测试id和managerId都存在
|
||
|
|
console.log('\n🔍 测试场景4: 测试id和managerId都存在');
|
||
|
|
onlineManagers.clear();
|
||
|
|
// 清空onlineStatusMap而不是重新赋值
|
||
|
|
Object.keys(onlineStatusMap).forEach(key => delete onlineStatusMap[key]);
|
||
|
|
simulateAuth(1001); // 使用person.id作为key
|
||
|
|
simulateAuth(22); // 使用person.managerId作为key
|
||
|
|
const result4 = simulateApiResponse(1001, 22);
|
||
|
|
console.log(`\n结果4: ${result4 ? '✅ 在线' : '❌ 离线'} - 应该在线`);
|
||
|
|
|
||
|
|
// 测试场景5: 测试isManagerOnline函数的边界情况
|
||
|
|
console.log('\n🔍 测试场景5: 边界情况测试');
|
||
|
|
onlineManagers.clear();
|
||
|
|
// 清空onlineStatusMap而不是重新赋值
|
||
|
|
Object.keys(onlineStatusMap).forEach(key => delete onlineStatusMap[key]);
|
||
|
|
simulateAuth(22);
|
||
|
|
|
||
|
|
// 测试undefined/null
|
||
|
|
console.log('\n 测试undefined/null:');
|
||
|
|
console.log(` isManagerOnline(undefined, 22): ${isManagerOnline(undefined, 22)}`);
|
||
|
|
console.log(` isManagerOnline(1001, null): ${isManagerOnline(1001, null)}`);
|
||
|
|
|
||
|
|
// 测试空字符串
|
||
|
|
console.log('\n 测试空字符串:');
|
||
|
|
console.log(` isManagerOnline("", 22): ${isManagerOnline("", 22)}`);
|
||
|
|
console.log(` isManagerOnline(1001, ""): ${isManagerOnline(1001, "")}`);
|
||
|
|
|
||
|
|
// 总结
|
||
|
|
console.log('\n=== 测试总结 ===');
|
||
|
|
console.log(`场景1: ${result1 ? '✅ 通过' : '❌ 失败'} - managerId完全匹配`);
|
||
|
|
console.log(`场景2: ${result2 ? '✅ 通过' : '❌ 失败'} - ID格式不匹配`);
|
||
|
|
console.log(`场景3: ${!result3 ? '✅ 通过' : '❌ 失败'} - 不存在的ID`);
|
||
|
|
console.log(`场景4: ${result4 ? '✅ 通过' : '❌ 失败'} - id和managerId都存在`);
|