feat: add home page and refactor workspace entry UI
This commit is contained in:
115
tests/unit/home/create-project-launch.test.ts
Normal file
115
tests/unit/home/create-project-launch.test.ts
Normal 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',
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
14
tests/unit/home/default-route.test.ts
Normal file
14
tests/unit/home/default-route.test.ts
Normal 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',
|
||||
})
|
||||
})
|
||||
})
|
||||
89
tests/unit/home/quick-start-textarea.test.ts
Normal file
89
tests/unit/home/quick-start-textarea.test.ts
Normal 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"')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user