bug修复
This commit is contained in:
@@ -4,6 +4,177 @@
|
||||
*/
|
||||
|
||||
import MessageService from '@/services/message'
|
||||
import type { ChatMessage } from '@/types'
|
||||
|
||||
/**
|
||||
* 创建测试消息
|
||||
*/
|
||||
function createTestMessages(): ChatMessage[] {
|
||||
return [
|
||||
{
|
||||
id: '1',
|
||||
content: '用户消息 1',
|
||||
type: 'user',
|
||||
timestamp: '2025-07-26 22:09:10',
|
||||
status: 'sent'
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
content: 'AI 回复 1',
|
||||
type: 'ai',
|
||||
timestamp: '2025-07-26 22:09:15',
|
||||
status: 'sent'
|
||||
},
|
||||
{
|
||||
id: '3',
|
||||
content: '用户消息 2',
|
||||
type: 'user',
|
||||
timestamp: '2025-07-26 22:09:20',
|
||||
status: 'sent'
|
||||
},
|
||||
{
|
||||
id: '4',
|
||||
content: 'AI 回复 2',
|
||||
type: 'ai',
|
||||
timestamp: '2025-07-26 22:09:25',
|
||||
status: 'sent'
|
||||
},
|
||||
{
|
||||
id: '5',
|
||||
content: '用户消息 3',
|
||||
type: 'user',
|
||||
timestamp: '2025-07-26 22:09:30',
|
||||
status: 'sent'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建乱序的测试消息
|
||||
*/
|
||||
function createDisorderedMessages(): ChatMessage[] {
|
||||
const messages = createTestMessages()
|
||||
// 打乱顺序
|
||||
return [messages[4], messages[1], messages[3], messages[0], messages[2]]
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建包含重复消息的测试数据
|
||||
*/
|
||||
function createDuplicateMessages(): ChatMessage[] {
|
||||
const messages = createTestMessages()
|
||||
// 添加重复消息
|
||||
return [...messages, messages[0], messages[2]]
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试基本排序
|
||||
*/
|
||||
export function testBasicSort(): void {
|
||||
console.log('🧪 测试 1: 基本排序')
|
||||
console.log('='.repeat(50))
|
||||
|
||||
const messages = createDisorderedMessages()
|
||||
console.log('原始消息顺序:')
|
||||
messages.forEach((msg, idx) => {
|
||||
console.log(` ${idx + 1}. [${msg.type}] ${msg.content} (${msg.timestamp})`)
|
||||
})
|
||||
|
||||
const sorted = MessageService.sortAndDeduplicateMessages(messages)
|
||||
console.log('\n排序后的消息顺序:')
|
||||
sorted.forEach((msg, idx) => {
|
||||
console.log(` ${idx + 1}. [${msg.type}] ${msg.content} (${msg.timestamp})`)
|
||||
})
|
||||
|
||||
// 验证排序结果
|
||||
const isCorrect = sorted.every((msg, idx) => {
|
||||
if (idx === 0) return true
|
||||
const prevTime = MessageService.parseTimestamp(sorted[idx - 1].timestamp)
|
||||
const currTime = MessageService.parseTimestamp(msg.timestamp)
|
||||
return prevTime <= currTime
|
||||
})
|
||||
|
||||
console.log(`\n✅ 排序结果: ${isCorrect ? '正确' : '错误'}`)
|
||||
console.log('='.repeat(50))
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试去重
|
||||
*/
|
||||
export function testDeduplication(): void {
|
||||
console.log('🧪 测试 2: 去重')
|
||||
console.log('='.repeat(50))
|
||||
|
||||
const messages = createDuplicateMessages()
|
||||
console.log(`原始消息数量: ${messages.length}`)
|
||||
console.log('原始消息:')
|
||||
messages.forEach((msg, idx) => {
|
||||
console.log(` ${idx + 1}. [${msg.type}] ${msg.content} (ID: ${msg.id})`)
|
||||
})
|
||||
|
||||
const deduped = MessageService.sortAndDeduplicateMessages(messages)
|
||||
console.log(`\n去重后消息数量: ${deduped.length}`)
|
||||
console.log('去重后的消息:')
|
||||
deduped.forEach((msg, idx) => {
|
||||
console.log(` ${idx + 1}. [${msg.type}] ${msg.content} (ID: ${msg.id})`)
|
||||
})
|
||||
|
||||
// 验证去重结果
|
||||
const ids = new Set(deduped.map(m => m.id))
|
||||
const isCorrect = ids.size === deduped.length
|
||||
|
||||
console.log(`\n✅ 去重结果: ${isCorrect ? '正确' : '错误'}`)
|
||||
console.log('='.repeat(50))
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试时间解析
|
||||
*/
|
||||
export function testTimeParser(): void {
|
||||
console.log('🧪 测试 3: 时间解析')
|
||||
console.log('='.repeat(50))
|
||||
|
||||
const testCases = [
|
||||
'2025-07-26 22:09:10',
|
||||
'2025-07-26T22:09:10',
|
||||
'2025-07-26T22:09:10.000Z',
|
||||
new Date('2025-07-26T22:09:10'),
|
||||
1721999350000
|
||||
]
|
||||
|
||||
testCases.forEach((timestamp, idx) => {
|
||||
const parsed = MessageService.parseTimestamp(timestamp as any)
|
||||
console.log(` ${idx + 1}. ${JSON.stringify(timestamp)} -> ${parsed}`)
|
||||
})
|
||||
|
||||
console.log('\n✅ 时间解析完成')
|
||||
console.log('='.repeat(50))
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行所有排序测试
|
||||
*/
|
||||
export function runSortTests(): void {
|
||||
console.log('\n')
|
||||
console.log('╔' + '═'.repeat(48) + '╗')
|
||||
console.log('║' + ' '.repeat(10) + '消息排序功能测试' + ' '.repeat(22) + '║')
|
||||
console.log('╚' + '═'.repeat(48) + '╝')
|
||||
console.log('\n')
|
||||
|
||||
testBasicSort()
|
||||
console.log('\n')
|
||||
|
||||
testDeduplication()
|
||||
console.log('\n')
|
||||
|
||||
testTimeParser()
|
||||
console.log('\n')
|
||||
|
||||
console.log('╔' + '═'.repeat(48) + '╗')
|
||||
console.log('║' + ' '.repeat(15) + '所有测试完成' + ' '.repeat(21) + '║')
|
||||
console.log('╚' + '═'.repeat(48) + '╝')
|
||||
console.log('\n')
|
||||
}
|
||||
|
||||
export const testMessageService = async () => {
|
||||
console.log('🧪 开始测试 MessageService...')
|
||||
@@ -27,7 +198,11 @@ export const testMessageService = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 在开发环境下可以在控制台调用 window.testMessageService() 进行测试
|
||||
// 在开发环境下可以在控制台调用这些函数进行测试
|
||||
if (typeof window !== 'undefined') {
|
||||
(window as any).testMessageService = testMessageService
|
||||
(window as any).testBasicSort = testBasicSort
|
||||
(window as any).testDeduplication = testDeduplication
|
||||
(window as any).testTimeParser = testTimeParser
|
||||
(window as any).runSortTests = runSortTests
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user