// Simple Test for AI News System import { AIServiceManager, AITranslationService, AIContentAnalysisService, type AIServiceConfig, type ContentInfo } from '../index.uts' /** * 简单的AI新闻系统测试 * 用于验证基本功能是否正常工作 */ export class SimpleAINewsTest { /** * 测试翻译服务基本功能 */ static async testTranslationService(): Promise { try { console.log('🧪 Testing Translation Service...') const config: AIServiceConfig = { openai: { apiKey: 'test-key', model: 'gpt-3.5-turbo', maxTokens: 1000, temperature: 0.7 } } const translationService = new AITranslationService(config) // 测试语言检测 const detection = await translationService.detectLanguage('Hello world') if (!detection.success) { console.error('❌ Language detection failed') return false } // 测试翻译功能 const translation = await translationService.translateText( 'Hello world', 'zh-CN', 'en' ) if (!translation.success) { console.error('❌ Translation failed:', translation.error) return false } console.log('✅ Translation service test passed') return true } catch (error) { console.error('❌ Translation service test failed:', error) return false } } /** * 测试内容分析服务基本功能 */ static async testAnalysisService(): Promise { try { console.log('🧪 Testing Content Analysis Service...') const config: AIServiceConfig = { openai: { apiKey: 'test-key', model: 'gpt-3.5-turbo', maxTokens: 1000, temperature: 0.7 } } const analysisService = new AIContentAnalysisService(config) const testContent = '今天是个好天气,阳光明媚,让人心情愉快。' const analysis = await analysisService.analyzeContent(testContent, { types: ['sentiment', 'keywords', 'readability'], language: 'zh-CN' }) if (!analysis.success) { console.error('❌ Content analysis failed:', analysis.error) return false } if (!analysis.data) { console.error('❌ Analysis data is missing') return false } // 检查基本结果 if (typeof analysis.data.sentimentScore !== 'number') { console.error('❌ Sentiment score is not a number') return false } if (!Array.isArray(analysis.data.keywords)) { console.error('❌ Keywords is not an array') return false } console.log('✅ Content analysis service test passed') return true } catch (error) { console.error('❌ Content analysis service test failed:', error) return false } } /** * 测试服务管理器基本功能 */ static async testServiceManager(): Promise { try { console.log('🧪 Testing Service Manager...') const config: AIServiceConfig = { openai: { apiKey: 'test-key', model: 'gpt-3.5-turbo', maxTokens: 1000, temperature: 0.7 }, costLimits: { dailyUSD: 100, monthlyUSD: 1000, perRequestUSD: 10 } } const serviceManager = new AIServiceManager(config) // 测试初始化 const initResult = await serviceManager.initialize() if (!initResult.success) { console.error('❌ Service manager initialization failed:', initResult.error) return false } // 测试服务获取 const translationService = serviceManager.getTranslationService() if (!translationService) { console.error('❌ Failed to get translation service') return false } const analysisService = serviceManager.getAnalysisService() if (!analysisService) { console.error('❌ Failed to get analysis service') return false } const chatService = serviceManager.getChatService() if (!chatService) { console.error('❌ Failed to get chat service') return false } const recommendationService = serviceManager.getRecommendationService() if (!recommendationService) { console.error('❌ Failed to get recommendation service') return false } // 测试提供商选择 const bestProvider = serviceManager.selectBestProvider() if (!bestProvider) { console.error('❌ Failed to select best provider') return false } // 测试成本检查 const costCheck = serviceManager.checkCostLimits(1.0) if (typeof costCheck !== 'boolean') { console.error('❌ Cost check failed') return false } // 获取统计信息 const stats = serviceManager.getManagerStatistics() if (!stats) { console.error('❌ Failed to get statistics') return false } // 清理 await serviceManager.shutdown() console.log('✅ Service manager test passed') return true } catch (error) { console.error('❌ Service manager test failed:', error) return false } } /** * 测试类型定义完整性 */ static testTypeDefinitions(): boolean { try { console.log('🧪 Testing Type Definitions...') // 测试基本类型是否可用 const testContent: ContentInfo = { id: 'test-123', title: '测试新闻', content: '这是一条测试新闻内容', originalLanguage: 'zh-CN', publishedAt: Date.now(), tags: ['测试'], keywords: ['测试', '新闻'], quality: 0.8, viewCount: 0, likeCount: 0, shareCount: 0, status: 'draft' } // 验证类型结构 if (typeof testContent.id !== 'string') { console.error('❌ ContentInfo.id type error') return false } if (typeof testContent.title !== 'string') { console.error('❌ ContentInfo.title type error') return false } if (!Array.isArray(testContent.tags)) { console.error('❌ ContentInfo.tags type error') return false } console.log('✅ Type definitions test passed') return true } catch (error) { console.error('❌ Type definitions test failed:', error) return false } } /** * 运行所有测试 */ static async runAllTests(): Promise { console.log('🚀 Starting AI News System Tests...') console.log('=====================================') const results: boolean[] = [] // 运行各项测试 results.push(this.testTypeDefinitions()) results.push(await this.testTranslationService()) results.push(await this.testAnalysisService()) results.push(await this.testServiceManager()) // 统计结果 const passedCount = results.filter(r => r).length const totalCount = results.length console.log('\n📊 Test Results:') console.log('================') console.log(`✅ Passed: ${passedCount}`) console.log(`❌ Failed: ${totalCount - passedCount}`) console.log(`📈 Success Rate: ${((passedCount / totalCount) * 100).toFixed(1)}%`) if (passedCount === totalCount) { console.log('\n🎉 All tests passed! AI News System is working correctly.') return true } else { console.log('\n💥 Some tests failed. Please check the errors above.') return false } } } // 导出测试运行函数 export async function runSimpleTests(): Promise { return await SimpleAINewsTest.runAllTests() } // 如果直接运行此文件,执行测试 if (typeof require !== 'undefined' && require.main === module) { runSimpleTests().then(success => { process.exit(success ? 0 : 1) }).catch(error => { console.error('Test execution failed:', error) process.exit(1) }) }