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
8.0 KiB

// 测试首页消息中心功能的脚本
const WebSocket = require('ws');
// 从.env文件加载配置
require('dotenv').config();
// 服务器配置 - 使用本地服务器地址
const SERVER_URL = 'ws://localhost:3003';
console.log(`连接到服务器: ${SERVER_URL}`);
// 客服账号信息(刘杨 - 电话号码17780155537)
const managerCredentials = {
managerId: '22', // 根据系统调试脚本,客服ID应为数字格式
phoneNumber: '17780155537',
userType: 'manager'
};
// 需要查找的测试会话ID
const TEST_CONVERSATION_ID = '963f9eed-950c-47e9-ade6-97e7e90915dc';
// 测试结果
const testResults = {
connection: false,
authentication: false,
messageCenterAccess: false,
messageListReceived: false,
testConversationFound: false
};
// 连接WebSocket并测试消息中心功能
function testMessageCenter() {
console.log('========================================');
console.log('开始测试首页消息中心功能');
console.log('========================================');
// 创建WebSocket连接
const ws = new WebSocket(SERVER_URL);
// 连接建立
ws.on('open', () => {
console.log('[1/5] ✅ WebSocket连接成功建立');
testResults.connection = true;
// 发送认证请求
const authMessage = {
type: 'auth',
managerId: managerCredentials.managerId,
userType: managerCredentials.userType
};
console.log('发送客服认证请求...');
ws.send(JSON.stringify(authMessage));
});
// 接收消息
ws.on('message', (data) => {
try {
const message = JSON.parse(data.toString());
console.log('收到服务器消息:', JSON.stringify(message, null, 2));
// 处理认证响应
if (message.type === 'auth_success') {
console.log('[2/5] ✅ 客服认证成功');
testResults.authentication = true;
// 尝试获取消息列表 - 使用多种格式
setTimeout(() => {
// 根据代码分析,尝试多种消息中心查询格式
const messageCenterFormats = [
{
type: 'get_message_list',
managerId: managerCredentials.managerId,
includeConversation: TEST_CONVERSATION_ID // 专门指定要查找的会话ID
},
{
type: 'query_chat_list',
managerId: managerCredentials.managerId,
conversationId: TEST_CONVERSATION_ID
},
{ type: 'query_chat_list',
managerId: managerCredentials.managerId
},
{
cmd: 'get_chat_list',
managerId: managerCredentials.managerId
},
{
type: 'get_messages',
managerId: managerCredentials.managerId
},
{
action: 'fetch_messages',
userId: managerCredentials.managerId,
role: 'manager'
},
{
type: 'query_message_center',
userId: managerCredentials.managerId
}
];
// 逐一尝试每种格式
messageCenterFormats.forEach((format, index) => {
setTimeout(() => {
const formatType = format.type || format.action || format.cmd || 'unknown';
console.log(`尝试获取消息列表 (格式${index + 1}: ${formatType})...`);
ws.send(JSON.stringify(format));
}, index * 1000);
});
}, 1000);
}
// 处理消息列表响应
if (message.type === 'message_list' || message.type === 'chat_list' || message.type === 'messages') {
console.log('[3/5] ✅ 成功接收到消息列表');
testResults.messageListReceived = true;
// 显示完整的消息中心响应内容
console.log('📋 消息中心响应详情:', JSON.stringify(message, null, 2));
// 检查消息列表
const messageList = message.data || message.messages || message.chat_list || message.payload || [];
console.log(`收到 ${Array.isArray(messageList) ? messageList.length : Object.keys(messageList).length} 条消息会话`);
// 检查我们测试的会话是否存在
if (Array.isArray(messageList)) {
const testConversation = messageList.find(msg =>
msg.conversation_id === TEST_CONVERSATION_ID ||
msg.id === TEST_CONVERSATION_ID ||
msg.conversationId === TEST_CONVERSATION_ID
);
if (testConversation) {
console.log('[4/5] ✅ 在消息中心找到测试会话');
testResults.testConversationFound = true;
console.log('测试会话信息:', testConversation);
} else {
console.log('🔍 消息列表中的会话:');
messageList.forEach((item, index) => {
console.log(` ${index + 1}. 会话ID: ${item.conversationId || item.id || item.conversation_id}, 未读数: ${item.unreadCount || item.unread_count || 0}`);
if (item.lastMessage || item.last_message) {
console.log(` 最后消息: ${item.lastMessage || item.last_message}`);
}
});
}
} else {
console.log('🔍 消息列表格式为对象,不是数组:', messageList);
}
}
// 监听实时消息更新 - 支持多种消息类型
if (message.type === 'message_center_update' ||
message.type === 'new_message' ||
message.type === 'chat_list' ||
message.type === 'unread_count' ||
message.type === 'messages' ||
message.type === 'message_list' ||
message.action === 'message_update' ||
message.cmd === 'message_list') {
console.log('[5/5] ✅ 接收到消息中心更新通知');
testResults.messageCenterAccess = true;
}
} catch (error) {
console.error('解析消息失败:', error);
}
});
// 连接错误
ws.on('error', (error) => {
console.error('WebSocket错误:', error);
});
// 连接关闭
ws.on('close', () => {
console.log('WebSocket连接已关闭');
// 显示测试结果
console.log('\n========================================');
console.log('消息中心功能测试结果:');
console.log('----------------------------------------');
console.log(`连接建立: ${testResults.connection ? '✅ 通过' : '❌ 失败'}`);
console.log(`客服认证: ${testResults.authentication ? '✅ 通过' : '❌ 失败'}`);
console.log(`消息列表接收: ${testResults.messageListReceived ? '✅ 通过' : '❌ 失败'}`);
console.log(`测试会话找到: ${testResults.testConversationFound ? '✅ 通过' : '❌ 失败'}`);
console.log(`消息中心更新: ${testResults.messageCenterAccess ? '✅ 通过' : '❌ 失败'}`);
console.log('----------------------------------------');
// 判断是否全部通过
const allPassed = Object.values(testResults).every(result => result);
if (allPassed) {
console.log('🎉 消息中心功能测试全部通过!');
console.log('✅ 首页消息中心可以正常显示消息');
} else {
console.log('🔴 部分测试未通过,但基本功能可能仍然可用');
// 即使部分测试失败,如果成功认证并且收到了消息列表,也认为基本功能可用
if (testResults.authentication && testResults.messageListReceived) {
console.log('✅ 消息中心基本功能可用: 可以认证并获取消息列表');
}
}
console.log('========================================');
});
// 设置测试超时 - 延长至15秒以确保有足够时间接收所有消息
const testTimeout = setTimeout(() => {
if (ws.readyState === WebSocket.OPEN) {
console.log('\n⏰ 测试超时,关闭连接');
ws.close();
}
}, 15000); // 15秒超时
}
// 开始测试
testMessageCenter();