fix(short-drama-agent-front): 修复资产库请求与管理流程

This commit is contained in:
GJ committed 2026-11-01 22:17:59 +08:00
1 parent 41d1cfe3ed
commit f8b0fbb8ca
10 files changed
+672 -77

No files matched your search

+137 -1
View File
@@ -1,7 +1,7 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { DOMWrapper, flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import { createMemoryHistory, createRouter } from 'vue-router'
import { NSelect, NTooltip } from 'naive-ui'
import { NButton, NSelect, NTooltip } from 'naive-ui'
import AssetLibraryPage from '@/features/project-assets/AssetLibraryPage.vue'
import AssetCategorySelect from '@/features/project-assets/components/AssetCategorySelect.vue'
@@ -14,6 +14,139 @@ afterEach(() => {
})
describe('全局资产库', () => {
it('删除进行中只标记删除loading,不把保存按钮显示为正在保存', async () => {
const asset = {
id: 'deleting-asset',
projectId: 'p',
name: '待删除测试图',
type: 'image',
category: 'reference',
mimeType: 'image/png',
extension: 'png',
size: 1024,
publicUrl: '/storage/fixture.png',
metadata: null,
createdAt: '2026-11-01',
updatedAt: '2026-11-01',
project: { id: 'p', title: '测试剧本', topic: '主题', status: 'completed' }
}
let finish!: (response: Response) => void
vi.stubGlobal(
'fetch',
vi.fn<typeof fetch>().mockImplementation(async (input, init) => {
if (init?.method === 'DELETE')
return new Promise<Response>(resolve => {
finish = resolve
})
return new Response(
JSON.stringify({ data: String(input).endsWith('/projects') ? [asset.project] : [asset] })
)
})
)
wrapper = mount(AssetLibraryPage, { attachTo: document.body })
await flushPromises()
await wrapper.get('.asset-library-card').trigger('click')
await flushPromises()
;[...document.querySelectorAll<HTMLButtonElement>('button')]
.find(button => button.textContent?.trim() === '删除素材')!
.click()
await flushPromises()
;[...document.querySelectorAll<HTMLButtonElement>('button')]
.find(button => button.textContent?.trim() === '确认删除')!
.click()
await flushPromises()
try {
const buttons = wrapper.findAllComponents(NButton)
expect(buttons.find(button => button.text().trim() === '保存修改')!.props('loading')).toBe(false)
expect(buttons.find(button => button.text().trim() === '删除素材')!.props('loading')).toBe(true)
} finally {
finish(new Response(JSON.stringify({ data: asset })))
await flushPromises()
}
})
it('保存成功后列表刷新失败时保留旧结果,只读详情不再允许重复写入', async () => {
const asset = {
id: 'retained-asset',
projectId: 'p',
name: '保留图片',
type: 'image',
category: 'reference',
mimeType: 'image/png',
extension: 'png',
size: 1024,
publicUrl: '/storage/fixture.png',
metadata: null,
createdAt: '2026-11-01',
updatedAt: '2026-11-01',
project: { id: 'p', title: '保留剧本', topic: '主题', status: 'completed' }
}
let updated = false
vi.stubGlobal(
'fetch',
vi.fn<typeof fetch>().mockImplementation(async (input, init) => {
if (init?.method === 'PUT') {
updated = true
return new Response(JSON.stringify({ data: asset }))
}
if (String(input).endsWith('/projects')) return new Response(JSON.stringify({ data: [asset.project] }))
return updated ? new Response('', { status: 502 }) : new Response(JSON.stringify({ data: [asset] }))
})
)
wrapper = mount(AssetLibraryPage, { attachTo: document.body })
await flushPromises()
await wrapper.get('.asset-library-card').trigger('click')
await flushPromises()
const first = document.querySelector('[role="dialog"][aria-label="素材详情与管理"]')!
;[...first.querySelectorAll<HTMLButtonElement>('button')]
.find(button => button.textContent?.trim() === '保存修改')!
.click()
await flushPromises()
expect(wrapper.findAll('.asset-library-card')).toHaveLength(1)
expect(wrapper.text()).toContain('写操作已暂停')
await wrapper.get('.asset-library-card').trigger('click')
await flushPromises()
const current = document.querySelector('[role="dialog"][aria-label="素材详情与管理"]')!
expect(
[...current.querySelectorAll<HTMLButtonElement>('button')]
.filter(button => ['保存修改', '删除素材'].includes(button.textContent?.trim() ?? ''))
.every(button => button.disabled)
).toBe(true)
expect(
[...current.querySelectorAll<HTMLButtonElement>('button')].find(
button => button.textContent?.trim() === '取消'
)!.disabled
).toBe(false)
})
it('首次502失败后结束加载,一次重连同时恢复资产和剧本选项', async () => {
let available = false
const fetcher = vi.fn<typeof fetch>().mockImplementation(async input =>
available
? new Response(
JSON.stringify({
data: String(input).endsWith('/projects')
? [{ id: 'project-one', title: '可用剧本', topic: '主题' }]
: []
})
)
: new Response('', { status: 502 })
)
vi.stubGlobal('fetch', fetcher)
wrapper = mount(AssetLibraryPage, { attachTo: document.body })
await flushPromises()
expect(wrapper.find('[role="status"]').exists()).toBe(false)
expect(wrapper.find('.empty-state').exists()).toBe(false)
available = true
const reconnect = wrapper.findAll('button').find(button => button.text().trim() === '重新连接')!
await reconnect.trigger('click')
await flushPromises()
expect(wrapper.text()).not.toContain('剧本列表读取失败')
expect(wrapper.text()).toContain('资产库还是空的')
const upload = wrapper.findAll('button').find(button => button.text().trim() === '上传图片')!
expect(upload.attributes('disabled')).toBeUndefined()
})
it('单次读取并直接显示全部剧本图片,支持按图片与剧本搜索', async () => {
const assets = [
{
@@ -124,5 +257,8 @@ describe('全局资产库', () => {
expect(wrapper.findAll('.asset-library-card')).toHaveLength(1)
expect(wrapper.text()).toContain('林默正面照')
expect(wrapper.text()).not.toContain('雨夜街道')
await wrapper.get('input[aria-label="搜索资产图片"]').setValue('人物')
expect(wrapper.findAll('.asset-library-card')).toHaveLength(1)
expect(wrapper.text()).toContain('林默正面照')
})
})