| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- import fs from 'fs';
- import path from 'path';
- import { fileURLToPath } from 'url';
- const __filename = fileURLToPath(import.meta.url);
- const __dirname = path.dirname(__filename);
- // 测试配置
- const TEST_CONFIG = {
- routerFile: path.join(__dirname, '..', 'src', 'router.tsx'),
- navFile: path.join(__dirname, '..', 'src', 'pages', 'layout', 'components', 'Nav.tsx'),
- layoutFile: path.join(__dirname, '..', 'src', 'pages', 'layout', 'index.tsx'),
- };
- // 测试结果收集器
- class TestResultCollector {
- constructor() {
- this.results = [];
- this.passed = 0;
- this.failed = 0;
- }
- addResult(testName, passed, details = '') {
- const result = { testName, passed, details, timestamp: new Date() };
- this.results.push(result);
- if (passed) {
- this.passed++;
- } else {
- this.failed++;
- }
- }
- printResults() {
- console.log('\n📊 智谱AI路由测试结果:');
- console.log(` 总测试数: ${this.results.length}`);
- console.log(` 通过: ${this.passed}`);
- console.log(` 失败: ${this.failed}`);
- console.log(` 成功率: ${this.results.length > 0 ? (this.passed / this.results.length * 100).toFixed(2) : 0}%`);
-
- console.log('\n📋 详细结果:');
- this.results.forEach((result, index) => {
- const status = result.passed ? '✅' : '❌';
- console.log(` ${index + 1}. ${status} ${result.testName}`);
- if (!result.passed && result.details) {
- console.log(` 详情: ${result.details}`);
- }
- });
- }
- }
- // 测试工具函数
- class TestUtils {
- static readFile(filePath) {
- try {
- return fs.readFileSync(filePath, 'utf8');
- } catch (error) {
- return null;
- }
- }
- static countOccurrences(text, pattern) {
- const matches = text.match(new RegExp(pattern, 'g'));
- return matches ? matches.length : 0;
- }
- static findPattern(text, pattern) {
- const matches = text.match(new RegExp(pattern, 'g'));
- return matches || [];
- }
- }
- // 智谱AI路由测试
- class ZhipuRoutingTests {
- constructor() {
- this.collector = new TestResultCollector();
- }
- // 测试1: 验证路由配置中的智谱AI路径
- testRouterZhipuPaths() {
- console.log('\n🛣️ 测试路由配置中的智谱AI路径...');
-
- const routerContent = TestUtils.readFile(TEST_CONFIG.routerFile);
- if (!routerContent) {
- this.collector.addResult('路由文件可读', false, '无法读取路由文件');
- return;
- }
- // 测试智谱AI路由路径
- const zhipuPaths = [
- '/zhipu/questionAnswer',
- '/zhipu/knowledgeLib',
- '/zhipu/dataExport'
- ];
- zhipuPaths.forEach(path => {
- const count = TestUtils.countOccurrences(routerContent, path.replace('/', '\\/'));
- this.collector.addResult(
- `路由路径 ${path} 存在`,
- count > 0,
- count === 0 ? `未找到路由路径: ${path}` : ''
- );
- });
- // 测试智谱AI导入路径
- const zhipuImports = [
- '@/pages/platforms/zhipu/questionAnswer/list/index',
- '@/pages/platforms/zhipu/questionAnswer/info/index',
- '@/pages/platforms/zhipu/knowledgeLib/list/index',
- '@/pages/platforms/zhipu/knowledgeLib/detail/index',
- '@/pages/platforms/zhipu/dataExport/index'
- ];
- zhipuImports.forEach(importPath => {
- const count = TestUtils.countOccurrences(routerContent, importPath.replace(/\//g, '\\/'));
- this.collector.addResult(
- `导入路径 ${importPath} 存在`,
- count > 0,
- count === 0 ? `未找到导入路径: ${importPath}` : ''
- );
- });
- }
- // 测试2: 验证导航组件中的智谱AI路径
- testNavZhipuPaths() {
- console.log('\n🧭 测试导航组件中的智谱AI路径...');
-
- const navContent = TestUtils.readFile(TEST_CONFIG.navFile);
- if (!navContent) {
- this.collector.addResult('导航文件可读', false, '无法读取导航文件');
- return;
- }
- // 测试智谱AI菜单项
- const zhipuMenuItems = [
- '/zhipu/questionAnswer',
- '/zhipu/knowledgeLib',
- '/zhipu/dataExport'
- ];
- zhipuMenuItems.forEach(path => {
- const count = TestUtils.countOccurrences(navContent, path.replace('/', '\\/'));
- this.collector.addResult(
- `导航菜单项 ${path} 存在`,
- count > 0,
- count === 0 ? `未找到导航菜单项: ${path}` : ''
- );
- });
- // 测试智谱AI路由跳转
- const zhipuNavigations = [
- 'router\\.navigate\\(\\s*\\{\\s*pathname:\\s*[\'"]/zhipu/questionAnswer[\'"]\\s*\\}\\)',
- 'router\\.navigate\\(\\s*\\{\\s*pathname:\\s*[\'"]/zhipu/knowledgeLib[\'"]\\s*\\}\\)',
- 'router\\.navigate\\(\\s*\\{\\s*pathname:\\s*[\'"]/zhipu/dataExport[\'"]\\s*\\}\\)'
- ];
- zhipuNavigations.forEach(navPattern => {
- const count = TestUtils.countOccurrences(navContent, navPattern);
- this.collector.addResult(
- `路由跳转 ${navPattern} 存在`,
- count > 0,
- count === 0 ? `未找到路由跳转: ${navPattern}` : ''
- );
- });
- }
- // 测试3: 验证布局组件中的智谱AI默认路径
- testLayoutZhipuDefaultPath() {
- console.log('\n🏗️ 测试布局组件中的智谱AI默认路径...');
-
- const layoutContent = TestUtils.readFile(TEST_CONFIG.layoutFile);
- if (!layoutContent) {
- this.collector.addResult('布局文件可读', false, '无法读取布局文件');
- return;
- }
- // 测试智谱AI默认路径
- const zhipuDefaultPath = '/zhipu/questionAnswer';
- const count = TestUtils.countOccurrences(layoutContent, zhipuDefaultPath.replace('/', '\\/'));
- this.collector.addResult(
- `智谱AI默认路径 ${zhipuDefaultPath} 存在`,
- count > 0,
- count === 0 ? `未找到智谱AI默认路径: ${zhipuDefaultPath}` : ''
- );
- // 测试菜单类型切换逻辑
- const menuTypeLogic = 'value === 1 \\? \'/deepseek/questionAnswer\' : \'/zhipu/questionAnswer\'';
- const logicCount = TestUtils.countOccurrences(layoutContent, menuTypeLogic);
- this.collector.addResult(
- '菜单类型切换逻辑正确',
- logicCount > 0,
- logicCount === 0 ? '未找到正确的菜单类型切换逻辑' : ''
- );
- }
- // 测试4: 验证没有遗留的旧路径
- testNoLegacyPaths() {
- console.log('\n🚫 测试没有遗留的旧路径...');
-
- const routerContent = TestUtils.readFile(TEST_CONFIG.routerFile);
- const navContent = TestUtils.readFile(TEST_CONFIG.navFile);
- const layoutContent = TestUtils.readFile(TEST_CONFIG.layoutFile);
-
- const allContent = [routerContent, navContent, layoutContent].filter(Boolean).join('\n');
- // 检查旧路径
- const oldPaths = [
- '/questionAnswer',
- '/knowledgeLib',
- '/dataExport'
- ];
- oldPaths.forEach(path => {
- const count = TestUtils.countOccurrences(allContent, path.replace('/', '\\/'));
- this.collector.addResult(
- `没有遗留旧路径 ${path}`,
- count === 0,
- count > 0 ? `发现${count}个遗留的旧路径: ${path}` : ''
- );
- });
- // 检查旧导入路径
- const oldImports = [
- '@/pages/questionAnswer',
- '@/pages/knowledgeLib',
- '@/pages/dataExport'
- ];
- oldImports.forEach(importPath => {
- const count = TestUtils.countOccurrences(allContent, importPath.replace(/\//g, '\\/'));
- this.collector.addResult(
- `没有遗留旧导入路径 ${importPath}`,
- count === 0,
- count > 0 ? `发现${count}个遗留的旧导入路径: ${importPath}` : ''
- );
- });
- }
- // 测试5: 验证路由完整性
- testRouteCompleteness() {
- console.log('\n✅ 测试路由完整性...');
-
- const routerContent = TestUtils.readFile(TEST_CONFIG.routerFile);
- if (!routerContent) {
- this.collector.addResult('路由文件可读', false, '无法读取路由文件');
- return;
- }
- // 检查所有必要的路由都存在
- const requiredRoutes = [
- { path: '/zhipu/questionAnswer', name: '智谱AI问答应用' },
- { path: '/zhipu/questionAnswer/create', name: '智谱AI创建应用' },
- { path: '/zhipu/questionAnswer/modify', name: '智谱AI修改应用' },
- { path: '/zhipu/knowledgeLib', name: '智谱AI知识库' },
- { path: '/zhipu/knowledgeLib/:knowledgeId', name: '智谱AI知识库详情' },
- { path: '/zhipu/dataExport', name: '智谱AI数据导出' }
- ];
- requiredRoutes.forEach(route => {
- const count = TestUtils.countOccurrences(routerContent, route.path.replace(/\//g, '\\/'));
- this.collector.addResult(
- `路由 ${route.name} 存在`,
- count > 0,
- count === 0 ? `未找到路由: ${route.path}` : ''
- );
- });
- // 检查面包屑配置
- const breadcrumbCount = TestUtils.countOccurrences(routerContent, 'breadcrumbName');
- this.collector.addResult(
- '面包屑配置完整',
- breadcrumbCount >= 6,
- breadcrumbCount < 6 ? `面包屑配置不足,期望至少6个,实际${breadcrumbCount}个` : ''
- );
- }
- // 运行所有测试
- runAllTests() {
- console.log('🚀 开始智谱AI路由测试...\n');
-
- this.testRouterZhipuPaths();
- this.testNavZhipuPaths();
- this.testLayoutZhipuDefaultPath();
- this.testNoLegacyPaths();
- this.testRouteCompleteness();
- // 输出结果
- this.collector.printResults();
- // 返回测试结果
- const summary = { passed: this.collector.passed, failed: this.collector.failed, total: this.collector.results.length };
- if (summary.failed > 0) {
- console.log('\n⚠️ 部分测试失败,智谱AI路由可能存在问题。');
- process.exit(1);
- } else {
- console.log('\n🎉 所有测试通过!智谱AI路由配置正确。');
- }
- }
- }
- // 运行测试
- const tests = new ZhipuRoutingTests();
- tests.runAllTests();
|