zhipu-routing.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import fs from 'fs';
  2. import path from 'path';
  3. import { fileURLToPath } from 'url';
  4. const __filename = fileURLToPath(import.meta.url);
  5. const __dirname = path.dirname(__filename);
  6. // 测试配置
  7. const TEST_CONFIG = {
  8. routerFile: path.join(__dirname, '..', 'src', 'router.tsx'),
  9. navFile: path.join(__dirname, '..', 'src', 'pages', 'layout', 'components', 'Nav.tsx'),
  10. layoutFile: path.join(__dirname, '..', 'src', 'pages', 'layout', 'index.tsx'),
  11. };
  12. // 测试结果收集器
  13. class TestResultCollector {
  14. constructor() {
  15. this.results = [];
  16. this.passed = 0;
  17. this.failed = 0;
  18. }
  19. addResult(testName, passed, details = '') {
  20. const result = { testName, passed, details, timestamp: new Date() };
  21. this.results.push(result);
  22. if (passed) {
  23. this.passed++;
  24. } else {
  25. this.failed++;
  26. }
  27. }
  28. printResults() {
  29. console.log('\n📊 智谱AI路由测试结果:');
  30. console.log(` 总测试数: ${this.results.length}`);
  31. console.log(` 通过: ${this.passed}`);
  32. console.log(` 失败: ${this.failed}`);
  33. console.log(` 成功率: ${this.results.length > 0 ? (this.passed / this.results.length * 100).toFixed(2) : 0}%`);
  34. console.log('\n📋 详细结果:');
  35. this.results.forEach((result, index) => {
  36. const status = result.passed ? '✅' : '❌';
  37. console.log(` ${index + 1}. ${status} ${result.testName}`);
  38. if (!result.passed && result.details) {
  39. console.log(` 详情: ${result.details}`);
  40. }
  41. });
  42. }
  43. }
  44. // 测试工具函数
  45. class TestUtils {
  46. static readFile(filePath) {
  47. try {
  48. return fs.readFileSync(filePath, 'utf8');
  49. } catch (error) {
  50. return null;
  51. }
  52. }
  53. static countOccurrences(text, pattern) {
  54. const matches = text.match(new RegExp(pattern, 'g'));
  55. return matches ? matches.length : 0;
  56. }
  57. static findPattern(text, pattern) {
  58. const matches = text.match(new RegExp(pattern, 'g'));
  59. return matches || [];
  60. }
  61. }
  62. // 智谱AI路由测试
  63. class ZhipuRoutingTests {
  64. constructor() {
  65. this.collector = new TestResultCollector();
  66. }
  67. // 测试1: 验证路由配置中的智谱AI路径
  68. testRouterZhipuPaths() {
  69. console.log('\n🛣️ 测试路由配置中的智谱AI路径...');
  70. const routerContent = TestUtils.readFile(TEST_CONFIG.routerFile);
  71. if (!routerContent) {
  72. this.collector.addResult('路由文件可读', false, '无法读取路由文件');
  73. return;
  74. }
  75. // 测试智谱AI路由路径
  76. const zhipuPaths = [
  77. '/zhipu/questionAnswer',
  78. '/zhipu/knowledgeLib',
  79. '/zhipu/dataExport'
  80. ];
  81. zhipuPaths.forEach(path => {
  82. const count = TestUtils.countOccurrences(routerContent, path.replace('/', '\\/'));
  83. this.collector.addResult(
  84. `路由路径 ${path} 存在`,
  85. count > 0,
  86. count === 0 ? `未找到路由路径: ${path}` : ''
  87. );
  88. });
  89. // 测试智谱AI导入路径
  90. const zhipuImports = [
  91. '@/pages/platforms/zhipu/questionAnswer/list/index',
  92. '@/pages/platforms/zhipu/questionAnswer/info/index',
  93. '@/pages/platforms/zhipu/knowledgeLib/list/index',
  94. '@/pages/platforms/zhipu/knowledgeLib/detail/index',
  95. '@/pages/platforms/zhipu/dataExport/index'
  96. ];
  97. zhipuImports.forEach(importPath => {
  98. const count = TestUtils.countOccurrences(routerContent, importPath.replace(/\//g, '\\/'));
  99. this.collector.addResult(
  100. `导入路径 ${importPath} 存在`,
  101. count > 0,
  102. count === 0 ? `未找到导入路径: ${importPath}` : ''
  103. );
  104. });
  105. }
  106. // 测试2: 验证导航组件中的智谱AI路径
  107. testNavZhipuPaths() {
  108. console.log('\n🧭 测试导航组件中的智谱AI路径...');
  109. const navContent = TestUtils.readFile(TEST_CONFIG.navFile);
  110. if (!navContent) {
  111. this.collector.addResult('导航文件可读', false, '无法读取导航文件');
  112. return;
  113. }
  114. // 测试智谱AI菜单项
  115. const zhipuMenuItems = [
  116. '/zhipu/questionAnswer',
  117. '/zhipu/knowledgeLib',
  118. '/zhipu/dataExport'
  119. ];
  120. zhipuMenuItems.forEach(path => {
  121. const count = TestUtils.countOccurrences(navContent, path.replace('/', '\\/'));
  122. this.collector.addResult(
  123. `导航菜单项 ${path} 存在`,
  124. count > 0,
  125. count === 0 ? `未找到导航菜单项: ${path}` : ''
  126. );
  127. });
  128. // 测试智谱AI路由跳转
  129. const zhipuNavigations = [
  130. 'router\\.navigate\\(\\s*\\{\\s*pathname:\\s*[\'"]/zhipu/questionAnswer[\'"]\\s*\\}\\)',
  131. 'router\\.navigate\\(\\s*\\{\\s*pathname:\\s*[\'"]/zhipu/knowledgeLib[\'"]\\s*\\}\\)',
  132. 'router\\.navigate\\(\\s*\\{\\s*pathname:\\s*[\'"]/zhipu/dataExport[\'"]\\s*\\}\\)'
  133. ];
  134. zhipuNavigations.forEach(navPattern => {
  135. const count = TestUtils.countOccurrences(navContent, navPattern);
  136. this.collector.addResult(
  137. `路由跳转 ${navPattern} 存在`,
  138. count > 0,
  139. count === 0 ? `未找到路由跳转: ${navPattern}` : ''
  140. );
  141. });
  142. }
  143. // 测试3: 验证布局组件中的智谱AI默认路径
  144. testLayoutZhipuDefaultPath() {
  145. console.log('\n🏗️ 测试布局组件中的智谱AI默认路径...');
  146. const layoutContent = TestUtils.readFile(TEST_CONFIG.layoutFile);
  147. if (!layoutContent) {
  148. this.collector.addResult('布局文件可读', false, '无法读取布局文件');
  149. return;
  150. }
  151. // 测试智谱AI默认路径
  152. const zhipuDefaultPath = '/zhipu/questionAnswer';
  153. const count = TestUtils.countOccurrences(layoutContent, zhipuDefaultPath.replace('/', '\\/'));
  154. this.collector.addResult(
  155. `智谱AI默认路径 ${zhipuDefaultPath} 存在`,
  156. count > 0,
  157. count === 0 ? `未找到智谱AI默认路径: ${zhipuDefaultPath}` : ''
  158. );
  159. // 测试菜单类型切换逻辑
  160. const menuTypeLogic = 'value === 1 \\? \'/deepseek/questionAnswer\' : \'/zhipu/questionAnswer\'';
  161. const logicCount = TestUtils.countOccurrences(layoutContent, menuTypeLogic);
  162. this.collector.addResult(
  163. '菜单类型切换逻辑正确',
  164. logicCount > 0,
  165. logicCount === 0 ? '未找到正确的菜单类型切换逻辑' : ''
  166. );
  167. }
  168. // 测试4: 验证没有遗留的旧路径
  169. testNoLegacyPaths() {
  170. console.log('\n🚫 测试没有遗留的旧路径...');
  171. const routerContent = TestUtils.readFile(TEST_CONFIG.routerFile);
  172. const navContent = TestUtils.readFile(TEST_CONFIG.navFile);
  173. const layoutContent = TestUtils.readFile(TEST_CONFIG.layoutFile);
  174. const allContent = [routerContent, navContent, layoutContent].filter(Boolean).join('\n');
  175. // 检查旧路径
  176. const oldPaths = [
  177. '/questionAnswer',
  178. '/knowledgeLib',
  179. '/dataExport'
  180. ];
  181. oldPaths.forEach(path => {
  182. const count = TestUtils.countOccurrences(allContent, path.replace('/', '\\/'));
  183. this.collector.addResult(
  184. `没有遗留旧路径 ${path}`,
  185. count === 0,
  186. count > 0 ? `发现${count}个遗留的旧路径: ${path}` : ''
  187. );
  188. });
  189. // 检查旧导入路径
  190. const oldImports = [
  191. '@/pages/questionAnswer',
  192. '@/pages/knowledgeLib',
  193. '@/pages/dataExport'
  194. ];
  195. oldImports.forEach(importPath => {
  196. const count = TestUtils.countOccurrences(allContent, importPath.replace(/\//g, '\\/'));
  197. this.collector.addResult(
  198. `没有遗留旧导入路径 ${importPath}`,
  199. count === 0,
  200. count > 0 ? `发现${count}个遗留的旧导入路径: ${importPath}` : ''
  201. );
  202. });
  203. }
  204. // 测试5: 验证路由完整性
  205. testRouteCompleteness() {
  206. console.log('\n✅ 测试路由完整性...');
  207. const routerContent = TestUtils.readFile(TEST_CONFIG.routerFile);
  208. if (!routerContent) {
  209. this.collector.addResult('路由文件可读', false, '无法读取路由文件');
  210. return;
  211. }
  212. // 检查所有必要的路由都存在
  213. const requiredRoutes = [
  214. { path: '/zhipu/questionAnswer', name: '智谱AI问答应用' },
  215. { path: '/zhipu/questionAnswer/create', name: '智谱AI创建应用' },
  216. { path: '/zhipu/questionAnswer/modify', name: '智谱AI修改应用' },
  217. { path: '/zhipu/knowledgeLib', name: '智谱AI知识库' },
  218. { path: '/zhipu/knowledgeLib/:knowledgeId', name: '智谱AI知识库详情' },
  219. { path: '/zhipu/dataExport', name: '智谱AI数据导出' }
  220. ];
  221. requiredRoutes.forEach(route => {
  222. const count = TestUtils.countOccurrences(routerContent, route.path.replace(/\//g, '\\/'));
  223. this.collector.addResult(
  224. `路由 ${route.name} 存在`,
  225. count > 0,
  226. count === 0 ? `未找到路由: ${route.path}` : ''
  227. );
  228. });
  229. // 检查面包屑配置
  230. const breadcrumbCount = TestUtils.countOccurrences(routerContent, 'breadcrumbName');
  231. this.collector.addResult(
  232. '面包屑配置完整',
  233. breadcrumbCount >= 6,
  234. breadcrumbCount < 6 ? `面包屑配置不足,期望至少6个,实际${breadcrumbCount}个` : ''
  235. );
  236. }
  237. // 运行所有测试
  238. runAllTests() {
  239. console.log('🚀 开始智谱AI路由测试...\n');
  240. this.testRouterZhipuPaths();
  241. this.testNavZhipuPaths();
  242. this.testLayoutZhipuDefaultPath();
  243. this.testNoLegacyPaths();
  244. this.testRouteCompleteness();
  245. // 输出结果
  246. this.collector.printResults();
  247. // 返回测试结果
  248. const summary = { passed: this.collector.passed, failed: this.collector.failed, total: this.collector.results.length };
  249. if (summary.failed > 0) {
  250. console.log('\n⚠️ 部分测试失败,智谱AI路由可能存在问题。');
  251. process.exit(1);
  252. } else {
  253. console.log('\n🎉 所有测试通过!智谱AI路由配置正确。');
  254. }
  255. }
  256. }
  257. // 运行测试
  258. const tests = new ZhipuRoutingTests();
  259. tests.runAllTests();