经 ReactiveExtensionClient.watch(Watcher) 全局通道观测扩展模型更新/删除事件 (v2.26.1 源码级验证:仅 status 变化或无变化不派发、GC 走存储层无事件), 按用户可写面 diff 降噪后落库,执行者记 system,路径为资源规范 API 路径。 归属区分:过滤器为所有写方法请求登记 HttpWriteMarker 目标标记与活动宽限窗, HTTP 触发的变更不重复记录;插件自身配置 ConfigMap 直接排除。 新增设置项 recordSystemChanges(默认开);UI 操作者 system 展示为「系统」。 服务端 156 个用例、UI 47 个用例全绿。
281 lines
10 KiB
TypeScript
281 lines
10 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
||
import type { AuditLogSpec } from './audit'
|
||
import {
|
||
changeTypeLabel,
|
||
changeValueDisplay,
|
||
deriveAction,
|
||
describeOperation,
|
||
displayUsername,
|
||
SEMANTIC_ACTIONS,
|
||
SYSTEM_USERNAME,
|
||
UNKNOWN_USERNAME,
|
||
} 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')
|
||
})
|
||
})
|
||
|
||
describe('deriveAction 语义动作推导', () => {
|
||
it('登录/登出/认证操作由路径判定(含查询串剥离)', () => {
|
||
expect(deriveAction('POST', 'auth', '/login')).toBe('login')
|
||
expect(deriveAction('GET', 'auth', '/login?redirect_uri=https%3A%2F%2Fwww.guichumo.com'))
|
||
.toBe('login')
|
||
expect(deriveAction('POST', 'auth', '/logout')).toBe('logout')
|
||
expect(deriveAction('POST', 'auth', '/auth/other')).toBe('auth')
|
||
})
|
||
|
||
it('子动作段优先于 HTTP 方法', () => {
|
||
expect(deriveAction('PUT', 'posts',
|
||
'/apis/api.console.halo.run/v1alpha1/posts/post-abc/publish')).toBe('publish')
|
||
expect(deriveAction('POST', 'posts',
|
||
'/apis/api.console.halo.run/v1alpha1/posts/post-abc/restore')).toBe('restore')
|
||
// 激活与启用归并为同一动作键
|
||
expect(deriveAction('PUT', 'themes',
|
||
'/apis/api.console.halo.run/v1alpha1/themes/theme-abc/activation')).toBe('enable')
|
||
expect(deriveAction('PUT', 'users',
|
||
'/apis/api.console.halo.run/v1alpha1/users/admin/password')).toBe('password')
|
||
})
|
||
|
||
it('无子动作段时由 HTTP 方法归为 create/update/delete/view', () => {
|
||
expect(deriveAction('POST', 'users', '/apis/api.console.halo.run/v1alpha1/users'))
|
||
.toBe('create')
|
||
expect(deriveAction('PUT', 'posts', '/apis/content.halo.run/v1alpha1/posts/post-abc'))
|
||
.toBe('update')
|
||
expect(deriveAction('DELETE', 'plugins',
|
||
'/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc')).toBe('delete')
|
||
expect(deriveAction('GET', 'posts', '/apis/content.halo.run/v1alpha1/posts/post-abc'))
|
||
.toBe('view')
|
||
})
|
||
|
||
it('未知方法与未识别子动作回退为方法键或 null', () => {
|
||
expect(deriveAction('TRACE', 'posts', '/apis/content.halo.run/v1alpha1/posts/post-abc'))
|
||
.toBeNull()
|
||
expect(deriveAction('PUT', 'posts',
|
||
'/apis/content.halo.run/v1alpha1/posts/post-abc/content')).toBe('update')
|
||
})
|
||
})
|
||
|
||
describe('describeOperation 落库标注优先', () => {
|
||
it('spec.action 存在时直接由动作键推导(历史记录无 action 仍走 method/path 推导)', () => {
|
||
expect(describeOperation(spec({
|
||
method: 'PUT',
|
||
resourceType: 'posts',
|
||
path: '/apis/content.halo.run/v1alpha1/posts/post-abc',
|
||
action: 'publish',
|
||
}))).toBe('发布文章')
|
||
expect(describeOperation(spec({
|
||
method: 'POST',
|
||
resourceType: 'auth',
|
||
path: '/login',
|
||
action: 'login',
|
||
}))).toBe('登录')
|
||
// 与 method/path 现场推导结果一致(同一套推导规则)
|
||
expect(describeOperation(spec({
|
||
method: 'PUT',
|
||
resourceType: 'posts',
|
||
path: '/apis/api.console.halo.run/v1alpha1/posts/post-abc/publish',
|
||
action: 'publish',
|
||
}))).toBe(describeOperation(spec({
|
||
method: 'PUT',
|
||
resourceType: 'posts',
|
||
path: '/apis/api.console.halo.run/v1alpha1/posts/post-abc/publish',
|
||
})))
|
||
})
|
||
|
||
it('动作键未知时回退为 method 动词 + 资源名', () => {
|
||
expect(describeOperation(spec({
|
||
method: 'PUT',
|
||
resourceType: 'posts',
|
||
path: '/somewhere/odd',
|
||
action: 'bogus',
|
||
}))).toBe('修改文章')
|
||
})
|
||
})
|
||
|
||
describe('SEMANTIC_ACTIONS 筛选选项', () => {
|
||
it('值唯一且标签非空', () => {
|
||
const values = SEMANTIC_ACTIONS.map((a) => a.value)
|
||
expect(new Set(values).size).toBe(values.length)
|
||
for (const a of SEMANTIC_ACTIONS) {
|
||
expect(a.label).not.toBe('')
|
||
}
|
||
})
|
||
|
||
it('覆盖全部可落库的语义动作键(view 不会被捕获规则记录,除外)', () => {
|
||
// 全量枚举:登录/登出/认证 + 方法动作 + 全部子动作段(含 activation 归并)
|
||
const storable = new Set([
|
||
'login', 'logout', 'auth',
|
||
'create', 'update', 'delete',
|
||
'publish', 'unpublish', 'recycle', 'restore', 'enable', 'disable',
|
||
'upgrade', 'uninstall', 'reload', 'reply', 'recalculate',
|
||
'password', 'resetpassword',
|
||
])
|
||
expect(new Set(SEMANTIC_ACTIONS.map((a) => a.value))).toEqual(storable)
|
||
// 每个选项都能由 describeOperation 从动作键推出非空短语(下拉与描述保持一致)
|
||
for (const a of SEMANTIC_ACTIONS) {
|
||
expect(describeOperation(spec({ resourceType: 'posts', action: a.value })))
|
||
.not.toBe('')
|
||
}
|
||
})
|
||
})
|
||
|
||
describe('字段级变更展示辅助', () => {
|
||
it('changeTypeLabel:三种类型映射中文,未知回退原始值', () => {
|
||
expect(changeTypeLabel('ADDED')).toBe('新增')
|
||
expect(changeTypeLabel('UPDATED')).toBe('修改')
|
||
expect(changeTypeLabel('REMOVED')).toBe('删除')
|
||
expect(changeTypeLabel('unknown')).toBe('unknown')
|
||
expect(changeTypeLabel(null)).toBe('修改')
|
||
})
|
||
|
||
it('changeValueDisplay:缺失侧显示 ∅,其余原样', () => {
|
||
expect(changeValueDisplay('新值')).toBe('新值')
|
||
expect(changeValueDisplay(null)).toBe('∅')
|
||
expect(changeValueDisplay(undefined)).toBe('∅')
|
||
expect(changeValueDisplay('')).toBe('∅')
|
||
})
|
||
})
|
||
|
||
describe('displayUsername 操作者展示', () => {
|
||
it('登录失败占位显示为 未知用户,其余原样', () => {
|
||
expect(displayUsername(null)).toBe(UNKNOWN_USERNAME)
|
||
expect(displayUsername(undefined)).toBe(UNKNOWN_USERNAME)
|
||
expect(displayUsername(UNKNOWN_USERNAME)).toBe('未知用户')
|
||
expect(displayUsername('admin')).toBe('admin')
|
||
})
|
||
|
||
it('系统内部变更操作者显示为 系统(系统内部变更,产品设计 V2)', () => {
|
||
expect(SYSTEM_USERNAME).toBe('system')
|
||
expect(displayUsername(SYSTEM_USERNAME)).toBe('系统')
|
||
})
|
||
})
|