refactor: analysis workflow architecture
fix: NEXTAUTH_URL fix: prevent project model edits from affecting default model
This commit is contained in:
@@ -80,4 +80,26 @@ describe('task state service helpers', () => {
|
||||
expect(state.lastError?.code).toBe('INVALID_PARAMS')
|
||||
expect(state.lastError?.message).toBe('bad input')
|
||||
})
|
||||
|
||||
it('treats canceled task as failed presentation state', () => {
|
||||
const state = resolveTargetState(
|
||||
{ targetType: 'GlobalCharacter', targetId: 'c1' },
|
||||
[
|
||||
{
|
||||
id: 'task-3',
|
||||
type: 'asset_hub_image',
|
||||
status: 'canceled',
|
||||
progress: 100,
|
||||
payload: { ui: { intent: 'modify', hasOutputAtStart: true } },
|
||||
errorCode: 'TASK_CANCELLED',
|
||||
errorMessage: 'Task cancelled by user',
|
||||
updatedAt: new Date('2026-02-25T00:00:00.000Z'),
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
expect(state.phase).toBe('failed')
|
||||
expect(state.lastError?.code).toBe('CONFLICT')
|
||||
expect(state.lastError?.message).toBe('Task cancelled by user')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -209,4 +209,48 @@ describe('useVoiceRuntimeSync', () => {
|
||||
errorMessage: 'QwenTTS voiceId missing',
|
||||
})
|
||||
})
|
||||
|
||||
it('treats canceled task as terminal failure for pending voice generation', () => {
|
||||
const loadData = vi.fn(async () => undefined)
|
||||
const setPendingVoiceGenerationByLineId = vi.fn()
|
||||
const onTaskFailure = vi.fn()
|
||||
const effectCallbacks: Array<() => void | (() => void)> = []
|
||||
|
||||
useEffectMock.mockImplementation((callback: () => void | (() => void)) => {
|
||||
effectCallbacks.push(callback)
|
||||
})
|
||||
|
||||
useVoiceRuntimeSync({
|
||||
loadData,
|
||||
voiceLines: [buildVoiceLine({
|
||||
id: 'line-10',
|
||||
lineIndex: 10,
|
||||
})],
|
||||
activeVoiceTaskLineIds: new Set(),
|
||||
pendingVoiceGenerationByLineId: {
|
||||
'line-10': {
|
||||
submittedUpdatedAt: '2026-03-07T12:00:00.000Z',
|
||||
startedAt: '2026-03-07T12:24:10.000Z',
|
||||
taskId: 'task-canceled-1',
|
||||
taskStatus: 'canceled',
|
||||
taskErrorMessage: 'Task cancelled by user',
|
||||
},
|
||||
},
|
||||
setPendingVoiceGenerationByLineId,
|
||||
onTaskFailure,
|
||||
})
|
||||
|
||||
const renderEffects = effectCallbacks.splice(0)
|
||||
renderEffects[1]?.()
|
||||
|
||||
expect(onTaskFailure).toHaveBeenCalledWith({
|
||||
lineId: 'line-10',
|
||||
line: expect.objectContaining({
|
||||
id: 'line-10',
|
||||
lineIndex: 10,
|
||||
}),
|
||||
taskId: 'task-canceled-1',
|
||||
errorMessage: 'Task cancelled by user',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,140 +0,0 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
executePipelineGraph,
|
||||
GraphCancellationError,
|
||||
type GraphExecutorState,
|
||||
} from '@/lib/run-runtime/graph-executor'
|
||||
|
||||
const { createCheckpointMock, getRunByIdMock } = vi.hoisted(() => ({
|
||||
createCheckpointMock: vi.fn(),
|
||||
getRunByIdMock: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/run-runtime/service', () => ({
|
||||
buildLeanState: vi.fn((value: unknown) => value),
|
||||
createCheckpoint: createCheckpointMock,
|
||||
getRunById: getRunByIdMock,
|
||||
}))
|
||||
|
||||
describe('graph executor', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
getRunByIdMock.mockResolvedValue({
|
||||
id: 'run_1',
|
||||
userId: 'user_1',
|
||||
status: 'running',
|
||||
})
|
||||
})
|
||||
|
||||
it('retries retryable node error and writes checkpoint once success', async () => {
|
||||
const state: GraphExecutorState = {
|
||||
refs: {},
|
||||
meta: {},
|
||||
}
|
||||
|
||||
const runMock = vi
|
||||
.fn()
|
||||
.mockRejectedValueOnce(new TypeError('fetch failed sending request'))
|
||||
.mockResolvedValueOnce({
|
||||
output: { ok: true },
|
||||
})
|
||||
|
||||
await executePipelineGraph({
|
||||
runId: 'run_1',
|
||||
projectId: 'project_1',
|
||||
userId: 'user_1',
|
||||
state,
|
||||
nodes: [
|
||||
{
|
||||
key: 'node_a',
|
||||
title: 'Node A',
|
||||
maxAttempts: 2,
|
||||
run: runMock,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
expect(runMock).toHaveBeenCalledTimes(2)
|
||||
expect(createCheckpointMock).toHaveBeenCalledTimes(1)
|
||||
expect(createCheckpointMock).toHaveBeenCalledWith(expect.objectContaining({
|
||||
runId: 'run_1',
|
||||
nodeKey: 'node_a',
|
||||
version: 2,
|
||||
}))
|
||||
})
|
||||
|
||||
it('throws cancellation error when run status is canceling', async () => {
|
||||
getRunByIdMock.mockResolvedValue({
|
||||
id: 'run_1',
|
||||
userId: 'user_1',
|
||||
status: 'canceling',
|
||||
})
|
||||
|
||||
await expect(
|
||||
executePipelineGraph({
|
||||
runId: 'run_1',
|
||||
projectId: 'project_1',
|
||||
userId: 'user_1',
|
||||
state: {
|
||||
refs: {},
|
||||
meta: {},
|
||||
},
|
||||
nodes: [
|
||||
{
|
||||
key: 'node_a',
|
||||
title: 'Node A',
|
||||
run: async () => ({ output: { ok: true } }),
|
||||
},
|
||||
],
|
||||
}),
|
||||
).rejects.toBeInstanceOf(GraphCancellationError)
|
||||
})
|
||||
|
||||
it('merges refs into state and persists lean checkpoint', async () => {
|
||||
const state: GraphExecutorState = {
|
||||
refs: {
|
||||
scriptId: 'script_1',
|
||||
},
|
||||
meta: {
|
||||
tag: 'v1',
|
||||
},
|
||||
}
|
||||
|
||||
await executePipelineGraph({
|
||||
runId: 'run_1',
|
||||
projectId: 'project_1',
|
||||
userId: 'user_1',
|
||||
state,
|
||||
nodes: [
|
||||
{
|
||||
key: 'node_b',
|
||||
title: 'Node B',
|
||||
run: async () => ({
|
||||
checkpointRefs: {
|
||||
storyboardId: 'storyboard_1',
|
||||
},
|
||||
checkpointMeta: {
|
||||
done: true,
|
||||
},
|
||||
}),
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
expect(state.refs).toEqual({
|
||||
scriptId: 'script_1',
|
||||
storyboardId: 'storyboard_1',
|
||||
voiceLineBatchId: undefined,
|
||||
versionHash: undefined,
|
||||
cursor: undefined,
|
||||
})
|
||||
expect(createCheckpointMock).toHaveBeenCalledWith(expect.objectContaining({
|
||||
state: expect.objectContaining({
|
||||
refs: expect.objectContaining({
|
||||
scriptId: 'script_1',
|
||||
storyboardId: 'storyboard_1',
|
||||
}),
|
||||
}),
|
||||
}))
|
||||
})
|
||||
})
|
||||
@@ -1,136 +0,0 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { GraphExecutorState } from '@/lib/run-runtime/graph-executor'
|
||||
|
||||
const executePipelineGraphMock = vi.hoisted(() =>
|
||||
vi.fn(async (input: {
|
||||
runId: string
|
||||
projectId: string
|
||||
userId: string
|
||||
state: GraphExecutorState
|
||||
nodes: Array<{
|
||||
key: string
|
||||
run: (context: {
|
||||
runId: string
|
||||
projectId: string
|
||||
userId: string
|
||||
nodeKey: string
|
||||
attempt: number
|
||||
state: GraphExecutorState
|
||||
}) => Promise<unknown>
|
||||
}>
|
||||
}) => {
|
||||
for (const node of input.nodes) {
|
||||
await node.run({
|
||||
runId: input.runId,
|
||||
projectId: input.projectId,
|
||||
userId: input.userId,
|
||||
nodeKey: node.key,
|
||||
attempt: 1,
|
||||
state: input.state,
|
||||
})
|
||||
}
|
||||
return input.state
|
||||
}),
|
||||
)
|
||||
|
||||
vi.mock('@/lib/run-runtime/graph-executor', () => ({
|
||||
executePipelineGraph: executePipelineGraphMock,
|
||||
}))
|
||||
|
||||
import { runLangGraphPipeline } from '@/lib/run-runtime/langgraph-pipeline'
|
||||
|
||||
type TestState = GraphExecutorState & {
|
||||
order: string[]
|
||||
}
|
||||
|
||||
describe('langgraph pipeline adapter', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('runs nodes in declared order through langgraph', async () => {
|
||||
const state: TestState = {
|
||||
refs: {},
|
||||
meta: {},
|
||||
order: [],
|
||||
}
|
||||
|
||||
const result = await runLangGraphPipeline({
|
||||
runId: 'run-1',
|
||||
projectId: 'project-1',
|
||||
userId: 'user-1',
|
||||
state,
|
||||
nodes: [
|
||||
{
|
||||
key: 'node_a',
|
||||
title: 'Node A',
|
||||
run: async (context) => {
|
||||
const typedState = context.state as TestState
|
||||
typedState.order.push('node_a')
|
||||
return { output: { ok: true } }
|
||||
},
|
||||
},
|
||||
{
|
||||
key: 'node_b',
|
||||
title: 'Node B',
|
||||
run: async (context) => {
|
||||
const typedState = context.state as TestState
|
||||
typedState.order.push('node_b')
|
||||
return { output: { ok: true } }
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
expect(result.order).toEqual(['node_a', 'node_b'])
|
||||
expect(executePipelineGraphMock).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('returns input state when graph has no nodes', async () => {
|
||||
const state: TestState = {
|
||||
refs: {},
|
||||
meta: {},
|
||||
order: [],
|
||||
}
|
||||
|
||||
const result = await runLangGraphPipeline({
|
||||
runId: 'run-1',
|
||||
projectId: 'project-1',
|
||||
userId: 'user-1',
|
||||
state,
|
||||
nodes: [],
|
||||
})
|
||||
|
||||
expect(result).toBe(state)
|
||||
expect(executePipelineGraphMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('fails explicitly on duplicate node keys', async () => {
|
||||
const state: TestState = {
|
||||
refs: {},
|
||||
meta: {},
|
||||
order: [],
|
||||
}
|
||||
|
||||
await expect(
|
||||
runLangGraphPipeline({
|
||||
runId: 'run-1',
|
||||
projectId: 'project-1',
|
||||
userId: 'user-1',
|
||||
state,
|
||||
nodes: [
|
||||
{
|
||||
key: 'dup',
|
||||
title: 'Dup 1',
|
||||
run: async () => ({ output: { ok: true } }),
|
||||
},
|
||||
{
|
||||
key: 'dup',
|
||||
title: 'Dup 2',
|
||||
run: async () => ({ output: { ok: true } }),
|
||||
},
|
||||
],
|
||||
}),
|
||||
).rejects.toThrow('LANGGRAPH_NODE_KEY_DUPLICATE: dup')
|
||||
})
|
||||
})
|
||||
128
tests/unit/task/publisher.direct-run-events.test.ts
Normal file
128
tests/unit/task/publisher.direct-run-events.test.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
type TaskEventRow = {
|
||||
id: number
|
||||
taskId: string
|
||||
projectId: string
|
||||
userId: string
|
||||
eventType: string
|
||||
payload: Record<string, unknown> | null
|
||||
createdAt: Date
|
||||
}
|
||||
|
||||
const taskEventCreateMock = vi.hoisted(() =>
|
||||
vi.fn<(...args: unknown[]) => Promise<TaskEventRow | null>>(async () => null),
|
||||
)
|
||||
const taskEventFindManyMock = vi.hoisted(() =>
|
||||
vi.fn<(...args: unknown[]) => Promise<TaskEventRow[]>>(async () => []),
|
||||
)
|
||||
const taskFindManyMock = vi.hoisted(() =>
|
||||
vi.fn<(...args: unknown[]) => Promise<Array<Record<string, unknown>>>>(async () => []),
|
||||
)
|
||||
const redisPublishMock = vi.hoisted(() => vi.fn(async () => 1))
|
||||
const mapTaskSSEEventToRunEventsMock = vi.hoisted(() =>
|
||||
vi.fn(() => [{
|
||||
runId: 'run-1',
|
||||
projectId: 'project-1',
|
||||
userId: 'user-1',
|
||||
eventType: 'step.chunk',
|
||||
stepKey: 'split_clips',
|
||||
attempt: 1,
|
||||
lane: 'text',
|
||||
payload: { ok: true },
|
||||
}]),
|
||||
)
|
||||
const publishRunEventMock = vi.hoisted(() => vi.fn(async () => undefined))
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
prisma: {
|
||||
taskEvent: {
|
||||
create: taskEventCreateMock,
|
||||
findMany: taskEventFindManyMock,
|
||||
},
|
||||
task: {
|
||||
findMany: taskFindManyMock,
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/redis', () => ({
|
||||
redis: {
|
||||
publish: redisPublishMock,
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/run-runtime/task-bridge', () => ({
|
||||
mapTaskSSEEventToRunEvents: mapTaskSSEEventToRunEventsMock,
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/run-runtime/publisher', () => ({
|
||||
publishRunEvent: publishRunEventMock,
|
||||
}))
|
||||
|
||||
import { publishTaskStreamEvent } from '@/lib/task/publisher'
|
||||
|
||||
describe('task publisher direct run event boundary', () => {
|
||||
beforeEach(() => {
|
||||
taskEventCreateMock.mockReset()
|
||||
taskEventFindManyMock.mockReset()
|
||||
taskFindManyMock.mockReset()
|
||||
redisPublishMock.mockReset()
|
||||
mapTaskSSEEventToRunEventsMock.mockClear()
|
||||
publishRunEventMock.mockClear()
|
||||
})
|
||||
|
||||
it('does not mirror run events for story_to_script task stream events', async () => {
|
||||
await publishTaskStreamEvent({
|
||||
taskId: 'task-1',
|
||||
projectId: 'project-1',
|
||||
userId: 'user-1',
|
||||
taskType: 'story_to_script_run',
|
||||
targetType: 'NovelPromotionEpisode',
|
||||
targetId: 'episode-1',
|
||||
episodeId: 'episode-1',
|
||||
payload: {
|
||||
stepId: 'split_clips',
|
||||
stream: {
|
||||
kind: 'text',
|
||||
seq: 1,
|
||||
lane: 'main',
|
||||
delta: 'hello',
|
||||
},
|
||||
},
|
||||
persist: false,
|
||||
})
|
||||
|
||||
expect(redisPublishMock).toHaveBeenCalledTimes(1)
|
||||
expect(mapTaskSSEEventToRunEventsMock).not.toHaveBeenCalled()
|
||||
expect(publishRunEventMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('continues mirroring run events for non-core task types', async () => {
|
||||
await publishTaskStreamEvent({
|
||||
taskId: 'task-2',
|
||||
projectId: 'project-1',
|
||||
userId: 'user-1',
|
||||
taskType: 'voice_line',
|
||||
targetType: 'VoiceLine',
|
||||
targetId: 'line-1',
|
||||
payload: {
|
||||
stepId: 'voice',
|
||||
stream: {
|
||||
kind: 'text',
|
||||
seq: 1,
|
||||
lane: 'main',
|
||||
delta: 'world',
|
||||
},
|
||||
},
|
||||
persist: false,
|
||||
})
|
||||
|
||||
expect(mapTaskSSEEventToRunEventsMock).toHaveBeenCalledTimes(1)
|
||||
expect(publishRunEventMock).toHaveBeenCalledWith(expect.objectContaining({
|
||||
runId: 'run-1',
|
||||
eventType: 'step.chunk',
|
||||
stepKey: 'split_clips',
|
||||
}))
|
||||
})
|
||||
})
|
||||
@@ -132,4 +132,36 @@ describe('createWorkerLLMStreamCallbacks', () => {
|
||||
expect(payload.stepTitle).toBe('A')
|
||||
expect(payload.output).toBe('characters-final')
|
||||
})
|
||||
|
||||
it('uses injected active controller for run-owned workflows', async () => {
|
||||
const job = buildJob()
|
||||
const context = createWorkerLLMStreamContext(job, 'story_to_script')
|
||||
const assertActive = vi.fn(async (_stage: string) => undefined)
|
||||
const isActive = vi.fn(async () => true)
|
||||
const callbacks = createWorkerLLMStreamCallbacks(job, context, {
|
||||
assertActive,
|
||||
isActive,
|
||||
})
|
||||
|
||||
callbacks.onChunk?.({
|
||||
kind: 'text',
|
||||
delta: 'hello',
|
||||
seq: 1,
|
||||
lane: 'main',
|
||||
step: { id: 'split_clips', attempt: 1, title: 'split', index: 1, total: 1 },
|
||||
})
|
||||
await callbacks.flush()
|
||||
|
||||
expect(assertActive).toHaveBeenCalledWith('worker_llm_stream')
|
||||
expect(assertTaskActiveMock).not.toHaveBeenCalled()
|
||||
expect(reportTaskStreamChunkMock).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.objectContaining({
|
||||
delta: 'hello',
|
||||
}),
|
||||
expect.objectContaining({
|
||||
stepId: 'split_clips',
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -47,32 +47,17 @@ const runScriptToStoryboardOrchestratorMock = vi.hoisted(() =>
|
||||
},
|
||||
})),
|
||||
)
|
||||
const graphExecutorMock = vi.hoisted(() => ({
|
||||
executePipelineGraph: vi.fn(async (input: {
|
||||
runId: string
|
||||
projectId: string
|
||||
userId: string
|
||||
state: Record<string, unknown>
|
||||
nodes: Array<{ key: string; run: (ctx: Record<string, unknown>) => Promise<unknown> }>
|
||||
}) => {
|
||||
for (const node of input.nodes) {
|
||||
await node.run({
|
||||
runId: input.runId,
|
||||
projectId: input.projectId,
|
||||
userId: input.userId,
|
||||
nodeKey: node.key,
|
||||
attempt: 1,
|
||||
state: input.state,
|
||||
})
|
||||
}
|
||||
return input.state
|
||||
}),
|
||||
}))
|
||||
|
||||
const parseVoiceLinesJsonMock = vi.hoisted(() => vi.fn())
|
||||
const persistStoryboardsAndPanelsMock = vi.hoisted(() => vi.fn())
|
||||
const parseStoryboardRetryTargetMock = vi.hoisted(() => vi.fn())
|
||||
const runScriptToStoryboardAtomicRetryMock = vi.hoisted(() => vi.fn())
|
||||
const workflowLeaseMock = vi.hoisted(() => ({
|
||||
assertWorkflowRunActive: vi.fn(async () => undefined),
|
||||
withWorkflowRunLease: vi.fn(async (params: { run: () => Promise<unknown> }) => ({
|
||||
claimed: true,
|
||||
result: await params.run(),
|
||||
})),
|
||||
}))
|
||||
|
||||
const txState = vi.hoisted(() => ({
|
||||
createdRows: [] as Array<Record<string, unknown>>,
|
||||
@@ -145,10 +130,6 @@ vi.mock('@/lib/novel-promotion/script-to-storyboard/orchestrator', () => ({
|
||||
}
|
||||
},
|
||||
}))
|
||||
vi.mock('@/lib/run-runtime/graph-executor', () => ({
|
||||
executePipelineGraph: graphExecutorMock.executePipelineGraph,
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/workers/handlers/llm-stream', () => ({
|
||||
createWorkerLLMStreamContext: vi.fn(() => ({ streamRunId: 'run-1', nextSeqByStepLane: {} })),
|
||||
createWorkerLLMStreamCallbacks: vi.fn(() => ({
|
||||
@@ -192,6 +173,7 @@ vi.mock('@/lib/workers/handlers/script-to-storyboard-atomic-retry', () => ({
|
||||
parseStoryboardRetryTarget: parseStoryboardRetryTargetMock,
|
||||
runScriptToStoryboardAtomicRetry: runScriptToStoryboardAtomicRetryMock,
|
||||
}))
|
||||
vi.mock('@/lib/run-runtime/workflow-lease', () => workflowLeaseMock)
|
||||
|
||||
import { handleScriptToStoryboardTask } from '@/lib/workers/handlers/script-to-storyboard'
|
||||
|
||||
|
||||
175
tests/unit/worker/shared.direct-run-events.test.ts
Normal file
175
tests/unit/worker/shared.direct-run-events.test.ts
Normal file
@@ -0,0 +1,175 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { Job } from 'bullmq'
|
||||
import type { TaskJobData } from '@/lib/task/types'
|
||||
|
||||
const tryUpdateTaskProgressMock = vi.hoisted(() => vi.fn(async () => true))
|
||||
const publishTaskEventMock = vi.hoisted(() => vi.fn(async () => ({})))
|
||||
const publishTaskStreamEventMock = vi.hoisted(() => vi.fn(async () => ({})))
|
||||
const publishRunEventMock = vi.hoisted(() => vi.fn(async () => undefined))
|
||||
const mapTaskSSEEventToRunEventsMock = vi.hoisted(() =>
|
||||
vi.fn(() => [{
|
||||
runId: 'run-1',
|
||||
projectId: 'project-1',
|
||||
userId: 'user-1',
|
||||
eventType: 'step.start',
|
||||
stepKey: 'split_clips',
|
||||
attempt: 1,
|
||||
lane: null,
|
||||
payload: { mirrored: true },
|
||||
}]),
|
||||
)
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
prisma: {
|
||||
project: {
|
||||
findUnique: vi.fn(async () => null),
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/logging/core', () => ({
|
||||
createScopedLogger: () => ({
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/task/service', () => ({
|
||||
rollbackTaskBillingForTask: vi.fn(async () => ({ attempted: false, rolledBack: false, billingInfo: null })),
|
||||
touchTaskHeartbeat: vi.fn(async () => undefined),
|
||||
tryMarkTaskCompleted: vi.fn(async () => true),
|
||||
tryMarkTaskFailed: vi.fn(async () => true),
|
||||
tryMarkTaskProcessing: vi.fn(async () => true),
|
||||
tryUpdateTaskProgress: tryUpdateTaskProgressMock,
|
||||
updateTaskBillingInfo: vi.fn(async () => undefined),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/task/publisher', () => ({
|
||||
publishTaskEvent: publishTaskEventMock,
|
||||
publishTaskStreamEvent: publishTaskStreamEventMock,
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/task/progress-message', () => ({
|
||||
buildTaskProgressMessage: vi.fn(() => 'progress-message'),
|
||||
getTaskStageLabel: vi.fn((stage: string) => `label:${stage}`),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/errors/normalize', () => ({
|
||||
normalizeAnyError: vi.fn((error: Error) => ({
|
||||
code: 'ERROR',
|
||||
message: error.message,
|
||||
retryable: false,
|
||||
provider: null,
|
||||
})),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/billing', () => ({
|
||||
rollbackTaskBilling: vi.fn(async () => null),
|
||||
settleTaskBilling: vi.fn(async () => null),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/billing/runtime-usage', () => ({
|
||||
withTextUsageCollection: vi.fn(async (fn: () => Promise<unknown>) => ({
|
||||
result: await fn(),
|
||||
textUsage: null,
|
||||
})),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/logging/file-writer', () => ({
|
||||
onProjectNameAvailable: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/run-runtime/task-bridge', () => ({
|
||||
mapTaskSSEEventToRunEvents: mapTaskSSEEventToRunEventsMock,
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/run-runtime/publisher', () => ({
|
||||
publishRunEvent: publishRunEventMock,
|
||||
}))
|
||||
|
||||
import { reportTaskProgress, reportTaskStreamChunk, withTaskLifecycle } from '@/lib/workers/shared'
|
||||
|
||||
function buildJob(taskType: TaskJobData['type']): Job<TaskJobData> {
|
||||
return {
|
||||
data: {
|
||||
taskId: 'task-1',
|
||||
type: taskType,
|
||||
locale: 'zh',
|
||||
projectId: 'project-1',
|
||||
episodeId: 'episode-1',
|
||||
targetType: 'NovelPromotionEpisode',
|
||||
targetId: 'episode-1',
|
||||
userId: 'user-1',
|
||||
payload: {
|
||||
runId: 'run-1',
|
||||
},
|
||||
trace: null,
|
||||
},
|
||||
queueName: 'text',
|
||||
} as unknown as Job<TaskJobData>
|
||||
}
|
||||
|
||||
describe('worker shared direct run events', () => {
|
||||
beforeEach(() => {
|
||||
tryUpdateTaskProgressMock.mockReset()
|
||||
tryUpdateTaskProgressMock.mockResolvedValue(true)
|
||||
publishTaskEventMock.mockReset()
|
||||
publishTaskStreamEventMock.mockReset()
|
||||
publishRunEventMock.mockReset()
|
||||
mapTaskSSEEventToRunEventsMock.mockClear()
|
||||
})
|
||||
|
||||
it('publishes run events directly for core analysis progress updates', async () => {
|
||||
await reportTaskProgress(buildJob('story_to_script_run'), 42, {
|
||||
stage: 'story_to_script_step',
|
||||
stepId: 'split_clips',
|
||||
stepTitle: 'Split',
|
||||
})
|
||||
|
||||
expect(publishTaskEventMock).toHaveBeenCalledWith(expect.objectContaining({
|
||||
taskType: 'story_to_script_run',
|
||||
type: 'task.progress',
|
||||
}))
|
||||
expect(mapTaskSSEEventToRunEventsMock).toHaveBeenCalledTimes(1)
|
||||
expect(publishRunEventMock).toHaveBeenCalledWith(expect.objectContaining({
|
||||
runId: 'run-1',
|
||||
eventType: 'step.start',
|
||||
stepKey: 'split_clips',
|
||||
}))
|
||||
})
|
||||
|
||||
it('publishes run events directly for core analysis stream chunks', async () => {
|
||||
await reportTaskStreamChunk(buildJob('script_to_storyboard_run'), {
|
||||
kind: 'text',
|
||||
delta: 'hello',
|
||||
seq: 1,
|
||||
lane: 'main',
|
||||
}, {
|
||||
stepId: 'clip_1_phase1',
|
||||
stepTitle: 'Phase 1',
|
||||
})
|
||||
|
||||
expect(publishTaskStreamEventMock).toHaveBeenCalledWith(expect.objectContaining({
|
||||
taskType: 'script_to_storyboard_run',
|
||||
persist: true,
|
||||
}))
|
||||
expect(mapTaskSSEEventToRunEventsMock).toHaveBeenCalledTimes(1)
|
||||
expect(publishRunEventMock).toHaveBeenCalledWith(expect.objectContaining({
|
||||
runId: 'run-1',
|
||||
eventType: 'step.start',
|
||||
stepKey: 'split_clips',
|
||||
}))
|
||||
})
|
||||
|
||||
it('emits run.start directly when the core analysis worker begins execution', async () => {
|
||||
await withTaskLifecycle(buildJob('story_to_script_run'), async () => ({
|
||||
ok: true,
|
||||
}))
|
||||
|
||||
expect(publishRunEventMock).toHaveBeenCalledWith(expect.objectContaining({
|
||||
runId: 'run-1',
|
||||
eventType: 'run.start',
|
||||
}))
|
||||
})
|
||||
})
|
||||
@@ -26,33 +26,18 @@ const configMock = vi.hoisted(() => ({
|
||||
const orchestratorMock = vi.hoisted(() => ({
|
||||
runStoryToScriptOrchestrator: vi.fn(),
|
||||
}))
|
||||
const graphExecutorMock = vi.hoisted(() => ({
|
||||
executePipelineGraph: vi.fn(async (input: {
|
||||
runId: string
|
||||
projectId: string
|
||||
userId: string
|
||||
state: Record<string, unknown>
|
||||
nodes: Array<{ key: string; run: (ctx: Record<string, unknown>) => Promise<unknown> }>
|
||||
}) => {
|
||||
for (const node of input.nodes) {
|
||||
await node.run({
|
||||
runId: input.runId,
|
||||
projectId: input.projectId,
|
||||
userId: input.userId,
|
||||
nodeKey: node.key,
|
||||
attempt: 1,
|
||||
state: input.state,
|
||||
})
|
||||
}
|
||||
return input.state
|
||||
}),
|
||||
}))
|
||||
|
||||
const helperMock = vi.hoisted(() => ({
|
||||
persistAnalyzedCharacters: vi.fn(async () => [{ id: 'character-new-1' }]),
|
||||
persistAnalyzedLocations: vi.fn(async () => [{ id: 'location-new-1' }]),
|
||||
persistClips: vi.fn(async () => [{ clipKey: 'clip-1', id: 'clip-row-1' }]),
|
||||
}))
|
||||
const workflowLeaseMock = vi.hoisted(() => ({
|
||||
assertWorkflowRunActive: vi.fn(async () => undefined),
|
||||
withWorkflowRunLease: vi.fn(async (params: { run: () => Promise<unknown> }) => ({
|
||||
claimed: true,
|
||||
result: await params.run(),
|
||||
})),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({ prisma: prismaMock }))
|
||||
vi.mock('@/lib/llm-client', () => ({
|
||||
@@ -69,9 +54,6 @@ vi.mock('@/lib/logging/file-writer', () => ({ onProjectNameAvailable: vi.fn() })
|
||||
vi.mock('@/lib/workers/shared', () => ({ reportTaskProgress: workerMock.reportTaskProgress }))
|
||||
vi.mock('@/lib/workers/utils', () => ({ assertTaskActive: workerMock.assertTaskActive }))
|
||||
vi.mock('@/lib/novel-promotion/story-to-script/orchestrator', () => orchestratorMock)
|
||||
vi.mock('@/lib/run-runtime/graph-executor', () => ({
|
||||
executePipelineGraph: graphExecutorMock.executePipelineGraph,
|
||||
}))
|
||||
vi.mock('@/lib/workers/handlers/llm-stream', () => ({
|
||||
createWorkerLLMStreamContext: vi.fn(() => ({ streamRunId: 'run-1', nextSeqByStepLane: {} })),
|
||||
createWorkerLLMStreamCallbacks: vi.fn(() => ({
|
||||
@@ -100,6 +82,7 @@ vi.mock('@/lib/workers/handlers/story-to-script-helpers', () => ({
|
||||
persistClips: helperMock.persistClips,
|
||||
resolveClipRecordId: (clipIdMap: Map<string, string>, clipId: string) => clipIdMap.get(clipId) ?? null,
|
||||
}))
|
||||
vi.mock('@/lib/run-runtime/workflow-lease', () => workflowLeaseMock)
|
||||
|
||||
import { handleStoryToScriptTask } from '@/lib/workers/handlers/story-to-script'
|
||||
|
||||
|
||||
@@ -82,6 +82,32 @@ describe('useRebuildConfirm', () => {
|
||||
|
||||
expect(action).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('story to script without downstream confirm clears pending action after action completes', async () => {
|
||||
const getProjectStoryboardStats = vi.fn(async () => ({ storyboardCount: 0, panelCount: 0 }))
|
||||
const action = vi.fn(async () => undefined)
|
||||
|
||||
const hook = useRebuildConfirm({
|
||||
episodeId: 'episode-1',
|
||||
episodeStoryboards: [],
|
||||
getProjectStoryboardStats,
|
||||
t: (key: string) => key,
|
||||
})
|
||||
|
||||
await hook.runWithRebuildConfirm('storyToScript', action)
|
||||
|
||||
expect(action).toHaveBeenCalledTimes(1)
|
||||
expect(setPendingActionTypeMock).toHaveBeenCalledTimes(2)
|
||||
expect(setPendingActionTypeMock).toHaveBeenNthCalledWith(1, 'storyToScript')
|
||||
|
||||
const resetCall = setPendingActionTypeMock.mock.calls[1]?.[0]
|
||||
expect(typeof resetCall).toBe('function')
|
||||
if (typeof resetCall !== 'function') {
|
||||
throw new Error('expected reset pending action updater')
|
||||
}
|
||||
expect(resetCall('storyToScript')).toBeNull()
|
||||
expect(resetCall('scriptToStoryboard')).toBe('scriptToStoryboard')
|
||||
})
|
||||
})
|
||||
|
||||
describe('hasDownstreamStoryboardData', () => {
|
||||
|
||||
Reference in New Issue
Block a user