feat: 操作审计动作列与详情增加中文操作描述(登录/新建用户/发布文章等)

- utils/audit.ts 新增 describeOperation 纯函数:由 method/resourceType/path
  推导中文短语(登录、登出、认证操作、动词+资源中文名、子动作拼接如
  发布文章/启用插件、完整短语如修改密码),纯展示层推导,历史记录同样适用
- 列表动作列在徽标后渲染描述(列宽 6rem→10rem),题头改为「动作 / 描述」
- 详情抽屉顶部新增「描述」行
- 新增 utils/audit.spec.ts 10 项用例覆盖全部推导与回退路径,
  外观回归测试同步列宽与动作列结构
This commit is contained in:
2026-11-21 20:28:09 +08:00
parent 55c50706b5
commit 8aae68c95c
5 changed files with 273 additions and 10 deletions
@@ -0,0 +1,135 @@
import { describe, expect, it } from 'vitest'
import type { AuditLogSpec } from './audit'
import { describeOperation } from './audit'
// 操作描述推导(这条操作真正干了什么):由 method / resourceType / path 推导中文短语。
// 历史记录不落库新字段,描述纯展示层推导,因此必须覆盖所有回退路径,防止出现英文原始值。
function spec(overrides: Partial<AuditLogSpec>): AuditLogSpec {
return {
username: 'admin',
method: 'POST',
resourceType: 'other',
path: '/apis/api.console.halo.run/v1alpha1/other',
statusCode: 200,
remoteIp: '127.0.0.1',
...overrides,
}
}
describe('describeOperation', () => {
it('登录端点(含查询串)描述为 登录', () => {
expect(describeOperation(spec({
method: 'POST',
resourceType: 'auth',
path: '/login',
}))).toBe('登录')
// GET /login?redirect_uri=... 也应识别为登录(spec.path 含查询串)
expect(describeOperation(spec({
method: 'GET',
resourceType: 'auth',
path: '/login?redirect_uri=https%3A%2F%2Fwww.guichumo.com%2Fconsole',
}))).toBe('登录')
})
it('登出端点描述为 登出', () => {
expect(describeOperation(spec({
method: 'POST',
resourceType: 'auth',
path: '/logout',
}))).toBe('登出')
})
it('auth 类型但非登录/登出路径回退为 认证操作', () => {
expect(describeOperation(spec({
method: 'POST',
resourceType: 'auth',
path: '/auth/other',
}))).toBe('认证操作')
})
it('基础动作用 方法动词 + 资源中文名(新建用户 / 修改文章 / 删除插件)', () => {
expect(describeOperation(spec({
method: 'POST',
resourceType: 'users',
path: '/apis/api.console.halo.run/v1alpha1/users',
}))).toBe('新建用户')
expect(describeOperation(spec({
method: 'PUT',
resourceType: 'posts',
path: '/apis/content.halo.run/v1alpha1/posts/post-abc',
}))).toBe('修改文章')
expect(describeOperation(spec({
method: 'PATCH',
resourceType: 'themes',
path: '/apis/api.console.halo.run/v1alpha1/themes/theme-abc',
}))).toBe('修改主题')
expect(describeOperation(spec({
method: 'DELETE',
resourceType: 'plugins',
path: '/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc',
}))).toBe('删除插件')
})
it('子动作段与资源名拼接(发布文章 / 启用插件 / 停用插件)', () => {
expect(describeOperation(spec({
method: 'POST',
resourceType: 'posts',
path: '/apis/api.console.halo.run/v1alpha1/posts/post-abc/publish',
}))).toBe('发布文章')
expect(describeOperation(spec({
method: 'POST',
resourceType: 'plugins',
path: '/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc/enable',
}))).toBe('启用插件')
expect(describeOperation(spec({
method: 'POST',
resourceType: 'plugins',
path: '/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc/disable',
}))).toBe('停用插件')
expect(describeOperation(spec({
method: 'POST',
resourceType: 'posts',
path: '/apis/api.console.halo.run/v1alpha1/posts/post-abc/recycle',
}))).toBe('移入回收站文章')
})
it('子动作为完整短语时不拼接资源名(修改密码)', () => {
expect(describeOperation(spec({
method: 'PUT',
resourceType: 'users',
path: '/apis/api.console.halo.run/v1alpha1/users/admin/password',
}))).toBe('修改密码')
})
it('未识别的子动作回退为基础动词(/posts/{name}/content → 修改文章)', () => {
expect(describeOperation(spec({
method: 'PUT',
resourceType: 'posts',
path: '/apis/content.halo.run/v1alpha1/posts/post-abc/content',
}))).toBe('修改文章')
})
it('未识别的资源类型回退为原始值(动词 + resourceType)', () => {
expect(describeOperation(spec({
method: 'POST',
resourceType: 'workflows',
path: '/apis/audit.guichumo.com/v1alpha1/workflows',
}))).toBe('新建workflows')
})
it('未识别的 HTTP 方法回退为原始方法', () => {
expect(describeOperation(spec({
method: 'TRACE',
resourceType: 'posts',
path: '/apis/content.halo.run/v1alpha1/posts/post-abc',
}))).toBe('TRACE文章')
})
it('无法解析路径(other)回退为 动词 + other', () => {
expect(describeOperation(spec({
method: 'DELETE',
resourceType: 'other',
path: '/somewhere/odd',
}))).toBe('删除other')
})
})