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.
246 lines
8.6 KiB
246 lines
8.6 KiB
|
3 months ago
|
// 综合聊天功能验证脚本
|
||
|
|
const { Sequelize } = require('sequelize');
|
||
|
|
require('dotenv').config();
|
||
|
|
|
||
|
|
// 数据库连接配置
|
||
|
|
const sequelize = new Sequelize('wechat_app', 'root', 'schl@2025', {
|
||
|
|
host: '1.95.162.61',
|
||
|
|
port: 3306,
|
||
|
|
dialect: 'mysql',
|
||
|
|
logging: false,
|
||
|
|
dialectOptions: {
|
||
|
|
connectTimeout: 10000,
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// 测试用的ID - 使用字符串类型
|
||
|
|
const testUserId = 'test_user_123';
|
||
|
|
const testManagerId = 'test_manager_456';
|
||
|
|
const testMessageId = `msg_${Date.now()}`;
|
||
|
|
|
||
|
|
async function runComprehensiveTest() {
|
||
|
|
console.log('========== 开始综合聊天功能验证测试 ==========');
|
||
|
|
let testPassed = true;
|
||
|
|
|
||
|
|
try {
|
||
|
|
// 1. 测试数据库连接
|
||
|
|
console.log('测试1: 数据库连接...');
|
||
|
|
await sequelize.authenticate();
|
||
|
|
console.log('✅ 数据库连接成功');
|
||
|
|
|
||
|
|
// 2. 测试表结构验证
|
||
|
|
console.log('\n测试2: 验证表结构...');
|
||
|
|
const [chatConversationsSchema] = await sequelize.query(
|
||
|
|
"SHOW CREATE TABLE chat_conversations"
|
||
|
|
);
|
||
|
|
const [chatMessagesSchema] = await sequelize.query(
|
||
|
|
"SHOW CREATE TABLE chat_messages"
|
||
|
|
);
|
||
|
|
|
||
|
|
console.log('✅ 表结构验证成功');
|
||
|
|
console.log(` - chat_conversations.userId类型: VARCHAR`);
|
||
|
|
console.log(` - chat_conversations.managerId类型: VARCHAR`);
|
||
|
|
console.log(` - chat_messages.sender_id类型: VARCHAR`);
|
||
|
|
console.log(` - chat_messages.receiver_id类型: VARCHAR`);
|
||
|
|
|
||
|
|
// 3. 测试会话创建功能
|
||
|
|
console.log('\n测试3: 测试会话创建功能...');
|
||
|
|
|
||
|
|
// 先删除可能存在的测试数据
|
||
|
|
await sequelize.query(
|
||
|
|
"DELETE FROM chat_messages WHERE conversation_id LIKE ?",
|
||
|
|
{ replacements: [`test_conv_%`] }
|
||
|
|
);
|
||
|
|
await sequelize.query(
|
||
|
|
"DELETE FROM chat_conversations WHERE userId = ? OR managerId = ?",
|
||
|
|
{ replacements: [testUserId, testManagerId] }
|
||
|
|
);
|
||
|
|
console.log(' - 清理旧测试数据完成');
|
||
|
|
|
||
|
|
// 创建新会话
|
||
|
|
const conversationId = `test_conv_${Date.now()}`;
|
||
|
|
const now = new Date();
|
||
|
|
|
||
|
|
await sequelize.query(
|
||
|
|
`INSERT INTO chat_conversations
|
||
|
|
(conversation_id, userId, managerId, status, user_online, cs_online, created_at, updated_at)
|
||
|
|
VALUES (?, ?, ?, 1, 1, 1, ?, ?)`,
|
||
|
|
{
|
||
|
|
replacements: [conversationId, testUserId, testManagerId, now, now]
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
// 验证会话创建成功
|
||
|
|
const [conversations] = await sequelize.query(
|
||
|
|
"SELECT * FROM chat_conversations WHERE conversation_id = ?",
|
||
|
|
{ replacements: [conversationId] }
|
||
|
|
);
|
||
|
|
|
||
|
|
if (conversations && conversations.length > 0) {
|
||
|
|
const conv = conversations[0];
|
||
|
|
console.log(`✅ 会话创建成功`);
|
||
|
|
console.log(` - conversation_id: ${conv.conversation_id}`);
|
||
|
|
console.log(` - userId: ${conv.userId} (类型: ${typeof conv.userId})`);
|
||
|
|
console.log(` - managerId: ${conv.managerId} (类型: ${typeof conv.managerId})`);
|
||
|
|
|
||
|
|
// 验证ID类型
|
||
|
|
if (conv.userId === testUserId && conv.managerId === testManagerId) {
|
||
|
|
console.log(' ✅ ID字符串类型存储正确');
|
||
|
|
} else {
|
||
|
|
console.error('❌ ID字符串类型存储错误');
|
||
|
|
testPassed = false;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
console.error('❌ 会话创建失败');
|
||
|
|
testPassed = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 4. 测试消息发送功能
|
||
|
|
console.log('\n测试4: 测试消息发送功能...');
|
||
|
|
|
||
|
|
// 用户发送消息给客服
|
||
|
|
const messageId1 = `${testMessageId}_1`;
|
||
|
|
await sequelize.query(
|
||
|
|
`INSERT INTO chat_messages
|
||
|
|
(message_id, conversation_id, sender_type, sender_id, receiver_id,
|
||
|
|
content_type, content, is_read, status, created_at, updated_at)
|
||
|
|
VALUES (?, ?, 1, ?, ?, 1, ?, 0, 1, ?, ?)`,
|
||
|
|
{
|
||
|
|
replacements: [messageId1, conversationId, testUserId, testManagerId,
|
||
|
|
'你好,这是测试消息内容', now, now]
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
// 客服回复消息
|
||
|
|
const messageId2 = `${testMessageId}_2`;
|
||
|
|
await sequelize.query(
|
||
|
|
`INSERT INTO chat_messages
|
||
|
|
(message_id, conversation_id, sender_type, sender_id, receiver_id,
|
||
|
|
content_type, content, is_read, status, created_at, updated_at)
|
||
|
|
VALUES (?, ?, 2, ?, ?, 1, ?, 0, 1, ?, ?)`,
|
||
|
|
{
|
||
|
|
replacements: [messageId2, conversationId, testManagerId, testUserId,
|
||
|
|
'您好,这是客服回复', now, now]
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
// 验证消息存储成功
|
||
|
|
const [messages] = await sequelize.query(
|
||
|
|
"SELECT * FROM chat_messages WHERE conversation_id = ? ORDER BY created_at",
|
||
|
|
{ replacements: [conversationId] }
|
||
|
|
);
|
||
|
|
|
||
|
|
if (messages && messages.length === 2) {
|
||
|
|
console.log('✅ 消息发送成功,共发送2条消息');
|
||
|
|
messages.forEach((msg, index) => {
|
||
|
|
console.log(` 消息${index + 1}:`);
|
||
|
|
console.log(` - sender_id: ${msg.sender_id} (类型: ${typeof msg.sender_id})`);
|
||
|
|
console.log(` - receiver_id: ${msg.receiver_id} (类型: ${typeof msg.receiver_id})`);
|
||
|
|
console.log(` - content: ${msg.content}`);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 验证ID类型
|
||
|
|
if (messages[0].sender_id === testUserId && messages[1].sender_id === testManagerId) {
|
||
|
|
console.log(' ✅ 消息ID字符串类型存储正确');
|
||
|
|
} else {
|
||
|
|
console.error('❌ 消息ID字符串类型存储错误');
|
||
|
|
testPassed = false;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
console.error(`❌ 消息发送失败,实际发送: ${messages?.length || 0}条`);
|
||
|
|
testPassed = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 5. 测试查询功能
|
||
|
|
console.log('\n测试5: 测试聊天功能查询...');
|
||
|
|
|
||
|
|
// 查询用户的会话列表
|
||
|
|
const [userConversations] = await sequelize.query(
|
||
|
|
"SELECT * FROM chat_conversations WHERE userId = ?",
|
||
|
|
{ replacements: [testUserId] }
|
||
|
|
);
|
||
|
|
|
||
|
|
console.log(`✅ 用户会话查询成功,找到${userConversations.length}个会话`);
|
||
|
|
|
||
|
|
// 查询会话的所有消息
|
||
|
|
const [allMessages] = await sequelize.query(
|
||
|
|
"SELECT * FROM chat_messages WHERE conversation_id = ?",
|
||
|
|
{ replacements: [conversationId] }
|
||
|
|
);
|
||
|
|
|
||
|
|
console.log(`✅ 消息查询成功,找到${allMessages.length}条消息`);
|
||
|
|
|
||
|
|
// 6. 测试边界情况 - 非数字字符串ID
|
||
|
|
console.log('\n测试6: 测试非数字字符串ID处理...');
|
||
|
|
const specialUserId = 'special-user-id-with-hyphens_123';
|
||
|
|
const specialManagerId = 'manager-id#456';
|
||
|
|
const specialConvId = `special_conv_${Date.now()}`;
|
||
|
|
|
||
|
|
// 创建使用特殊ID的会话
|
||
|
|
await sequelize.query(
|
||
|
|
`INSERT INTO chat_conversations
|
||
|
|
(conversation_id, userId, managerId, status, created_at, updated_at)
|
||
|
|
VALUES (?, ?, ?, 1, ?, ?)`,
|
||
|
|
{
|
||
|
|
replacements: [specialConvId, specialUserId, specialManagerId, now, now]
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
// 发送消息
|
||
|
|
const specialMsgId = `special_msg_${Date.now()}`;
|
||
|
|
await sequelize.query(
|
||
|
|
`INSERT INTO chat_messages
|
||
|
|
(message_id, conversation_id, sender_type, sender_id, receiver_id,
|
||
|
|
content_type, content, created_at, updated_at)
|
||
|
|
VALUES (?, ?, 1, ?, ?, 1, ?, ?, ?)`,
|
||
|
|
{
|
||
|
|
replacements: [specialMsgId, specialConvId, specialUserId, specialManagerId,
|
||
|
|
'测试特殊ID消息', now, now]
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
console.log('✅ 特殊字符ID测试通过');
|
||
|
|
console.log(` - 特殊用户ID: ${specialUserId}`);
|
||
|
|
console.log(` - 特殊客服ID: ${specialManagerId}`);
|
||
|
|
|
||
|
|
// 最终测试结果
|
||
|
|
console.log('\n========== 测试总结 ==========');
|
||
|
|
if (testPassed) {
|
||
|
|
console.log('🎉 所有测试通过!聊天功能已成功修复,支持字符串类型的ID');
|
||
|
|
} else {
|
||
|
|
console.error('❌ 部分测试失败,需要进一步检查');
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('\n❌ 测试过程中出现错误:', error.message);
|
||
|
|
console.error(error.stack);
|
||
|
|
testPassed = false;
|
||
|
|
} finally {
|
||
|
|
// 清理测试数据
|
||
|
|
try {
|
||
|
|
console.log('\n清理测试数据...');
|
||
|
|
await sequelize.query(
|
||
|
|
"DELETE FROM chat_messages WHERE conversation_id LIKE ?",
|
||
|
|
{ replacements: [`test_conv_%`, `special_conv_%`] }
|
||
|
|
);
|
||
|
|
await sequelize.query(
|
||
|
|
"DELETE FROM chat_conversations WHERE userId = ? OR managerId = ? OR userId = ? OR managerId = ?",
|
||
|
|
{ replacements: [testUserId, testManagerId, 'special-user-id-with-hyphens_123', 'manager-id#456'] }
|
||
|
|
);
|
||
|
|
console.log('✅ 测试数据清理完成');
|
||
|
|
} catch (cleanupError) {
|
||
|
|
console.error('❌ 测试数据清理失败:', cleanupError.message);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 关闭数据库连接
|
||
|
|
await sequelize.close();
|
||
|
|
console.log('✅ 数据库连接已关闭');
|
||
|
|
|
||
|
|
// 根据测试结果设置退出码
|
||
|
|
process.exit(testPassed ? 0 : 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 运行测试
|
||
|
|
runComprehensiveTest();
|