feat: add home page and refactor workspace entry UI

This commit is contained in:
saturn
2026-03-23 17:45:17 +08:00
parent a6ad11b9c4
commit 4e469074e0
48 changed files with 2970 additions and 453 deletions

View File

@@ -0,0 +1,60 @@
import { describe, expect, it } from 'vitest'
import {
isGlobalAnalyzeTaskRunning,
resolveGlobalAnalyzeCompletion,
} from '@/app/[locale]/workspace/[projectId]/modes/novel-promotion/components/assets/hooks/useAssetsGlobalActions'
describe('assets global actions task state helpers', () => {
it('treats queued and processing analyze task as running', () => {
expect(isGlobalAnalyzeTaskRunning({
phase: 'queued',
runningTaskId: 'task-1',
lastError: null,
})).toBe(true)
expect(isGlobalAnalyzeTaskRunning({
phase: 'processing',
runningTaskId: 'task-1',
lastError: null,
})).toBe(true)
})
it('keeps completion idle when there is no previously running task', () => {
expect(resolveGlobalAnalyzeCompletion(null, {
phase: 'completed',
runningTaskId: null,
lastError: null,
})).toEqual({
status: 'idle',
finishedTaskId: null,
errorMessage: null,
})
})
it('marks previously running task as succeeded once runtime state stops running', () => {
expect(resolveGlobalAnalyzeCompletion('task-2', {
phase: 'completed',
runningTaskId: null,
lastError: null,
})).toEqual({
status: 'succeeded',
finishedTaskId: 'task-2',
errorMessage: null,
})
})
it('surfaces failed completion message from task state', () => {
expect(resolveGlobalAnalyzeCompletion('task-3', {
phase: 'failed',
runningTaskId: null,
lastError: {
code: 'MODEL_NOT_CONFIGURED',
message: 'No model configured',
},
})).toEqual({
status: 'failed',
finishedTaskId: 'task-3',
errorMessage: 'No model configured',
})
})
})

View File

@@ -0,0 +1,80 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
const {
useQueryClientMock,
useMutationMock,
requestTaskResponseWithErrorMock,
} = vi.hoisted(() => ({
useQueryClientMock: vi.fn(() => ({ invalidateQueries: vi.fn() })),
useMutationMock: vi.fn((options: unknown) => options),
requestTaskResponseWithErrorMock: vi.fn(),
}))
vi.mock('@tanstack/react-query', () => ({
useQueryClient: () => useQueryClientMock(),
useMutation: (options: unknown) => useMutationMock(options),
}))
vi.mock('@/lib/query/mutations/mutation-shared', async () => {
const actual = await vi.importActual<typeof import('@/lib/query/mutations/mutation-shared')>(
'@/lib/query/mutations/mutation-shared',
)
return {
...actual,
requestTaskResponseWithError: requestTaskResponseWithErrorMock,
}
})
import { useAnalyzeProjectGlobalAssets } from '@/lib/query/mutations/useProjectConfigMutations'
interface AnalyzeGlobalMutation {
mutationFn: () => Promise<unknown>
}
describe('project global analyze mutation', () => {
beforeEach(() => {
useQueryClientMock.mockClear()
useMutationMock.mockClear()
requestTaskResponseWithErrorMock.mockReset()
})
it('returns async task submission instead of waiting for final task result', async () => {
requestTaskResponseWithErrorMock.mockResolvedValue({
json: async () => ({
async: true,
taskId: 'task-global-1',
status: 'queued',
deduped: false,
}),
} as Response)
const mutation = useAnalyzeProjectGlobalAssets('project-1') as unknown as AnalyzeGlobalMutation
const result = await mutation.mutationFn() as { taskId: string; async: boolean }
expect(requestTaskResponseWithErrorMock).toHaveBeenCalledWith(
'/api/novel-promotion/project-1/analyze-global',
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ async: true }),
},
'Failed to analyze global assets',
)
expect(result).toEqual({
async: true,
taskId: 'task-global-1',
status: 'queued',
deduped: false,
})
})
it('fails explicitly when route does not return an async task submission payload', async () => {
requestTaskResponseWithErrorMock.mockResolvedValue({
json: async () => ({ success: true }),
} as Response)
const mutation = useAnalyzeProjectGlobalAssets('project-1') as unknown as AnalyzeGlobalMutation
await expect(mutation.mutationFn()).rejects.toThrow('Failed to submit global asset analysis task')
})
})

View File

@@ -0,0 +1,81 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { useEffectMock, useRefMock } = vi.hoisted(() => ({
useEffectMock: vi.fn(),
useRefMock: vi.fn(),
}))
vi.mock('react', async () => {
const actual = await vi.importActual<typeof import('react')>('react')
return {
...actual,
useEffect: useEffectMock,
useRef: useRefMock,
}
})
import { useWorkspaceAutoRun } from '@/app/[locale]/workspace/[projectId]/modes/novel-promotion/hooks/useWorkspaceAutoRun'
describe('useWorkspaceAutoRun', () => {
beforeEach(() => {
useEffectMock.mockReset()
useRefMock.mockReset()
useRefMock.mockImplementation((initialValue: unknown) => ({
current: initialValue,
}))
})
it('consumes autoRun=storyToScript and starts the story-to-script flow once', async () => {
const effectCallbacks: Array<() => void | (() => void)> = []
const router = { replace: vi.fn() }
const runWithRebuildConfirm = vi.fn(async () => undefined)
const runStoryToScriptFlow = vi.fn(async () => undefined)
useEffectMock.mockImplementation((callback: () => void | (() => void)) => {
effectCallbacks.push(callback)
})
useWorkspaceAutoRun({
searchParams: new URLSearchParams('episode=episode-1&autoRun=storyToScript'),
router,
episodeId: 'episode-1',
novelText: '第一章内容',
isTransitioning: false,
isStoryToScriptRunning: false,
runWithRebuildConfirm,
runStoryToScriptFlow,
})
effectCallbacks[0]?.()
expect(router.replace).toHaveBeenCalledWith('?episode=episode-1', { scroll: false })
expect(runWithRebuildConfirm).toHaveBeenCalledWith('storyToScript', runStoryToScriptFlow)
})
it('does not auto-run when the episode text is still empty', () => {
const effectCallbacks: Array<() => void | (() => void)> = []
const router = { replace: vi.fn() }
const runWithRebuildConfirm = vi.fn(async () => undefined)
const runStoryToScriptFlow = vi.fn(async () => undefined)
useEffectMock.mockImplementation((callback: () => void | (() => void)) => {
effectCallbacks.push(callback)
})
useWorkspaceAutoRun({
searchParams: new URLSearchParams('episode=episode-1&autoRun=storyToScript'),
router,
episodeId: 'episode-1',
novelText: ' ',
isTransitioning: false,
isStoryToScriptRunning: false,
runWithRebuildConfirm,
runStoryToScriptFlow,
})
effectCallbacks[0]?.()
expect(router.replace).not.toHaveBeenCalled()
expect(runWithRebuildConfirm).not.toHaveBeenCalled()
})
})