329 lines
16 KiB
TypeScript
329 lines
16 KiB
TypeScript
import { h } from 'vue'
|
|
import { createMemoryHistory, createRouter } from 'vue-router'
|
|
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { readFileSync } from 'node:fs'
|
|
import App from '@/App.vue'
|
|
import ProjectLayout from '@/features/projects/ProjectLayout.vue'
|
|
import { isProjectComplete } from '@/features/projects/access'
|
|
import { useProjectContext } from '@/features/projects/context'
|
|
import { projectsApi } from '@/features/projects/api'
|
|
import { getOperation } from '@/features/workflows/operations'
|
|
import type { ProjectDetail, ProjectStageReadiness, ProjectStatus } from '@/features/projects/types'
|
|
import { readAllStyles } from '@/testing/styles'
|
|
|
|
const downstream = ['breakdown', 'visual-style', 'subject-identity', 'subject-images', 'storyboard', 'production']
|
|
/**
|
|
* 构造服务端完整阶段状态,允许测试单独模拟各步骤的就绪变化。
|
|
* @param id 项目编号
|
|
* @param ready 所有制作阶段是否就绪
|
|
* @returns 项目阶段响应
|
|
*/
|
|
function readiness(id: string, ready = true): ProjectStageReadiness {
|
|
/** 每个阶段独立创建,避免修改某一步时污染其它前置状态。 */
|
|
const stage = () => ({ ready, reason: ready ? null : '请先完成剧本创作' })
|
|
return {
|
|
projectId: id,
|
|
script: stage(),
|
|
breakdown: stage(),
|
|
visualStyle: stage(),
|
|
identity: stage(),
|
|
subjectAssets: stage(),
|
|
storyboard: stage()
|
|
}
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.spyOn(projectsApi, 'stageReadiness').mockImplementation(async id => readiness(id))
|
|
})
|
|
let wrapper: VueWrapper | undefined
|
|
afterEach(() => {
|
|
wrapper?.unmount()
|
|
wrapper = undefined
|
|
document.body.innerHTML = ''
|
|
localStorage.clear()
|
|
vi.useRealTimers()
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
/**
|
|
* 用已有剧集模拟中途生成的项目,完成与否必须取 status 而非数组长度。
|
|
* @param id 项目编号
|
|
* @param status 项目状态
|
|
* @returns 含部分剧集的项目详情
|
|
*/
|
|
function project(id: string, status: ProjectStatus): ProjectDetail {
|
|
return {
|
|
id,
|
|
status,
|
|
title: '访问限制测试',
|
|
topic: '',
|
|
style: null,
|
|
createdAt: '',
|
|
updatedAt: '',
|
|
episodes: [{ episode: 1, title: '部分剧集', content: '已经写入的内容' }],
|
|
characters: [],
|
|
world: null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 真实项目布局与侧栏,子工作区以挂载探针代替,防止测试发起实际生成请求。
|
|
* @param initialPath 首次访问的项目地址
|
|
* @returns 真实路由与工作区挂载记录
|
|
*/
|
|
async function openProject(initialPath: string) {
|
|
const mounted = vi.fn<(path: string) => void>()
|
|
const router = createRouter({
|
|
history: createMemoryHistory(),
|
|
routes: [
|
|
{ path: '/assets', component: { render: () => h('div', '资产库') } },
|
|
{ path: '/projects', component: { render: () => h('div', '项目列表') } },
|
|
{
|
|
path: '/projects/:projectId',
|
|
component: ProjectLayout,
|
|
children: ['create-drama', ...downstream].map(path => ({
|
|
path,
|
|
component: {
|
|
setup() {
|
|
mounted(path)
|
|
/** 测试探针只执行手动项目读取,不发起制作任务。 */
|
|
const context = useProjectContext()
|
|
return () => [
|
|
h('div', { class: 'workspace-probe' }, path),
|
|
h('button', { class: 'probe-refresh', onClick: context.refresh }, '手动刷新')
|
|
]
|
|
}
|
|
}
|
|
}))
|
|
}
|
|
]
|
|
})
|
|
await router.push(initialPath)
|
|
wrapper = mount(App, { attachTo: document.body, global: { plugins: [router] } })
|
|
await flushPromises()
|
|
return { router, mounted }
|
|
}
|
|
|
|
describe('剧本完成前的下游访问限制', () => {
|
|
it('切换图库与其他工作区不自动重复读取项目状态', async () => {
|
|
vi.useFakeTimers()
|
|
const detail = vi.spyOn(projectsApi, 'detail').mockResolvedValue(project('manual-gallery', 'completed'))
|
|
const checkpoints = vi.spyOn(projectsApi, 'checkpoints').mockResolvedValue([])
|
|
const { router } = await openProject('/projects/manual-gallery/production')
|
|
await router.push('/projects/manual-gallery/subject-images')
|
|
await flushPromises()
|
|
await vi.advanceTimersByTimeAsync(30_000)
|
|
expect(detail).toHaveBeenCalledTimes(1)
|
|
expect(checkpoints).toHaveBeenCalledTimes(1)
|
|
await router.push('/projects/manual-gallery/production')
|
|
await flushPromises()
|
|
await vi.advanceTimersByTimeAsync(6000)
|
|
expect(detail).toHaveBeenCalledTimes(1)
|
|
expect(checkpoints).toHaveBeenCalledTimes(1)
|
|
expect(projectsApi.stageReadiness).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('图库直接链接只读取一次项目,未完成时可手动刷新解锁', async () => {
|
|
vi.useFakeTimers()
|
|
let status: ProjectStatus = 'generating'
|
|
vi.mocked(projectsApi.stageReadiness).mockImplementation(async id => readiness(id, status === 'completed'))
|
|
const detail = vi.spyOn(projectsApi, 'detail').mockImplementation(async id => project(id, status))
|
|
vi.spyOn(projectsApi, 'checkpoints').mockResolvedValue([])
|
|
await openProject('/projects/manual-gate/subject-images')
|
|
await vi.advanceTimersByTimeAsync(30_000)
|
|
expect(detail).toHaveBeenCalledTimes(1)
|
|
expect(wrapper!.get('.project-access-gate').text()).not.toContain('状态会自动更新')
|
|
status = 'completed'
|
|
const refresh = wrapper!.findAll('button').find(button => button.text() === '刷新项目状态')!
|
|
await refresh.trigger('click')
|
|
await flushPromises()
|
|
expect(detail).toHaveBeenCalledTimes(2)
|
|
expect(wrapper!.get('.workspace-probe').text()).toBe('subject-images')
|
|
})
|
|
|
|
it.each(['draft', 'generating', 'need_review', 'failed'] as const)(
|
|
'%s 不解锁导航,也不挂载直接链接对应的工作区',
|
|
async status => {
|
|
vi.mocked(projectsApi.stageReadiness).mockResolvedValue(readiness('unfinished', false))
|
|
vi.spyOn(projectsApi, 'detail').mockResolvedValue(project('unfinished', status))
|
|
vi.spyOn(projectsApi, 'checkpoints').mockResolvedValue([])
|
|
const { router, mounted } = await openProject('/projects/unfinished/production')
|
|
for (const path of downstream) {
|
|
await router.push(`/projects/unfinished/${path}`)
|
|
await flushPromises()
|
|
expect(wrapper!.get('.project-access-gate').text()).toContain('请先完成剧本创作')
|
|
expect(wrapper!.find('.workspace-probe').exists()).toBe(false)
|
|
expect(wrapper!.find(`.n-menu a[href="/projects/unfinished/${path}"]`).exists()).toBe(false)
|
|
}
|
|
expect(mounted).not.toHaveBeenCalled()
|
|
expect(wrapper!.findAll('.n-menu [aria-disabled="true"]')).toHaveLength(6)
|
|
await wrapper!.get('.project-access-gate button').trigger('click')
|
|
await flushPromises()
|
|
expect(router.currentRoute.value.path).toBe('/projects/unfinished/breakdown')
|
|
expect(mounted).not.toHaveBeenCalled()
|
|
await wrapper!.get('.project-access-gate button').trigger('click')
|
|
await flushPromises()
|
|
expect(router.currentRoute.value.path).toBe('/projects/unfinished/create-drama')
|
|
expect(wrapper!.get('.workspace-probe').text()).toBe('create-drama')
|
|
expect(mounted).toHaveBeenCalledExactlyOnceWith('create-drama')
|
|
}
|
|
)
|
|
|
|
it('手动刷新就绪状态解锁;服务端撤销就绪后撤下下游面板', async () => {
|
|
vi.useFakeTimers()
|
|
let ready = false
|
|
vi.spyOn(projectsApi, 'detail').mockResolvedValue(project('manual-refresh', 'completed'))
|
|
vi.mocked(projectsApi.stageReadiness).mockImplementation(async id => readiness(id, ready))
|
|
vi.spyOn(projectsApi, 'checkpoints').mockResolvedValue([])
|
|
const { router, mounted } = await openProject('/projects/manual-refresh/subject-images')
|
|
expect(mounted).not.toHaveBeenCalled()
|
|
ready = true
|
|
await vi.advanceTimersByTimeAsync(30_000)
|
|
expect(mounted).not.toHaveBeenCalled()
|
|
const refresh = wrapper!.findAll('button').find(button => button.text() === '刷新项目状态')!
|
|
await refresh.trigger('click')
|
|
await flushPromises()
|
|
expect(wrapper!.get('.workspace-probe').text()).toBe('subject-images')
|
|
expect(wrapper!.findAll('.n-menu a')).toHaveLength(9)
|
|
await router.push('/projects/manual-refresh/production')
|
|
await flushPromises()
|
|
ready = false
|
|
await wrapper!.get('.probe-refresh').trigger('click')
|
|
await flushPromises()
|
|
expect(wrapper!.find('.workspace-probe').exists()).toBe(false)
|
|
expect(wrapper!.get('.project-access-gate').text()).toContain('请先完成剧本创作')
|
|
expect(wrapper!.findAll('.n-menu a')).toHaveLength(3)
|
|
expect(mounted.mock.calls.map(([path]) => path)).toEqual(['subject-images', 'production'])
|
|
})
|
|
|
|
it('切换项目与首次读取期间不能沿用上一个已完成项目的权限', async () => {
|
|
let resolveSecond!: (value: ProjectDetail) => void
|
|
const pending = new Promise<ProjectDetail>(resolve => {
|
|
resolveSecond = resolve
|
|
})
|
|
vi.spyOn(projectsApi, 'detail').mockImplementation(async id =>
|
|
id === 'first' ? project(id, 'completed') : pending
|
|
)
|
|
vi.spyOn(projectsApi, 'checkpoints').mockResolvedValue([])
|
|
vi.mocked(projectsApi.stageReadiness).mockImplementation(async id => readiness(id, id === 'first'))
|
|
const { router, mounted } = await openProject('/projects/first/production')
|
|
expect(wrapper!.findAll('.n-menu a')).toHaveLength(9)
|
|
await router.push('/projects/second/production')
|
|
await flushPromises()
|
|
expect(wrapper!.findAll('.n-menu a')).toHaveLength(3)
|
|
expect(wrapper!.find('.workspace-probe').exists()).toBe(false)
|
|
resolveSecond(project('second', 'draft'))
|
|
await flushPromises()
|
|
expect(wrapper!.find('.workspace-probe').exists()).toBe(false)
|
|
expect(mounted).toHaveBeenCalledExactlyOnceWith('production')
|
|
await router.push('/projects')
|
|
await flushPromises()
|
|
expect(wrapper!.findAll('.n-menu a')).toHaveLength(2)
|
|
})
|
|
|
|
it('读取失败保持锁定;重试成功后解锁,标题只保留项目配置入口', async () => {
|
|
const detail = vi
|
|
.spyOn(projectsApi, 'detail')
|
|
.mockRejectedValueOnce(new Error('项目读取失败'))
|
|
.mockResolvedValue(project('retry', 'completed'))
|
|
vi.spyOn(projectsApi, 'checkpoints').mockResolvedValue([])
|
|
const { mounted } = await openProject('/projects/retry/storyboard')
|
|
expect(mounted).not.toHaveBeenCalled()
|
|
expect(wrapper!.findAll('.n-menu a')).toHaveLength(3)
|
|
expect(wrapper!.get('.project-header .n-button').text()).toBe('项目配置')
|
|
expect(wrapper!.get('.project-header button').attributes('disabled')).toBeDefined()
|
|
expect(wrapper!.find('.project-header .n-tag').exists()).toBe(false)
|
|
await wrapper!.get('.project-notices button').trigger('click')
|
|
await flushPromises()
|
|
expect(detail).toHaveBeenCalledTimes(2)
|
|
expect(mounted).toHaveBeenCalledExactlyOnceWith('storyboard')
|
|
})
|
|
|
|
it('剧本完成仍按服务端前置步骤逐步解锁,不能直接进入生产', async () => {
|
|
const state = readiness('staged', false)
|
|
state.script = { ready: true, reason: null }
|
|
state.breakdown.reason = '请先完成剧本拆解'
|
|
vi.mocked(projectsApi.stageReadiness).mockImplementation(async () => structuredClone(state))
|
|
vi.spyOn(projectsApi, 'detail').mockResolvedValue(project('staged', 'completed'))
|
|
vi.spyOn(projectsApi, 'checkpoints').mockResolvedValue([])
|
|
const { router, mounted } = await openProject('/projects/staged/production')
|
|
expect(mounted).not.toHaveBeenCalled()
|
|
expect(wrapper!.get('.project-access-gate').text()).toContain('请先完成剧本拆解')
|
|
expect(wrapper!.find('.n-menu a[href="/projects/staged/breakdown"]').exists()).toBe(true)
|
|
expect(wrapper!.find('.n-menu a[href="/projects/staged/visual-style"]').exists()).toBe(false)
|
|
await wrapper!.get('.project-access-gate button').trigger('click')
|
|
await flushPromises()
|
|
expect(router.currentRoute.value.path).toBe('/projects/staged/breakdown')
|
|
state.breakdown = { ready: true, reason: null }
|
|
state.subjectAssets = { ready: true, reason: null }
|
|
state.storyboard = { ready: false, reason: '请先完成分镜设计' }
|
|
await wrapper!.get('.probe-refresh').trigger('click')
|
|
await flushPromises()
|
|
await router.push('/projects/staged/production')
|
|
await flushPromises()
|
|
expect(wrapper!.find('.workspace-probe').exists()).toBe(false)
|
|
expect(wrapper!.get('.project-access-gate').text()).toContain('请先完成分镜设计')
|
|
await wrapper!.get('.project-access-gate button').trigger('click')
|
|
await flushPromises()
|
|
expect(router.currentRoute.value.path).toBe('/projects/staged/storyboard')
|
|
expect(wrapper!.get('.workspace-probe').text()).toBe('storyboard')
|
|
})
|
|
|
|
it('不存在、未知状态或项目 ID 不匹配时默认锁定', () => {
|
|
expect(isProjectComplete(null, 'p')).toBe(false)
|
|
expect(isProjectComplete({ id: 'other', status: 'completed' }, 'p')).toBe(false)
|
|
expect(isProjectComplete({ id: 'p', status: 'unknown' as ProjectStatus }, 'p')).toBe(false)
|
|
expect(isProjectComplete({ id: 'p', status: 'completed' }, 'p')).toBe(true)
|
|
})
|
|
|
|
it('镜头提示词缺少主首帧时给出下一步,并定位到对应镜头的首帧生成', async () => {
|
|
const projectId = 'guided-keyframe'
|
|
vi.spyOn(projectsApi, 'detail').mockResolvedValue(project(projectId, 'completed'))
|
|
vi.spyOn(projectsApi, 'checkpoints').mockResolvedValue([])
|
|
const { router } = await openProject(`/projects/${projectId}/storyboard`)
|
|
Object.assign(getOperation(projectId), {
|
|
pending: false,
|
|
label: '读取/生成镜头 3 提示词',
|
|
error: '服务器内部错误 Shot 缺少可用于视频 0 秒起点校验的 Primary Keyframe: shot-db-2-3',
|
|
notice: '',
|
|
recovery: { target: 'keyframe', shotId: 'shot-db-2-3', episodeNo: 2 }
|
|
})
|
|
await flushPromises()
|
|
|
|
expect(wrapper!.get('.operation-guide').text()).toContain('缺少镜头主首帧')
|
|
expect(wrapper!.get('.operation-guide').text()).toContain('先生成并确认主首帧')
|
|
await wrapper!.get('.operation-guide button').trigger('click')
|
|
await flushPromises()
|
|
|
|
expect(router.currentRoute.value.path).toBe(`/projects/${projectId}/production`)
|
|
expect(router.currentRoute.value.query).toEqual({
|
|
episodeNo: '2',
|
|
shotId: 'shot-db-2-3',
|
|
action: 'keyframe'
|
|
})
|
|
})
|
|
|
|
it('配置复选框居中对齐,输入表面随面板背景分层而非增加边框', () => {
|
|
// 保护布局契约;实际像素对齐仍需浏览器视觉验收。
|
|
const css = readAllStyles()
|
|
expect(css).toMatch(
|
|
/\.n-checkbox\.control-row-checkbox\s*\{[^}]*align-items:\s*center;[^}]*min-height:\s*34px;[^}]*padding-block:\s*0/
|
|
)
|
|
expect(css).toMatch(/\.app-dialog\s*\{[^}]*--app-field:\s*var\(--app-control\)/)
|
|
expect(readFileSync('src/styles/styles.css', 'utf8')).toMatch(
|
|
/\.panel\s*\{[^}]*--app-field:\s*var\(--app-control\)/
|
|
)
|
|
for (const path of [
|
|
'production/ProductionPage.vue',
|
|
'storyboard/StoryboardPage.vue',
|
|
'subject-identity/SubjectIdentityPage.vue',
|
|
'subject-images/SubjectImagesPage.vue'
|
|
]) {
|
|
const source = readFileSync(`src/features/${path}`, 'utf8')
|
|
expect(source).toContain('control-row-checkbox')
|
|
expect(source).not.toMatch(/<NCheckbox\b[^>]*class="[^"]*pb-[23]/)
|
|
}
|
|
})
|
|
})
|