import { validationPreviewResponse } from '../../fixtures/image-preview' import { mount, flushPromises, type VueWrapper } from '@vue/test-utils' import { defineComponent, h } from 'vue' import { afterEach, describe, expect, it, vi } from 'vitest' import ImageGalleryDialog from '@/features/subject-images/components/ImageGalleryDialog.vue' import { getOperation } from '@/features/workflows/operations' import { formFixture, imageFixture } from '@/features/subject-images/testing/fixtures' import { subjectImagesApi } from '@/features/subject-images/api' /** 当前测试挂载的图库。 */ let wrapper: VueWrapper | undefined /** 清理测试请求、活动锁和弹层。 */ afterEach(() => { wrapper?.unmount() document.body.innerHTML = '' vi.unstubAllGlobals() Object.assign(getOperation('quality-flow'), { pending: false, cause: null, error: '', notice: '' }) }) /** * 根据中文名称选择真实确认按钮。 * @param label 完整按钮文本 * @returns 原生按钮 */ function button(label: string) { const value = [...document.querySelectorAll('button')].find(item => item.textContent?.trim() === label) if (!value) throw new Error(`缺少按钮 ${label}`) return value } describe('候选质量与主图门禁', () => { it('completed 但质量失败的候选保留,GET 不切主图,审核不自动采纳', async () => { const original = imageFixture({ id: 'old' }) const candidate = imageFixture({ id: 'candidate', isPrimary: false, qualityPassed: false, qualityIssues: [{ type: 'form_mismatch', severity: 'error', description: '原始数量不一致' }] }) const fetcher = vi.fn().mockImplementation(async (url, init) => String(url).endsWith('/validate/preview') ? validationPreviewResponse(String(url)) : new Response( JSON.stringify({ data: init?.method === 'POST' ? { subjectId: 'subject-db-1', subjectFormId: 'form-db-1', imageId: 'candidate', passed: false, sourceRequirementsPassed: false, sourceVisualRequirements: [], summary: '需核对数量', issues: [] } : [candidate, original] }) ) ) vi.stubGlobal('fetch', fetcher) wrapper = mount(ImageGalleryDialog, { attachTo: document.body, props: { projectId: 'quality-flow', form: formFixture(), disabled: false, open: true } }) await flushPromises() document.querySelector('[aria-label="查看图片 candidate"]')!.click() await flushPromises() expect(button('设为主参考图').disabled).toBe(true) expect(document.body.textContent).toContain('质量未通过') expect(fetcher.mock.calls.every(([, init]) => init?.method === 'GET')).toBe(true) button('审核此图片').click() await flushPromises() expect(fetcher.mock.calls.every(([, init]) => init?.method === 'GET')).toBe(true) button('确认费用并审核').click() await flushPromises() expect(fetcher.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(1) expect(fetcher.mock.calls.some(([, init]) => init?.method === 'PUT')).toBe(false) expect(original.isPrimary).toBe(true) expect(document.body.textContent).toContain('需核对数量') }) it('来源过期禁止发起审核与主图确认,保留该候选', async () => { const image = imageFixture({ isPrimary: false, qualityPassed: true }) const fetcher = vi .fn() .mockImplementation(async url => String(url).endsWith('/validate/preview') ? validationPreviewResponse(String(url), false) : new Response(JSON.stringify({ data: [image] })) ) vi.stubGlobal('fetch', fetcher) wrapper = mount(ImageGalleryDialog, { attachTo: document.body, props: { projectId: 'quality-flow', form: formFixture(), disabled: false, open: true } }) await flushPromises() expect(document.body.textContent).toContain('本次机位采用的几何已过期') expect(button('审核此图片').disabled).toBe(true) expect(button('设为主参考图').disabled).toBe(true) expect(fetcher.mock.calls.every(([, init]) => init?.method === 'GET')).toBe(true) }) it.each([504, 409])('审核回执 %s 后锁定同一候选的付费操作,重开图库仍保持锁定', async status => { const image = imageFixture({ id: 'unknown-review-' + status, isPrimary: false, qualityPassed: true }) const fetcher = vi.fn().mockImplementation(async (url, init) => { if (String(url).endsWith('/validate/preview')) return validationPreviewResponse(String(url)) if (init?.method === 'POST') return new Response(JSON.stringify({ message: '审核结果待核对' }), { status }) return Response.json({ data: [image] }) }) vi.stubGlobal('fetch', fetcher) const props = { projectId: 'quality-flow', form: formFixture(), disabled: false, open: true } /** 正式父页在项目请求进行中会同步禁用图库写入。 */ const parent = defineComponent({ setup: () => () => h(ImageGalleryDialog, { ...props, disabled: getOperation(props.projectId).pending }) }) wrapper = mount(parent, { attachTo: document.body }) await flushPromises() button('审核此图片').click() await flushPromises() button('确认费用并审核').click() await flushPromises() wrapper.unmount() wrapper = mount(parent, { attachTo: document.body }) await flushPromises() expect(document.body.textContent).toContain('费用待核对') expect(button('审核此图片').disabled).toBe(true) expect(button('审核与修复').disabled).toBe(true) expect(button('设为主参考图').disabled).toBe(true) button('刷新图片').click() await flushPromises() expect(button('审核此图片').disabled).toBe(true) expect(fetcher.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(1) }) it.each(['刷新图片', '确认费用并审核'])('同一候选来源变化后,%s 重新预检并阻止付费请求', async action => { let current = true const image = imageFixture({ id: 'stale-after-open', isPrimary: false, qualityPassed: true }) const fetcher = vi .fn() .mockImplementation(async url => String(url).endsWith('/validate/preview') ? validationPreviewResponse(String(url), current) : Response.json({ data: [image] }) ) vi.stubGlobal('fetch', fetcher) wrapper = mount(ImageGalleryDialog, { attachTo: document.body, props: { projectId: 'quality-flow', form: formFixture(), disabled: false, open: true } }) await flushPromises() expect(button('审核此图片').disabled).toBe(false) button('审核此图片').click() await flushPromises() current = false button(action).click() await flushPromises() expect(document.body.textContent).toContain('本次机位采用的几何已过期') expect(button('审核此图片').disabled).toBe(true) expect(button('设为主参考图').disabled).toBe(true) expect(fetcher.mock.calls.every(([, init]) => init?.method === 'GET')).toBe(true) }) it('主图晋升 409 保留原主图,不乐观切换或自动重试', async () => { const original = imageFixture({ id: 'old' }) const fetcher = vi.fn().mockImplementation(async (url, init) => String(url).endsWith('/validate/preview') ? validationPreviewResponse(String(url)) : init?.method === 'PUT' ? new Response(JSON.stringify({ message: '审核证据已过期,请明确重新审核' }), { status: 409 }) : new Response( JSON.stringify({ data: [imageFixture({ id: 'candidate', isPrimary: false, qualityPassed: true }), original] }) ) ) vi.stubGlobal('fetch', fetcher) wrapper = mount(ImageGalleryDialog, { attachTo: document.body, props: { projectId: 'quality-flow', form: formFixture(), disabled: false, open: true } }) await flushPromises() document.querySelector('[aria-label="查看图片 candidate"]')!.click() await flushPromises() button('设为主参考图').click() await flushPromises() button('确认费用并设为主图').click() await flushPromises() expect(fetcher.mock.calls.filter(([, init]) => init?.method === 'PUT')).toHaveLength(1) expect(document.body.textContent).toContain('审核证据已过期') expect(wrapper.emitted('changed')).toBeUndefined() expect(original.isPrimary).toBe(true) }) it('质量处理默认预算为零,只有明确确认后发出一次请求', async () => { const image = imageFixture({ isPrimary: false }) const fetcher = vi.fn().mockImplementation(async (url, init) => String(url).endsWith('/validate/preview') ? validationPreviewResponse(String(url)) : new Response( JSON.stringify({ data: init?.method === 'POST' ? { subjectFormId: 'form-db-1', initialImageId: image.id, finalImageId: image.id, passed: false, repaired: false, repairAttempts: 0, maxRepairAttempts: 0, primaryChanged: false, attempts: [] } : [image] }) ) ) vi.stubGlobal('fetch', fetcher) wrapper = mount(ImageGalleryDialog, { attachTo: document.body, props: { projectId: 'quality-flow', form: formFixture(), disabled: false, open: true } }) await flushPromises() button('审核与修复').click() await flushPromises() expect(fetcher.mock.calls.some(([, init]) => init?.method === 'POST')).toBe(false) expect(document.querySelector('#quality-repair-budget')?.value).toBe('0') button('确认费用并执行').click() await flushPromises() const calls = fetcher.mock.calls.filter(([, init]) => init?.method === 'POST') expect(calls).toHaveLength(1) expect(JSON.parse(calls[0]![1]!.body as string)).toEqual({ imageId: image.id, provider: image.provider, maxRepairAttempts: 0 }) expect(document.body.textContent).toContain('主图未切换') }) it('预览携带尺寸与负面描述,结构准备只调用正式文字入口', async () => { const fetcher = vi.fn().mockImplementation(async () => new Response(JSON.stringify({ data: {} }))) vi.stubGlobal('fetch', fetcher) const input = { width: 1024, height: 1024, negativePrompt: '不要额外配件', prompt: '原始要求' } await subjectImagesApi.preview('form/1', input) await subjectImagesApi.prepareStructure('form/1') expect(JSON.parse(fetcher.mock.calls[0]![1]!.body as string)).toEqual(input) expect(fetcher.mock.calls[1]![0]).toBe('/api/subject-forms/form%2F1/prop-structure-spec/generate') expect(fetcher).toHaveBeenCalledTimes(2) }) })