import { afterEach, describe, expect, it, vi } from 'vitest' import { flushPromises, mount, type VueWrapper } from '@vue/test-utils' import { NSelect, type MessageApi } from 'naive-ui' import { ref } from 'vue' import CreativeProfileSettings from '@/features/generation-config/components/CreativeProfileSettings.vue' import { generationConfigApi } from '@/features/generation-config/api' import { projectGenerationRevisionKey } from '@/features/generation-config/context' import { actionMessageKey } from '@/components/ui/useActionMessage' import { ApiError } from '@/lib/http' import { generationProfile, imageCatalog } from '../../fixtures/generation' /** 当前配置表单实例,测试后释放观察器。 */ let wrapper: VueWrapper | undefined afterEach(() => wrapper?.unmount()) /** * 使用真实表单和隔离消息层,不请求真实服务或保存项目数据。 * @returns 草稿原值、保存接口、消息及配置修订号 */ async function settings() { const saved = generationProfile('config-contract') const catalog = { ...imageCatalog, videos: [ { ...imageCatalog.images[0]!, type: 'video' as const, provider: 'seedance', model: 'seedance', generationOptions: [], defaults: {} } ] } vi.spyOn(generationConfigApi, 'catalog').mockResolvedValue(catalog) vi.spyOn(generationConfigApi, 'profile').mockResolvedValue(saved) const save = vi.spyOn(generationConfigApi, 'saveProfile').mockResolvedValue(saved) const messages = { success: vi.fn(), warning: vi.fn(), error: vi.fn(), info: vi.fn() } const revision = ref(0) wrapper = mount(CreativeProfileSettings, { props: { projectId: saved.projectId }, global: { provide: { [actionMessageKey as symbol]: messages as unknown as MessageApi, [projectGenerationRevisionKey as symbol]: revision } } }) await flushPromises() return { saved, save, messages, revision } } describe('项目配置的业务冲突反馈', () => { it.each([ ['CANVAS_CHANGE_REQUIRES_REVALIDATION', '画幅'], ['CREATIVE_PROFILE_ACTIVE_WORKFLOW', '任务'], ['GENERATION_CONFIG_CHANGE_REQUIRES_REVALIDATION', '模型'] ])('保存遇到 %s 时只提示业务原因、保留草稿且不重试', async (code, label) => { const { saved, save, messages, revision } = await settings() await wrapper!.findAllComponents(NSelect)[0]!.vm.$emit('update:value', '9:16') save.mockRejectedValue(new ApiError('请求失败(409)', 409, { code, keyframeCount: 1 })) await wrapper!.find('button').trigger('click') await flushPromises() expect(save).toHaveBeenCalledTimes(1) expect(messages.warning).toHaveBeenCalledWith(expect.stringContaining(label), expect.any(Object)) expect(messages.error).not.toHaveBeenCalled() expect(messages.success).not.toHaveBeenCalled() expect(wrapper!.findAllComponents(NSelect)[0]!.props('value')).toBe('9:16') expect(wrapper!.emitted('saved')).toBeUndefined() expect(revision.value).toBe(0) expect(saved.aspectRatio).toBe('1:1') expect(wrapper!.text()).not.toContain('请求失败') }) it('保存成功使用统一消息,不遗留重复的常驻成功提示', async () => { const { save, messages, revision } = await settings() await wrapper!.find('button').trigger('click') await flushPromises() expect(save).toHaveBeenCalledTimes(1) expect(messages.success).toHaveBeenCalledTimes(1) expect(wrapper!.find('.context-notice-success').exists()).toBe(false) expect(revision.value).toBe(1) }) })