/** * Python Core 测试页面 * 用于测试增强版Python Core的各项功能 */ import React, { useState } from 'react' import { invoke } from '@tauri-apps/api/tauri' import { Play, CheckCircle, XCircle, Loader, RefreshCw } from 'lucide-react' interface PythonResponse { status: boolean msg: string data?: any } interface TestResult { name: string status: 'pending' | 'running' | 'success' | 'error' response?: PythonResponse error?: string duration?: number } const PythonCoreTestPage: React.FC = () => { const [testResults, setTestResults] = useState([]) const [isRunning, setIsRunning] = useState(false) const tests = [ { name: '基础功能测试', command: 'test_python_core_basic', description: '测试Python Core的基本功能和响应格式' }, { name: '模板管理测试', command: 'test_template_manager', description: '测试模板管理服务的功能' }, { name: '项目管理测试', command: 'test_project_manager', description: '测试项目管理服务的功能' }, { name: '文件管理测试', command: 'test_file_manager', description: '测试文件管理服务的功能', params: { path: '.' } }, { name: 'AI视频测试', command: 'test_ai_video', description: '测试AI视频生成环境和功能' }, { name: '资源分类测试', command: 'test_get_categories', description: '测试资源分类管理功能' }, { name: '批量导入测试', command: 'test_batch_import_templates', description: '测试模板批量导入功能', params: { folder_path: './templates' } }, { name: '创建项目测试', command: 'test_create_project', description: '测试项目创建功能', params: { name: 'Test Project', description: 'A test project' } } ] const runSingleTest = async (test: any, index: number) => { const startTime = Date.now() // 更新状态为运行中 setTestResults(prev => { const newResults = [...prev] newResults[index] = { name: test.name, status: 'running' } return newResults }) try { let response: PythonResponse if (test.params) { response = await invoke(test.command, test.params) } else { response = await invoke(test.command) } const duration = Date.now() - startTime // 更新状态为成功 setTestResults(prev => { const newResults = [...prev] newResults[index] = { name: test.name, status: 'success', response, duration } return newResults }) } catch (error) { const duration = Date.now() - startTime // 更新状态为错误 setTestResults(prev => { const newResults = [...prev] newResults[index] = { name: test.name, status: 'error', error: error as string, duration } return newResults }) } } const runAllTests = async () => { setIsRunning(true) // 初始化所有测试状态 setTestResults(tests.map(test => ({ name: test.name, status: 'pending' as const }))) // 逐个运行测试 for (let i = 0; i < tests.length; i++) { await runSingleTest(tests[i], i) // 在测试之间添加短暂延迟 await new Promise(resolve => setTimeout(resolve, 500)) } setIsRunning(false) } const runComprehensiveTest = async () => { setIsRunning(true) try { const startTime = Date.now() const results: PythonResponse[] = await invoke('test_all_python_core_functions') const duration = Date.now() - startTime // 将综合测试结果映射到各个测试 const mappedResults = tests.map((test, index) => ({ name: test.name, status: results[index]?.status ? 'success' : 'error' as const, response: results[index], duration: duration / tests.length // 平均分配时间 })) setTestResults(mappedResults) } catch (error) { console.error('Comprehensive test failed:', error) setTestResults(tests.map(test => ({ name: test.name, status: 'error' as const, error: error as string }))) } setIsRunning(false) } const clearResults = () => { setTestResults([]) } const getStatusIcon = (status: TestResult['status']) => { switch (status) { case 'pending': return
case 'running': return case 'success': return case 'error': return } } const getStatusColor = (status: TestResult['status']) => { switch (status) { case 'pending': return 'border-gray-200 bg-gray-50' case 'running': return 'border-blue-200 bg-blue-50' case 'success': return 'border-green-200 bg-green-50' case 'error': return 'border-red-200 bg-red-50' } } const successCount = testResults.filter(r => r.status === 'success').length const errorCount = testResults.filter(r => r.status === 'error').length const totalTests = testResults.length return (
{/* 页面标题 */}

Python Core 功能测试

测试增强版Python Core的各项功能,验证sidecar和模拟服务是否正常工作

{/* 控制按钮 */}
{/* 测试统计 */} {totalTests > 0 && (
总计: {totalTests} 成功: {successCount} 失败: {errorCount} 成功率: {totalTests > 0 ? Math.round((successCount / totalTests) * 100) : 0}%
)}
{/* 测试结果 */}
{tests.map((test, index) => { const result = testResults[index] return (
{result ? getStatusIcon(result.status) : getStatusIcon('pending')}

{test.name}

{test.description}

{result?.duration && ( {result.duration}ms )}
{/* 测试结果详情 */} {result && (
{result.status === 'success' && result.response && (
状态: {result.response.status ? '成功' : '失败'}
消息: {result.response.msg}
{result.response.data && (
查看数据
                              {JSON.stringify(result.response.data, null, 2)}
                            
)}
)} {result.status === 'error' && (
错误: {result.error}
)}
)}
) })}
) } export default PythonCoreTestPage