refactor: analysis workflow architecture

fix: NEXTAUTH_URL

fix: prevent project model edits from affecting default model
This commit is contained in:
saturn
2026-03-16 21:48:57 +08:00
parent ecbd183a77
commit 9aff44e37a
58 changed files with 2753 additions and 7985 deletions

View 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',
}))
})
})

View File

@@ -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',

View File

@@ -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()
})
})

View File

@@ -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,
})
})
})

View File

@@ -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,
}))
})

View File

@@ -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'])
})
})