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() }) describe('项目质量策略', () => { it('进入和新增例外不立即报错,保存时拦截缺失字段,修正后才写入', async () => { const fetcher = vi.fn().mockImplementation( async (_url, init) => new Response( JSON.stringify({ data: { projectId: 'p', policy: init?.method === 'PUT' ? JSON.parse(String(init.body)) : { ...saved, actionExceptions: [] } } }) ) ) vi.stubGlobal('fetch', fetcher) const wrapper = mount(QualityPolicySettings, { props: { projectId: 'p', disabled: false } }) settings = wrapper await flushPromises() expect(wrapper.find('.text-danger').exists()).toBe(false) await wrapper .findAll('button') .find(button => button.text() === '添加例外')! .trigger('click') expect(wrapper.text()).not.toContain('精确例外需填写') expect(wrapper.vm.canSave).toBe(true) await wrapper.vm.save() expect(wrapper.text()).toContain('精确例外需填写') expect(fetcher.mock.calls.filter(([, init]) => init?.method === 'PUT')).toHaveLength(0) await wrapper.get('[aria-label="例外 1 主体引用"] input').setValue('@CH0001') expect(wrapper.text()).not.toContain('精确例外需填写') await wrapper.vm.save() expect(wrapper.text()).toContain('精确例外需填写') expect(fetcher.mock.calls.filter(([, init]) => init?.method === 'PUT')).toHaveLength(0) await wrapper.get('[aria-label="例外 1 确认依据"] input').setValue('剧本明确的动作许可') await wrapper.vm.save() await flushPromises() const writes = fetcher.mock.calls.filter(([, init]) => init?.method === 'PUT') expect(writes).toHaveLength(1) expect(JSON.parse(String(writes[0]?.[1]?.body))).toEqual({ version: 1, physicsMode: 'realistic', allowExaggeratedMotion: false, allowNonPhysicalActions: false, allowSupernaturalAbilities: false, customRules: ['规则'], actionExceptions: [ { code: 'action_resource_conflict', ownerRef: '@CH0001', resource: 'left_hand', reason: '剧本明确的动作许可' } ] }) expect(wrapper.text()).toContain('质量策略已保存') }) 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() .mockImplementation(async () => new Response(JSON.stringify({ data: { projectId: 'p', policy: saved } }))) vi.stubGlobal('fetch', fetcher) await projectsApi.saveQualityPolicy('p', saved) 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(saved) }) })