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.

334 lines
9.9 KiB

// 客服功能测试脚本
// 用于验证客服认证、身份判断和双向沟通功能
console.log('===== 开始客服功能测试 =====');
// 模拟用户信息和环境
const mockUserInfo = {
customerUser: {
id: 'test_customer_001',
userType: null,
type: null,
isService: false,
isManager: false
},
serviceUser: {
id: 'test_service_001',
userType: 'customer_service',
type: 'service',
isService: true,
isManager: false
},
managerUser: {
id: 'test_manager_001',
userType: 'customer_service',
type: 'manager',
isService: false,
isManager: true
}
};
// 测试1: 用户类型判断逻辑
console.log('\n测试1: 用户类型判断逻辑');
testUserTypeDetection();
// 测试2: WebSocket消息格式
console.log('\n测试2: WebSocket消息格式');
testWebSocketMessageFormat();
// 测试3: 消息处理逻辑
console.log('\n测试3: 消息处理逻辑');
testMessageProcessing();
// 测试4: 双向通信模式
console.log('\n测试4: 双向通信模式');
testBidirectionalCommunication();
console.log('\n===== 测试完成 =====');
// 测试用户类型判断逻辑
function testUserTypeDetection() {
console.log('- 测试用户类型判断函数');
// 模拟用户类型判断函数
function detectUserType(userInfo) {
if (!userInfo) return 'customer';
if (userInfo.userType === 'customer_service' ||
userInfo.type === 'service' ||
userInfo.type === 'manager' ||
userInfo.isService ||
userInfo.isManager) {
return 'customer_service';
}
return 'customer';
}
// 测试各种用户类型
const testCases = [
{ input: mockUserInfo.customerUser, expected: 'customer', desc: '普通用户' },
{ input: mockUserInfo.serviceUser, expected: 'customer_service', desc: '客服用户' },
{ input: mockUserInfo.managerUser, expected: 'customer_service', desc: '管理员用户' },
{ input: null, expected: 'customer', desc: '空用户信息' },
{ input: {}, expected: 'customer', desc: '空对象' }
];
let passed = 0;
let failed = 0;
testCases.forEach((testCase, index) => {
const result = detectUserType(testCase.input);
const isPass = result === testCase.expected;
if (isPass) {
passed++;
console.log(` ✓ 测试${index + 1} (${testCase.desc}): 期望 ${testCase.expected}, 结果 ${result}`);
} else {
failed++;
console.log(` ✗ 测试${index + 1} (${testCase.desc}): 期望 ${testCase.expected}, 结果 ${result}`);
}
});
console.log(` 结果: 通过 ${passed}, 失败 ${failed}`);
}
// 测试WebSocket消息格式
function testWebSocketMessageFormat() {
console.log('- 测试WebSocket消息格式');
// 模拟创建消息函数
function createWebSocketMessage(senderId, receiverId, content, senderType) {
return {
type: 'chat_message',
direction: senderType === 'customer_service' ? 'service_to_customer' : 'customer_to_service',
data: {
receiverId: receiverId,
senderId: senderId,
senderType: senderType,
content: content,
contentType: 1,
timestamp: Date.now()
}
};
}
// 测试客服发送消息
const serviceMsg = createWebSocketMessage(
mockUserInfo.serviceUser.id,
mockUserInfo.customerUser.id,
'您好,有什么可以帮助您的吗?',
'customer_service'
);
// 测试客户发送消息
const customerMsg = createWebSocketMessage(
mockUserInfo.customerUser.id,
mockUserInfo.serviceUser.id,
'我想咨询一下产品信息',
'customer'
);
console.log(' 客服消息格式:');
console.log(` - type: ${serviceMsg.type}`);
console.log(` - direction: ${serviceMsg.direction}`);
console.log(` - senderId: ${serviceMsg.data.senderId}`);
console.log(` - receiverId: ${serviceMsg.data.receiverId}`);
console.log(` - senderType: ${serviceMsg.data.senderType}`);
console.log(' 客户消息格式:');
console.log(` - type: ${customerMsg.type}`);
console.log(` - direction: ${customerMsg.direction}`);
console.log(` - senderId: ${customerMsg.data.senderId}`);
console.log(` - receiverId: ${customerMsg.data.receiverId}`);
console.log(` - senderType: ${customerMsg.data.senderType}`);
// 验证必要字段
const requiredFields = ['type', 'direction', 'data'];
const requiredDataFields = ['receiverId', 'senderId', 'senderType', 'content', 'contentType', 'timestamp'];
let hasAllRequiredFields = true;
requiredFields.forEach(field => {
if (!(field in serviceMsg)) {
console.log(` ✗ 消息缺少必要字段: ${field}`);
hasAllRequiredFields = false;
}
});
requiredDataFields.forEach(field => {
if (!(field in serviceMsg.data)) {
console.log(` ✗ 消息data缺少必要字段: ${field}`);
hasAllRequiredFields = false;
}
});
if (hasAllRequiredFields) {
console.log(' ✓ 消息格式验证通过');
} else {
console.log(' ✗ 消息格式验证失败');
}
}
// 测试消息处理逻辑
function testMessageProcessing() {
console.log('- 测试消息处理逻辑');
// 模拟处理接收到的消息
function processChatMessage(message, currentUserId, currentUserType) {
if (!message || !message.data) {
return null;
}
// 判断消息方向
const isFromMe = message.data.senderId === currentUserId;
const isFromService = message.data.senderType === 'customer_service';
const isFromCustomer = message.data.senderType === 'customer';
// 构建本地消息对象
const localMessage = {
id: message.id || Date.now().toString(),
content: message.data.content || '',
contentType: message.data.contentType || 1,
timestamp: message.data.timestamp || Date.now(),
isFromMe: isFromMe,
senderType: message.data.senderType || 'unknown',
serverData: message,
status: 'received'
};
return localMessage;
}
// 测试消息
const testMessage = {
id: 'msg_001',
type: 'chat_message',
direction: 'customer_to_service',
data: {
receiverId: mockUserInfo.serviceUser.id,
senderId: mockUserInfo.customerUser.id,
senderType: 'customer',
content: '测试消息',
contentType: 1,
timestamp: Date.now()
}
};
// 从客服视角处理
const serviceProcessed = processChatMessage(
testMessage,
mockUserInfo.serviceUser.id,
'customer_service'
);
// 从客户视角处理
const customerProcessed = processChatMessage(
testMessage,
mockUserInfo.customerUser.id,
'customer'
);
console.log(' 客服视角处理结果:');
console.log(` - 是否来自自己: ${serviceProcessed.isFromMe}`);
console.log(` - 发送方类型: ${serviceProcessed.senderType}`);
console.log(` - 内容: ${serviceProcessed.content}`);
console.log(' 客户视角处理结果:');
console.log(` - 是否来自自己: ${customerProcessed.isFromMe}`);
console.log(` - 发送方类型: ${customerProcessed.senderType}`);
console.log(` - 内容: ${customerProcessed.content}`);
// 验证处理逻辑
const isServiceLogicCorrect = !serviceProcessed.isFromMe && serviceProcessed.senderType === 'customer';
const isCustomerLogicCorrect = customerProcessed.isFromMe && customerProcessed.senderType === 'customer';
if (isServiceLogicCorrect && isCustomerLogicCorrect) {
console.log(' ✓ 消息处理逻辑验证通过');
} else {
console.log(' ✗ 消息处理逻辑验证失败');
if (!isServiceLogicCorrect) console.log(' - 客服视角处理错误');
if (!isCustomerLogicCorrect) console.log(' - 客户视角处理错误');
}
}
// 测试双向通信模式
function testBidirectionalCommunication() {
console.log('- 测试双向通信模式');
// 模拟对话流程
const conversation = [
{
sender: 'customer',
content: '您好,我想咨询一下产品价格',
expectedDirection: 'customer_to_service'
},
{
sender: 'service',
content: '您好,请问您想了解哪种产品的价格呢?',
expectedDirection: 'service_to_customer'
},
{
sender: 'customer',
content: '就是你们的主打产品',
expectedDirection: 'customer_to_service'
},
{
sender: 'service',
content: '我们的主打产品价格是¥199,现在有优惠活动',
expectedDirection: 'service_to_customer'
}
];
let conversationLog = [];
conversation.forEach((msg, index) => {
const isFromService = msg.sender === 'service';
const senderId = isFromService ? mockUserInfo.serviceUser.id : mockUserInfo.customerUser.id;
const receiverId = isFromService ? mockUserInfo.customerUser.id : mockUserInfo.serviceUser.id;
const senderType = isFromService ? 'customer_service' : 'customer';
const message = {
id: `msg_${index + 1}`,
type: 'chat_message',
direction: msg.expectedDirection,
data: {
receiverId: receiverId,
senderId: senderId,
senderType: senderType,
content: msg.content,
contentType: 1,
timestamp: Date.now() + index
}
};
conversationLog.push({
role: isFromService ? '客服' : '客户',
content: msg.content,
direction: msg.expectedDirection
});
// 验证消息方向
if (message.direction !== msg.expectedDirection) {
console.log(` ✗ 消息${index + 1}方向错误: 期望${msg.expectedDirection}, 实际${message.direction}`);
}
});
// 打印对话流程
console.log(' 双向对话流程:');
conversationLog.forEach((msg, index) => {
console.log(` ${index + 1}. [${msg.role}] ${msg.content} (${msg.direction})`);
});
console.log(' ✓ 双向通信模式验证完成');
}
// 导出测试结果
module.exports = {
mockUserInfo,
testUserTypeDetection,
testWebSocketMessageFormat,
testMessageProcessing,
testBidirectionalCommunication
};