161 lines
7.7 KiB
TypeScript
161 lines
7.7 KiB
TypeScript
import { mount, flushPromises, type VueWrapper } from '@vue/test-utils'
|
|
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<typeof fetch>().mockImplementation(
|
|
async (_url, init) =>
|
|
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<HTMLButtonElement>('[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).toHaveBeenCalledOnce()
|
|
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('主图晋升 409 保留原主图,不乐观切换或自动重试', async () => {
|
|
const original = imageFixture({ id: 'old' })
|
|
const fetcher = vi.fn<typeof fetch>().mockImplementation(async (_url, init) =>
|
|
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<HTMLButtonElement>('[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<typeof fetch>().mockImplementation(
|
|
async (_url, init) =>
|
|
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<HTMLInputElement>('#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<typeof fetch>().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)
|
|
})
|
|
})
|