// 简单的聊天功能验证脚本 const mysql = require('mysql2/promise'); // 数据库连接配置 const config = { host: '1.95.162.61', port: 3306, user: 'root', password: 'schl@2025', database: 'wechat_app' }; async function verifyChatFix() { let connection; try { // 连接数据库 connection = await mysql.createConnection(config); console.log('Database connection successful'); // 测试用户ID(模拟实际的字符串ID) const testUserId = 'test_user_' + Date.now(); const testManagerId = '22'; const testConversationId = 'conv_' + Date.now(); console.log('\nTest user ID:', testUserId); console.log('Test manager ID:', testManagerId); // 1. 创建测试会话 console.log('\nCreating test conversation...'); await connection.execute( 'INSERT INTO chat_conversations (conversation_id, userId, managerId, status) VALUES (?, ?, ?, 1)', [testConversationId, testUserId, testManagerId] ); console.log('✓ Conversation created successfully'); // 2. 验证会话数据 const [conversations] = await connection.execute( 'SELECT * FROM chat_conversations WHERE conversation_id = ?', [testConversationId] ); if (conversations.length > 0) { const conv = conversations[0]; console.log('\nVerifying conversation data:'); console.log(' userId stored as:', conv.userId); console.log(' userId type:', typeof conv.userId); if (conv.userId === testUserId) { console.log('✓ String userId stored correctly'); } else { console.log('❌ userId mismatch!'); } } // 3. 测试查询功能 const [queryResult] = await connection.execute( 'SELECT * FROM chat_conversations WHERE userId = ? AND managerId = ?', [testUserId, testManagerId] ); console.log('\nQuery test result:', queryResult.length, 'records found'); // 4. 测试发送一条消息 const testMessage = 'Test message at ' + new Date().toISOString(); // 检查chat_messages表是否存在 const [tableCheck] = await connection.execute( "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'wechat_app' AND TABLE_NAME = 'chat_messages'" ); if (tableCheck.length > 0) { console.log('\nTesting message storage...'); await connection.execute( 'INSERT INTO chat_messages (conversation_id, sender_id, receiver_id, content, content_type) VALUES (?, ?, ?, ?, ?)', [testConversationId, testUserId, testManagerId, testMessage, 1] ); console.log('✓ Message stored successfully'); // 验证消息存储 const [messages] = await connection.execute( 'SELECT * FROM chat_messages WHERE conversation_id = ?', [testConversationId] ); console.log('Messages found:', messages.length); if (messages.length > 0) { console.log('Message sender_id:', messages[0].sender_id); console.log('Message content:', messages[0].content); } } // 5. 清理测试数据 console.log('\nCleaning up test data...'); if (tableCheck.length > 0) { await connection.execute('DELETE FROM chat_messages WHERE conversation_id = ?', [testConversationId]); } await connection.execute('DELETE FROM chat_conversations WHERE conversation_id = ?', [testConversationId]); console.log('✓ Test data cleaned up'); console.log('\n🎉 Chat functionality fix verified successfully!'); } catch (error) { console.error('Verification error:', error.message); } finally { if (connection) await connection.end(); } } // Run verification verifyChatFix();