import { afterEach, describe, expect, it, vi } from 'vitest' import { policyDraft, policyIssue } from '@/features/projects/quality-policy' import { projectsApi } from '@/features/projects/api' import type { ProjectQualityPolicy } from '@/features/projects/types' /** 保留已有精确例外的服务端完整策略。 */ const saved: ProjectQualityPolicy = { version: 1, physicsMode: 'realistic', allowExaggeratedMotion: false, allowNonPhysicalActions: false, allowSupernaturalAbilities: false, customRules: ['规则'], actionExceptions: [ { code: 'action_resource_conflict', ownerRef: '@人物', resource: 'left_hand', reason: '明确依据' } ] } afterEach(() => vi.unstubAllGlobals()) describe('项目质量策略', () => { 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) }) })