refactor: analysis workflow architecture
fix: NEXTAUTH_URL fix: prevent project model edits from affecting default model
This commit is contained in:
96
tests/integration/api/contract/run-cancel.route.test.ts
Normal file
96
tests/integration/api/contract/run-cancel.route.test.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { buildMockRequest } from '../../../helpers/request'
|
||||
|
||||
const authState = vi.hoisted(() => ({ authenticated: true }))
|
||||
const getRunByIdMock = vi.hoisted(() => vi.fn())
|
||||
const requestRunCancelMock = vi.hoisted(() => vi.fn())
|
||||
const cancelTaskMock = vi.hoisted(() => vi.fn())
|
||||
const publishRunEventMock = vi.hoisted(() => vi.fn(async () => undefined))
|
||||
|
||||
vi.mock('@/lib/api-auth', () => {
|
||||
const unauthorized = () => new Response(
|
||||
JSON.stringify({ error: { code: 'UNAUTHORIZED' } }),
|
||||
{ status: 401, headers: { 'content-type': 'application/json' } },
|
||||
)
|
||||
|
||||
return {
|
||||
isErrorResponse: (value: unknown) => value instanceof Response,
|
||||
requireUserAuth: async () => {
|
||||
if (!authState.authenticated) return unauthorized()
|
||||
return { session: { user: { id: 'user-1' } } }
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('@/lib/run-runtime/service', () => ({
|
||||
getRunById: getRunByIdMock,
|
||||
requestRunCancel: requestRunCancelMock,
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/task/service', () => ({
|
||||
cancelTask: cancelTaskMock,
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/run-runtime/publisher', () => ({
|
||||
publishRunEvent: publishRunEventMock,
|
||||
}))
|
||||
|
||||
describe('api contract - run cancel route', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
authState.authenticated = true
|
||||
getRunByIdMock.mockResolvedValue({
|
||||
id: 'run-1',
|
||||
userId: 'user-1',
|
||||
projectId: 'project-1',
|
||||
taskId: 'task-1',
|
||||
})
|
||||
requestRunCancelMock.mockResolvedValue({
|
||||
id: 'run-1',
|
||||
userId: 'user-1',
|
||||
projectId: 'project-1',
|
||||
taskId: 'task-1',
|
||||
status: 'canceling',
|
||||
})
|
||||
cancelTaskMock.mockResolvedValue({
|
||||
task: {
|
||||
id: 'task-1',
|
||||
status: 'canceled',
|
||||
errorCode: 'TASK_CANCELLED',
|
||||
errorMessage: 'Run cancelled by user',
|
||||
},
|
||||
cancelled: true,
|
||||
})
|
||||
})
|
||||
|
||||
it('marks the run canceled and mirrors task cancellation without failing the task', async () => {
|
||||
const { POST } = await import('@/app/api/runs/[runId]/cancel/route')
|
||||
|
||||
const req = buildMockRequest({
|
||||
path: '/api/runs/run-1/cancel',
|
||||
method: 'POST',
|
||||
})
|
||||
const res = await POST(req, {
|
||||
params: Promise.resolve({ runId: 'run-1' }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const payload = await res.json() as {
|
||||
success: boolean
|
||||
run: {
|
||||
id: string
|
||||
status: string
|
||||
}
|
||||
}
|
||||
expect(payload.success).toBe(true)
|
||||
expect(payload.run).toMatchObject({
|
||||
id: 'run-1',
|
||||
status: 'canceling',
|
||||
})
|
||||
expect(cancelTaskMock).toHaveBeenCalledWith('task-1', 'Run cancelled by user')
|
||||
expect(publishRunEventMock).toHaveBeenCalledWith(expect.objectContaining({
|
||||
runId: 'run-1',
|
||||
eventType: 'run.canceled',
|
||||
}))
|
||||
})
|
||||
})
|
||||
@@ -144,7 +144,7 @@ describe('api contract - task infra routes (behavior)', () => {
|
||||
cancelTaskMock.mockResolvedValue({
|
||||
task: {
|
||||
...baseTask,
|
||||
status: TASK_STATUS.FAILED,
|
||||
status: TASK_STATUS.CANCELED,
|
||||
errorCode: 'TASK_CANCELLED',
|
||||
errorMessage: 'Task cancelled by user',
|
||||
},
|
||||
@@ -336,8 +336,11 @@ describe('api contract - task infra routes (behavior)', () => {
|
||||
const req = buildMockRequest({ path: '/api/tasks/task-1', method: 'DELETE' })
|
||||
const res = await DELETE(req, { params: Promise.resolve({ taskId: 'task-1' }) } as RouteContext)
|
||||
expect(res.status).toBe(200)
|
||||
const payload = await res.json() as { task: TaskRecord; cancelled: boolean }
|
||||
|
||||
expect(removeTaskJobMock).toHaveBeenCalledWith('task-1')
|
||||
expect(payload.cancelled).toBe(true)
|
||||
expect(payload.task.status).toBe(TASK_STATUS.CANCELED)
|
||||
expect(publishTaskEventMock).toHaveBeenCalledWith(expect.objectContaining({
|
||||
taskId: 'task-1',
|
||||
projectId: 'project-1',
|
||||
|
||||
@@ -60,7 +60,7 @@ describe('api specific - novel promotion project art style validation', () => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('accepts valid artStyle and syncs to user preference', async () => {
|
||||
it('accepts valid artStyle and keeps user preference unchanged', async () => {
|
||||
const mod = await import('@/app/api/novel-promotion/[projectId]/route')
|
||||
const req = buildMockRequest({
|
||||
path: '/api/novel-promotion/project-1',
|
||||
@@ -77,11 +77,7 @@ describe('api specific - novel promotion project art style validation', () => {
|
||||
data: expect.objectContaining({ artStyle: 'realistic' }),
|
||||
}),
|
||||
)
|
||||
expect(prismaMock.userPreference.upsert).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
update: expect.objectContaining({ artStyle: 'realistic' }),
|
||||
}),
|
||||
)
|
||||
expect(prismaMock.userPreference.upsert).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects invalid artStyle with invalid params', async () => {
|
||||
@@ -102,7 +98,7 @@ describe('api specific - novel promotion project art style validation', () => {
|
||||
expect(prismaMock.userPreference.upsert).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('accepts audioModel and syncs it to user preference', async () => {
|
||||
it('accepts audioModel and keeps user preference unchanged', async () => {
|
||||
const mod = await import('@/app/api/novel-promotion/[projectId]/route')
|
||||
const req = buildMockRequest({
|
||||
path: '/api/novel-promotion/project-1',
|
||||
@@ -121,12 +117,6 @@ describe('api specific - novel promotion project art style validation', () => {
|
||||
}),
|
||||
}),
|
||||
)
|
||||
expect(prismaMock.userPreference.upsert).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
update: expect.objectContaining({
|
||||
audioModel: 'bailian::qwen3-tts-vd-2026-01-26',
|
||||
}),
|
||||
}),
|
||||
)
|
||||
expect(prismaMock.userPreference.upsert).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { ApiError } from '@/lib/api-errors'
|
||||
import { buildDefaultTaskBillingInfo } from '@/lib/billing/task-policy'
|
||||
import { createRun } from '@/lib/run-runtime/service'
|
||||
import { submitTask } from '@/lib/task/submitter'
|
||||
import { TASK_TYPE } from '@/lib/task/types'
|
||||
import { TASK_STATUS, TASK_TYPE } from '@/lib/task/types'
|
||||
import { prisma } from '../../helpers/prisma'
|
||||
import { resetBillingState } from '../../helpers/db-reset'
|
||||
import { createTestUser, seedBalance } from '../../helpers/billing-fixtures'
|
||||
@@ -11,19 +12,23 @@ const queueState = vi.hoisted(() => ({
|
||||
mode: 'success' as 'success' | 'fail',
|
||||
errorMessage: 'queue add failed',
|
||||
}))
|
||||
const addTaskJobMock = vi.hoisted(() => vi.fn(async () => ({ id: 'mock-job' })))
|
||||
const publishTaskEventMock = vi.hoisted(() => vi.fn(async () => ({})))
|
||||
|
||||
vi.mock('@/lib/task/queues', () => ({
|
||||
addTaskJob: vi.fn(async () => {
|
||||
addTaskJob: addTaskJobMock,
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/task/publisher', () => ({
|
||||
publishTaskEvent: publishTaskEventMock,
|
||||
}))
|
||||
|
||||
addTaskJobMock.mockImplementation(async () => {
|
||||
if (queueState.mode === 'fail') {
|
||||
throw new Error(queueState.errorMessage)
|
||||
}
|
||||
return { id: 'mock-job' }
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/task/publisher', () => ({
|
||||
publishTaskEvent: vi.fn(async () => ({})),
|
||||
}))
|
||||
})
|
||||
|
||||
describe('billing/submitter integration', () => {
|
||||
beforeEach(async () => {
|
||||
@@ -31,6 +36,7 @@ describe('billing/submitter integration', () => {
|
||||
process.env.BILLING_MODE = 'ENFORCE'
|
||||
queueState.mode = 'success'
|
||||
queueState.errorMessage = 'queue add failed'
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('builds billing info server-side for billable task submission', async () => {
|
||||
@@ -181,4 +187,140 @@ describe('billing/submitter integration', () => {
|
||||
const freeze = await prisma.balanceFreeze.findFirst({ orderBy: { createdAt: 'desc' } })
|
||||
expect(freeze?.status).toBe('rolled_back')
|
||||
})
|
||||
|
||||
it('reuses the active core analysis run instead of creating a second run', async () => {
|
||||
process.env.BILLING_MODE = 'OFF'
|
||||
const user = await createTestUser()
|
||||
const existingTask = await prisma.task.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
projectId: 'project-core',
|
||||
episodeId: 'episode-core',
|
||||
type: TASK_TYPE.STORY_TO_SCRIPT_RUN,
|
||||
targetType: 'NovelPromotionEpisode',
|
||||
targetId: 'episode-core',
|
||||
status: TASK_STATUS.QUEUED,
|
||||
payload: {
|
||||
episodeId: 'episode-core',
|
||||
analysisModel: 'model-core',
|
||||
meta: { locale: 'zh' },
|
||||
},
|
||||
queuedAt: new Date(),
|
||||
},
|
||||
})
|
||||
const run = await createRun({
|
||||
userId: user.id,
|
||||
projectId: 'project-core',
|
||||
episodeId: 'episode-core',
|
||||
workflowType: TASK_TYPE.STORY_TO_SCRIPT_RUN,
|
||||
taskType: TASK_TYPE.STORY_TO_SCRIPT_RUN,
|
||||
taskId: existingTask.id,
|
||||
targetType: 'NovelPromotionEpisode',
|
||||
targetId: 'episode-core',
|
||||
input: {
|
||||
episodeId: 'episode-core',
|
||||
analysisModel: 'model-core',
|
||||
meta: { locale: 'zh' },
|
||||
},
|
||||
})
|
||||
await prisma.task.update({
|
||||
where: { id: existingTask.id },
|
||||
data: {
|
||||
payload: {
|
||||
episodeId: 'episode-core',
|
||||
analysisModel: 'model-core',
|
||||
runId: run.id,
|
||||
meta: { locale: 'zh', runId: run.id },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const result = await submitTask({
|
||||
userId: user.id,
|
||||
locale: 'zh',
|
||||
projectId: 'project-core',
|
||||
episodeId: 'episode-core',
|
||||
type: TASK_TYPE.STORY_TO_SCRIPT_RUN,
|
||||
targetType: 'NovelPromotionEpisode',
|
||||
targetId: 'episode-core',
|
||||
payload: {
|
||||
episodeId: 'episode-core',
|
||||
analysisModel: 'model-core',
|
||||
},
|
||||
dedupeKey: 'story_to_script:episode-core',
|
||||
})
|
||||
|
||||
expect(result.deduped).toBe(true)
|
||||
expect(result.taskId).toBe(existingTask.id)
|
||||
expect(result.runId).toBe(run.id)
|
||||
expect(await prisma.graphRun.count()).toBe(1)
|
||||
expect(addTaskJobMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('reattaches a new task to the existing active run when the old task is already terminal', async () => {
|
||||
process.env.BILLING_MODE = 'OFF'
|
||||
const user = await createTestUser()
|
||||
const failedTask = await prisma.task.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
projectId: 'project-core-retry',
|
||||
episodeId: 'episode-core-retry',
|
||||
type: TASK_TYPE.SCRIPT_TO_STORYBOARD_RUN,
|
||||
targetType: 'NovelPromotionEpisode',
|
||||
targetId: 'episode-core-retry',
|
||||
status: TASK_STATUS.FAILED,
|
||||
errorCode: 'TEST_FAILED',
|
||||
errorMessage: 'old task already failed',
|
||||
payload: {
|
||||
episodeId: 'episode-core-retry',
|
||||
analysisModel: 'model-core',
|
||||
meta: { locale: 'zh' },
|
||||
},
|
||||
queuedAt: new Date(),
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
})
|
||||
const run = await createRun({
|
||||
userId: user.id,
|
||||
projectId: 'project-core-retry',
|
||||
episodeId: 'episode-core-retry',
|
||||
workflowType: TASK_TYPE.SCRIPT_TO_STORYBOARD_RUN,
|
||||
taskType: TASK_TYPE.SCRIPT_TO_STORYBOARD_RUN,
|
||||
taskId: failedTask.id,
|
||||
targetType: 'NovelPromotionEpisode',
|
||||
targetId: 'episode-core-retry',
|
||||
input: {
|
||||
episodeId: 'episode-core-retry',
|
||||
analysisModel: 'model-core',
|
||||
meta: { locale: 'zh' },
|
||||
},
|
||||
})
|
||||
|
||||
const result = await submitTask({
|
||||
userId: user.id,
|
||||
locale: 'zh',
|
||||
projectId: 'project-core-retry',
|
||||
episodeId: 'episode-core-retry',
|
||||
type: TASK_TYPE.SCRIPT_TO_STORYBOARD_RUN,
|
||||
targetType: 'NovelPromotionEpisode',
|
||||
targetId: 'episode-core-retry',
|
||||
payload: {
|
||||
episodeId: 'episode-core-retry',
|
||||
analysisModel: 'model-core',
|
||||
},
|
||||
dedupeKey: 'script_to_storyboard:episode-core-retry',
|
||||
})
|
||||
|
||||
expect(result.deduped).toBe(false)
|
||||
expect(result.runId).toBe(run.id)
|
||||
expect(result.taskId).not.toBe(failedTask.id)
|
||||
|
||||
const refreshedRun = await prisma.graphRun.findUnique({ where: { id: run.id } })
|
||||
const newTask = await prisma.task.findUnique({ where: { id: result.taskId } })
|
||||
expect(refreshedRun?.taskId).toBe(result.taskId)
|
||||
expect(newTask?.status).toBe(TASK_STATUS.QUEUED)
|
||||
expect(newTask?.payload).toMatchObject({
|
||||
runId: run.id,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -122,7 +122,30 @@ describe('chain contract - text queue behavior', () => {
|
||||
expect(calls).toHaveLength(1)
|
||||
expect(calls[0]).toEqual(expect.objectContaining({
|
||||
jobName: TASK_TYPE.SCRIPT_TO_STORYBOARD_RUN,
|
||||
options: expect.objectContaining({ jobId: 'task-text-1', priority: 0 }),
|
||||
options: expect.objectContaining({ jobId: 'task-text-1', priority: 0, attempts: 1 }),
|
||||
}))
|
||||
})
|
||||
|
||||
it('forces single queue attempt for core analysis workflows', async () => {
|
||||
const { addTaskJob, QUEUE_NAME } = await import('@/lib/task/queues')
|
||||
|
||||
await addTaskJob({
|
||||
taskId: 'task-text-story-1',
|
||||
type: TASK_TYPE.STORY_TO_SCRIPT_RUN,
|
||||
locale: 'zh',
|
||||
projectId: 'project-1',
|
||||
episodeId: 'episode-1',
|
||||
targetType: 'NovelPromotionEpisode',
|
||||
targetId: 'episode-1',
|
||||
payload: { episodeId: 'episode-1' },
|
||||
userId: 'user-1',
|
||||
}, { attempts: 5 })
|
||||
|
||||
const calls = queueState.addCallsByQueue.get(QUEUE_NAME.TEXT) || []
|
||||
expect(calls).toHaveLength(1)
|
||||
expect(calls[0]?.options).toEqual(expect.objectContaining({
|
||||
jobId: 'task-text-story-1',
|
||||
attempts: 1,
|
||||
}))
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,343 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest'
|
||||
import { retryFailedStep } from '@/lib/run-runtime/service'
|
||||
import { RUN_STATUS, RUN_STEP_STATUS } from '@/lib/run-runtime/types'
|
||||
import { prisma } from '../../helpers/prisma'
|
||||
import { resetBillingState } from '../../helpers/db-reset'
|
||||
import { createTestUser } from '../../helpers/billing-fixtures'
|
||||
|
||||
describe('run runtime retryFailedStep invalidation', () => {
|
||||
beforeEach(async () => {
|
||||
await resetBillingState()
|
||||
})
|
||||
|
||||
it('invalidates downstream story-to-script steps and artifacts', async () => {
|
||||
const user = await createTestUser()
|
||||
const run = await prisma.graphRun.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
projectId: 'project-retry-story',
|
||||
episodeId: 'episode-retry-story',
|
||||
workflowType: 'story_to_script_run',
|
||||
taskType: 'story_to_script_run',
|
||||
targetType: 'NovelPromotionEpisode',
|
||||
targetId: 'episode-retry-story',
|
||||
status: RUN_STATUS.FAILED,
|
||||
queuedAt: new Date(),
|
||||
startedAt: new Date(),
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.graphStep.createMany({
|
||||
data: [
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'analyze_characters',
|
||||
stepTitle: 'Analyze Characters',
|
||||
status: RUN_STEP_STATUS.FAILED,
|
||||
currentAttempt: 1,
|
||||
stepIndex: 1,
|
||||
stepTotal: 5,
|
||||
startedAt: new Date(),
|
||||
finishedAt: new Date(),
|
||||
lastErrorCode: 'STEP_FAILED',
|
||||
lastErrorMessage: 'characters failed',
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'analyze_locations',
|
||||
stepTitle: 'Analyze Locations',
|
||||
status: RUN_STEP_STATUS.COMPLETED,
|
||||
currentAttempt: 1,
|
||||
stepIndex: 2,
|
||||
stepTotal: 5,
|
||||
startedAt: new Date(),
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'split_clips',
|
||||
stepTitle: 'Split Clips',
|
||||
status: RUN_STEP_STATUS.COMPLETED,
|
||||
currentAttempt: 1,
|
||||
stepIndex: 3,
|
||||
stepTotal: 5,
|
||||
startedAt: new Date(),
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'screenplay_clip-a',
|
||||
stepTitle: 'Screenplay A',
|
||||
status: RUN_STEP_STATUS.COMPLETED,
|
||||
currentAttempt: 1,
|
||||
stepIndex: 4,
|
||||
stepTotal: 5,
|
||||
startedAt: new Date(),
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'screenplay_clip-b',
|
||||
stepTitle: 'Screenplay B',
|
||||
status: RUN_STEP_STATUS.COMPLETED,
|
||||
currentAttempt: 1,
|
||||
stepIndex: 5,
|
||||
stepTotal: 5,
|
||||
startedAt: new Date(),
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
await prisma.graphArtifact.createMany({
|
||||
data: [
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'analyze_characters',
|
||||
artifactType: 'analysis.characters',
|
||||
refId: 'episode-retry-story',
|
||||
payload: { rows: [{ name: 'Hero' }] },
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'analyze_locations',
|
||||
artifactType: 'analysis.locations',
|
||||
refId: 'episode-retry-story',
|
||||
payload: { rows: [{ name: 'City' }] },
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'split_clips',
|
||||
artifactType: 'clips',
|
||||
refId: 'episode-retry-story',
|
||||
payload: { clips: [{ id: 'clip-a' }] },
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'screenplay_clip-a',
|
||||
artifactType: 'screenplay.clip',
|
||||
refId: 'clip-a',
|
||||
payload: { scenes: [{ id: 1 }] },
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const retried = await retryFailedStep({
|
||||
runId: run.id,
|
||||
userId: user.id,
|
||||
stepKey: 'analyze_characters',
|
||||
})
|
||||
|
||||
expect(retried?.retryAttempt).toBe(2)
|
||||
expect(retried?.invalidatedStepKeys.slice().sort()).toEqual([
|
||||
'analyze_characters',
|
||||
'screenplay_clip-a',
|
||||
'screenplay_clip-b',
|
||||
'split_clips',
|
||||
])
|
||||
|
||||
const steps = await prisma.graphStep.findMany({
|
||||
where: { runId: run.id },
|
||||
orderBy: { stepIndex: 'asc' },
|
||||
})
|
||||
const stepMap = new Map(steps.map((step) => [step.stepKey, step]))
|
||||
expect(stepMap.get('analyze_characters')).toMatchObject({
|
||||
status: RUN_STEP_STATUS.PENDING,
|
||||
currentAttempt: 2,
|
||||
lastErrorCode: null,
|
||||
lastErrorMessage: null,
|
||||
})
|
||||
expect(stepMap.get('split_clips')).toMatchObject({
|
||||
status: RUN_STEP_STATUS.PENDING,
|
||||
currentAttempt: 0,
|
||||
})
|
||||
expect(stepMap.get('screenplay_clip-a')).toMatchObject({
|
||||
status: RUN_STEP_STATUS.PENDING,
|
||||
currentAttempt: 0,
|
||||
})
|
||||
expect(stepMap.get('analyze_locations')).toMatchObject({
|
||||
status: RUN_STEP_STATUS.COMPLETED,
|
||||
currentAttempt: 1,
|
||||
})
|
||||
|
||||
const artifacts = await prisma.graphArtifact.findMany({
|
||||
where: { runId: run.id },
|
||||
orderBy: { stepKey: 'asc' },
|
||||
})
|
||||
expect(artifacts.map((artifact) => artifact.stepKey)).toEqual(['analyze_locations'])
|
||||
|
||||
const refreshedRun = await prisma.graphRun.findUnique({ where: { id: run.id } })
|
||||
expect(refreshedRun?.status).toBe(RUN_STATUS.RUNNING)
|
||||
expect(refreshedRun?.errorCode).toBeNull()
|
||||
expect(refreshedRun?.errorMessage).toBeNull()
|
||||
})
|
||||
|
||||
it('invalidates only the dependent storyboard branch plus voice analyze', async () => {
|
||||
const user = await createTestUser()
|
||||
const run = await prisma.graphRun.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
projectId: 'project-retry-storyboard',
|
||||
episodeId: 'episode-retry-storyboard',
|
||||
workflowType: 'script_to_storyboard_run',
|
||||
taskType: 'script_to_storyboard_run',
|
||||
targetType: 'NovelPromotionEpisode',
|
||||
targetId: 'episode-retry-storyboard',
|
||||
status: RUN_STATUS.FAILED,
|
||||
queuedAt: new Date(),
|
||||
startedAt: new Date(),
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.graphStep.createMany({
|
||||
data: [
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'clip_clip-1_phase1',
|
||||
stepTitle: 'Clip 1 Phase 1',
|
||||
status: RUN_STEP_STATUS.FAILED,
|
||||
currentAttempt: 1,
|
||||
stepIndex: 1,
|
||||
stepTotal: 6,
|
||||
startedAt: new Date(),
|
||||
finishedAt: new Date(),
|
||||
lastErrorCode: 'STEP_FAILED',
|
||||
lastErrorMessage: 'phase1 failed',
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'clip_clip-1_phase2_cinematography',
|
||||
stepTitle: 'Clip 1 Phase 2 Cine',
|
||||
status: RUN_STEP_STATUS.COMPLETED,
|
||||
currentAttempt: 1,
|
||||
stepIndex: 2,
|
||||
stepTotal: 6,
|
||||
startedAt: new Date(),
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'clip_clip-1_phase2_acting',
|
||||
stepTitle: 'Clip 1 Phase 2 Acting',
|
||||
status: RUN_STEP_STATUS.COMPLETED,
|
||||
currentAttempt: 1,
|
||||
stepIndex: 3,
|
||||
stepTotal: 6,
|
||||
startedAt: new Date(),
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'clip_clip-1_phase3_detail',
|
||||
stepTitle: 'Clip 1 Phase 3',
|
||||
status: RUN_STEP_STATUS.COMPLETED,
|
||||
currentAttempt: 1,
|
||||
stepIndex: 4,
|
||||
stepTotal: 6,
|
||||
startedAt: new Date(),
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'clip_clip-2_phase3_detail',
|
||||
stepTitle: 'Clip 2 Phase 3',
|
||||
status: RUN_STEP_STATUS.COMPLETED,
|
||||
currentAttempt: 1,
|
||||
stepIndex: 5,
|
||||
stepTotal: 6,
|
||||
startedAt: new Date(),
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'voice_analyze',
|
||||
stepTitle: 'Voice Analyze',
|
||||
status: RUN_STEP_STATUS.COMPLETED,
|
||||
currentAttempt: 1,
|
||||
stepIndex: 6,
|
||||
stepTotal: 6,
|
||||
startedAt: new Date(),
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
await prisma.graphArtifact.createMany({
|
||||
data: [
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'clip_clip-1_phase1',
|
||||
artifactType: 'storyboard.clip.phase1',
|
||||
refId: 'clip-1',
|
||||
payload: { panels: [] },
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'clip_clip-1_phase2_cinematography',
|
||||
artifactType: 'storyboard.clip.phase2.cine',
|
||||
refId: 'clip-1',
|
||||
payload: { rules: [] },
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'clip_clip-2_phase3_detail',
|
||||
artifactType: 'storyboard.clip.phase3',
|
||||
refId: 'clip-2',
|
||||
payload: { panels: [] },
|
||||
},
|
||||
{
|
||||
runId: run.id,
|
||||
stepKey: 'voice_analyze',
|
||||
artifactType: 'voice.lines',
|
||||
refId: 'episode-retry-storyboard',
|
||||
payload: { lines: [] },
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const retried = await retryFailedStep({
|
||||
runId: run.id,
|
||||
userId: user.id,
|
||||
stepKey: 'clip_clip-1_phase1',
|
||||
})
|
||||
|
||||
expect(retried?.retryAttempt).toBe(2)
|
||||
expect(retried?.invalidatedStepKeys.slice().sort()).toEqual([
|
||||
'clip_clip-1_phase1',
|
||||
'clip_clip-1_phase2_acting',
|
||||
'clip_clip-1_phase2_cinematography',
|
||||
'clip_clip-1_phase3_detail',
|
||||
'voice_analyze',
|
||||
])
|
||||
|
||||
const steps = await prisma.graphStep.findMany({
|
||||
where: { runId: run.id },
|
||||
orderBy: { stepIndex: 'asc' },
|
||||
})
|
||||
const stepMap = new Map(steps.map((step) => [step.stepKey, step]))
|
||||
expect(stepMap.get('clip_clip-1_phase1')).toMatchObject({
|
||||
status: RUN_STEP_STATUS.PENDING,
|
||||
currentAttempt: 2,
|
||||
})
|
||||
expect(stepMap.get('clip_clip-1_phase2_cinematography')).toMatchObject({
|
||||
status: RUN_STEP_STATUS.PENDING,
|
||||
currentAttempt: 0,
|
||||
})
|
||||
expect(stepMap.get('voice_analyze')).toMatchObject({
|
||||
status: RUN_STEP_STATUS.PENDING,
|
||||
currentAttempt: 0,
|
||||
})
|
||||
expect(stepMap.get('clip_clip-2_phase3_detail')).toMatchObject({
|
||||
status: RUN_STEP_STATUS.COMPLETED,
|
||||
currentAttempt: 1,
|
||||
})
|
||||
|
||||
const artifacts = await prisma.graphArtifact.findMany({
|
||||
where: { runId: run.id },
|
||||
orderBy: { stepKey: 'asc' },
|
||||
})
|
||||
expect(artifacts.map((artifact) => artifact.stepKey)).toEqual(['clip_clip-2_phase3_detail'])
|
||||
})
|
||||
})
|
||||
@@ -10,6 +10,7 @@ type WaitTaskOptions = {
|
||||
const TERMINAL_STATUSES = new Set<TaskStatus>([
|
||||
TASK_STATUS.COMPLETED,
|
||||
TASK_STATUS.FAILED,
|
||||
TASK_STATUS.CANCELED,
|
||||
TASK_STATUS.DISMISSED,
|
||||
])
|
||||
|
||||
|
||||
@@ -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