feat(short-drama-agent-front): 完善主体身份骨架屏与交互反馈

This commit is contained in:
GJ committed 2026-10-11 17:23:14 +08:00
1 parent 5fa1c251a9
commit dd0bd79a35
19 files changed
+1552 -419

No files matched your search

@@ -1,6 +1,18 @@
import { describe, expect, it, vi } from 'vitest'
import { getOperation, runOperation } from '@/features/workflows/operations'
/**
* 根据真实校验值决定消息,不能只依据 Promise 成功。
* @param result 后端业务校验结果
* @returns 与通过状态相符的临时反馈
*/
function validationFeedback(result: { passed: boolean }) {
return {
type: result.passed ? ('success' as const) : ('warning' as const),
content: result.passed ? '身份图片校验通过' : '身份图片校验未通过'
}
}
describe('项目级长请求互斥', () => {
it('同一项目不同时执行两个 graph,错误不伪装成功', async () => {
let fail!: (reason: Error) => void
@@ -17,4 +29,36 @@ describe('项目级长请求互斥', () => {
expect(await first).toBe(false)
expect(getOperation('operation-test')).toMatchObject({ pending: false, error: '连接中断', notice: '' })
})
it('HTTP 成功但业务校验未通过时保留警告语义,再次通过时更新结果', async () => {
const projectId = 'operation-validation-test'
expect(await runOperation(projectId, '校验', async () => ({ passed: false }), validationFeedback)).toBe(true)
expect(getOperation(projectId)).toMatchObject({
noticeType: 'warning',
notice: '身份图片校验未通过',
pending: false
})
expect(await runOperation(projectId, '校验', async () => ({ passed: true }), validationFeedback)).toBe(true)
expect(getOperation(projectId)).toMatchObject({ noticeType: 'success', notice: '身份图片校验通过' })
expect(
await runOperation(
projectId,
'校验',
async () => {
throw new Error('服务不可用')
},
validationFeedback
)
).toBe(false)
expect(getOperation(projectId)).toMatchObject({ noticeType: null, notice: '', error: '服务不可用' })
})
it('异步任务提交只提示已提交,不提前提示生成完成', async () => {
const projectId = 'operation-submission-test'
await runOperation(projectId, '提交图片生成', async () => ({ status: 'pending' }), {
type: 'info',
content: '任务已提交生成'
})
expect(getOperation(projectId)).toMatchObject({ noticeType: 'info', notice: '任务已提交生成' })
})
})
+303 -5
View File
@@ -28,6 +28,9 @@ import { getImageSession } from '@/features/subject-images/model'
import VisualStylePage from '@/features/visual-style/VisualStylePage.vue'
import { visualStyleApi } from '@/features/visual-style/api'
import SubjectIdentityPage from '@/features/subject-identity/SubjectIdentityPage.vue'
import IdentityGallery from '@/features/subject-identity/components/IdentityGallery.vue'
import { subjectIdentityApi } from '@/features/subject-identity/api'
import { subjectImagesApi } from '@/features/subject-images/api'
import { styleFixture, styleImageFixture } from '@/features/visual-style/testing/fixtures'
import { identityFixture, identityImageFixture } from '@/features/subject-identity/testing/fixtures'
import { getIdentitySession } from '@/features/subject-identity/model'
@@ -213,7 +216,7 @@ afterEach(() => {
Object.assign(getStoryboardSession(fixture.id), { receipt: null, prompts: {}, regeneratedEpisodes: {} })
getImageSession(fixture.id).receipt = null
getImageSession(fixture.id).promptReceipt = null
Object.assign(getOperation(fixture.id), { pending: false, label: '', error: '', notice: '' })
Object.assign(getOperation(fixture.id), { pending: false, label: '', error: '', notice: '', noticeType: null })
})
describe('内容级滚动下的生产反馈', () => {
@@ -304,6 +307,217 @@ describe('内容级滚动下的生产反馈', () => {
})
describe('视觉风格与主体身份工作区', () => {
it('当前主体已确认但其它角色未完成时,隐藏形态入口;保存后按新阶段状态开放', async () => {
/** 当前主体已锁定,其它角色仍未确认,用真实项目准入规则约束页面入口。 */
const saved = identityFixture({ isLocked: true })
vi.spyOn(subjectIdentityApi, 'get').mockResolvedValue(saved)
vi.spyOn(subjectIdentityApi, 'save').mockImplementation(async (_id, input) => ({ ...saved, ...input }))
vi.spyOn(subjectIdentityApi, 'listImages').mockResolvedValue([identityImageFixture()])
vi.spyOn(subjectIdentityApi, 'castingReadiness').mockResolvedValue({
total: 0,
ready: 0,
missingIdentity: 0,
missingAnchor: 0,
candidatePending: 0,
unlocked: 0,
locked: 0,
items: []
})
stubFetch(
vi
.fn<typeof fetch>()
.mockImplementation(async url =>
jsonResponse(String(url).endsWith('/subject-forms') ? [formFixture()] : styleFixture())
)
)
const provided = await mountAssets('identity')
provided.data.value!.stageReadiness.identity = { ready: false, reason: '还有角色身份需要确认' }
await flushPromises()
expect(wrapper!.get('.identity-next-action').text()).toContain('还有角色身份需要确认')
expect(wrapper!.get('.identity-next-action').find('button').exists()).toBe(false)
expect(wrapper!.get('.identity-subject-forms').text()).not.toContain('查看此主体形态图片')
expect(wrapper!.find('a[href*="/subject-images"]').exists()).toBe(false)
vi.mocked(provided.refreshStageReadiness).mockImplementation(async () => {
provided.data.value!.stageReadiness.identity = { ready: true, reason: null }
})
await wrapper!.get('#identity-description').setValue('保存后仍保留已确认母版')
button('保存主体身份').click()
await flushPromises()
expect(provided.refreshStageReadiness).toHaveBeenCalledOnce()
expect(button('查看形态图片').disabled).toBe(false)
expect(wrapper!.get('.identity-detail a[href*="/subject-images"]').attributes('href')).toContain(
'subjectRef=@CH0001'
)
})
it('生成进度与失败提示位于两栏上方,切换主体后不会保留其它主体的状态,完成后隐藏', async () => {
/** 两个主体分别处于生成中与失败状态,所有查询均使用隔离的测试响应。 */
const first = formFixture()
const second = { ...formFixture(), id: 'second-form', subjectId: 'second-subject', images: [] }
second.subject = { ...second.subject, id: second.subjectId, name: '第二个主体', ref: '@CH0002' }
vi.spyOn(subjectIdentityApi, 'get').mockImplementation(async id =>
identityFixture({ id: `identity-${id}`, subjectId: id })
)
vi.spyOn(subjectIdentityApi, 'listImages').mockImplementation(async id => [
identityImageFixture({
identityId: `identity-${id}`,
status: id === first.subjectId ? 'generating' : 'failed',
isAnchor: false,
imageUrl: null,
error: id === first.subjectId ? null : '模型暂时不可用'
})
])
vi.spyOn(subjectIdentityApi, 'castingReadiness').mockResolvedValue({
total: 0,
ready: 0,
missingIdentity: 0,
missingAnchor: 0,
candidatePending: 0,
unlocked: 0,
locked: 0,
items: []
})
stubFetch(
vi
.fn<typeof fetch>()
.mockImplementation(async url =>
jsonResponse(String(url).endsWith('/subject-forms') ? [first, second] : styleFixture())
)
)
await mountAssets('identity')
const feedback = wrapper!.get('.identity-generation-feedback')
const workspace = wrapper!.get('.identity-workspace')
expect(feedback.element.closest('.identity-workspace-region')).toBe(
wrapper!.get('.identity-workspace-region').element
)
expect(feedback.element.closest('.identity-detail')).toBeNull()
expect(feedback.element.compareDocumentPosition(workspace.element) & 4).toBe(4)
expect(feedback.text()).toContain('林知夏 的身份图片正在生成')
await wrapper!
.findAll('.identity-subject-item')
.find(item => item.text().includes('第二个主体'))!
.trigger('click')
await flushPromises()
expect(wrapper!.get('.identity-generation-feedback').text()).toContain('模型暂时不可用')
expect(wrapper!.get('.identity-generation-feedback').text()).not.toContain('林知夏 的身份图片正在生成')
expect(wrapper!.findAll('.identity-generation-feedback')).toHaveLength(1)
vi.mocked(subjectIdentityApi.listImages).mockResolvedValue([
identityImageFixture({ identityId: `identity-${second.subjectId}`, status: 'completed' })
])
await wrapper!
.get('.identity-detail')
.findAll('button')
.find(item => item.text() === '刷新身份')!
.trigger('click')
await flushPromises()
expect(wrapper!.find('.identity-generation-feedback').exists()).toBe(false)
})
it('主体目录与详情分别显示骨架,查询失败结束占位并可重试恢复', async () => {
/** 分别阻塞目录与详情,避免将未完成查询误显示为空身份。 */
let rejectCatalog!: (error: Error) => void
let rejectDetail!: (error: Error) => void
vi.spyOn(subjectImagesApi, 'listForms')
.mockImplementationOnce(
() =>
new Promise((_resolve, reject) => {
rejectCatalog = reject
})
)
.mockResolvedValue([formFixture()])
vi.spyOn(subjectIdentityApi, 'get')
.mockImplementationOnce(
() =>
new Promise((_resolve, reject) => {
rejectDetail = reject
})
)
.mockResolvedValue(null)
vi.spyOn(subjectIdentityApi, 'castingReadiness').mockResolvedValue({
total: 0,
ready: 0,
missingIdentity: 0,
missingAnchor: 0,
candidatePending: 0,
unlocked: 0,
locked: 0,
items: []
})
stubFetch(vi.fn<typeof fetch>().mockImplementation(async () => jsonResponse(styleFixture())))
await mountAssets('identity')
expect(wrapper!.get('.subject-identity-skeleton').attributes('aria-busy')).toBe('true')
expect(wrapper!.get('.subject-identity-skeleton').text()).toBe('')
expect(wrapper!.find('#identity-description').exists()).toBe(false)
expect(wrapper!.text()).not.toContain('尚无身份参考图')
rejectCatalog(new Error('主体目录读取失败'))
await flushPromises()
expect(wrapper!.find('.subject-identity-skeleton').exists()).toBe(false)
expect(wrapper!.get('.workspace-feedback [role="alert"]').text()).toContain('主体目录读取失败')
button('重试读取目录').click()
await flushPromises()
expect(wrapper!.find('.subject-identity-skeleton').exists()).toBe(false)
expect(wrapper!.findAll('.identity-subject-item')).toHaveLength(1)
expect(wrapper!.get('.identity-detail').text()).toContain('林知夏')
expect(wrapper!.get('.identity-detail-skeleton').attributes('aria-busy')).toBe('true')
rejectDetail(new Error('身份读取失败'))
await flushPromises()
expect(wrapper!.find('.identity-detail-skeleton').exists()).toBe(false)
expect(wrapper!.get('.identity-detail [role="alert"]').text()).toContain('身份读取失败')
expect(wrapper!.find('#identity-description').exists()).toBe(false)
button('刷新身份').click()
await flushPromises()
expect(wrapper!.get<HTMLInputElement>('#identity-description').element.value).toBe('')
expect(wrapper!.text()).toContain('尚无身份参考图')
})
it('已有身份刷新保留草稿与图库,图片操作归属图库并遵守未保存限制', async () => {
/** 刷新返回前后均保持编辑器实例,防止骨架导致草稿丢失。 */
let finishRefresh!: (value: ReturnType<typeof identityFixture>) => void
vi.spyOn(subjectIdentityApi, 'get')
.mockResolvedValueOnce(identityFixture())
.mockImplementationOnce(
() =>
new Promise(resolve => {
finishRefresh = resolve
})
)
vi.spyOn(subjectIdentityApi, 'listImages').mockResolvedValue([identityImageFixture()])
vi.spyOn(subjectIdentityApi, 'castingReadiness').mockResolvedValue({
total: 0,
ready: 0,
missingIdentity: 0,
missingAnchor: 0,
candidatePending: 0,
unlocked: 0,
locked: 0,
items: []
})
stubFetch(
vi
.fn<typeof fetch>()
.mockImplementation(async url =>
jsonResponse(String(url).endsWith('/subject-forms') ? [formFixture()] : styleFixture())
)
)
await mountAssets('identity')
const editor = wrapper!.get('#identity-description').element
const gallery = wrapper!.get('[aria-label="身份参考图库"]')
expect(gallery.get('.identity-gallery-actions').text()).toContain('生成辅助参考图')
expect(wrapper!.get('.page-form-actions').text()).not.toContain('生成辅助参考图')
expect(button('生成辅助参考图').disabled).toBe(false)
await wrapper!.get('#identity-description').setValue('保留人工身份草稿')
expect(button('生成辅助参考图').disabled).toBe(true)
button('刷新身份').click()
await flushPromises()
expect(wrapper!.find('.identity-detail-skeleton').exists()).toBe(false)
expect(wrapper!.get('#identity-description').element).toBe(editor)
expect(gallery.find('img').exists()).toBe(true)
finishRefresh(identityFixture({ description: '服务器刷新结果' }))
await flushPromises()
expect(wrapper!.get<HTMLTextAreaElement>('#identity-description').element.value).toBe('保留人工身份草稿')
expect(button('生成辅助参考图').disabled).toBe(true)
})
it('风格首读显示骨架,失败后展示重试,恢复后分类与规则始终可见', async () => {
/** 延迟首次响应,确认骨架不会混入尚未创建或空参考图提示。 */
let rejectStyle!: (error: Error) => void
@@ -503,7 +717,9 @@ describe('视觉风格与主体身份工作区', () => {
const put = fetcher.mock.calls.find(([, init]) => init?.method === 'PUT')!
expect(put[0]).toBe('/api/subjects/subject-db-1/identity')
expect(JSON.parse(put[1]!.body as string)).toMatchObject({ description: '人工稳定身份', isLocked: true })
expect(button('生成选角候选').disabled).toBe(false)
expect(wrapper!.get('.page-form-actions').text()).not.toContain('当前内容已保存到项目')
expect(wrapper!.get('.page-form-actions button[type="submit"]').text()).toBe('已保存')
expect(button('AI 生成角色候选图').disabled).toBe(false)
await new DOMWrapper(document.body).get('#identity-description').setValue('新的未保存草稿')
await wrapper!.findAll('.identity-subject-item')[1]!.trigger('click')
expect(document.body.textContent).toContain('切换会丢弃草稿')
@@ -515,6 +731,8 @@ describe('视觉风格与主体身份工作区', () => {
expect((new DOMWrapper(document.body).get('#identity-description').element as HTMLTextAreaElement).value).toBe(
''
)
expect(wrapper!.get('.page-form-actions button[type="submit"]').text()).toBe('保存主体身份')
expect(wrapper!.get('.page-form-actions').text()).not.toContain('已保存')
})
it('链接添加拒绝危险 URL,只传已有地址及分类排序,成功后关闭并清空弹窗', async () => {
@@ -697,7 +915,34 @@ describe('视觉风格与主体身份工作区', () => {
expect(wrapper!.text()).toContain('2 个形态')
expect(fetcher.mock.calls.some(([url]) => String(url).endsWith('/subjects/subject-db-1/identity'))).toBe(true)
expect(fetcher.mock.calls.some(([url]) => String(url).endsWith('/identity/images'))).toBe(false)
expect(button('生成身份文本').disabled).toBe(false)
expect(button('AI 生成主体身份').disabled).toBe(false)
})
it('AI 主体身份生成在表单底部保留唯一入口,覆盖已保存文本需要确认', async () => {
let identity = identityFixture()
const fetcher = vi.fn<typeof fetch>().mockImplementation(async (url, init) => {
if (String(url).endsWith('/subject-forms')) return jsonResponse([formFixture()])
if (String(url).endsWith('/visual-style')) return jsonResponse(styleFixture())
if (String(url).endsWith('/identity/images')) return jsonResponse([])
if (init?.method === 'POST') identity = identityFixture({ description: 'AI 重新生成的身份描述' })
return jsonResponse(identity)
})
stubFetch(fetcher)
await mountAssets('identity')
const actions = wrapper!.get('.page-form-actions')
expect(actions.text()).toContain('AI 生成主体身份')
expect(actions.findAll('button').map(item => item.text())).toEqual(['已保存', 'AI 生成主体身份'])
expect(actions.text()).not.toContain('当前内容已保存到项目')
expect(wrapper!.findAll('button').filter(item => item.text() === 'AI 生成主体身份')).toHaveLength(1)
button('AI 生成主体身份').click()
await flushPromises()
expect(fetcher.mock.calls.some(([, init]) => init?.method === 'POST')).toBe(false)
button('确认AI 生成主体身份').click()
await flushPromises()
const post = fetcher.mock.calls.find(([, init]) => init?.method === 'POST')!
expect(post[0]).toBe('/api/subjects/subject-db-1/identity/generate')
expect(JSON.parse(post[1]!.body as string)).toEqual({ force: true })
expect(wrapper!.get<HTMLTextAreaElement>('#identity-description').element.value).toBe('AI 重新生成的身份描述')
})
it('锁定阻止身份 AI 覆盖但不阻止人工编辑,并按步骤进入后续操作', async () => {
@@ -711,6 +956,7 @@ describe('视觉风格与主体身份工作区', () => {
stubFetch(fetcher)
await mountAssets('identity')
expect(wrapper!.text()).toContain('主体身份已完成')
expect(button('AI 生成主体身份').disabled).toBe(true)
await new DOMWrapper(document.body).get('#identity-description').setValue('人工稳定身份草稿')
expect(button('保存主体身份').disabled).toBe(false)
expect(fetcher.mock.calls.some(([, init]) => init?.method === 'POST')).toBe(false)
@@ -727,6 +973,7 @@ describe('视觉风格与主体身份工作区', () => {
)
await mountAssets('identity')
expect(button('设置视觉风格').disabled).toBe(false)
expect(button('AI 生成主体身份').disabled).toBe(true)
await new DOMWrapper(document.body).get('#identity-description').setValue('人工稳定身份草稿')
expect(button('保存主体身份').disabled).toBe(false)
})
@@ -758,6 +1005,50 @@ describe('视觉风格与主体身份工作区', () => {
expect(drawerPanel().text()).toContain('文本模型超时')
})
it.each([true, false])(
'身份图片校验按真实 passed=%s 反馈,等待时显示目标按钮加载,结果不驻留页面',
async passed => {
/** 延迟校验回执以覆盖请求期间的真实按钮状态。 */
let finish!: (response: Response) => void
const response = new Promise<Response>(resolve => {
finish = resolve
})
const fetcher = vi.fn<typeof fetch>().mockImplementation(async (url, init) => {
const path = String(url)
if (path.endsWith('/subject-forms')) return jsonResponse([formFixture()])
if (path.endsWith('/visual-style')) return jsonResponse(styleFixture())
if (init?.method === 'POST' && path.endsWith('/validate')) return response
if (path.endsWith('/identity/images')) return jsonResponse([identityImageFixture()])
return jsonResponse(identityFixture())
})
stubFetch(fetcher)
await mountAssets('identity')
const gallery = wrapper!.findComponent(IdentityGallery)
gallery.vm.$emit('validate', 'identity-image-1')
await flushPromises()
expect(gallery.props('pendingOperation')).toMatchObject({ imageId: 'identity-image-1', kind: 'validate' })
expect(button('校验此身份图片').disabled).toBe(true)
expect(getOperation(fixture.id).pending).toBe(true)
finish(
jsonResponse({
subjectId: 'subject-db-1',
imageId: 'identity-image-1',
passed,
summary: '本次视觉检查结果',
issues: []
})
)
await flushPromises()
expect(getOperation(fixture.id)).toMatchObject({
pending: false,
noticeType: passed ? 'success' : 'warning',
notice: `林知夏 的身份图片校验${passed ? '通过' : '未通过'}:本次视觉检查结果`
})
expect(gallery.props('pendingOperation')).toBeNull()
expect(wrapper!.text()).not.toContain('本次视觉检查结果')
}
)
it('身份候选母版切换需确认,只发送正确 Subject ID 的 anchor PUT', async () => {
let selected = false
const form = formFixture()
@@ -789,6 +1080,10 @@ describe('视觉风格与主体身份工作区', () => {
'/api/subjects/subject-db-1/identity/images/candidate/stable-anchor'
)
expect(wrapper!.text()).toContain('当前身份母版')
expect(getOperation(fixture.id)).toMatchObject({
noticeType: 'success',
notice: '林知夏 的身份母版已确认并锁定。'
})
const anchorImage = wrapper!
.findAllComponents(NImage)
.find(image => image.props('alt')?.endsWith('当前身份母版'))!
@@ -943,8 +1238,11 @@ describe('视觉风格与主体身份工作区', () => {
})
stubFetch(fetcher)
await mountAssets('identity')
expect(wrapper!.find('.subject-identity-skeleton').exists()).toBe(false)
expect(wrapper!.find('.identity-detail-skeleton').exists()).toBe(true)
await wrapper!.findAll('.identity-subject-item')[1]!.trigger('click')
await flushPromises()
expect(wrapper!.find('.identity-detail-skeleton').exists()).toBe(false)
expect(
fetcher.mock.calls.find(([url]) => String(url).endsWith('/subject-db-1/identity'))?.[1]?.signal?.aborted
).toBe(true)
@@ -966,14 +1264,14 @@ describe('视觉风格与主体身份工作区', () => {
})
stubFetch(fetcher)
const provided = await mountAssets('identity')
button('生成身份文本').click()
button('AI 生成主体身份').click()
await flushPromises()
expect(fetcher.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(1)
expect(getOperation(fixture.id).error).toContain('无法连接后端')
provided.data.value!.project = { ...fixture, status: 'generating' }
await flushPromises()
expect(new DOMWrapper(document.body).get('#identity-description').attributes('disabled')).toBeDefined()
expect(button('生成身份文本').disabled).toBe(true)
expect(button('AI 生成主体身份').disabled).toBe(true)
})
})