270 lines
13 KiB
TypeScript
270 lines
13 KiB
TypeScript
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 { NButton, NSelect, NTooltip } from 'naive-ui'
|
|
import AssetLibraryPage from '@/features/project-assets/AssetLibraryPage.vue'
|
|
import AssetCategorySelect from '@/features/project-assets/components/AssetCategorySelect.vue'
|
|
|
|
let wrapper: VueWrapper | undefined
|
|
afterEach(() => {
|
|
wrapper?.unmount()
|
|
wrapper = undefined
|
|
document.body.innerHTML = ''
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
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 = [
|
|
{
|
|
id: 'asset-scene',
|
|
projectId: 'project-one',
|
|
name: '雨夜街道',
|
|
type: 'image',
|
|
category: 'scene-layout',
|
|
mimeType: 'image/png',
|
|
extension: 'png',
|
|
size: 4096,
|
|
publicUrl: '/storage/scene.png',
|
|
metadata: { originalName: 'rain-street-original.png' },
|
|
createdAt: '2026-09-22T00:00:00Z',
|
|
updatedAt: '2026-09-22T00:00:00Z',
|
|
project: { id: 'project-one', title: '记忆当铺', topic: '记忆交易', status: 'completed' }
|
|
},
|
|
{
|
|
id: 'asset-character',
|
|
projectId: 'project-two',
|
|
name: '林默正面照',
|
|
type: 'image',
|
|
category: 'character',
|
|
mimeType: 'image/jpeg',
|
|
extension: 'jpg',
|
|
size: 8192,
|
|
publicUrl: '/storage/character.jpg',
|
|
metadata: null,
|
|
createdAt: '2026-09-22T01:00:00Z',
|
|
updatedAt: '2026-09-22T01:00:00Z',
|
|
project: { id: 'project-two', title: '天降甘霖', topic: '乡村故事', status: 'completed' }
|
|
}
|
|
]
|
|
const projects = assets.map(asset => ({
|
|
...asset.project,
|
|
style: null,
|
|
createdAt: '',
|
|
updatedAt: ''
|
|
}))
|
|
const fetcher = vi.fn<typeof fetch>().mockImplementation(async (input, init) => {
|
|
const url = String(input)
|
|
if (init?.method === 'PUT')
|
|
return new Response(JSON.stringify({ data: { ...assets[0], ...JSON.parse(String(init.body)) } }))
|
|
return new Response(JSON.stringify({ data: url.endsWith('/projects') ? projects : assets }))
|
|
})
|
|
vi.stubGlobal('fetch', fetcher)
|
|
const router = createRouter({
|
|
history: createMemoryHistory(),
|
|
routes: [
|
|
{ path: '/assets', component: AssetLibraryPage },
|
|
{ path: '/projects/:projectId/assets', component: { template: '<div />' } }
|
|
]
|
|
})
|
|
await router.push('/assets')
|
|
wrapper = mount(AssetLibraryPage, { attachTo: document.body, global: { plugins: [router] } })
|
|
await flushPromises()
|
|
|
|
expect(fetcher).toHaveBeenCalledTimes(2)
|
|
expect(fetcher).toHaveBeenCalledWith('/api/assets', expect.objectContaining({ method: 'GET' }))
|
|
expect(wrapper.findAll('.asset-library-card')).toHaveLength(2)
|
|
expect(wrapper.text()).toContain('雨夜街道')
|
|
expect(wrapper.get('.asset-library-tag').text()).toBe('场景布局')
|
|
expect(wrapper.text()).toContain('林默正面照')
|
|
expect(wrapper.text()).toContain('记忆当铺')
|
|
expect(wrapper.findAllComponents(NTooltip)).toHaveLength(2)
|
|
const title = wrapper.get('.asset-library-title')
|
|
expect(title.classes()).toContain('asset-library-title')
|
|
expect(wrapper.findComponent(NTooltip).props('disabled')).toBe(true)
|
|
Object.defineProperties(title.element, {
|
|
scrollWidth: { configurable: true, value: 240 },
|
|
clientWidth: { configurable: true, value: 120 }
|
|
})
|
|
await title.trigger('mouseenter')
|
|
expect(wrapper.findComponent(NTooltip).props('disabled')).toBe(false)
|
|
expect(wrapper.get('.asset-library-card').text()).not.toContain('rain-street-original.png')
|
|
expect(wrapper.get('.asset-library-card').text()).not.toContain('4.0 KB')
|
|
|
|
expect(wrapper.find('[aria-label="预览图片"]').exists()).toBe(false)
|
|
expect(wrapper.find('[aria-label="查看素材详情"]').exists()).toBe(false)
|
|
await wrapper.get('.asset-library-card').trigger('click')
|
|
await flushPromises()
|
|
const dialog = document.querySelector('[role="dialog"][aria-label="素材详情与管理"]')!
|
|
expect(dialog).not.toBeNull()
|
|
expect(
|
|
[...dialog.querySelectorAll('.n-card__footer button')].map(button => button.textContent?.trim())
|
|
).toEqual(['删除素材', '取消', '保存修改'])
|
|
expect(dialog.querySelector('.asset-management-layout button[type="submit"]')).toBeNull()
|
|
expect(dialog.textContent).toContain('rain-street-original.png')
|
|
expect(dialog.textContent).toContain('PNG 图片')
|
|
expect(dialog.textContent).toContain('4.0 KB')
|
|
expect(router.currentRoute.value.path).toBe('/assets')
|
|
expect(wrapper.getComponent(AssetCategorySelect).getComponent(NSelect).props('options')).toContainEqual({
|
|
label: '视觉风格',
|
|
value: 'visual-style'
|
|
})
|
|
expect(wrapper.getComponent(AssetCategorySelect).getComponent(NSelect).props('options')).toContainEqual({
|
|
label: '场景布局',
|
|
value: 'scene-layout'
|
|
})
|
|
await new DOMWrapper(dialog).get('#library-asset-name').setValue('雨夜街道新版')
|
|
wrapper.getComponent(AssetCategorySelect).vm.$emit('update:value', 'visual-style')
|
|
await flushPromises()
|
|
const save = [...dialog.querySelectorAll<HTMLButtonElement>('.n-card__footer button')].find(
|
|
button => button.textContent?.trim() === '保存修改'
|
|
)!
|
|
save.click()
|
|
await flushPromises()
|
|
const update = fetcher.mock.calls.find(([, init]) => init?.method === 'PUT')!
|
|
expect(update[0]).toBe('/api/projects/project-one/assets/asset-scene')
|
|
expect(JSON.parse(String(update[1]?.body))).toEqual({ name: '雨夜街道新版', category: 'visual-style' })
|
|
|
|
await wrapper.get('input[aria-label="搜索资产图片"]').setValue('天降甘霖')
|
|
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('林默正面照')
|
|
})
|
|
})
|