diff --git a/plugin-operation-audit/README.md b/plugin-operation-audit/README.md index 233f49d..c40a2c1 100644 --- a/plugin-operation-audit/README.md +++ b/plugin-operation-audit/README.md @@ -69,8 +69,9 @@ Console「插件 → 操作审计 → 设置」: - 定时清理采用 Reactor 调度方案(技术方案风险项 #3 预案),不依赖 `@Scheduled` 在插件上下文的可用性。 - 关于测试:`./gradlew test`(当前 50 个用例)覆盖捕获规则、资源解析、快照、直写降级、 - 过滤器行为、查询端点参数校验、设置缓存与保留期清理;Console UI 以类型检查 + 构建通过 - 为准(技术方案 §7,MVP 不引入组件测试)。 + 过滤器行为、查询端点参数校验、设置缓存与保留期清理;Console UI 以 vitest 断言 + 路由注册形态(必须挂在 `Root` 布局路由下,否则页面会全屏渲染)+ 类型检查 + 构建通过为准 + (技术方案 §7,MVP 不引入组件测试)。 ## 开发 diff --git a/plugin-operation-audit/ui/build.gradle b/plugin-operation-audit/ui/build.gradle index 062d612..9426c56 100644 --- a/plugin-operation-audit/ui/build.gradle +++ b/plugin-operation-audit/ui/build.gradle @@ -26,8 +26,22 @@ tasks.register('buildFrontend', NpmTask) { dependsOn tasks.named('npmInstall') } +// 前端单元测试(vitest:路由注册形态等回归断言) +tasks.register('testFrontend', NpmTask) { + group = 'verification' + description = 'Runs the Console UI unit tests (vitest).' + args = ['run', 'test'] + inputs.files( + fileTree('src'), + file('package.json'), + file('vitest.config.ts'), + ) + dependsOn tasks.named('npmInstall') +} + tasks.named('check') { dependsOn tasks.named('buildFrontend') + dependsOn tasks.named('testFrontend') } tasks.named('assemble') { diff --git a/plugin-operation-audit/ui/package-lock.json b/plugin-operation-audit/ui/package-lock.json index 8143bae..bc54b8b 100644 --- a/plugin-operation-audit/ui/package-lock.json +++ b/plugin-operation-audit/ui/package-lock.json @@ -18,6 +18,7 @@ "@vue/tsconfig": "^0.7.0", "typescript": "~5.8.3", "vite": "^6.0.0", + "vitest": "^5.1.1", "vue-tsc": "^2.2.10" } }, @@ -608,12 +609,33 @@ "vite": "^6.0.0 || ^7.0.0 || ^8.0.0" } }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.3.tgz", + "integrity": "sha512-lhTtjtoEArWBjismtfS761S9Hlyn+nIZqOZVYoZ1Xn82PMNMVnk+jj/NhC4fAXGTs7ydONcJiR5vyQBTMDu27g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/@jridgewell/sourcemap-codec": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.6.0.tgz", "integrity": "sha512-T7jf+5zgsZHwNJ4lvQ7/aezbyk0nNX+zJVWpmHA7VYsEx7a7qr5Rg5IbtJFqkgze5Y2sruq1RUY8Q837Od7iFw==", "license": "MIT" }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.31", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.31.tgz", + "integrity": "sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/resolve-uri": "^3.1.0", + "@jridgewell/sourcemap-codec": "^1.4.14" + } + }, "node_modules/@rollup/rollup-android-arm-eabi": { "version": "4.68.8", "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.68.8.tgz", @@ -1017,6 +1039,24 @@ "win32" ] }, + "node_modules/@types/chai": { + "version": "5.2.3", + "resolved": "https://registry.npmjs.org/@types/chai/-/chai-5.2.3.tgz", + "integrity": "sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/deep-eql": "*", + "assertion-error": "^2.0.1" + } + }, + "node_modules/@types/deep-eql": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@types/deep-eql/-/deep-eql-4.0.2.tgz", + "integrity": "sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/estree": { "version": "1.0.10", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.10.tgz", @@ -1048,6 +1088,64 @@ "vue": "^3.2.25" } }, + "node_modules/@vitest/mocker": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-5.1.1.tgz", + "integrity": "sha512-b2OgTB4+3ImWIVafXKazJXwLKwQxUNnix6b3xUvUTMh7RyXlrtzoxfZr8aEz9zkHOMAS4K5lu3TvFWoiVeVTQg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/trace-mapping": "0.3.31", + "@vitest/spy": "5.1.1", + "estree-walker": "^3.0.3", + "magic-string": "^1.4.5" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "msw": "^2.4.9 || ^3.0.0", + "vite": "^6.0.0 || ^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "msw": { + "optional": true + }, + "vite": { + "optional": true + } + } + }, + "node_modules/@vitest/mocker/node_modules/estree-walker": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz", + "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/estree": "^1.0.0" + } + }, + "node_modules/@vitest/mocker/node_modules/magic-string": { + "version": "1.4.5", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-1.4.5.tgz", + "integrity": "sha512-oLLC2+YvQUfAa+vxoufE/yc1icb87M9iXIRqAoR/2EhE/8cb3SF/+Gi0c2By1B+5Vz2e2vQtUvHiu1+63Znh+w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.6.0" + } + }, + "node_modules/@vitest/spy": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-5.1.1.tgz", + "integrity": "sha512-6K8C6alQG1/Yc8/ZI22iJS7JEsxPdVzqtqDq+/xM4Z0mGVykemMVjLhqMy/q6/IHYG3kKD8hKSwRen/ZdyI9vQ==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://opencollective.com/vitest" + } + }, "node_modules/@volar/language-core": { "version": "2.4.15", "resolved": "https://registry.npmjs.org/@volar/language-core/-/language-core-2.4.15.tgz", @@ -1244,6 +1342,16 @@ "dev": true, "license": "Python-2.0" }, + "node_modules/assertion-error": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz", + "integrity": "sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -1290,6 +1398,16 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/chai": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-6.3.0.tgz", + "integrity": "sha512-XWAtwJ6OHO+tj0EKCs0Y2UamnyOxseZWltU4x2U2wh8g4AigdjwvtUjvLP2tqkA/avxHEtzxNaqGq/YGNwckKg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, "node_modules/confbox": { "version": "0.3.2", "resolved": "https://registry.npmjs.org/confbox/-/confbox-0.3.2.tgz", @@ -1421,6 +1539,16 @@ "integrity": "sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==", "license": "MIT" }, + "node_modules/expect-type": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.4.0.tgz", + "integrity": "sha512-KfYbmpRm0VbLjEvVa9yGwCi9GI34xvi7A/HXYWQO65CSD2u3MczUJSuwXKFIxlGsgBQizV9q5J9NHj4VG0n+pA==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/exsolve": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/exsolve/-/exsolve-1.2.0.tgz", @@ -1665,6 +1793,20 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/obug": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/obug/-/obug-3.0.0.tgz", + "integrity": "sha512-5vvB5+W7ePv+p3uqxi+RcW1XAzLW0/hxt3/4X4Lc4qHudzOhmBiBwOY6DRob4WnanAEGvNcLjF+KNOufrUoEQw==", + "dev": true, + "funding": [ + "https://github.com/sponsors/sxzz", + "https://opencollective.com/debug" + ], + "license": "MIT", + "engines": { + "node": ">=12.20.0" + } + }, "node_modules/path-browserify": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz", @@ -1894,6 +2036,33 @@ "node": ">=6.0.0" } }, + "node_modules/std-env": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/std-env/-/std-env-4.3.0.tgz", + "integrity": "sha512-OtU/EgQ1kIm5KwqQpBC6ZEMXrZRui11w8zgfTWp8cdO9B8OaPsbA8bTHO2P+HNo1VlUTGMVBwPhydu6poeXiag==", + "dev": true, + "license": "MIT" + }, + "node_modules/tinybench": { + "version": "6.2.4", + "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-6.2.4.tgz", + "integrity": "sha512-SQ52jEncXxbMCzJuY2A/jGIbbAu6NYsBWW7grz2pNE+EnzQdoGaoBfpk4qC/opE7qiUcGmGgaGIz5EoHdS1O9w==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/tinyexec": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-1.3.1.tgz", + "integrity": "sha512-GCvB3aoys96IuDFBMcTB46JOR6mdMtAToqwiW8JlWhsoh1mhHi/xn9ss/Dg7N555GiJyEt2qzoG/NHCwM6h1EA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, "node_modules/tinyglobby": { "version": "0.2.17", "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.17.tgz", @@ -2007,6 +2176,99 @@ } } }, + "node_modules/vitest": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/vitest/-/vitest-5.1.1.tgz", + "integrity": "sha512-trVmzUFOSraCZcI3uZIMD5FqBh4atFkC/XuJP/IU7qtyQay0FCs3Ia+0QiQ2JJwGyWG/xrcBoJNel2+O7kEZ0g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/chai": "^5.2.2", + "@vitest/mocker": "5.1.1", + "chai": "^6.3.0", + "es-module-lexer": "^2.3.2", + "expect-type": "^1.4.0", + "magic-string": "^1.4.5", + "obug": "^3.0.0", + "picomatch": "^4.0.7", + "std-env": "^4.3.0", + "tinybench": "^6.2.3", + "tinyexec": "^1.3.1", + "tinyglobby": "^0.2.17", + "why-is-node-running": "3.2.1" + }, + "bin": { + "vitest": "vitest.mjs" + }, + "engines": { + "node": "^22.12.0 || ^24.0.0 || >=26.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "@edge-runtime/vm": "*", + "@opentelemetry/api": "^1.9.0", + "@types/node": "^22.0.0 || >=24.0.0", + "@vitest/browser-playwright": "5.1.1", + "@vitest/browser-preview": "5.1.1", + "@vitest/browser-webdriverio": "^5.0.0-beta.5 || >=5.0.0", + "@vitest/coverage-istanbul": "5.1.1", + "@vitest/coverage-v8": "5.1.1", + "@vitest/ui": "5.1.1", + "happy-dom": "*", + "jsdom": "*", + "vite": "^6.4.0 || ^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "@edge-runtime/vm": { + "optional": true + }, + "@opentelemetry/api": { + "optional": true + }, + "@types/node": { + "optional": true + }, + "@vitest/browser-playwright": { + "optional": true + }, + "@vitest/browser-preview": { + "optional": true + }, + "@vitest/browser-webdriverio": { + "optional": true + }, + "@vitest/coverage-istanbul": { + "optional": true + }, + "@vitest/coverage-v8": { + "optional": true + }, + "@vitest/ui": { + "optional": true + }, + "happy-dom": { + "optional": true + }, + "jsdom": { + "optional": true + }, + "vite": { + "optional": false + } + } + }, + "node_modules/vitest/node_modules/magic-string": { + "version": "1.4.5", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-1.4.5.tgz", + "integrity": "sha512-oLLC2+YvQUfAa+vxoufE/yc1icb87M9iXIRqAoR/2EhE/8cb3SF/+Gi0c2By1B+5Vz2e2vQtUvHiu1+63Znh+w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.6.0" + } + }, "node_modules/vscode-uri": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/vscode-uri/-/vscode-uri-3.2.0.tgz", @@ -2051,6 +2313,19 @@ "peerDependencies": { "typescript": ">=5.0.0" } + }, + "node_modules/why-is-node-running": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/why-is-node-running/-/why-is-node-running-3.2.1.tgz", + "integrity": "sha512-Tb2FUhB4vUsGQlfSquQLYkApkuPAFQXGFzxWKHHumVz2dK+X1RUm/HnID4+TfIGYJ1kTcwOaCk/buYCEJr6YjQ==", + "dev": true, + "license": "MIT", + "bin": { + "why-is-node-running": "cli.js" + }, + "engines": { + "node": ">=20.11" + } } } } diff --git a/plugin-operation-audit/ui/package.json b/plugin-operation-audit/ui/package.json index b210449..e86cc3c 100644 --- a/plugin-operation-audit/ui/package.json +++ b/plugin-operation-audit/ui/package.json @@ -6,7 +6,8 @@ "build": "npm run type-check && npm run build-only", "build-only": "vite build", "dev": "vite build --watch --mode=development", - "type-check": "vue-tsc --noEmit" + "type-check": "vue-tsc --noEmit", + "test": "vitest run" }, "dependencies": { "@halo-dev/api-client": "2.26.0", @@ -21,6 +22,7 @@ "@vue/tsconfig": "^0.7.0", "typescript": "~5.8.3", "vite": "^6.0.0", + "vitest": "^5.1.1", "vue-tsc": "^2.2.10" } } diff --git a/plugin-operation-audit/ui/src/index.spec.ts b/plugin-operation-audit/ui/src/index.spec.ts new file mode 100644 index 0000000..52a9df2 --- /dev/null +++ b/plugin-operation-audit/ui/src/index.spec.ts @@ -0,0 +1,48 @@ +import { describe, expect, it, vi } from 'vitest' +import type { RouteRecordAppend } from '@halo-dev/console-shared' + +// 结构性回归测试:无需真实渲染,将视图组件与图标库打桩, +// 仅断言 definePlugin 暴露的路由注册形态。 +vi.mock('@/views/AuditLogList.vue', () => ({ default: { name: 'AuditLogListStub' } })) +vi.mock('@halo-dev/components', () => ({ IconEye: { name: 'IconEyeStub' } })) + +const { default: plugin } = await import('./index') + +/** + * 回归测试:操作审计页面必须以 RouteRecordAppend 形式追加到控制台 Root 路由下。 + * + * 背景:Halo 2.21+ 统一控制台中,插件 `routes` 里的独立 RouteRecordRaw 会作为 + * 顶级路由注册(router.addRoute),不经过 BasicLayout,导致页面全屏渲染、 + * 左侧导航消失。只有挂到名为 Root 的布局路由(component: BasicLayout)下, + * 页面才会保留侧边栏。 + */ +describe('console 路由注册', () => { + const routes = plugin.routes ?? [] + const appended = routes[0] as RouteRecordAppend + + it('必须只有一个路由条目,且以 RouteRecordAppend 声明', () => { + expect(routes).toHaveLength(1) + expect(appended.parentName).toBeDefined() + expect(appended.route).toBeDefined() + }) + + it('必须追加到 Root(BasicLayout)路由下,保留左侧导航', () => { + expect(appended.parentName).toBe('Root') + }) + + it('路由路径与命名保持不变(/operation-audit)', () => { + expect(appended.route.path).toBe('/operation-audit') + expect(appended.route.name).toBe('OperationAudit') + }) + + it('菜单元信息完整(名称/分组/图标/权限过滤)', () => { + const meta = appended.route.meta ?? {} + expect(meta.title).toBe('操作审计') + expect(meta.permissions).toContain('plugin:operation-audit:view') + const menu = meta.menu as Record + expect(menu).toBeDefined() + expect(menu.name).toBe('操作审计') + expect(menu.group).toBe('plugin') + expect(menu.icon).toBeDefined() + }) +}) diff --git a/plugin-operation-audit/ui/src/index.ts b/plugin-operation-audit/ui/src/index.ts index 7df9393..628bac4 100644 --- a/plugin-operation-audit/ui/src/index.ts +++ b/plugin-operation-audit/ui/src/index.ts @@ -7,19 +7,24 @@ export default definePlugin({ components: {}, routes: [ { - path: '/operation-audit', - name: 'OperationAudit', - component: AuditLogList, - meta: { - title: '操作审计', - searchable: true, - // 菜单按角色模板过滤(技术方案 D4):未授权用户不占用任何菜单 - permissions: ['plugin:operation-audit:view'], - menu: { - name: '操作审计', - group: 'plugin', - icon: markRaw(IconEye), - priority: 0, + // 追加到控制台 Root 路由(BasicLayout)下,页面才能保留左侧导航, + // 否则作为独立顶级路由注册会全屏渲染(Halo 2.21+ 统一控制台行为)。 + parentName: 'Root', + route: { + path: '/operation-audit', + name: 'OperationAudit', + component: AuditLogList, + meta: { + title: '操作审计', + searchable: true, + // 菜单按角色模板过滤(技术方案 D4):未授权用户不占用任何菜单 + permissions: ['plugin:operation-audit:view'], + menu: { + name: '操作审计', + group: 'plugin', + icon: markRaw(IconEye), + priority: 0, + }, }, }, }, diff --git a/plugin-operation-audit/ui/tsconfig.json b/plugin-operation-audit/ui/tsconfig.json index cb40b8b..ba4cc1c 100644 --- a/plugin-operation-audit/ui/tsconfig.json +++ b/plugin-operation-audit/ui/tsconfig.json @@ -14,5 +14,5 @@ }, "types": ["vite/client", "node"] }, - "include": ["src/**/*.ts", "src/**/*.vue", "vite.config.ts"] + "include": ["src/**/*.ts", "src/**/*.vue", "vite.config.ts", "vitest.config.ts"] } diff --git a/plugin-operation-audit/ui/vitest.config.ts b/plugin-operation-audit/ui/vitest.config.ts new file mode 100644 index 0000000..d5bda4c --- /dev/null +++ b/plugin-operation-audit/ui/vitest.config.ts @@ -0,0 +1,16 @@ +import vue from '@vitejs/plugin-vue' +import { fileURLToPath, URL } from 'url' +import { defineConfig } from 'vitest/config' + +// 与 vite.config.ts 保持一致的 @ 别名,测试运行在 node 环境(仅断言模块结构,无 DOM 依赖) +export default defineConfig({ + plugins: [vue()], + resolve: { + alias: { + '@': fileURLToPath(new URL('./src', import.meta.url)), + }, + }, + test: { + environment: 'node', + }, +})