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.
138 lines
3.7 KiB
138 lines
3.7 KiB
// 简化版聊天功能测试
|
|
|
|
// 服务器配置
|
|
const SERVER_URL = 'ws://localhost:3003';
|
|
|
|
// 测试数据
|
|
const managerData = {
|
|
userId: 'manager_001',
|
|
type: 'manager',
|
|
name: '客服小刘'
|
|
};
|
|
|
|
const userData = {
|
|
userId: 'user_001',
|
|
type: 'user',
|
|
name: '测试用户'
|
|
};
|
|
|
|
// 测试结果跟踪
|
|
const testResults = {
|
|
managerConnection: false,
|
|
managerAuth: false,
|
|
userConnection: false,
|
|
userAuth: false,
|
|
messageExchange: false,
|
|
onlineStatusDetection: false,
|
|
messageCenterFunctionality: false
|
|
};
|
|
|
|
function runSimpleChatTest() {
|
|
console.log('=== 开始简化版聊天功能测试 ===');
|
|
|
|
// 模拟客服连接
|
|
try {
|
|
const WebSocket = require('ws');
|
|
const managerSocket = new WebSocket(SERVER_URL);
|
|
|
|
managerSocket.on('open', () => {
|
|
console.log('[✅] 客服连接已建立');
|
|
testResults.managerConnection = true;
|
|
|
|
// 发送客服认证
|
|
const authMessage = {
|
|
type: 'auth',
|
|
data: {
|
|
userId: managerData.userId,
|
|
type: managerData.type,
|
|
name: managerData.name
|
|
}
|
|
};
|
|
console.log('发送客服认证:', authMessage);
|
|
managerSocket.send(JSON.stringify(authMessage));
|
|
});
|
|
|
|
managerSocket.on('message', (data) => {
|
|
console.log('[客服收到消息]:', data.toString());
|
|
const message = JSON.parse(data);
|
|
|
|
// 检查认证结果
|
|
if (message.type === 'auth_success' || message.action === 'auth_response') {
|
|
console.log('[✅] 客服认证成功');
|
|
testResults.managerAuth = true;
|
|
}
|
|
});
|
|
|
|
managerSocket.on('error', (error) => {
|
|
console.error('[❌] 客服连接错误:', error.message);
|
|
});
|
|
|
|
managerSocket.on('close', () => {
|
|
console.log('[🔌] 客服连接已关闭');
|
|
});
|
|
|
|
// 延迟创建用户连接
|
|
setTimeout(() => {
|
|
const userSocket = new WebSocket(SERVER_URL);
|
|
|
|
userSocket.on('open', () => {
|
|
console.log('[✅] 用户连接已建立');
|
|
testResults.userConnection = true;
|
|
|
|
// 发送用户认证
|
|
const userAuth = {
|
|
type: 'auth',
|
|
data: {
|
|
userId: userData.userId,
|
|
type: userData.type,
|
|
name: userData.name
|
|
}
|
|
};
|
|
console.log('发送用户认证:', userAuth);
|
|
userSocket.send(JSON.stringify(userAuth));
|
|
});
|
|
|
|
userSocket.on('message', (data) => {
|
|
console.log('[用户收到消息]:', data.toString());
|
|
});
|
|
|
|
// 5秒后发送测试消息
|
|
setTimeout(() => {
|
|
if (userSocket.readyState === WebSocket.OPEN) {
|
|
const testMessage = {
|
|
type: 'chat',
|
|
from: userData.userId,
|
|
to: managerData.userId,
|
|
content: '你好,这是一条测试消息',
|
|
timestamp: Date.now()
|
|
};
|
|
console.log('用户发送测试消息:', testMessage);
|
|
userSocket.send(JSON.stringify(testMessage));
|
|
}
|
|
}, 5000);
|
|
|
|
}, 3000);
|
|
|
|
// 15秒后显示测试结果
|
|
setTimeout(() => {
|
|
console.log('\n=== 测试结果 ===');
|
|
console.log('客服连接:', testResults.managerConnection ? '✅ 成功' : '❌ 失败');
|
|
console.log('客服认证:', testResults.managerAuth ? '✅ 成功' : '❌ 失败');
|
|
console.log('用户连接:', testResults.userConnection ? '✅ 成功' : '❌ 失败');
|
|
console.log('\n测试完成!');
|
|
|
|
// 关闭连接
|
|
managerSocket.close();
|
|
process.exit(0);
|
|
|
|
}, 15000);
|
|
|
|
} catch (error) {
|
|
console.error('测试运行失败:', error.message);
|
|
}
|
|
}
|
|
|
|
// 运行测试
|
|
if (require.main === module) {
|
|
runSimpleChatTest();
|
|
}
|
|
|