432 lines
14 KiB
Plaintext
432 lines
14 KiB
Plaintext
/**
|
|
* 消息系统集成测试
|
|
* 用于验证消息系统各项功能是否正常工作
|
|
*/
|
|
|
|
import { MsgDataServiceReal } from '@/utils/msgDataServiceReal.uts'
|
|
import supa from '@/components/supadb/aksupainstance.uts'
|
|
import type {
|
|
MessageQueryParams,
|
|
SendMessageParams,
|
|
MessageType,
|
|
Message
|
|
} from '@/utils/msgTypes.uts'
|
|
|
|
/**
|
|
* 测试结果类型
|
|
*/
|
|
type TestResult = {
|
|
name: string
|
|
passed: boolean
|
|
message: string
|
|
duration: number
|
|
}
|
|
|
|
/**
|
|
* 消息系统集成测试类
|
|
*/
|
|
export class MessageSystemIntegrationTest {
|
|
private results: Array<TestResult> = []
|
|
private testUserId: string = 'test-user-id'
|
|
|
|
/**
|
|
* 运行所有测试
|
|
*/
|
|
async runAllTests(): Promise<Array<TestResult>> {
|
|
console.log('Starting Message System Integration Tests...')
|
|
|
|
this.results = []
|
|
|
|
// 检查初始化
|
|
await this.testSupabaseInitialization()
|
|
|
|
// 测试消息类型
|
|
await this.testGetMessageTypes()
|
|
|
|
// 测试发送消息
|
|
await this.testSendMessage()
|
|
|
|
// 测试获取消息列表
|
|
await this.testGetMessages()
|
|
|
|
// 测试消息详情
|
|
await this.testGetMessageById()
|
|
|
|
// 测试标记已读
|
|
await this.testMarkAsRead()
|
|
|
|
// 测试批量操作
|
|
await this.testBatchOperations()
|
|
|
|
// 测试统计功能
|
|
await this.testGetStats()
|
|
|
|
// 测试搜索功能
|
|
await this.testSearchUsers()
|
|
|
|
// 测试群组功能
|
|
await this.testGetGroups()
|
|
|
|
this.printResults()
|
|
return this.results
|
|
}
|
|
|
|
/**
|
|
* 执行单个测试
|
|
*/
|
|
private async runTest(testName: string, testFunction: () => Promise<void>): Promise<void> {
|
|
const startTime = Date.now()
|
|
|
|
try {
|
|
await testFunction()
|
|
const duration = Date.now() - startTime
|
|
this.results.push({
|
|
name: testName,
|
|
passed: true,
|
|
message: 'Test passed',
|
|
duration
|
|
})
|
|
console.log(`✓ ${testName} - ${duration}ms`)
|
|
} catch (error) {
|
|
const duration = Date.now() - startTime
|
|
const message = error instanceof Error ? error.message : 'Unknown error'
|
|
this.results.push({
|
|
name: testName,
|
|
passed: false,
|
|
message,
|
|
duration
|
|
})
|
|
console.error(`✗ ${testName} - ${message} - ${duration}ms`)
|
|
}
|
|
}
|
|
/**
|
|
* 测试 Supabase 初始化
|
|
*/ private async testSupabaseInitialization(): Promise<void> {
|
|
await this.runTest('Supabase Initialization', async () => {
|
|
// MsgDataServiceReal 现在直接使用 aksupainstance.uts 中的 supa 实例
|
|
// 无需手动初始化
|
|
|
|
// 测试获取会话
|
|
const session = supa.getSession()
|
|
if (session.user === null) {
|
|
console.warn('No active session - this is expected for anonymous users')
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 测试获取消息类型
|
|
*/
|
|
private async testGetMessageTypes(): Promise<void> {
|
|
await this.runTest('Get Message Types', async () => {
|
|
const response = await MsgDataServiceReal.getMessageTypes()
|
|
|
|
if (!response.success) {
|
|
throw new Error(`API Error: ${response.message}`)
|
|
}
|
|
|
|
if (response.data === null || response.data.length === 0) {
|
|
throw new Error('No message types returned')
|
|
}
|
|
|
|
// 验证数据结构
|
|
const firstType = response.data[0]
|
|
if (!firstType.id || !firstType.code || !firstType.name) {
|
|
throw new Error('Invalid message type structure')
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 测试发送消息
|
|
*/
|
|
private async testSendMessage(): Promise<void> {
|
|
await this.runTest('Send Message', async () => {
|
|
const params: SendMessageParams = {
|
|
message_type_id: '1',
|
|
sender_type: 'user',
|
|
sender_id: this.testUserId,
|
|
sender_name: 'Test User',
|
|
receiver_type: 'user',
|
|
receiver_id: this.testUserId,
|
|
title: 'Test Message',
|
|
content: 'This is a test message',
|
|
content_type: 'text',
|
|
attachments: null,
|
|
media_urls: null,
|
|
metadata: null,
|
|
is_urgent: false,
|
|
scheduled_at: null,
|
|
expires_at: null,
|
|
receivers: [this.testUserId]
|
|
}
|
|
|
|
const response = await MsgDataServiceReal.sendMessage(params)
|
|
|
|
if (!response.success) {
|
|
throw new Error(`Send Message Error: ${response.message}`)
|
|
}
|
|
|
|
if (response.data === null) {
|
|
throw new Error('No message data returned')
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 测试获取消息列表
|
|
*/
|
|
private async testGetMessages(): Promise<void> {
|
|
await this.runTest('Get Messages', async () => {
|
|
const params: MessageQueryParams = {
|
|
page: 1,
|
|
limit: 10,
|
|
message_type_id: null,
|
|
receiver_id: this.testUserId,
|
|
keyword: null,
|
|
status: null,
|
|
is_urgent: null
|
|
}
|
|
|
|
const response = await MsgDataServiceReal.getMessages(params)
|
|
|
|
if (!response.success) {
|
|
throw new Error(`Get Messages Error: ${response.message}`)
|
|
}
|
|
|
|
if (response.data === null) {
|
|
throw new Error('No paged response returned')
|
|
}
|
|
|
|
// 验证分页结构
|
|
if (!Array.isArray(response.data.items)) {
|
|
throw new Error('Invalid items array')
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 测试获取消息详情
|
|
*/
|
|
private async testGetMessageById(): Promise<void> {
|
|
await this.runTest('Get Message By ID', async () => {
|
|
// 首先获取一条消息ID
|
|
const listResponse = await MsgDataServiceReal.getMessages({
|
|
page: 1,
|
|
limit: 1,
|
|
message_type_id: null,
|
|
receiver_id: this.testUserId,
|
|
keyword: null,
|
|
status: null,
|
|
is_urgent: null
|
|
})
|
|
|
|
if (!listResponse.success || listResponse.data === null || listResponse.data.items.length === 0) {
|
|
console.warn('No messages found for detail test')
|
|
return
|
|
}
|
|
|
|
const messageId = listResponse.data.items[0].id
|
|
const response = await MsgDataServiceReal.getMessageById(messageId)
|
|
|
|
if (!response.success) {
|
|
throw new Error(`Get Message Detail Error: ${response.message}`)
|
|
}
|
|
|
|
if (response.data === null) {
|
|
throw new Error('No message detail returned')
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 测试标记已读
|
|
*/
|
|
private async testMarkAsRead(): Promise<void> {
|
|
await this.runTest('Mark As Read', async () => {
|
|
// 首先获取一条未读消息
|
|
const listResponse = await MsgDataServiceReal.getMessages({
|
|
page: 1,
|
|
limit: 1,
|
|
message_type_id: null,
|
|
receiver_id: this.testUserId,
|
|
keyword: null,
|
|
status: 'unread',
|
|
is_urgent: null
|
|
})
|
|
|
|
if (!listResponse.success || listResponse.data === null || listResponse.data.items.length === 0) {
|
|
console.warn('No unread messages found for mark as read test')
|
|
return
|
|
}
|
|
|
|
const messageId = listResponse.data.items[0].id
|
|
const response = await MsgDataServiceReal.markAsRead(messageId, this.testUserId)
|
|
|
|
if (!response.success) {
|
|
throw new Error(`Mark As Read Error: ${response.message}`)
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 测试批量操作
|
|
*/
|
|
private async testBatchOperations(): Promise<void> {
|
|
await this.runTest('Batch Operations', async () => {
|
|
// 获取一些消息ID
|
|
const listResponse = await MsgDataServiceReal.getMessages({
|
|
page: 1,
|
|
limit: 3,
|
|
message_type_id: null,
|
|
receiver_id: this.testUserId,
|
|
keyword: null,
|
|
status: null,
|
|
is_urgent: null
|
|
})
|
|
|
|
if (!listResponse.success || listResponse.data === null || listResponse.data.items.length === 0) {
|
|
console.warn('No messages found for batch operations test')
|
|
return
|
|
}
|
|
|
|
const messageIds = listResponse.data.items.map(msg => msg.id)
|
|
|
|
// 测试批量标记已读
|
|
const readResponse = await MsgDataServiceReal.batchOperation(messageIds, 'read', this.testUserId)
|
|
if (!readResponse.success) {
|
|
throw new Error(`Batch Read Error: ${readResponse.message}`)
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 测试统计功能
|
|
*/
|
|
private async testGetStats(): Promise<void> {
|
|
await this.runTest('Get Statistics', async () => {
|
|
const response = await MsgDataServiceReal.getMessageStats(this.testUserId)
|
|
|
|
if (!response.success) {
|
|
throw new Error(`Get Stats Error: ${response.message}`)
|
|
}
|
|
|
|
if (response.data === null) {
|
|
throw new Error('No stats data returned')
|
|
}
|
|
|
|
// 验证统计数据结构
|
|
const stats = response.data
|
|
if (typeof stats.total !== 'number' ||
|
|
typeof stats.unread !== 'number' ||
|
|
typeof stats.urgent !== 'number' ||
|
|
typeof stats.read !== 'number') {
|
|
throw new Error('Invalid stats data structure')
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 测试搜索用户
|
|
*/
|
|
private async testSearchUsers(): Promise<void> {
|
|
await this.runTest('Search Users', async () => {
|
|
const response = await MsgDataServiceReal.searchUsers('test')
|
|
|
|
if (!response.success) {
|
|
throw new Error(`Search Users Error: ${response.message}`)
|
|
}
|
|
|
|
if (response.data === null) {
|
|
throw new Error('No user search results returned')
|
|
}
|
|
|
|
// 验证用户数据结构
|
|
if (response.data.length > 0) {
|
|
const firstUser = response.data[0]
|
|
if (!firstUser.id || !firstUser.name) {
|
|
throw new Error('Invalid user option structure')
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 测试获取群组
|
|
*/
|
|
private async testGetGroups(): Promise<void> {
|
|
await this.runTest('Get Groups', async () => {
|
|
const response = await MsgDataServiceReal.getGroups(this.testUserId)
|
|
|
|
if (!response.success) {
|
|
throw new Error(`Get Groups Error: ${response.message}`)
|
|
}
|
|
|
|
if (response.data === null) {
|
|
throw new Error('No groups data returned')
|
|
}
|
|
|
|
// 验证群组数据结构
|
|
if (response.data.length > 0) {
|
|
const firstGroup = response.data[0]
|
|
if (!firstGroup.id || !firstGroup.name) {
|
|
throw new Error('Invalid group option structure')
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 打印测试结果
|
|
*/
|
|
private printResults(): void {
|
|
console.log('\n=== Message System Integration Test Results ===')
|
|
|
|
const passed = this.results.filter(r => r.passed).length
|
|
const total = this.results.length
|
|
const totalTime = this.results.reduce((sum, r) => sum + r.duration, 0)
|
|
|
|
console.log(`Total Tests: ${total}`)
|
|
console.log(`Passed: ${passed}`)
|
|
console.log(`Failed: ${total - passed}`)
|
|
console.log(`Total Time: ${totalTime}ms`)
|
|
console.log(`Success Rate: ${((passed / total) * 100).toFixed(2)}%`)
|
|
|
|
// 显示失败的测试
|
|
const failed = this.results.filter(r => !r.passed)
|
|
if (failed.length > 0) {
|
|
console.log('\n=== Failed Tests ===')
|
|
failed.forEach(test => {
|
|
console.log(`✗ ${test.name}: ${test.message}`)
|
|
})
|
|
}
|
|
|
|
console.log('\n=== Test Summary ===')
|
|
this.results.forEach(test => {
|
|
const status = test.passed ? '✓' : '✗'
|
|
console.log(`${status} ${test.name} - ${test.duration}ms`)
|
|
})
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 运行集成测试的便捷函数
|
|
*/
|
|
export async function runMessageSystemTests(): Promise<Array<TestResult>> {
|
|
const tester = new MessageSystemIntegrationTest()
|
|
return await tester.runAllTests()
|
|
}
|
|
|
|
/**
|
|
* 在页面中运行测试的示例
|
|
*
|
|
* // 在任意页面的 script 中调用
|
|
* import { runMessageSystemTests } from '@/utils/msgSystemTest.uts'
|
|
*
|
|
* // 在某个按钮点击事件中
|
|
* async function testMessageSystem() {
|
|
* const results = await runMessageSystemTests()
|
|
* console.log('Test completed:', results)
|
|
* }
|
|
*/
|