feat(home): add AI story expand button and modal
This commit is contained in:
@@ -163,6 +163,7 @@ const ROUTE_FILES = [
|
||||
'src/app/api/user/balance/route.ts',
|
||||
'src/app/api/user/costs/details/route.ts',
|
||||
'src/app/api/user/costs/route.ts',
|
||||
'src/app/api/user/ai-story-expand/route.ts',
|
||||
'src/app/api/user/models/route.ts',
|
||||
'src/app/api/user/transactions/route.ts',
|
||||
] as const
|
||||
|
||||
@@ -31,6 +31,7 @@ const TASK_TYPE_OWNER_MAP = {
|
||||
[TASK_TYPE.SCREENPLAY_CONVERT]: 'tests/unit/worker/screenplay-convert.test.ts',
|
||||
[TASK_TYPE.VOICE_ANALYZE]: 'tests/unit/worker/voice-analyze.test.ts',
|
||||
[TASK_TYPE.ANALYZE_GLOBAL]: 'tests/unit/worker/analyze-global.test.ts',
|
||||
[TASK_TYPE.AI_STORY_EXPAND]: 'tests/unit/worker/ai-story-expand.test.ts',
|
||||
[TASK_TYPE.AI_MODIFY_APPEARANCE]: 'tests/unit/worker/shot-ai-prompt-appearance.test.ts',
|
||||
[TASK_TYPE.AI_MODIFY_LOCATION]: 'tests/unit/worker/shot-ai-prompt-location.test.ts',
|
||||
[TASK_TYPE.AI_MODIFY_SHOT_PROMPT]: 'tests/unit/worker/shot-ai-prompt-shot.test.ts',
|
||||
|
||||
@@ -171,6 +171,13 @@ const ROUTE_CASES: ReadonlyArray<LLMRouteCase> = [
|
||||
expectedTargetType: 'NovelPromotionLocationDesign',
|
||||
expectedProjectId: 'project-1',
|
||||
},
|
||||
{
|
||||
routeFile: 'src/app/api/user/ai-story-expand/route.ts',
|
||||
body: { prompt: '宫廷复仇女主回京' },
|
||||
expectedTaskType: TASK_TYPE.AI_STORY_EXPAND,
|
||||
expectedTargetType: 'HomeAiStoryExpand',
|
||||
expectedProjectId: 'home-ai-write',
|
||||
},
|
||||
{
|
||||
routeFile: 'src/app/api/novel-promotion/[projectId]/ai-modify-appearance/route.ts',
|
||||
body: {
|
||||
@@ -336,7 +343,7 @@ describe('api contract - llm observe routes (behavior)', () => {
|
||||
})
|
||||
|
||||
it('keeps expected coverage size', () => {
|
||||
expect(ROUTE_CASES.length).toBe(22)
|
||||
expect(ROUTE_CASES.length).toBe(23)
|
||||
})
|
||||
|
||||
for (const routeCase of ROUTE_CASES) {
|
||||
|
||||
50
tests/unit/home/ai-story-expand.test.ts
Normal file
50
tests/unit/home/ai-story-expand.test.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { expandHomeStory } from '@/lib/home/ai-story-expand'
|
||||
|
||||
vi.mock('@/lib/task/client', () => ({
|
||||
resolveTaskResponse: vi.fn(),
|
||||
}))
|
||||
|
||||
import { resolveTaskResponse } from '@/lib/task/client'
|
||||
|
||||
function buildJsonResponse(body: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
}
|
||||
|
||||
describe('expandHomeStory', () => {
|
||||
it('posts the prompt to the user ai-story-expand route and returns expanded text', async () => {
|
||||
const apiFetch = vi.fn(async () => buildJsonResponse({ async: true, taskId: 'task-1' }))
|
||||
vi.mocked(resolveTaskResponse).mockResolvedValue({
|
||||
expandedText: '扩写后的故事正文',
|
||||
})
|
||||
|
||||
const result = await expandHomeStory({
|
||||
apiFetch,
|
||||
prompt: '宫廷复仇女主回京',
|
||||
})
|
||||
|
||||
expect(apiFetch).toHaveBeenCalledWith('/api/user/ai-story-expand', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
prompt: '宫廷复仇女主回京',
|
||||
}),
|
||||
})
|
||||
expect(result).toEqual({
|
||||
expandedText: '扩写后的故事正文',
|
||||
})
|
||||
})
|
||||
|
||||
it('fails explicitly when the route does not return expandedText', async () => {
|
||||
const apiFetch = vi.fn(async () => buildJsonResponse({ async: true, taskId: 'task-1' }))
|
||||
vi.mocked(resolveTaskResponse).mockResolvedValue({})
|
||||
|
||||
await expect(expandHomeStory({
|
||||
apiFetch,
|
||||
prompt: '宫廷复仇女主回京',
|
||||
})).rejects.toThrow('AI story expand response missing expandedText')
|
||||
})
|
||||
})
|
||||
@@ -59,6 +59,10 @@ vi.mock('@/lib/home/create-project-launch', () => ({
|
||||
createHomeProjectLaunch: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/home/ai-story-expand', () => ({
|
||||
expandHomeStory: vi.fn(),
|
||||
}))
|
||||
|
||||
describe('resolveTextareaTargetHeight', () => {
|
||||
it('keeps the home quick-start input at least three rows tall', () => {
|
||||
expect(resolveTextareaTargetHeight({
|
||||
|
||||
@@ -7,6 +7,7 @@ describe('resolveTaskIntent', () => {
|
||||
expect(resolveTaskIntent(TASK_TYPE.IMAGE_CHARACTER)).toBe('generate')
|
||||
expect(resolveTaskIntent(TASK_TYPE.IMAGE_LOCATION)).toBe('generate')
|
||||
expect(resolveTaskIntent(TASK_TYPE.VIDEO_PANEL)).toBe('generate')
|
||||
expect(resolveTaskIntent(TASK_TYPE.AI_STORY_EXPAND)).toBe('generate')
|
||||
})
|
||||
|
||||
it('maps regenerate and modify task types', () => {
|
||||
|
||||
84
tests/unit/worker/ai-story-expand.test.ts
Normal file
84
tests/unit/worker/ai-story-expand.test.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import type { Job } from 'bullmq'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { TASK_TYPE, type TaskJobData } from '@/lib/task/types'
|
||||
|
||||
const aiRuntimeMock = vi.hoisted(() => ({
|
||||
executeAiTextStep: vi.fn(async () => ({
|
||||
text: '扩写后的完整故事内容',
|
||||
reasoning: '',
|
||||
})),
|
||||
}))
|
||||
|
||||
const workerMock = vi.hoisted(() => ({
|
||||
reportTaskProgress: vi.fn(async () => undefined),
|
||||
assertTaskActive: vi.fn(async () => undefined),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/ai-runtime', () => aiRuntimeMock)
|
||||
vi.mock('@/lib/llm-observe/internal-stream-context', () => ({
|
||||
withInternalLLMStreamCallbacks: vi.fn(async (_callbacks: unknown, fn: () => Promise<unknown>) => await fn()),
|
||||
}))
|
||||
vi.mock('@/lib/prompt-i18n', () => ({
|
||||
PROMPT_IDS: { NP_AI_STORY_EXPAND: 'np_ai_story_expand' },
|
||||
buildPrompt: vi.fn(() => 'story-expand-prompt'),
|
||||
}))
|
||||
vi.mock('@/lib/workers/shared', () => ({ reportTaskProgress: workerMock.reportTaskProgress }))
|
||||
vi.mock('@/lib/workers/utils', () => ({ assertTaskActive: workerMock.assertTaskActive }))
|
||||
vi.mock('@/lib/workers/handlers/llm-stream', () => ({
|
||||
createWorkerLLMStreamContext: vi.fn(() => ({ streamRunId: 'run-1', nextSeqByStepLane: {} })),
|
||||
createWorkerLLMStreamCallbacks: vi.fn(() => ({
|
||||
onStage: vi.fn(),
|
||||
onChunk: vi.fn(),
|
||||
onComplete: vi.fn(),
|
||||
onError: vi.fn(),
|
||||
flush: vi.fn(async () => undefined),
|
||||
})),
|
||||
}))
|
||||
|
||||
import { handleAiStoryExpandTask } from '@/lib/workers/handlers/ai-story-expand'
|
||||
|
||||
function buildJob(payload: Record<string, unknown>): Job<TaskJobData> {
|
||||
return {
|
||||
data: {
|
||||
taskId: 'task-ai-story-expand-1',
|
||||
type: TASK_TYPE.AI_STORY_EXPAND,
|
||||
locale: 'zh',
|
||||
projectId: 'home-ai-write',
|
||||
targetType: 'HomeAiStoryExpand',
|
||||
targetId: 'user-1',
|
||||
payload,
|
||||
userId: 'user-1',
|
||||
},
|
||||
} as unknown as Job<TaskJobData>
|
||||
}
|
||||
|
||||
describe('worker ai-story-expand behavior', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('missing prompt -> explicit error', async () => {
|
||||
const job = buildJob({ prompt: ' ', analysisModel: 'provider::analysis-model' })
|
||||
await expect(handleAiStoryExpandTask(job)).rejects.toThrow('prompt is required')
|
||||
})
|
||||
|
||||
it('missing analysis model -> explicit error', async () => {
|
||||
const job = buildJob({ prompt: '宫廷复仇女主回京' })
|
||||
await expect(handleAiStoryExpandTask(job)).rejects.toThrow('analysisModel is required')
|
||||
})
|
||||
|
||||
it('success path -> returns expanded text without touching episode persistence', async () => {
|
||||
const job = buildJob({ prompt: '宫廷复仇女主回京', analysisModel: 'provider::analysis-model' })
|
||||
const result = await handleAiStoryExpandTask(job)
|
||||
|
||||
expect(result).toEqual({
|
||||
expandedText: '扩写后的完整故事内容',
|
||||
})
|
||||
expect(aiRuntimeMock.executeAiTextStep).toHaveBeenCalledWith(expect.objectContaining({
|
||||
userId: 'user-1',
|
||||
model: 'provider::analysis-model',
|
||||
projectId: 'home-ai-write',
|
||||
action: 'ai_story_expand',
|
||||
}))
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user