import { flushPromises, mount } from '@vue/test-utils' import { NCollapse } from 'naive-ui' import { afterEach, describe, expect, it, vi } from 'vitest' import FormSourceDiagnostics from '@/features/subject-images/components/FormSourceDiagnostics.vue' import { formFixture } from '@/features/subject-images/testing/fixtures' afterEach(() => vi.unstubAllGlobals()) describe('道具与场景来源诊断', () => { it.each([ ['missing', '尚未准备结构规格', null, false], ['stale', '结构来源已过期', 'failed', false], ['stale', '结构来源已过期', 'completed', true] ])('道具 %s 只读取原文和历史,不调用生成', async (status, label, attemptStatus, canReuse) => { const form = formFixture('source-project') form.subject.module = 'prop' const fetcher = vi.fn( async () => new Response( JSON.stringify({ data: { subjectFormId: form.id, status, spec: null, sourceEpisodes: [ { episodeId: 'origin', episode: 1, title: '基础形态' }, { episodeId: 'change', episode: 2, title: '拆卸' } ], ownershipEvidence: [ { name: '搭扣', quantity: 2, episodeId: 'origin', sourceQuote: '工具箱有两个搭扣', relationEpisodeId: 'change', relationQuote: '搭扣从工具箱脱落' } ], lastGenerationAttempt: attemptStatus && { status: attemptStatus, sourceCurrent: false, error: '无法核对归属', evidence: { candidates: [{ semanticClass: '工具箱' }] }, inputSnapshot: {} } } }) ) ) vi.stubGlobal('fetch', fetcher) const wrapper = mount(FormSourceDiagnostics, { props: { form, projectId: 'source-project' } }) try { await flushPromises() expect(fetcher).not.toHaveBeenCalled() wrapper.getComponent(NCollapse).vm.$emit('update:expandedNames', ['source']) await flushPromises() expect(wrapper.text()).toContain(label) expect(wrapper.text()).toContain('数量来源 · 第 1 集') expect(wrapper.text()).toContain('拆装来源 · 第 2 集') expect(wrapper.text().includes('准备时的来源已过期')).toBe(Boolean(attemptStatus)) expect(wrapper.findAll('button').some(button => button.text() === '免费复用已有规格')).toBe(canReuse) expect(fetcher.mock.calls).toHaveLength(1) expect(fetcher.mock.calls[0]?.[1]?.method).toBe('GET') } finally { wrapper.unmount() } }) })