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