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.
 
 

134 lines
3.9 KiB

// 测试客服在线状态功能
const WebSocket = require('ws');
const http = require('http');
// 服务器地址
const SERVER_URL = 'ws://localhost:3003';
const API_URL = 'http://localhost:3003/api/managers';
// 模拟客服登录信息
const testManagerId = '22'; // 刘杨的ID
const testManagerInfo = {
type: 'auth',
data: {
managerId: testManagerId,
type: 'manager',
name: '测试客服'
}
};
console.log('开始测试客服在线状态功能...');
console.log(`目标客服ID: ${testManagerId}`);
// 连接WebSocket并进行认证
function connectAndAuthenticate() {
return new Promise((resolve, reject) => {
const ws = new WebSocket(SERVER_URL);
ws.on('open', () => {
console.log('✅ WebSocket连接已建立');
// 发送认证消息
console.log('发送客服认证消息:', JSON.stringify(testManagerInfo));
ws.send(JSON.stringify(testManagerInfo));
});
ws.on('message', (data) => {
try {
const message = JSON.parse(data.toString());
console.log('收到服务器响应:', message);
// 检查是否认证成功
if (message.type === 'auth_success' && message.payload && message.payload.type === 'manager') {
console.log('✅ 客服认证成功');
resolve(ws);
} else if (message.type === 'auth_error') {
console.error('❌ 客服认证失败:', message.message);
reject(new Error('认证失败'));
}
} catch (e) {
console.error('解析消息失败:', e);
reject(e);
}
});
ws.on('error', (error) => {
console.error('❌ WebSocket错误:', error);
reject(error);
});
// 超时处理
setTimeout(() => {
reject(new Error('认证超时'));
}, 5000);
});
}
// 调用API检查客服在线状态
function checkManagerOnlineStatus() {
return new Promise((resolve, reject) => {
http.get(API_URL, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const result = JSON.parse(data);
resolve(result);
} catch (e) {
reject(new Error('解析API响应失败'));
}
});
}).on('error', (error) => {
reject(error);
});
});
}
// 主测试函数
async function runTest() {
try {
// 1. 先检查初始状态
console.log('\n=== 步骤1: 检查初始状态 ===');
const initialStatus = await checkManagerOnlineStatus();
const initialManager = initialStatus.data.find(m => m.id.toString() === testManagerId || m.managerId === testManagerId);
console.log(`初始状态 - 客服在线: ${initialManager ? initialManager.online : '未找到'}`);
// 2. 连接并认证
console.log('\n=== 步骤2: WebSocket连接和认证 ===');
const ws = await connectAndAuthenticate();
// 3. 等待1秒后再次检查在线状态
console.log('\n=== 步骤3: 检查连接后的在线状态 ===');
setTimeout(async () => {
try {
const updatedStatus = await checkManagerOnlineStatus();
const updatedManager = updatedStatus.data.find(m => m.id.toString() === testManagerId || m.managerId === testManagerId);
console.log(`更新后状态 - 客服在线: ${updatedManager ? updatedManager.online : '未找到'}`);
if (updatedManager && updatedManager.online) {
console.log('\n🎉 测试成功!客服在线状态正确显示');
} else {
console.log('\n❌ 测试失败!客服仍然显示离线');
}
// 关闭连接
ws.close();
} catch (error) {
console.error('\n❌ 检查在线状态失败:', error);
ws.close();
}
}, 1000);
} catch (error) {
console.error('\n❌ 测试过程中出现错误:', error);
}
}
// 运行测试
runTest();