feat(short-drama-agent-front): 优化身份管理与镜头素材布局

This commit is contained in:
GJ committed 2026-10-11 21:41:56 +08:00
1 parent dd0bd79a35
commit 3d2c198e52
14 files changed
+1060 -629

No files matched your search

+235 -161
View File
@@ -1,10 +1,11 @@
import { imageCatalog, generationProfile } from '../../fixtures/generation'
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { NButton, NImage, NScrollbar } from 'naive-ui'
import { AppDialog, AssetImage } from '@/components/ui'
import { NButton, NImage } from 'naive-ui'
import { AppDialog } from '@/components/ui'
import IdentityImageDialog from '@/features/subject-identity/components/IdentityImageDialog.vue'
import IdentityGallery from '@/features/subject-identity/components/IdentityGallery.vue'
import IdentityImageCard from '@/features/subject-identity/components/IdentityImageCard.vue'
import CastingCandidateDialog from '@/features/subject-identity/components/CastingCandidateDialog.vue'
import { subjectIdentityApi } from '@/features/subject-identity/api'
import {
@@ -16,7 +17,7 @@ import {
} from '@/features/subject-identity/model'
import { identityImageFixture } from '@/features/subject-identity/testing/fixtures'
import { formFixture } from '@/features/subject-images/testing/fixtures'
import { expandSections, selectControl } from '@/testing/naive'
import { selectControl } from '@/testing/naive'
let wrapper: VueWrapper | undefined
afterEach(() => {
@@ -26,13 +27,29 @@ afterEach(() => {
vi.unstubAllGlobals()
})
/** 从真实 Naive 弹窗内获取确认按钮。 */
/**
* 按可见文本或无障碍名称查找真实弹窗与标签操作按钮。
* @param label 按钮文本或无障碍名称
* @returns 对应的原生按钮
*/
function button(label: string) {
const item = [...document.querySelectorAll('button')].find(element => element.textContent?.trim() === label)
const item = [...document.querySelectorAll('button')].find(
element => element.textContent?.trim() === label || element.getAttribute('aria-label') === label
)
if (!item) throw new Error(`缺少按钮 ${label}`)
return item
}
/**
* 在指定图片卡片中获取操作按钮,避免不同候选的同名按钮混用。
* @param card 当前图片卡片
* @param label 目标操作文本
* @returns 当前卡片的按钮包装器
*/
function cardButton(card: VueWrapper, label: string) {
return card.findAllComponents(NButton).find(item => item.text() === label)!
}
/** 修改 Portal 表单控件并触发 Vue 绑定。 */
function input(selector: string, value: string) {
if (selector === '#identity-view' || selector === '#identity-reference') {
@@ -48,7 +65,78 @@ function input(selector: string, value: string) {
const creativeProfile = generationProfile('project-1')
describe('身份图与母版契约', () => {
it('详情大图 contain 完整显示,历史仍为 cover,缩略图只切换记录', async () => {
it('主页面只显示权威母版,所有候选与辅助、失败和进行中记录在管理弹窗中浏览', async () => {
const images = [
identityImageFixture({ id: 'candidate', isAnchor: false }),
identityImageFixture({
id: 'failed',
status: 'failed',
imageUrl: null,
isAnchor: false,
error: '模型超时'
}),
identityImageFixture({
id: 'anchor',
imageUrl: '/storage/anchor.png',
provider: 'qwen-image',
model: 'qwen-image-3.0-pro'
}),
identityImageFixture({ id: 'front', viewType: 'front', isAnchor: false }),
identityImageFixture({ id: 'pending', status: 'pending', imageUrl: null, isAnchor: false }),
identityImageFixture({ id: 'generating', status: 'generating', imageUrl: null, isAnchor: false })
]
wrapper = mount(IdentityGallery, {
attachTo: document.body,
props: { subjectName: '林夏', module: 'character', identityLocked: true, disabled: false, images }
})
expect(wrapper.findAll('.identity-anchor-grid .identity-image-card')).toHaveLength(1)
expect(wrapper.getComponent(NImage).props()).toMatchObject({
objectFit: 'cover',
previewDisabled: false
})
expect(wrapper.getComponent(NImage).props('src')).toContain('/storage/anchor.png')
expect(wrapper.text()).not.toContain('qwen-image')
expect(wrapper.find('.image-history').exists()).toBe(false)
expect(document.querySelector('.identity-candidate-grid')).toBeNull()
button('管理候选(5)').click()
await flushPromises()
const dialog = document.querySelector('[role="dialog"][aria-label="身份候选管理"]')!
expect(dialog.querySelectorAll('.identity-image-card')).toHaveLength(5)
expect(dialog.querySelector('[aria-label="身份图片 anchor"]')).toBeNull()
for (const id of ['candidate', 'failed', 'front', 'pending', 'generating']) {
expect(dialog.querySelector(`[aria-label="身份图片 ${id}"]`)).not.toBeNull()
}
expect(dialog.textContent).toContain('排队中')
expect(dialog.textContent).toContain('生成中')
expect(wrapper.emitted('anchor')).toBeUndefined()
expect(wrapper.emitted('validate')).toBeUndefined()
expect(wrapper.emitted('regenerate')).toBeUndefined()
})
it('候选未确认时不填补母版空状态,无图片时不提供管理入口', async () => {
wrapper = mount(IdentityGallery, {
attachTo: document.body,
props: {
subjectName: '林夏',
module: 'character',
identityLocked: false,
disabled: false,
images: [identityImageFixture({ id: 'candidate', isAnchor: false })]
}
})
expect(wrapper.text()).toContain('尚未确认身份母版')
expect(wrapper.find('.identity-anchor-grid').exists()).toBe(false)
button('管理候选(1)').click()
await flushPromises()
expect(document.querySelector('[aria-label="身份图片 candidate"]')).not.toBeNull()
expect(wrapper.emitted('anchor')).toBeUndefined()
await wrapper.setProps({ images: [] })
expect(wrapper.text()).toContain('尚无身份母版')
expect(wrapper.find('.identity-anchor-grid').exists()).toBe(false)
expect(wrapper.findAll('button').some(item => item.text().includes('管理候选'))).toBe(false)
})
it('失败详情按需在浮层显示,关闭管理同步收起详情,重试仅发出原记录 ID', async () => {
wrapper = mount(IdentityGallery, {
attachTo: document.body,
props: {
@@ -57,141 +145,66 @@ describe('身份图与母版契约', () => {
identityLocked: true,
disabled: false,
images: [
identityImageFixture({ id: 'anchor', imageUrl: '/storage/anchor.png' }),
identityImageFixture({ id: 'anchor' }),
identityImageFixture({
id: 'front',
viewType: 'front',
id: 'interrupted',
status: 'failed',
imageUrl: null,
isAnchor: false,
imageUrl: '/storage/front.png'
error: '服务重启时图片 Provider 请求状态不可恢复,请人工确认后重新生成'
})
]
}
})
const images = wrapper.findAllComponents(NImage)
expect(images).toHaveLength(3)
expect(images.map(image => image.props('objectFit'))).toEqual(['contain', 'cover', 'cover'])
expect(images.map(image => image.props('previewDisabled'))).toEqual([false, true, true])
expect(wrapper.find('a[target="_blank"]').exists()).toBe(false)
expect(wrapper.text()).not.toContain('打开原图')
await wrapper.get('[aria-label="查看身份图片 front"] img').trigger('click')
expect(wrapper.find('.identity-image-failure').exists()).toBe(false)
button('管理候选(1)').click()
await flushPromises()
expect(wrapper.get('[aria-label="查看身份图片 front"]').attributes('aria-pressed')).toBe('true')
expect(document.querySelector('.n-image-preview-container')).toBeNull()
const main = wrapper.get('.asset-image-preview img')
expect((main.element as HTMLImageElement).style.objectFit).toBe('contain')
expect(main.attributes('src')).toContain('/storage/front.png')
await main.trigger('click')
const card = document.querySelector('[aria-label="身份图片 interrupted"]')!
expect(card.textContent).not.toContain('核对原任务结果')
expect(document.querySelector('.identity-image-failure')).toBeNull()
expect(button('生成失败,查看失败原因').getAttribute('aria-expanded')).toBe('false')
button('生成失败,查看失败原因').click()
await flushPromises()
const original = document.querySelector<HTMLImageElement>('.n-image-preview')!
expect(original.getAttribute('src')).toBe(main.attributes('src'))
expect(original.style.objectFit).not.toBe('cover')
const failure = document.querySelector('.identity-image-failure')!
expect(failure.closest('.identity-failure-scroll.n-scrollbar')).not.toBeNull()
expect(failure.closest('.identity-image-card')).toBeNull()
expect(failure.closest('.app-dialog')).toBeNull()
expect(button('生成失败,查看失败原因').getAttribute('aria-expanded')).toBe('true')
expect(failure.textContent).toContain('图片生成中断,结果未确认')
expect(failure.textContent).toContain('核对原任务结果')
expect(failure.textContent).toContain('重复生成和计费')
expect(failure.textContent).not.toContain('Provider')
expect(failure.querySelector('button')).toBeNull()
expect(failure.textContent).not.toContain('查看当前母版')
document.querySelector<HTMLButtonElement>('[aria-label="身份候选管理"] .n-card-header__close')!.click()
await flushPromises()
expect(
wrapper
.findAllComponents(AppDialog)
.find(item => item.props('title') === '身份候选管理')!
.props('open')
).toBe(false)
button('管理候选(1)').click()
await flushPromises()
expect(button('生成失败,查看失败原因').getAttribute('aria-expanded')).toBe('false')
button('生成失败,查看失败原因').click()
await flushPromises()
button('生成失败,查看失败原因').dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }))
await flushPromises()
expect(button('生成失败,查看失败原因').getAttribute('aria-expanded')).toBe('false')
button('重新生成角色候选图').click()
await flushPromises()
expect(wrapper.emitted('regenerate')).toEqual([['interrupted']])
expect(wrapper.emitted('anchor')).toBeUndefined()
expect(
wrapper
.findAllComponents(AppDialog)
.find(item => item.props('title') === '身份候选管理')!
.props('open')
).toBe(false)
})
it('身份图库使用精简资产,不展示已移除的提示词、模型信息和启停状态', () => {
wrapper = mount(IdentityGallery, {
props: {
subjectName: '林夏',
module: 'character',
identityLocked: true,
disabled: false,
images: [identityImageFixture({ provider: 'qwen-image', model: 'qwen-image-3.0-pro' })]
}
})
expect(wrapper.text()).toContain('当前身份母版')
expect(wrapper.text()).not.toContain('本次实际提示词')
expect(wrapper.text()).not.toContain('停用')
expect(wrapper.text()).not.toContain('qwen-image')
expect(wrapper.text()).toContain('创建于')
})
it('全部历史记录显示在预览下方,候选、辅助、失败和进行中图片均可切换查看', async () => {
const images = [
identityImageFixture({
id: 'candidate',
imageUrl: '/storage/candidate.png',
isAnchor: false
}),
identityImageFixture({
id: 'failed',
status: 'failed',
imageUrl: null,
isAnchor: false,
error: '图片模型超时'
}),
identityImageFixture({ id: 'anchor', imageUrl: '/storage/anchor.png', width: 4096, height: 4096 }),
identityImageFixture({ id: 'front', imageUrl: '/storage/front.png', viewType: 'front', isAnchor: false }),
identityImageFixture({ id: 'pending', status: 'pending', imageUrl: null, isAnchor: false }),
identityImageFixture({
id: 'generating',
status: 'generating',
imageUrl: null,
isAnchor: false
})
]
wrapper = mount(IdentityGallery, {
attachTo: document.body,
props: { subjectName: '林夏', module: 'character', identityLocked: true, disabled: false, images }
})
const history = wrapper.get('[aria-label="身份图片历史"]')
expect(history.findAll('.image-history-item')).toHaveLength(6)
expect(wrapper.getComponent(NScrollbar).props()).toMatchObject({
xScrollable: true,
trigger: 'none',
contentStyle: { width: 'max-content' }
})
expect(wrapper.get('.asset-image-preview').element.nextElementSibling?.textContent).toContain('历史记录 · 6 条')
expect(wrapper.get('.image-history-heading').element.nextElementSibling).toBe(history.element)
expect(history.text()).toContain('当前母版')
expect(history.text()).toContain('母版候选')
expect(history.text()).toContain('正面')
expect(history.text()).toContain('生成失败')
expect(history.text()).toContain('排队中')
expect(history.text()).toContain('生成中')
expect(history.get('[aria-label="查看身份图片 anchor"]').attributes('aria-pressed')).toBe('true')
for (const image of images) {
await history.get(`[aria-label="查看身份图片 ${image.id}"]`).trigger('click')
expect(history.findAll('[aria-pressed="true"]')).toHaveLength(1)
expect(history.get(`[aria-label="查看身份图片 ${image.id}"]`).attributes('aria-pressed')).toBe('true')
const preview = wrapper
.findAllComponents(AssetImage)
.find(item => item.classes().includes('asset-image-preview'))!
expect(preview.props('src')).toBe(image.status === 'completed' ? image.imageUrl : null)
expect(preview.props('alt')).toBe(`林夏 · ${image.viewType === 'primary' ? '身份母版' : '正面'}`)
}
await history.get('[aria-label="查看身份图片 failed"]').trigger('click')
expect(wrapper.get('.asset-image-preview').text()).toContain('本次生成失败')
expect(wrapper.findAll('[role="alert"]').map(item => item.text())).toContain('图片模型超时')
expect(button('确认选角并锁定').disabled).toBe(true)
expect(wrapper.emitted('anchor')).toBeUndefined()
})
it('刷新新增历史不会切回母版或重置横向位置,当前记录移除后才回退', async () => {
const anchor = identityImageFixture({ id: 'anchor' })
const front = identityImageFixture({ id: 'front', viewType: 'front', isAnchor: false })
wrapper = mount(IdentityGallery, {
props: {
subjectName: '林夏',
module: 'character',
identityLocked: true,
disabled: false,
images: [anchor, front]
}
})
await wrapper.get('[aria-label="查看身份图片 front"]').trigger('click')
const viewport = wrapper.get<HTMLElement>('.image-history .n-scrollbar-container').element
viewport.scrollLeft = 120
await wrapper.setProps({ images: [identityImageFixture({ id: 'new', isAnchor: false }), anchor, front] })
expect(wrapper.get('[aria-label="查看身份图片 front"]').attributes('aria-pressed')).toBe('true')
expect(wrapper.get('.image-history .n-scrollbar-container').element).toBe(viewport)
expect(viewport.scrollLeft).toBe(120)
await wrapper.setProps({ images: [anchor] })
expect(wrapper.get('[aria-label="查看身份图片 anchor"]').attributes('aria-pressed')).toBe('true')
expect(wrapper.get('.image-history-heading').text()).toContain('历史记录 · 1 条')
})
it('切换历史取消上一张的确认,只有再次确认才能发出当前候选 ID', async () => {
it('辅助图失败保留真实原因,草稿阻塞时说明恢复方向并禁止重试', async () => {
wrapper = mount(IdentityGallery, {
attachTo: document.body,
props: {
@@ -199,30 +212,85 @@ describe('身份图与母版契约', () => {
module: 'character',
identityLocked: false,
disabled: false,
images: ['candidate-a', 'candidate-b'].map(id => identityImageFixture({ id, isAnchor: false }))
retryDisabledReason: '请先保存身份文本修改,再重新生成图片。',
images: [
identityImageFixture({
id: 'failed-front',
viewType: 'front',
status: 'failed',
imageUrl: null,
isAnchor: false,
error: '图片模型超时'
})
]
}
})
button('管理候选(1)').click()
await flushPromises()
expect(document.querySelector('[aria-label="身份图片 failed-front"]')!.textContent).not.toContain(
'图片模型超时'
)
button('生成失败,查看失败原因').click()
await flushPromises()
const notice = document.querySelector('.identity-image-failure')!
expect(notice.textContent).toContain('图片模型超时')
expect(notice.textContent).not.toContain('后台服务重启')
expect(notice.textContent).toContain('请先保存身份文本修改')
expect(button('重新生成辅助参考图').disabled).toBe(true)
button('重新生成辅助参考图').click()
await flushPromises()
expect(wrapper.emitted('regenerate')).toBeUndefined()
await wrapper.setProps({ retryDisabledReason: '' })
button('重新生成辅助参考图').click()
await flushPromises()
expect(wrapper.emitted('regenerate')).toEqual([['failed-front']])
})
it('明确确认目标后才提交指定候选,刷新同一母版保持管理弹窗,新母版返回后关闭弹窗', async () => {
const anchor = identityImageFixture({ id: 'anchor' })
const candidate = identityImageFixture({ id: 'candidate', isAnchor: false })
wrapper = mount(IdentityGallery, {
attachTo: document.body,
props: {
subjectName: '林夏',
module: 'character',
identityLocked: true,
disabled: false,
images: [anchor, candidate]
}
})
button('管理候选(1)').click()
await flushPromises()
const management = wrapper.findAllComponents(AppDialog).find(item => item.props('title') === '身份候选管理')!
button('确认选角并锁定').click()
await flushPromises()
expect(document.querySelector('[role="dialog"][aria-label="确认选角并锁定"]')).not.toBeNull()
expect(button('确认演员选择').disabled).toBe(false)
await wrapper.get('[aria-label="查看身份图片 candidate-b"]').trigger('click')
expect(wrapper.findComponent(AppDialog).props('open')).toBe(false)
expect(wrapper.emitted('anchor')).toBeUndefined()
button('取消').click()
await flushPromises()
expect(wrapper.emitted('anchor')).toBeUndefined()
button('确认选角并锁定').click()
await flushPromises()
button('确认演员选择').click()
expect(wrapper.emitted('anchor')).toEqual([['candidate-b']])
})
it('无历史图片时只展示空状态,不伪造缩略图或母版', () => {
wrapper = mount(IdentityGallery, {
props: { subjectName: '林夏', module: 'character', identityLocked: false, disabled: false, images: [] }
await flushPromises()
expect(wrapper.emitted('anchor')).toEqual([['candidate']])
expect(management.props('open')).toBe(true)
await wrapper.setProps({
images: [anchor, { ...candidate }, identityImageFixture({ id: 'new', isAnchor: false })]
})
expect(wrapper.text()).toContain('身份参考图 · 0')
expect(wrapper.text()).toContain('尚无身份参考图')
expect(wrapper.find('.image-history').exists()).toBe(false)
expect(wrapper.find('.asset-image-preview').exists()).toBe(false)
expect(management.props('open')).toBe(true)
expect(wrapper.get('.identity-anchor-grid .identity-image-card').attributes('aria-label')).toBe(
'身份图片 anchor'
)
await wrapper.setProps({
images: [
{ ...anchor, isAnchor: false },
{ ...candidate, isAnchor: true }
]
})
expect(management.props('open')).toBe(false)
expect(wrapper.get('.identity-anchor-grid .identity-image-card').attributes('aria-label')).toBe(
'身份图片 candidate'
)
})
it('启用的辅助视角不是母版,primary 候选停用时仍可选为母版', () => {
@@ -347,7 +415,7 @@ describe('身份图与母版契约', () => {
expect(select.props('value')).toBe('front')
})
it('辅助图不能切换母版,精简资产不展示内部提示词', async () => {
it('辅助图只有校验入口,不能通过候选弹窗切换母版', async () => {
wrapper = mount(IdentityGallery, {
attachTo: document.body,
props: {
@@ -358,10 +426,12 @@ describe('身份图与母版契约', () => {
images: [identityImageFixture({ viewType: 'front', isAnchor: false })]
}
})
expect(button('确认选角并锁定').disabled).toBe(true)
await expandSections()
expect(wrapper.text()).not.toContain('本次实际提示词')
expect(wrapper.find('script').exists()).toBe(false)
button('管理候选(1)').click()
await flushPromises()
const card = wrapper.findAllComponents(IdentityImageCard)[0]!
expect(card.findAllComponents(NButton).some(item => item.text() === '确认选角并锁定')).toBe(false)
expect(card.text()).toContain('不能直接设为身份母版')
expect(card.text()).not.toContain('本次实际提示词')
expect(wrapper.emitted('anchor')).toBeUndefined()
})
@@ -383,7 +453,7 @@ describe('身份图与母版契约', () => {
expect(wrapper.emitted('anchor')).toEqual([['identity-image-1']])
})
it('图片操作加载只对应提交目标,不能重复确认,切换图片不会错误显示加载', async () => {
it('多张候选同时可见时加载只绑定提交目标,操作期间所有重复提交均禁用', async () => {
wrapper = mount(IdentityGallery, {
attachTo: document.body,
props: {
@@ -394,24 +464,28 @@ describe('身份图与母版契约', () => {
images: ['first', 'second'].map(id => identityImageFixture({ id, isAnchor: false }))
}
})
button('管理候选(2)').click()
await flushPromises()
const cards = wrapper.findAllComponents(IdentityImageCard)
const first = cards.find(item => item.props('image').id === 'first')!
const second = cards.find(item => item.props('image').id === 'second')!
await wrapper.setProps({
pendingOperation: { projectId: 'project-1', subjectId: 'subject-1', imageId: 'first', kind: 'confirm' }
})
const confirm = wrapper.findAllComponents(NButton).find(item => item.text() === '确认选角并锁定')!
expect(confirm.props('loading')).toBe(true)
expect(button('确认选角并锁定').disabled).toBe(true)
button('确认选角并锁定').click()
expect(cardButton(first, '确认选角并锁定').props('loading')).toBe(true)
expect(cardButton(second, '确认选角并锁定').props('loading')).toBe(false)
expect(cardButton(first, '确认选角并锁定').props('disabled')).toBe(true)
expect(cardButton(second, '确认选角并锁定').props('disabled')).toBe(true)
await cardButton(second, '确认选角并锁定').trigger('click')
expect(wrapper.emitted('anchor')).toBeUndefined()
await wrapper.get('[aria-label="查看身份图片 second"]').trigger('click')
expect(confirm.props('loading')).toBe(false)
await wrapper.setProps({
pendingOperation: { projectId: 'project-1', subjectId: 'subject-1', imageId: 'second', kind: 'validate' }
})
const validate = wrapper.findAllComponents(NButton).find(item => item.text() === '校验此身份图片')!
expect(validate.props('loading')).toBe(true)
expect(button('校验此身份图片').disabled).toBe(true)
expect(cardButton(first, '校验此身份图片').props('loading')).toBe(false)
expect(cardButton(second, '校验此身份图片').props('loading')).toBe(true)
expect(cardButton(second, '校验此身份图片').props('disabled')).toBe(true)
await wrapper.setProps({ pendingOperation: null })
expect(validate.props('loading')).toBe(false)
expect(cardButton(second, '校验此身份图片').props('loading')).toBe(false)
})
it('选角候选发送当前图片 Provider 与动态参数,不携带旧宽高字段', async () => {