107 lines
6.4 KiB
TypeScript
107 lines
6.4 KiB
TypeScript
import { policyReceipt } from '../../fixtures/quality-policy'
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
|
|
import { policyDraft, policyIssue } from '@/features/projects/quality-policy'
|
|
import { projectsApi } from '@/features/projects/api'
|
|
import type { ProjectQualityPolicy } from '@/features/projects/types'
|
|
import QualityPolicySettings from '@/features/generation-config/components/QualityPolicySettings.vue'
|
|
|
|
/** 保留已有精确例外的服务端完整策略。 */
|
|
const saved: ProjectQualityPolicy = {
|
|
version: 1,
|
|
physicsMode: 'realistic',
|
|
allowExaggeratedMotion: false,
|
|
allowNonPhysicalActions: false,
|
|
allowSupernaturalAbilities: false,
|
|
customRules: ['规则'],
|
|
actionExceptions: [
|
|
{ code: 'action_resource_conflict', ownerRef: '@人物', resource: 'left_hand', reason: '明确依据' }
|
|
]
|
|
}
|
|
/** 真实策略表单实例,验证结束后移除组件与事件监听。 */
|
|
let settings: VueWrapper | undefined
|
|
afterEach(() => {
|
|
settings?.unmount()
|
|
settings = undefined
|
|
vi.unstubAllGlobals()
|
|
vi.restoreAllMocks()
|
|
localStorage.clear()
|
|
})
|
|
describe('项目质量策略', () => {
|
|
it('基础规则不再添加空白例外,已有例外只读展示且完整保留在提交草稿中', async () => {
|
|
const wrapper = mount(QualityPolicySettings, { props: { projectId: 'p', disabled: false, initialPolicy: policyReceipt('p', saved) } })
|
|
settings = wrapper
|
|
await flushPromises()
|
|
expect(wrapper.findAll('button').some(button => button.text() === '添加例外')).toBe(false)
|
|
expect(wrapper.find('[aria-label="例外 1 主体引用"]').exists()).toBe(false)
|
|
expect(wrapper.text()).toContain('已有动作例外 · 1 项')
|
|
expect(wrapper.vm.validatedDraft()?.actionExceptions).toEqual(saved.actionExceptions)
|
|
})
|
|
it('启动入口复用真实回执,保存前重读版本,不把保存当作明确确认', async () => {
|
|
let currentPolicy = policyDraft(saved)
|
|
const read = vi.spyOn(projectsApi, 'qualityPolicy').mockImplementation(async () => (policyReceipt('p', policyDraft(currentPolicy))))
|
|
vi.spyOn(projectsApi, 'saveQualityPolicy').mockImplementation(async (id, policy) => { currentPolicy = policyDraft(policy); return policyReceipt(id, policyDraft(policy)) })
|
|
const wrapper = mount(QualityPolicySettings, { props: { projectId: 'p', disabled: false, initialPolicy: policyReceipt('p', saved) } })
|
|
settings = wrapper
|
|
await flushPromises()
|
|
expect(read).not.toHaveBeenCalled()
|
|
expect(wrapper.vm.validatedDraft()).toEqual(saved)
|
|
await wrapper.vm.save()
|
|
expect(read).toHaveBeenCalledTimes(1)
|
|
await wrapper.get('#quality-custom-rules').setValue('新规则')
|
|
await wrapper.vm.save()
|
|
expect(read).toHaveBeenCalledTimes(2)
|
|
})
|
|
it('保存基础规则时合并后来新增的例外,不覆盖隐藏的服务端最新例外', async () => {
|
|
const latest = { ...saved, actionExceptions: [...saved.actionExceptions!, { code: 'action_resource_conflict' as const, ownerRef: '@CH0002', resource: 'right_hand' as const, reason: '另一处刚添加的例外' }] }
|
|
vi.spyOn(projectsApi, 'qualityPolicy').mockResolvedValue(policyReceipt('p', latest))
|
|
const write = vi.spyOn(projectsApi, 'saveQualityPolicy').mockImplementation(async (id, policy) => (policyReceipt(id, policyDraft(policy))))
|
|
const wrapper = mount(QualityPolicySettings, { props: { projectId: 'p', disabled: false, initialPolicy: policyReceipt('p', saved) } })
|
|
settings = wrapper
|
|
await flushPromises()
|
|
expect(wrapper.find('.text-danger').exists()).toBe(false)
|
|
await wrapper.get('#quality-custom-rules').setValue('新的整体规则')
|
|
await wrapper.vm.save()
|
|
expect(write).toHaveBeenCalledExactlyOnceWith('p', { ...saved, customRules: ['新的整体规则'], actionExceptions: latest.actionExceptions }, 2)
|
|
expect(wrapper.vm.validatedDraft()?.actionExceptions).toEqual(latest.actionExceptions)
|
|
expect(wrapper.text()).toContain('已有动作例外 · 2 项')
|
|
})
|
|
it('基础规则已被其他位置修改时拒绝覆盖,保留用户草稿', async () => {
|
|
vi.spyOn(projectsApi, 'qualityPolicy').mockResolvedValue(policyReceipt('p', { ...saved, physicsMode: 'fantasy' }))
|
|
const write = vi.spyOn(projectsApi, 'saveQualityPolicy')
|
|
const wrapper = mount(QualityPolicySettings, { props: { projectId: 'p', disabled: false, initialPolicy: policyReceipt('p', saved) } })
|
|
settings = wrapper
|
|
await flushPromises()
|
|
await wrapper.get('#quality-custom-rules').setValue('不能丢失的用户草稿')
|
|
await wrapper.vm.save()
|
|
expect(write).not.toHaveBeenCalled()
|
|
expect(wrapper.text()).toContain('质量规则已在其他位置更新')
|
|
expect(wrapper.vm.validatedDraft()?.customRules).toEqual(['不能丢失的用户草稿'])
|
|
})
|
|
it('草稿编辑不污染已保存策略,完整保留精确例外', () => {
|
|
const draft = policyDraft(saved)
|
|
draft.actionExceptions![0]!.reason = '新依据'
|
|
draft.customRules.push('新规则')
|
|
expect(saved.actionExceptions![0]!.reason).toBe('明确依据')
|
|
expect(saved.customRules).toEqual(['规则'])
|
|
expect(policyIssue(draft)).toBe('')
|
|
})
|
|
it('拒绝超过上限或缺少精确例外依据的规则', () => {
|
|
expect(policyIssue({ ...saved, customRules: [''] })).toContain('规则')
|
|
expect(policyIssue({ ...saved, customRules: Array(31).fill('规则') })).toContain('30')
|
|
expect(policyIssue({ ...saved, actionExceptions: [{ ...saved.actionExceptions![0]!, reason: '' }] })).toContain(
|
|
'依据'
|
|
)
|
|
})
|
|
it('完整提交正式质量策略接口,保留用户明确的权限选择', async () => {
|
|
const fetcher = vi
|
|
.fn<typeof fetch>()
|
|
.mockImplementation(async () => new Response(JSON.stringify({ data: policyReceipt('p', saved) })))
|
|
vi.stubGlobal('fetch', fetcher)
|
|
await projectsApi.saveQualityPolicy('p', saved, 2)
|
|
expect(fetcher.mock.calls[0]![0]).toContain('/projects/p/quality-policy')
|
|
expect(fetcher.mock.calls[0]![1]!.method).toBe('PUT')
|
|
expect(JSON.parse(fetcher.mock.calls[0]![1]!.body as string)).toEqual({ policy: saved, expectedRevision: 2 })
|
|
})
|
|
})
|