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

@@ -96,6 +96,7 @@ describe('Navbar download logs entry', () => {
const html = renderWithIntl(createElement(Navbar))
expect(html).toContain('下载日志')
expect(html).toContain('href="/home"')
expect(html).toContain('href="/api/admin/download-logs"')
expect(html).toContain('download=""')
})

View File

@@ -0,0 +1,115 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import {
buildHomeWorkspaceLaunchTarget,
createHomeProjectLaunch,
} from '@/lib/home/create-project-launch'
function buildJsonResponse(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), {
status,
headers: { 'Content-Type': 'application/json' },
})
}
describe('createHomeProjectLaunch', () => {
beforeEach(() => {
vi.restoreAllMocks()
})
it('creates project, config, first episode, and returns an auto-run workspace target', async () => {
const apiFetch = vi
.fn<(
input: string,
init?: RequestInit,
) => Promise<Response>>()
.mockResolvedValueOnce(buildJsonResponse({
project: { id: 'project-1' },
}, 201))
.mockResolvedValueOnce(buildJsonResponse({ success: true }, 200))
.mockResolvedValueOnce(buildJsonResponse({
episode: { id: 'episode-1' },
}, 201))
const result = await createHomeProjectLaunch({
apiFetch,
projectName: '开场白',
storyText: '第一章内容',
videoRatio: '9:16',
artStyle: 'american-comic',
episodeName: '第 1 集',
})
expect(apiFetch).toHaveBeenNthCalledWith(1, '/api/projects', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: '开场白',
description: '第一章内容',
mode: 'novel-promotion',
}),
})
expect(apiFetch).toHaveBeenNthCalledWith(2, '/api/novel-promotion/project-1', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
videoRatio: '9:16',
artStyle: 'american-comic',
}),
})
expect(apiFetch).toHaveBeenNthCalledWith(3, '/api/novel-promotion/project-1/episodes', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: '第 1 集',
novelText: '第一章内容',
}),
})
expect(result).toEqual({
projectId: 'project-1',
episodeId: 'episode-1',
target: {
pathname: '/workspace/project-1',
query: {
episode: 'episode-1',
autoRun: 'storyToScript',
},
},
})
})
it('fails explicitly when first episode creation does not return an episode id', async () => {
const apiFetch = vi
.fn<(
input: string,
init?: RequestInit,
) => Promise<Response>>()
.mockResolvedValueOnce(buildJsonResponse({
project: { id: 'project-1' },
}, 201))
.mockResolvedValueOnce(buildJsonResponse({ success: true }, 200))
.mockResolvedValueOnce(buildJsonResponse({
episode: {},
}, 201))
await expect(createHomeProjectLaunch({
apiFetch,
projectName: '开场白',
storyText: '第一章内容',
videoRatio: '9:16',
artStyle: 'american-comic',
episodeName: '第 1 集',
})).rejects.toThrow('Episode creation response missing episode id')
})
})
describe('buildHomeWorkspaceLaunchTarget', () => {
it('points workspace launch to the created episode and auto-runs story-to-script', () => {
expect(buildHomeWorkspaceLaunchTarget('project-9', 'episode-4')).toEqual({
pathname: '/workspace/project-9',
query: {
episode: 'episode-4',
autoRun: 'storyToScript',
},
})
})
})

View File

@@ -0,0 +1,14 @@
import { describe, expect, it } from 'vitest'
import {
AUTHENTICATED_HOME_PATHNAME,
buildAuthenticatedHomeTarget,
} from '@/lib/home/default-route'
describe('authenticated home default route', () => {
it('uses /home as the only authenticated default pathname', () => {
expect(AUTHENTICATED_HOME_PATHNAME).toBe('/home')
expect(buildAuthenticatedHomeTarget()).toEqual({
pathname: '/home',
})
})
})

View File

@@ -0,0 +1,89 @@
import * as React from 'react'
import { createElement } from 'react'
import { describe, expect, it, vi } from 'vitest'
import { renderToStaticMarkup } from 'react-dom/server'
import HomePage from '@/app/[locale]/home/page'
import {
HOME_QUICK_START_MIN_ROWS,
resolveTextareaTargetHeight,
} from '@/lib/home/quick-start-textarea'
vi.mock('next-auth/react', () => ({
useSession: () => ({
data: { user: { name: 'Earth' } },
status: 'authenticated',
}),
}))
vi.mock('next-intl', () => ({
useTranslations: (namespace: string) => (key: string) => `${namespace}.${key}`,
}))
vi.mock('@/components/Navbar', () => ({
default: () => createElement('nav', null, 'Navbar'),
}))
vi.mock('@/components/ui/icons', () => ({
AppIcon: ({ name, ...props }: { name: string } & Record<string, unknown>) =>
createElement('span', { ...props, 'data-icon': name }),
IconGradientDefs: (props: Record<string, unknown>) => createElement('span', props),
}))
vi.mock('@/components/selectors/RatioStyleSelectors', () => ({
RatioSelector: (props: Record<string, unknown>) => createElement('div', props, 'RatioSelector'),
StyleSelector: (props: Record<string, unknown>) => createElement('div', props, 'StyleSelector'),
}))
vi.mock('@/i18n/navigation', () => ({
Link: ({
href,
children,
...props
}: {
href: string | { pathname: string }
children: React.ReactNode
} & Record<string, unknown>) => {
const resolvedHref = typeof href === 'string' ? href : href.pathname
return createElement('a', { href: resolvedHref, ...props }, children)
},
useRouter: () => ({
push: vi.fn(),
}),
}))
vi.mock('@/lib/api-fetch', () => ({
apiFetch: vi.fn(),
}))
vi.mock('@/lib/home/create-project-launch', () => ({
createHomeProjectLaunch: vi.fn(),
}))
describe('resolveTextareaTargetHeight', () => {
it('keeps the home quick-start input at least three rows tall', () => {
expect(resolveTextareaTargetHeight({
minHeight: 96,
maxHeight: 320,
scrollHeight: 54,
})).toBe(96)
})
it('caps the auto-resized height to the viewport ceiling', () => {
expect(resolveTextareaTargetHeight({
minHeight: 96,
maxHeight: 180,
scrollHeight: 240,
})).toBe(180)
})
})
describe('HomePage quick-start input', () => {
it('renders the homepage textarea with a default three-row height baseline', () => {
Reflect.set(globalThis, 'React', React)
const html = renderToStaticMarkup(createElement(HomePage))
expect(HOME_QUICK_START_MIN_ROWS).toBe(3)
expect(html).toContain('rows="3"')
})
})

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

View File

@@ -0,0 +1,107 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { QueryClient } from '@tanstack/react-query'
import { queryKeys } from '@/lib/query/keys'
import type { TaskTargetOverlayMap } from '@/lib/query/task-target-overlay'
const {
apiFetchMock,
useQueryClientMock,
} = vi.hoisted(() => ({
apiFetchMock: vi.fn(),
useQueryClientMock: vi.fn(),
}))
vi.mock('@tanstack/react-query', async () => {
const actual = await vi.importActual<typeof import('@tanstack/react-query')>('@tanstack/react-query')
return {
...actual,
useQueryClient: () => useQueryClientMock(),
}
})
vi.mock('@/lib/api-fetch', () => ({
apiFetch: apiFetchMock,
}))
import { useAssetActions } from '@/lib/query/hooks/useAssets'
function getOverlay(
queryClient: QueryClient,
projectId: string,
key: string,
) {
const map = queryClient.getQueryData<TaskTargetOverlayMap>(
queryKeys.tasks.targetStateOverlay(projectId),
) || {}
return map[key] || null
}
function createOkResponse() {
return {
ok: true,
json: async () => ({ success: true }),
} as Response
}
describe('useAssetActions.generate optimistic overlay', () => {
beforeEach(() => {
useQueryClientMock.mockReset()
apiFetchMock.mockReset()
apiFetchMock.mockResolvedValue(createOkResponse())
})
it('keeps global prop in generating state immediately after submit', async () => {
const queryClient = new QueryClient()
useQueryClientMock.mockReturnValue(queryClient)
const actions = useAssetActions({ scope: 'global', kind: 'prop' })
await actions.generate({ id: 'prop-1' })
expect(apiFetchMock).toHaveBeenCalledWith('/api/assets/prop-1/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
scope: 'global',
kind: 'prop',
projectId: undefined,
id: 'prop-1',
}),
})
const overlay = getOverlay(queryClient, 'global-asset-hub', 'GlobalLocation:prop-1')
expect(overlay?.phase).toBe('queued')
expect(overlay?.intent).toBe('generate')
})
it('targets project prop generation overlay at the shared location-image task target', async () => {
const queryClient = new QueryClient()
useQueryClientMock.mockReturnValue(queryClient)
const actions = useAssetActions({
scope: 'project',
projectId: 'project-1',
kind: 'prop',
})
await actions.generate({ id: 'prop-2' })
const overlay = getOverlay(queryClient, 'project-1', 'LocationImage:prop-2')
expect(overlay?.phase).toBe('queued')
expect(overlay?.intent).toBe('generate')
})
it('clears the overlay when prop generation submission fails', async () => {
const queryClient = new QueryClient()
useQueryClientMock.mockReturnValue(queryClient)
apiFetchMock.mockResolvedValueOnce({
ok: false,
json: async () => ({}),
} as Response)
const actions = useAssetActions({ scope: 'global', kind: 'prop' })
await expect(actions.generate({ id: 'prop-3' })).rejects.toThrow('Failed to generate asset render')
const overlay = getOverlay(queryClient, 'global-asset-hub', 'GlobalLocation:prop-3')
expect(overlay).toBeNull()
})
})

View File

@@ -7,7 +7,7 @@ describe('select prop prompt template', () => {
expect(template).toContain('关键剧情道具资产分析师')
expect(template).toContain('宁缺毋滥')
expect(template).toContain('必须在剧情中承担明确功能')
expect(template).toContain('必须有明确剧情作用')
expect(template).toContain('如果不确定它是否值得进入资产库,直接不输出')
expect(template).toContain('仅因外观具体、名词明确,不足以成为关键道具')
})
@@ -17,7 +17,7 @@ describe('select prop prompt template', () => {
expect(template).toContain('key story prop extractor')
expect(template).toContain('Be conservative')
expect(template).toContain('clear story function')
expect(template).toContain('explicit story function')
expect(template).toContain('If you are unsure whether it deserves an asset entry, do not output it')
expect(template).toContain('A specific-looking noun is not enough')
})