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.
215 lines
7.1 KiB
215 lines
7.1 KiB
// 简化的聊天功能测试脚本
|
|
const WebSocket = require('ws');
|
|
|
|
// 配置信息
|
|
const WS_URL = 'ws://localhost:3003';
|
|
const TEST_CUSTOMER_ID = 'test_customer_1';
|
|
const TEST_MANAGER_ID = '22';
|
|
|
|
// 测试结果跟踪
|
|
const testResults = {
|
|
customerConnected: false,
|
|
customerAuthenticated: false,
|
|
managerConnected: false,
|
|
managerAuthenticated: false,
|
|
customerMessageSent: false,
|
|
managerMessageReceived: false,
|
|
managerReplySent: false,
|
|
customerReplyReceived: false
|
|
};
|
|
|
|
// 辅助函数:等待指定时间
|
|
function wait(ms) {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
|
|
// 主测试函数
|
|
async function runTest() {
|
|
console.log('=== 开始聊天功能测试 ===\n');
|
|
|
|
let customerWs = null;
|
|
let managerWs = null;
|
|
|
|
try {
|
|
// 1. 创建客户WebSocket连接并认证
|
|
customerWs = await createConnection(WS_URL, 'customer');
|
|
|
|
// 2. 创建客服WebSocket连接并认证
|
|
managerWs = await createConnection(WS_URL, 'manager');
|
|
|
|
// 3. 等待连接稳定
|
|
await wait(2000);
|
|
|
|
// 4. 客户发送消息给客服
|
|
const testMessage = '你好,我是测试客户,有问题咨询';
|
|
console.log('🔍 客户发送消息给客服:', testMessage);
|
|
|
|
// 使用更简单的格式,只发送必要字段
|
|
const customerMessageId = 'test_customer_' + Date.now();
|
|
customerWs.send(JSON.stringify({
|
|
type: 'chat_message',
|
|
messageId: customerMessageId,
|
|
managerId: TEST_MANAGER_ID,
|
|
content: testMessage,
|
|
contentType: 1
|
|
}));
|
|
|
|
testResults.customerMessageSent = true;
|
|
console.log('✅ 客户发送消息成功');
|
|
|
|
// 5. 客服接收消息并回复
|
|
let conversationId = null;
|
|
let replyResolved = false;
|
|
|
|
// 客服监听消息
|
|
managerWs.on('message', (data) => {
|
|
const message = JSON.parse(data.toString());
|
|
console.log('📥 客服收到消息:', message);
|
|
|
|
// 忽略心跳消息
|
|
if (message.type === 'heartbeat') return;
|
|
|
|
// 处理新消息
|
|
if (message.type === 'new_message' && message.payload && message.payload.content === testMessage) {
|
|
conversationId = message.payload.conversationId;
|
|
testResults.managerMessageReceived = true;
|
|
console.log('✅ 客服收到客户消息:', message.payload.content);
|
|
|
|
// 客服回复客户
|
|
const replyMessage = '您好,我是客服,请问有什么可以帮助您的?';
|
|
console.log('🔍 客服回复客户:', replyMessage);
|
|
|
|
// 简化消息格式,只包含基本必要字段
|
|
// 注意:服务器会验证conversationId的有效性
|
|
const messageId = 'test_reply_' + Date.now();
|
|
const replyData = {
|
|
type: 'chat_message',
|
|
conversationId: conversationId,
|
|
content: replyMessage,
|
|
contentType: 1
|
|
};
|
|
|
|
console.log('📤 客服发送回复消息:', JSON.stringify(replyData));
|
|
|
|
console.log('📤 客服发送回复消息:', replyData);
|
|
managerWs.send(JSON.stringify(replyData));
|
|
testResults.managerReplySent = true;
|
|
console.log('✅ 客服回复消息已发送');
|
|
}
|
|
});
|
|
|
|
// 客户监听回复
|
|
customerWs.on('message', (data) => {
|
|
const message = JSON.parse(data.toString());
|
|
console.log('📥 客户收到消息:', message);
|
|
|
|
// 忽略心跳消息
|
|
if (message.type === 'heartbeat') return;
|
|
|
|
// 处理消息发送确认
|
|
if (message.type === 'message_sent') {
|
|
console.log('✅ 消息发送确认:', message.payload.status);
|
|
}
|
|
|
|
// 处理新消息(客服回复)
|
|
if (message.type === 'new_message' && message.payload && message.payload.senderId === TEST_MANAGER_ID) {
|
|
testResults.customerReplyReceived = true;
|
|
console.log('✅ 客户接收客服回复:', message.payload.content);
|
|
replyResolved = true;
|
|
}
|
|
});
|
|
|
|
// 等待消息传递完成或超时
|
|
const startTime = Date.now();
|
|
while (!replyResolved && (Date.now() - startTime) < 15000) {
|
|
await wait(500);
|
|
}
|
|
|
|
if (!replyResolved) {
|
|
console.log('❌ 测试超时:未能完成消息传递流程');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('❌ 测试错误:', error);
|
|
} finally {
|
|
// 输出测试结果
|
|
console.log('\n=== 测试结果汇总 ===');
|
|
console.log(`客户连接: ${testResults.customerConnected ? '✅ 成功' : '❌ 失败'}`);
|
|
console.log(`客服连接: ${testResults.managerConnected ? '✅ 成功' : '❌ 失败'}`);
|
|
console.log(`客户认证: ${testResults.customerAuthenticated ? '✅ 成功' : '❌ 失败'}`);
|
|
console.log(`客服认证: ${testResults.managerAuthenticated ? '✅ 成功' : '❌ 失败'}`);
|
|
console.log(`客户发送消息: ${testResults.customerMessageSent ? '✅ 成功' : '❌ 失败'}`);
|
|
console.log(`客服接收消息: ${testResults.managerMessageReceived ? '✅ 成功' : '❌ 失败'}`);
|
|
console.log(`客服回复消息: ${testResults.managerReplySent ? '✅ 成功' : '❌ 失败'}`);
|
|
console.log(`客户接收回复: ${testResults.customerReplyReceived ? '✅ 成功' : '❌ 失败'}`);
|
|
|
|
// 关闭连接
|
|
if (customerWs && customerWs.readyState === WebSocket.OPEN) {
|
|
customerWs.close();
|
|
console.log('客户WebSocket连接已关闭');
|
|
}
|
|
if (managerWs && managerWs.readyState === WebSocket.OPEN) {
|
|
managerWs.close();
|
|
console.log('客服WebSocket连接已关闭');
|
|
}
|
|
}
|
|
}
|
|
|
|
// 创建WebSocket连接并认证
|
|
function createConnection(url, userType) {
|
|
return new Promise((resolve, reject) => {
|
|
const ws = new WebSocket(url);
|
|
|
|
ws.on('open', () => {
|
|
if (userType === 'customer') {
|
|
testResults.customerConnected = true;
|
|
console.log('✅ 客户WebSocket连接已建立');
|
|
|
|
// 发送认证消息
|
|
ws.send(JSON.stringify({
|
|
type: 'auth',
|
|
userId: TEST_CUSTOMER_ID,
|
|
userType: 'user'
|
|
}));
|
|
} else {
|
|
testResults.managerConnected = true;
|
|
console.log('✅ 客服WebSocket连接已建立');
|
|
|
|
// 发送认证消息
|
|
ws.send(JSON.stringify({
|
|
type: 'auth',
|
|
managerId: TEST_MANAGER_ID,
|
|
userType: 'manager'
|
|
}));
|
|
}
|
|
});
|
|
|
|
ws.on('message', (data) => {
|
|
const message = JSON.parse(data.toString());
|
|
|
|
if (message.type === 'auth_success') {
|
|
if (userType === 'customer') {
|
|
testResults.customerAuthenticated = true;
|
|
console.log('✅ 客户认证成功');
|
|
} else {
|
|
testResults.managerAuthenticated = true;
|
|
console.log('✅ 客服认证成功');
|
|
}
|
|
resolve(ws);
|
|
}
|
|
});
|
|
|
|
ws.on('error', (error) => {
|
|
console.error(`❌ ${userType === 'customer' ? '客户' : '客服'}WebSocket错误:`, error);
|
|
reject(error);
|
|
});
|
|
|
|
// 设置超时
|
|
setTimeout(() => {
|
|
reject(new Error(`${userType === 'customer' ? '客户' : '客服'}连接或认证超时`));
|
|
}, 5000);
|
|
});
|
|
}
|
|
|
|
// 运行测试
|
|
runTest();
|
|
|