feat(short-drama-agent-front): 适配故事规划并优化任务弹窗
This commit is contained in:
1 parent
48fce3975f
commit
5836b90934
12 files changed
+960
-133
No files matched your search
@@ -0,0 +1,326 @@
|
||||
import { DOMWrapper, flushPromises, mount, type VueWrapper } from '@vue/test-utils'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import CreateDramaPage from '@/features/create-drama/CreateDramaPage.vue'
|
||||
import { projectContextKey } from '@/features/projects/context'
|
||||
import { workflowExecutionApi } from '@/features/workflows/execution-api'
|
||||
import type { WorkflowExecution } from '@/features/workflows/execution.types'
|
||||
import type { Checkpoint } from '@/features/workflows/types'
|
||||
import { testProjectContext } from '@/testing/project-context'
|
||||
import AppDialog from '@/components/ui/AppDialog.vue'
|
||||
import { projectsApi } from '@/features/projects/api'
|
||||
|
||||
/** 服务端已校验并保存的规划;前端不重新执行规划业务校验。 */
|
||||
const storyPlan = {
|
||||
premise: '一封信改变命运',
|
||||
episodeCount: 1,
|
||||
entities: [],
|
||||
episodes: [
|
||||
{
|
||||
episode: 1,
|
||||
goal: '找到寄信人',
|
||||
events: [{ id: 'event-1', description: '收到来信', entityIds: [], prerequisites: [], outcome: '开始调查' }],
|
||||
openQuestions: []
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造持久化任务,不连接真实模型。
|
||||
* @param projectId 所属项目
|
||||
* @param overrides 当前状态覆盖
|
||||
* @returns 真实任务查询结构
|
||||
*/
|
||||
function execution(projectId: string, overrides: Partial<WorkflowExecution> = {}): WorkflowExecution {
|
||||
return {
|
||||
id: `task-${projectId}`,
|
||||
projectId,
|
||||
workflowName: 'create-drama',
|
||||
status: 'running',
|
||||
stage: 'generateStoryPlan',
|
||||
current: 0,
|
||||
total: 0,
|
||||
currentResourceId: projectId,
|
||||
metadata: {},
|
||||
result: null,
|
||||
error: null,
|
||||
startedAt: '2026-10-28T00:00:00Z',
|
||||
completedAt: null,
|
||||
createdAt: '2026-10-28T00:00:00Z',
|
||||
updatedAt: '2026-10-28T00:01:00Z',
|
||||
...overrides
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按节点完成后持久化的契约构造保存记录。
|
||||
* @param state 保存的状态
|
||||
* @param nodeName 保存节点
|
||||
* @returns 创作保存记录
|
||||
*/
|
||||
function checkpoint(state: Checkpoint['state'], nodeName = 'generateStoryPlan'): Checkpoint {
|
||||
return {
|
||||
checkpointId: `cp-${nodeName}`,
|
||||
workflowName: 'create-drama',
|
||||
createdAt: '2026-10-28T00:00:30Z',
|
||||
metadata: { nodeName },
|
||||
state
|
||||
}
|
||||
}
|
||||
|
||||
/** 测试持有的页面与项目上下文。 */
|
||||
let wrapper: VueWrapper | undefined
|
||||
let context: ReturnType<typeof testProjectContext>
|
||||
|
||||
/**
|
||||
* 挂载真实创作页并读取指定的持久化任务。
|
||||
* @param task 任务查询结果
|
||||
* @returns 当前页面
|
||||
*/
|
||||
async function open(task: WorkflowExecution | null) {
|
||||
if (task) {
|
||||
localStorage.setItem(`short-drama:workflow-execution:${task.projectId}:create-drama`, task.id)
|
||||
vi.spyOn(workflowExecutionApi, 'get').mockResolvedValue(task)
|
||||
}
|
||||
wrapper = mount(CreateDramaPage, {
|
||||
attachTo: document.body,
|
||||
global: { provide: { [projectContextKey as symbol]: context } }
|
||||
})
|
||||
await flushPromises()
|
||||
return wrapper
|
||||
}
|
||||
|
||||
/**
|
||||
* 从页面入口打开真实任务弹窗。
|
||||
* @param page 当前创作页面
|
||||
* @returns 挂载到弹层的任务内容
|
||||
*/
|
||||
async function openTask(page = wrapper!) {
|
||||
await page.get('button[aria-label="查看创作任务"]').trigger('click')
|
||||
await flushPromises()
|
||||
return new DOMWrapper(document.querySelector<HTMLElement>('.app-dialog[aria-label="创作任务"]')!)
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
wrapper?.unmount()
|
||||
wrapper = undefined
|
||||
localStorage.clear()
|
||||
document.body.innerHTML = ''
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
describe('故事规划创作进度与恢复', () => {
|
||||
it('页面仅保留摘要和入口,任务与恢复说明按需进入弹窗且操作都在固定底栏', async () => {
|
||||
context = testProjectContext('compact-task')
|
||||
context.data.value!.project.status = 'failed'
|
||||
const resume = vi.spyOn(projectsApi, 'resume')
|
||||
const page = await open(
|
||||
execution(context.project.value!.id, { status: 'failed', stage: 'failed', error: '验收任务中断' })
|
||||
)
|
||||
expect(page.get('.script-task-summary').text()).toContain('创作任务失败')
|
||||
expect(page.text()).not.toContain('验收任务中断')
|
||||
expect(page.find('[aria-label="剧本创作任务"]').exists()).toBe(false)
|
||||
const dialog = await openTask(page)
|
||||
expect(dialog.text()).toContain('验收任务中断')
|
||||
expect(dialog.text()).toContain('缺少已保存的故事规划')
|
||||
expect(dialog.find('button[aria-label="隐藏制作任务记录"]').exists()).toBe(false)
|
||||
const footer = dialog.get('.dialog-footer')
|
||||
expect(footer.find('button[aria-label="恢复生成"]').exists()).toBe(true)
|
||||
expect(footer.find('button[aria-label="恢复改写"]').exists()).toBe(true)
|
||||
const close = footer.findAll('button').find(item => item.text() === '关闭')!
|
||||
expect(close.exists()).toBe(true)
|
||||
await close.trigger('click')
|
||||
await flushPromises()
|
||||
expect(page.getComponent(AppDialog).props('open')).toBe(false)
|
||||
expect(resume).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('运行中也能关闭任务弹窗,关闭后继续观察且重开显示新阶段', async () => {
|
||||
vi.useFakeTimers()
|
||||
context = testProjectContext('close-active-task')
|
||||
context.data.value!.project.status = 'generating'
|
||||
const task = execution(context.project.value!.id)
|
||||
const page = await open(task)
|
||||
const dialog = await openTask(page)
|
||||
await dialog
|
||||
.get('.dialog-footer')
|
||||
.findAll('button')
|
||||
.find(item => item.text() === '关闭')!
|
||||
.trigger('click')
|
||||
await flushPromises()
|
||||
expect(page.getComponent(AppDialog).props('open')).toBe(false)
|
||||
vi.mocked(workflowExecutionApi.get).mockResolvedValue({
|
||||
...task,
|
||||
stage: 'generateEpisode',
|
||||
updatedAt: '2026-10-28T00:02:00Z'
|
||||
})
|
||||
await vi.advanceTimersByTimeAsync(1600)
|
||||
await flushPromises()
|
||||
expect(page.get('.script-task-summary').text()).toContain('生成分集剧本')
|
||||
const reopened = await openTask(page)
|
||||
expect(reopened.text()).toContain('生成分集剧本')
|
||||
expect(reopened.get('button[aria-label="恢复生成"]').attributes('disabled')).toBeDefined()
|
||||
})
|
||||
|
||||
it('状态读取失败仍可关闭,通过底栏刷新重读后清除旧错误且不恢复生成', async () => {
|
||||
context = testProjectContext('task-read-error')
|
||||
context.data.value!.project.status = 'generating'
|
||||
const task = execution(context.project.value!.id)
|
||||
const page = await open(task)
|
||||
const dialog = await openTask(page)
|
||||
vi.mocked(workflowExecutionApi.get).mockRejectedValueOnce(new Error('任务读取暂时失败'))
|
||||
const refresh = dialog
|
||||
.get('.dialog-footer')
|
||||
.findAll('button')
|
||||
.find(item => item.text() === '刷新状态')!
|
||||
await refresh.trigger('click')
|
||||
await flushPromises()
|
||||
expect(dialog.text()).toContain('任务读取暂时失败')
|
||||
expect(page.get('.script-task-summary').text()).toContain('任务状态读取异常')
|
||||
expect(dialog.findAll('button').filter(item => item.text() === '关闭')).toHaveLength(1)
|
||||
expect(dialog.findAll('button').some(item => item.text() === '重试读取')).toBe(false)
|
||||
vi.mocked(workflowExecutionApi.get).mockResolvedValue({
|
||||
...task,
|
||||
stage: 'generateEpisode',
|
||||
updatedAt: '2026-10-28T00:02:00Z'
|
||||
})
|
||||
await refresh.trigger('click')
|
||||
await flushPromises()
|
||||
expect(dialog.text()).not.toContain('任务读取暂时失败')
|
||||
expect(page.get('.script-task-summary').text()).toContain('生成分集剧本')
|
||||
})
|
||||
it.each(['generateEpisode', 'generateCharacter', 'generateWorld', 'reviewDrama'])(
|
||||
'采用服务端当前 %s 节点,不把其它阶段顺带标为完成',
|
||||
async stage => {
|
||||
context = testProjectContext(`current-${stage}`)
|
||||
context.data.value!.project.status = 'generating'
|
||||
context.data.value!.project.episodes = []
|
||||
const page = await open(execution(context.project.value!.id, { stage }))
|
||||
expect(page.get(`[data-stage="${stage}"]`).attributes('data-status')).toBe('running')
|
||||
expect(page.findAll('.stage-item').some(item => item.classes().includes('done'))).toBe(false)
|
||||
}
|
||||
)
|
||||
it.each(['pending', 'running'] as const)('规划节点采用任务的 %s 状态,后续节点不抢先完成', async status => {
|
||||
context = testProjectContext(`plan-${status}`)
|
||||
context.data.value!.project.status = 'generating'
|
||||
context.data.value!.project.episodes = []
|
||||
const page = await open(execution(context.project.value!.id, { status }))
|
||||
const stages = page.findAll('.stage-item')
|
||||
expect(stages.map(item => item.attributes('data-stage'))).toEqual([
|
||||
'generateStoryPlan',
|
||||
'generateEpisode',
|
||||
'generateCharacter',
|
||||
'generateWorld',
|
||||
'reviewDrama',
|
||||
'finishProject'
|
||||
])
|
||||
expect(stages[0]!.attributes('data-status')).toBe(status)
|
||||
expect(stages.slice(1).every(item => item.attributes('data-status') !== 'completed')).toBe(true)
|
||||
})
|
||||
|
||||
it('真实失败回执只报告整体失败时,不猜测具体失败节点', async () => {
|
||||
context = testProjectContext('plan-failed-unknown')
|
||||
context.data.value!.project.status = 'failed'
|
||||
context.data.value!.project.episodes = []
|
||||
const page = await open(
|
||||
execution(context.project.value!.id, { status: 'failed', stage: 'failed', error: '故事规划生成中断' })
|
||||
)
|
||||
const dialog = await openTask(page)
|
||||
expect(dialog.get('[aria-label="剧本创作任务"]').text()).toContain('故事规划生成中断')
|
||||
expect(page.get('[data-stage="generateStoryPlan"]').attributes('data-status')).toBeUndefined()
|
||||
expect(page.findAll('.stage-item').every(item => !item.classes().includes('done'))).toBe(true)
|
||||
})
|
||||
|
||||
it('刷新后从当前保存规划恢复完成状态,剧集生成不依赖角色或世界观', async () => {
|
||||
context = testProjectContext('plan-saved')
|
||||
context.data.value!.project.status = 'failed'
|
||||
context.data.value!.project.episodes = []
|
||||
context.data.value!.checkpoints = [checkpoint({ storyPlan, episodeCount: 1 })]
|
||||
const page = await open(execution(context.project.value!.id, { status: 'failed', stage: 'failed' }))
|
||||
const dialog = await openTask(page)
|
||||
expect(page.get('[data-stage="generateStoryPlan"]').attributes('data-status')).toBe('completed')
|
||||
expect(page.get('[data-stage="generateEpisode"]').attributes('data-status')).toBeUndefined()
|
||||
expect(dialog.get('button[aria-label="恢复生成"]').attributes('disabled')).toBeUndefined()
|
||||
expect(dialog.get('button[aria-label="恢复改写"]').attributes('disabled')).toBeDefined()
|
||||
})
|
||||
|
||||
it('派生设定失败且正式剧集已齐全时仍允许恢复生成,不误开恢复改写', async () => {
|
||||
context = testProjectContext('derive-failed')
|
||||
context.data.value!.project.status = 'failed'
|
||||
context.data.value!.checkpoints = [
|
||||
checkpoint({ storyPlan, episodes: context.project.value!.episodes, episodeCount: 1 }, 'generateEpisode')
|
||||
]
|
||||
const page = await open(execution(context.project.value!.id, { status: 'failed', stage: 'failed' }))
|
||||
const dialog = await openTask(page)
|
||||
expect(page.get('[data-stage="generateEpisode"]').attributes('data-status')).toBe('completed')
|
||||
expect(page.get('[data-stage="generateCharacter"]').attributes('data-status')).toBeUndefined()
|
||||
expect(dialog.get('button[aria-label="恢复生成"]').attributes('disabled')).toBeUndefined()
|
||||
expect(dialog.get('button[aria-label="恢复改写"]').attributes('disabled')).toBeDefined()
|
||||
})
|
||||
|
||||
it('恢复期间不以旧保存记录或数据库设定把当前节点及审核标为完成', async () => {
|
||||
context = testProjectContext('resume-new')
|
||||
context.data.value!.project.status = 'generating'
|
||||
context.data.value!.project.characters = [{ id: 'old', name: '旧角色' }]
|
||||
context.data.value!.project.world = { era: '旧世界观' }
|
||||
context.data.value!.checkpoints = [
|
||||
{
|
||||
...checkpoint({ storyPlan, reviewPassed: true, episodes: context.project.value!.episodes }),
|
||||
createdAt: '2026-10-27T00:00:00Z'
|
||||
}
|
||||
]
|
||||
const page = await open(
|
||||
execution(context.project.value!.id, {
|
||||
stage: 'resumeGeneration',
|
||||
metadata: { action: 'resume-generation' }
|
||||
})
|
||||
)
|
||||
expect(page.get('[data-stage="reviewDrama"]').attributes('data-status')).not.toBe('completed')
|
||||
expect(page.get('[data-stage="generateCharacter"]').attributes('data-status')).not.toBe('completed')
|
||||
expect(page.get('[data-stage="generateWorld"]').attributes('data-status')).not.toBe('completed')
|
||||
})
|
||||
|
||||
it('审核未通过使用当前明确结论,允许恢复改写且不显示审核成功', async () => {
|
||||
context = testProjectContext('review-failed')
|
||||
context.data.value!.project.status = 'need_review'
|
||||
context.data.value!.checkpoints = [
|
||||
checkpoint({ storyPlan, reviewPassed: false, review: { passed: false } }, 'reviewDrama')
|
||||
]
|
||||
const page = await open(execution(context.project.value!.id, { status: 'completed', stage: 'completed' }))
|
||||
const dialog = await openTask(page)
|
||||
expect(page.get('[data-stage="reviewDrama"]').text()).toContain('未通过')
|
||||
expect(page.get('[data-stage="reviewDrama"]').classes()).not.toContain('done')
|
||||
expect(dialog.get('button[aria-label="恢复改写"]').attributes('disabled')).toBeUndefined()
|
||||
expect(dialog.get('button[aria-label="恢复生成"]').attributes('disabled')).toBeDefined()
|
||||
})
|
||||
|
||||
it('最新记录缺少规划时不回退历史规划或由已有剧集推断成功', async () => {
|
||||
context = testProjectContext('plan-missing')
|
||||
context.data.value!.project.status = 'failed'
|
||||
context.data.value!.project.episodes = []
|
||||
context.data.value!.checkpoints = [
|
||||
{ ...checkpoint({ storyPlan }), createdAt: '2026-10-27T00:00:00Z' },
|
||||
checkpoint({}, 'createProject')
|
||||
]
|
||||
const page = await open(execution(context.project.value!.id, { status: 'failed', stage: 'failed' }))
|
||||
const dialog = await openTask(page)
|
||||
expect(dialog.get('button[aria-label="恢复生成"]').attributes('disabled')).toBeDefined()
|
||||
expect(dialog.text()).toContain('缺少已保存的故事规划')
|
||||
})
|
||||
|
||||
it('节点切换重读项目与保存记录,不需要等到整个任务结束', async () => {
|
||||
context = testProjectContext('stage-update')
|
||||
context.data.value!.project.status = 'generating'
|
||||
const task = execution(context.project.value!.id)
|
||||
await open(task)
|
||||
await openTask()
|
||||
vi.mocked(context.refresh).mockClear()
|
||||
vi.mocked(workflowExecutionApi.get).mockResolvedValue({
|
||||
...task,
|
||||
stage: 'generateEpisode',
|
||||
updatedAt: '2026-10-28T00:02:00Z'
|
||||
})
|
||||
await wrapper!.getComponent({ name: 'WorkflowExecutionPanel' }).vm.$emit('refresh')
|
||||
await flushPromises()
|
||||
expect(context.refresh).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -31,6 +31,50 @@ afterEach(() => {
|
||||
})
|
||||
|
||||
describe('服务端任务发现', () => {
|
||||
it('创作任务无本地句柄也能发现,SSE 不按旧节点顺序过滤阶段,失败终态拒绝迟到事件', async () => {
|
||||
const task = { ...record('story-plan-discovery'), workflowName: 'create-drama', stage: 'generateStoryPlan' }
|
||||
/** 只模拟浏览器连接,查询和事件均使用正式任务结构。 */
|
||||
class StoryEventSource extends EventTarget {
|
||||
/** 本次任务订阅。 */
|
||||
static last: StoryEventSource
|
||||
/** 组件销毁时关闭连接。 */
|
||||
close = vi.fn<() => void>()
|
||||
/** 登记当前订阅供测试发送服务端事件。 */
|
||||
constructor() {
|
||||
super()
|
||||
StoryEventSource.last = this
|
||||
}
|
||||
}
|
||||
vi.stubGlobal('EventSource', StoryEventSource)
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn<typeof fetch>().mockImplementation(async () => new Response(JSON.stringify({ data: task })))
|
||||
)
|
||||
const scope = effectScope()
|
||||
const tracker = scope.run(() => useWorkflowExecution(ref(task.projectId), 'create-drama', { discover: true }))!
|
||||
await flushPromises()
|
||||
expect(tracker.query.data.value?.stage).toBe('generateStoryPlan')
|
||||
expect(tracker.active.value).toBe(true)
|
||||
for (const stage of ['generateWorld', 'generateEpisode', 'generateCharacter', 'reviewDrama']) {
|
||||
task.stage = stage
|
||||
StoryEventSource.last.dispatchEvent(new MessageEvent('message', { data: JSON.stringify(task) }))
|
||||
await flushPromises()
|
||||
expect(tracker.query.data.value?.stage).toBe(stage)
|
||||
}
|
||||
task.status = 'failed'
|
||||
task.stage = 'failed'
|
||||
StoryEventSource.last.dispatchEvent(new MessageEvent('message', { data: JSON.stringify(task) }))
|
||||
await flushPromises()
|
||||
StoryEventSource.last.dispatchEvent(
|
||||
new MessageEvent('message', {
|
||||
data: JSON.stringify({ ...task, status: 'running', stage: 'generateStoryPlan' })
|
||||
})
|
||||
)
|
||||
await flushPromises()
|
||||
expect(tracker.query.data.value?.status).toBe('failed')
|
||||
expect(tracker.active.value).toBe(false)
|
||||
scope.stop()
|
||||
})
|
||||
it('页面保持打开时通过事件接上新任务,终态不被迟到运行消息重新激活', async () => {
|
||||
const task = record('discovery-stream')
|
||||
/** 仅模拟浏览器事件连接,所有任务仍来自测试响应。 */
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { flushPromises, mount } from '@vue/test-utils'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { effectScope, ref } from 'vue'
|
||||
import { NTag } from 'naive-ui'
|
||||
import WorkflowExecutionPanel from '@/features/workflows/WorkflowExecutionPanel.vue'
|
||||
import ContextNotice from '@/components/ui/ContextNotice.vue'
|
||||
import { executionPercentage, executionStageLabel, useWorkflowExecution } from '@/features/workflows/execution'
|
||||
import type { WorkflowExecution } from '@/features/workflows/execution.types'
|
||||
|
||||
@@ -30,6 +32,54 @@ afterEach(() => {
|
||||
})
|
||||
|
||||
describe('WorkflowExecution 前端契约', () => {
|
||||
it.each([
|
||||
{ projectStatus: 'need_review', type: 'warning', role: 'alert' },
|
||||
{ projectStatus: 'completed', type: 'success', role: undefined },
|
||||
{ projectStatus: undefined, type: 'info', role: undefined }
|
||||
])(
|
||||
'弹窗提示按实际结果突出注意事项,不将待审或未知结果显示为成功:$projectStatus',
|
||||
({ projectStatus, type, role }) => {
|
||||
const wrapper = mount(WorkflowExecutionPanel, {
|
||||
props: {
|
||||
execution: execution({
|
||||
workflowName: 'create-drama',
|
||||
status: 'completed',
|
||||
result: { projectStatus }
|
||||
}),
|
||||
loading: false,
|
||||
error: '',
|
||||
embedded: true
|
||||
}
|
||||
})
|
||||
const notice = wrapper.getComponent(ContextNotice)
|
||||
expect(notice.props('type')).toBe(type)
|
||||
expect(notice.attributes('role')).toBe(role)
|
||||
expect(notice.find('strong').exists()).toBe(true)
|
||||
expect(notice.find('svg').exists()).toBe(true)
|
||||
wrapper.unmount()
|
||||
}
|
||||
)
|
||||
it.each([
|
||||
{ projectStatus: 'completed', color: 'success', label: '已完成' },
|
||||
{ projectStatus: 'need_review', color: 'warning', label: '待修订' },
|
||||
{ projectStatus: undefined, color: 'default', label: '结果待确认' }
|
||||
])('创作任务结束按明确项目审核结果展示:$projectStatus', ({ projectStatus, color, label }) => {
|
||||
const wrapper = mount(WorkflowExecutionPanel, {
|
||||
props: {
|
||||
execution: execution({
|
||||
workflowName: 'create-drama',
|
||||
status: 'completed',
|
||||
stage: 'completed',
|
||||
result: { projectStatus }
|
||||
}),
|
||||
loading: false,
|
||||
error: ''
|
||||
}
|
||||
})
|
||||
expect(wrapper.getComponent(NTag).props('type')).toBe(color)
|
||||
expect(wrapper.getComponent(NTag).text()).toContain(label)
|
||||
wrapper.unmount()
|
||||
})
|
||||
it('拆解进度区分抽取计数与工作流完成,校验失败不显示全成功', () => {
|
||||
const wrapper = mount(WorkflowExecutionPanel, {
|
||||
props: {
|
||||
|
||||
Reference in new issue
Block a user