Files
Genarrative/src/services/ai.test.ts
高物 09d4c0c31b
Some checks failed
CI / verify (push) Has been cancelled
11
2026-04-16 21:47:20 +08:00

1011 lines
31 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { AFFINITY_BACKSTORY_CHAPTER_THRESHOLDS } from '../data/affinityLevels';
import { getScenePresetsByWorld } from '../data/scenePresets';
import type {
Character,
Encounter,
SceneHostileNpc,
StoryMoment,
StoryOption,
} from '../types';
import { AnimationState, WorldType } from '../types';
const {
connectivityError,
fetchMock,
requestChatMessageContentMock,
requestPlainTextCompletionMock,
streamPlainTextCompletionMock,
timeoutError,
} = vi.hoisted(() => ({
connectivityError: new Error('LLM unavailable'),
fetchMock: vi.fn(),
requestChatMessageContentMock: vi.fn(),
requestPlainTextCompletionMock: vi.fn(),
streamPlainTextCompletionMock: vi.fn(),
timeoutError: new Error('LLM timed out'),
}));
vi.mock('./llmClient', () => ({
CUSTOM_WORLD_REQUEST_TIMEOUT_MS: 120000,
isLlmConnectivityError: (error: unknown) => error === connectivityError,
isLlmTimeoutError: (error: unknown) => error === timeoutError,
requestChatMessageContent: requestChatMessageContentMock,
requestPlainTextCompletion: requestPlainTextCompletionMock,
streamPlainTextCompletion: streamPlainTextCompletionMock,
}));
import {
generateCharacterPanelChatSuggestions,
generateCustomWorldProfile,
generateCustomWorldSceneImage,
generateInitialStory,
generateNextStep,
streamCharacterPanelChatReply,
streamNpcRecruitDialogue,
} from './ai';
import {
buildOfflineCharacterPanelChatReply,
buildOfflineCharacterPanelChatSuggestions,
buildOfflineNpcRecruitDialogue,
} from './aiFallbacks';
import type { StoryGenerationContext } from './aiTypes';
import type { CharacterChatTargetStatus } from './characterChatPrompt';
const [
BACKSTORY_UNLOCK_AFFINITY_EASED,
BACKSTORY_UNLOCK_AFFINITY_FRIENDLY,
BACKSTORY_UNLOCK_AFFINITY_TRUSTED,
BACKSTORY_UNLOCK_AFFINITY_CLOSE,
] = AFFINITY_BACKSTORY_CHAPTER_THRESHOLDS;
function createCharacter(overrides: Partial<Character> = {}): Character {
return {
id: 'hero',
name: 'Lin',
title: 'Wanderer',
description: 'A cautious traveler.',
backstory: 'Walked out of the northern mountains.',
avatar: '/avatars/lin.png',
portrait: '/portraits/lin.png',
assetFolder: 'lin',
assetVariant: 'default',
attributes: {
strength: 10,
agility: 8,
intelligence: 7,
spirit: 9,
},
personality: 'Calm, observant, and steady.',
skills: [],
adventureOpenings: {},
...overrides,
};
}
function createStoryOption(overrides: Partial<StoryOption> = {}): StoryOption {
return {
functionId: 'rest',
actionText: 'Pause and recover.',
text: 'Pause and recover.',
visuals: {
playerAnimation: AnimationState.IDLE,
playerMoveMeters: 0,
playerOffsetY: 0,
playerFacing: 'right',
scrollWorld: false,
monsterChanges: [],
},
...overrides,
};
}
function createContext(
overrides: Partial<StoryGenerationContext> = {},
): StoryGenerationContext {
return {
playerHp: 30,
playerMaxHp: 40,
playerMana: 12,
playerMaxMana: 20,
inBattle: false,
playerX: 0,
playerFacing: 'right',
playerAnimation: AnimationState.IDLE,
skillCooldowns: {},
sceneId: null,
sceneName: 'Forest Trail',
sceneDescription: 'A quiet mountain path.',
pendingSceneEncounter: false,
...overrides,
};
}
function createTargetStatus(
overrides: Partial<CharacterChatTargetStatus> = {},
): CharacterChatTargetStatus {
return {
roleLabel: 'Companion',
hp: 18,
maxHp: 20,
mana: 9,
maxMana: 12,
...overrides,
};
}
function createEncounter(overrides: Partial<Encounter> = {}): Encounter {
return {
npcName: 'Lan',
npcDescription: 'A sharp-eyed scout.',
npcAvatar: '/avatars/lan.png',
context: 'Campfire',
...overrides,
};
}
function createPlayableNpc(index: number) {
return {
name: `角色${index + 1}`,
title: `身份${index + 1}`,
role: `世界职责${index + 1}`,
description: `角色描述${index + 1}`,
backstory: `角色背景${index + 1}`,
personality: `角色性格${index + 1}`,
motivation: `角色动机${index + 1}`,
combatStyle: `战斗风格${index + 1}`,
initialAffinity: 18,
relationshipHooks: [`接触点${index + 1}`],
tags: [`标签${index + 1}`],
backstoryReveal: {
publicSummary: `公开背景${index + 1}`,
chapters: [
{
id: `surface-${index + 1}`,
title: '表层来意',
affinityRequired: BACKSTORY_UNLOCK_AFFINITY_EASED,
teaser: `提示${index + 1}-1`,
content: `内容${index + 1}-1`,
contextSnippet: `摘要${index + 1}-1`,
},
{
id: `scar-${index + 1}`,
title: '旧事裂痕',
affinityRequired: BACKSTORY_UNLOCK_AFFINITY_FRIENDLY,
teaser: `提示${index + 1}-2`,
content: `内容${index + 1}-2`,
contextSnippet: `摘要${index + 1}-2`,
},
{
id: `hidden-${index + 1}`,
title: '隐藏执念',
affinityRequired: BACKSTORY_UNLOCK_AFFINITY_TRUSTED,
teaser: `提示${index + 1}-3`,
content: `内容${index + 1}-3`,
contextSnippet: `摘要${index + 1}-3`,
},
{
id: `final-${index + 1}`,
title: '最终底牌',
affinityRequired: BACKSTORY_UNLOCK_AFFINITY_CLOSE,
teaser: `提示${index + 1}-4`,
content: `内容${index + 1}-4`,
contextSnippet: `摘要${index + 1}-4`,
},
],
},
skills: [
{ name: `技能${index + 1}-1`, summary: '技能说明1', style: '起手压制' },
{ name: `技能${index + 1}-2`, summary: '技能说明2', style: '机动周旋' },
{ name: `技能${index + 1}-3`, summary: '技能说明3', style: '爆发终结' },
],
initialItems: [
{
name: `物品${index + 1}-1`,
category: '武器',
quantity: 1,
rarity: 'rare',
description: '物品说明1',
tags: ['物品标签1'],
},
{
name: `物品${index + 1}-2`,
category: '消耗品',
quantity: 2,
rarity: 'uncommon',
description: '物品说明2',
tags: ['物品标签2'],
},
{
name: `物品${index + 1}-3`,
category: '专属物品',
quantity: 1,
rarity: 'rare',
description: '物品说明3',
tags: ['物品标签3'],
},
],
};
}
function createStoryNpc(index: number) {
return {
name: `世界NPC${index + 1}`,
title: `头衔${index + 1}`,
role: `职责${index + 1}`,
description: `世界NPC描述${index + 1}`,
backstory: `世界NPC背景${index + 1}`,
personality: `世界NPC性格${index + 1}`,
motivation: `世界NPC动机${index + 1}`,
combatStyle: `世界NPC战斗风格${index + 1}`,
initialAffinity: index % 4 === 0 ? -10 : 6,
relationshipHooks: [`关系${index + 1}`],
tags: [`线索${index + 1}`],
backstoryReveal: {
publicSummary: `世界公开背景${index + 1}`,
chapters: [
{
id: `surface-story-${index + 1}`,
title: '表层来意',
affinityRequired: BACKSTORY_UNLOCK_AFFINITY_EASED,
teaser: `提示${index + 1}-1`,
content: `内容${index + 1}-1`,
contextSnippet: `摘要${index + 1}-1`,
},
{
id: `scar-story-${index + 1}`,
title: '旧事裂痕',
affinityRequired: BACKSTORY_UNLOCK_AFFINITY_FRIENDLY,
teaser: `提示${index + 1}-2`,
content: `内容${index + 1}-2`,
contextSnippet: `摘要${index + 1}-2`,
},
{
id: `hidden-story-${index + 1}`,
title: '隐藏执念',
affinityRequired: BACKSTORY_UNLOCK_AFFINITY_TRUSTED,
teaser: `提示${index + 1}-3`,
content: `内容${index + 1}-3`,
contextSnippet: `摘要${index + 1}-3`,
},
{
id: `final-story-${index + 1}`,
title: '最终底牌',
affinityRequired: BACKSTORY_UNLOCK_AFFINITY_CLOSE,
teaser: `提示${index + 1}-4`,
content: `内容${index + 1}-4`,
contextSnippet: `摘要${index + 1}-4`,
},
],
},
skills: [
{
name: `世界技能${index + 1}-1`,
summary: '技能说明1',
style: '起手压制',
},
{
name: `世界技能${index + 1}-2`,
summary: '技能说明2',
style: '机动周旋',
},
{
name: `世界技能${index + 1}-3`,
summary: '技能说明3',
style: '爆发终结',
},
],
initialItems: [
{
name: `世界物品${index + 1}-1`,
category: '武器',
quantity: 1,
rarity: 'rare',
description: '物品说明1',
tags: ['物品标签1'],
},
{
name: `世界物品${index + 1}-2`,
category: '消耗品',
quantity: 2,
rarity: 'uncommon',
description: '物品说明2',
tags: ['物品标签2'],
},
{
name: `世界物品${index + 1}-3`,
category: '专属物品',
quantity: 1,
rarity: 'rare',
description: '物品说明3',
tags: ['物品标签3'],
},
],
};
}
function createLandmark(
index: number,
options?: {
storyNpcNames?: string[];
landmarkCount?: number;
},
) {
const landmarkCount = options?.landmarkCount ?? 10;
const nextName = `场景${((index + 1) % landmarkCount) + 1}`;
const prevName = `场景${((index - 1 + landmarkCount) % landmarkCount) + 1}`;
return {
name: `场景${index + 1}`,
description: `场景描述${index + 1}`,
dangerLevel: 'high',
sceneNpcNames: options?.storyNpcNames ?? [
`世界NPC${index + 1}`,
`世界NPC${index + 2}`,
`世界NPC${index + 3}`,
],
connections:
landmarkCount > 1
? [
{
targetLandmarkName: nextName,
relativePosition: 'forward',
summary: `沿主路可到${nextName}`,
},
{
targetLandmarkName: prevName,
relativePosition: 'back',
summary: `回身可返${prevName}`,
},
]
: [],
};
}
function createCustomWorldResponse(
overrides: Partial<{
name: string;
subtitle: string;
summary: string;
tone: string;
playerGoal: string;
templateWorldType: 'WUXIA' | 'XIANXIA';
playableNpcs: ReturnType<typeof createPlayableNpc>[];
storyNpcs: ReturnType<typeof createStoryNpc>[];
landmarks: ReturnType<typeof createLandmark>[];
items: Array<Record<string, unknown>>;
}> = {},
) {
const storyNpcs =
overrides.storyNpcs ??
Array.from({ length: 25 }, (_, index) => createStoryNpc(index));
const landmarks =
overrides.landmarks ??
Array.from({ length: 10 }, (_, index) =>
createLandmark(index, {
landmarkCount: 10,
storyNpcNames: [
storyNpcs[index % storyNpcs.length]?.name ?? `世界NPC${index + 1}`,
storyNpcs[(index + 1) % storyNpcs.length]?.name ??
`世界NPC${index + 2}`,
storyNpcs[(index + 2) % storyNpcs.length]?.name ??
`世界NPC${index + 3}`,
],
}),
);
return {
name: '测试世界',
subtitle: '副标题',
summary: '概述',
tone: '基调',
playerGoal: '目标',
templateWorldType: 'WUXIA' as const,
playableNpcs: Array.from({ length: 5 }, (_, index) =>
createPlayableNpc(index),
),
storyNpcs,
landmarks,
...overrides,
};
}
describe('ai orchestration fallbacks', () => {
const playerCharacter = createCharacter();
const targetCharacter = createCharacter({
id: 'ally',
name: 'Lan',
title: 'Scout',
personality: 'Dry, practical, and quietly protective.',
});
const context = createContext();
const targetStatus = createTargetStatus();
const monsters: SceneHostileNpc[] = [];
const storyHistory: StoryMoment[] = [];
beforeEach(() => {
vi.stubGlobal('fetch', fetchMock);
fetchMock.mockReset();
requestChatMessageContentMock.mockReset();
requestPlainTextCompletionMock.mockReset();
streamPlainTextCompletionMock.mockReset();
});
it('falls back to the offline story response when story generation loses connectivity', async () => {
const availableOptions = [createStoryOption()];
requestChatMessageContentMock.mockRejectedValue(connectivityError);
const response = await generateInitialStory(
WorldType.WUXIA,
playerCharacter,
monsters,
context,
{ availableOptions },
);
expect(response.options).toEqual(availableOptions);
expect(response.options).not.toBe(availableOptions);
expect(response.storyText.length).toBeGreaterThan(0);
});
it('repairs mixed-language story text before returning the story response', async () => {
const availableOptions = [
createStoryOption({
functionId: 'idle_explore_forward',
actionText: '继续沿山道探路。',
text: '继续沿山道探路。',
}),
];
requestChatMessageContentMock
.mockResolvedValueOnce(
JSON.stringify({
storyText: 'The forest is quiet. 你听见远处的风声。',
encounter: null,
options: [
{
functionId: 'idle_explore_forward',
actionText: 'Move forward carefully.',
},
],
}),
)
.mockResolvedValueOnce(
JSON.stringify({
storyText: '林间重新安静下来,你听见远处的风声。',
encounter: null,
options: [
{
functionId: 'idle_explore_forward',
actionText: '继续沿山道探路。',
},
],
}),
);
const response = await generateInitialStory(
WorldType.WUXIA,
playerCharacter,
monsters,
context,
{ availableOptions },
);
expect(response.storyText).toBe('林间重新安静下来,你听见远处的风声。');
expect(response.options[0]?.actionText).toBe('继续沿山道探路。');
expect(requestChatMessageContentMock).toHaveBeenCalledTimes(2);
expect(requestChatMessageContentMock.mock.calls[1]?.[2]).toEqual(
expect.objectContaining({
debugLabel: 'story-language-repair',
}),
);
});
it('ignores generated encounter payloads during post-battle continuations when no new scene encounter is pending', async () => {
const availableOptions = [
createStoryOption({
functionId: 'idle_explore_forward',
actionText: '先稳住呼吸,再看看前面的动静。',
text: '先稳住呼吸,再看看前面的动静。',
}),
];
const sceneWithNpc = getScenePresetsByWorld(WorldType.WUXIA).find(
(scene) => (scene.npcs?.length ?? 0) > 0,
);
const targetNpcId = sceneWithNpc?.npcs?.[0]?.id;
if (!sceneWithNpc || !targetNpcId) {
throw new Error('Expected a wuxia scene with at least one npc preset.');
}
requestChatMessageContentMock.mockResolvedValue(
JSON.stringify({
storyText: '山道总算安静下来,你收住气息,重新判断前路。',
encounter: {
kind: 'npc',
npcId: targetNpcId,
},
options: [
{
functionId: 'idle_explore_forward',
actionText: '先稳住呼吸,再看看前面的动静。',
},
],
}),
);
const response = await generateNextStep(
WorldType.WUXIA,
playerCharacter,
[],
[
{
text: '挥刀抢攻',
options: [],
historyRole: 'action',
},
{
text: '山道客已经败下阵来。',
options: [],
historyRole: 'result',
},
],
'挥刀抢攻',
createContext({
sceneId: sceneWithNpc.id,
sceneName: sceneWithNpc.name,
sceneDescription: sceneWithNpc.description,
pendingSceneEncounter: false,
}),
{ availableOptions },
);
expect(response.encounter).toBeUndefined();
expect(response.options).toEqual(availableOptions);
const userPrompt = requestChatMessageContentMock.mock.calls.at(-1)?.[1];
expect(userPrompt).toContain('encounter 必须为 null');
expect(userPrompt).toContain('战斗结束后的续写');
});
it('returns offline character chat suggestions when the plain-text client reports connectivity errors', async () => {
requestPlainTextCompletionMock.mockRejectedValue(connectivityError);
const suggestions = await generateCharacterPanelChatSuggestions(
WorldType.WUXIA,
playerCharacter,
targetCharacter,
storyHistory,
context,
[],
'',
targetStatus,
);
expect(suggestions).toEqual(
buildOfflineCharacterPanelChatSuggestions(targetCharacter),
);
});
it('streams the offline character chat reply and forwards it to onUpdate when connectivity fails', async () => {
const onUpdate = vi.fn();
const playerMessage = 'Tell me what you are really worried about.';
const conversationSummary = 'Lan has started to trust the player more.';
const fallbackReply = buildOfflineCharacterPanelChatReply(
targetCharacter,
playerMessage,
conversationSummary,
);
streamPlainTextCompletionMock.mockRejectedValue(connectivityError);
const reply = await streamCharacterPanelChatReply(
WorldType.WUXIA,
playerCharacter,
targetCharacter,
storyHistory,
context,
[],
conversationSummary,
playerMessage,
targetStatus,
{ onUpdate },
);
expect(reply).toBe(fallbackReply);
expect(onUpdate).toHaveBeenCalledOnce();
expect(onUpdate).toHaveBeenCalledWith(fallbackReply);
});
it('uses the extracted NPC recruit fallback when recruit dialogue streaming loses connectivity', async () => {
const onUpdate = vi.fn();
const encounter = createEncounter();
const fallbackReply = buildOfflineNpcRecruitDialogue(encounter);
streamPlainTextCompletionMock.mockRejectedValue(connectivityError);
const reply = await streamNpcRecruitDialogue(
WorldType.WUXIA,
playerCharacter,
encounter,
monsters,
storyHistory,
context,
'Join us.',
'The party is ready to travel together.',
{ onUpdate },
);
expect(reply).toBe(fallbackReply);
expect(onUpdate).toHaveBeenCalledOnce();
expect(onUpdate).toHaveBeenCalledWith(fallbackReply);
});
it('rejects custom world output when the model does not generate enough NPCs and scenes', async () => {
requestPlainTextCompletionMock.mockResolvedValue(
JSON.stringify(
createCustomWorldResponse({
storyNpcs: Array.from({ length: 10 }, (_, index) =>
createStoryNpc(index),
),
landmarks: Array.from({ length: 4 }, (_, index) =>
createLandmark(index, { landmarkCount: 4 }),
),
}),
),
);
await expect(
generateCustomWorldProfile('一个需要很多角色和场景的世界'),
).rejects.toThrow(
/requires at least 10 generated scenes|至少产出 10 个场景|至少需要 10 个场景/i,
);
});
it('keeps the generated custom world dossier item-free when the model output is valid', async () => {
requestPlainTextCompletionMock.mockResolvedValue(
JSON.stringify(
createCustomWorldResponse({
items: [
{
name: '不应保留的物品',
category: '材料',
rarity: 'rare',
description: '这个字段应该被清空',
tags: ['测试'],
},
],
}),
),
);
const profile =
await generateCustomWorldProfile('一个需要很多角色和场景的世界');
expect(profile.playableNpcs).toHaveLength(5);
expect(profile.storyNpcs).toHaveLength(25);
expect(profile.landmarks).toHaveLength(10);
expect(
profile.landmarks.every((landmark) => landmark.sceneNpcIds.length >= 3),
).toBe(true);
expect(
profile.landmarks.every((landmark) => landmark.connections.length > 0),
).toBe(true);
expect(profile.items).toEqual([]);
});
it('generates custom worlds through a framework stage plus segmented narrative and dossier batches', async () => {
requestPlainTextCompletionMock.mockResolvedValue(
JSON.stringify(createCustomWorldResponse()),
);
await generateCustomWorldProfile('一个需要拆分生成的世界');
const debugLabels = requestPlainTextCompletionMock.mock.calls.map(
(call) => (call[2] as { debugLabel?: string } | undefined)?.debugLabel,
);
expect(debugLabels).toContain('custom-world-framework');
expect(debugLabels).toContain('custom-world-playable-outline-batch-1');
expect(debugLabels).toContain('custom-world-story-outline-batch-1');
expect(debugLabels).toContain('custom-world-landmark-seed-batch-1');
expect(debugLabels).toContain('custom-world-landmark-network-batch-1');
expect(debugLabels).toContain('custom-world-playable-narrative-batch-1');
expect(debugLabels).toContain('custom-world-playable-dossier-batch-1');
expect(debugLabels).toContain('custom-world-story-narrative-batch-1');
expect(debugLabels).toContain('custom-world-story-dossier-batch-1');
});
it('reports staged progress while generating a custom world', async () => {
requestPlainTextCompletionMock.mockResolvedValue(
JSON.stringify(createCustomWorldResponse()),
);
const onProgress = vi.fn();
await generateCustomWorldProfile('一个需要展示真实进度的世界', {
onProgress,
});
const phaseIds = onProgress.mock.calls.map(
(call) =>
(call[0] as { phaseId?: string; overallProgress?: number }).phaseId,
);
const lastProgress = onProgress.mock.calls.at(-1)?.[0] as
| { overallProgress?: number; estimatedRemainingMs?: number | null }
| undefined;
expect(phaseIds).toContain('framework');
expect(phaseIds).toContain('playable-outline');
expect(phaseIds).toContain('story-outline');
expect(phaseIds).toContain('landmark-seed');
expect(phaseIds).toContain('landmark-network');
expect(phaseIds).toContain('playable-narrative');
expect(phaseIds).toContain('playable-dossier');
expect(phaseIds).toContain('story-narrative');
expect(phaseIds).toContain('story-dossier');
expect(phaseIds).toContain('finalize');
expect(lastProgress?.overallProgress).toBe(100);
expect(lastProgress?.estimatedRemainingMs).toBe(0);
});
it('passes abort signals through custom world generation and rejects when interrupted', async () => {
requestPlainTextCompletionMock.mockImplementation(
(_system: string, _user: string, options?: { signal?: AbortSignal }) =>
new Promise((_resolve, reject) => {
options?.signal?.addEventListener(
'abort',
() =>
reject(options.signal?.reason ?? new Error('世界生成已中断。')),
{ once: true },
);
}),
);
const abortController = new AbortController();
const generation = generateCustomWorldProfile('一个会被中断的世界', {
signal: abortController.signal,
});
abortController.abort(new Error('手动中断生成'));
await expect(generation).rejects.toThrow('手动中断生成');
expect(requestPlainTextCompletionMock).toHaveBeenCalledWith(
expect.any(String),
expect.any(String),
expect.objectContaining({
signal: abortController.signal,
}),
);
});
it('retries custom world generation with a longer timeout after the first timeout attempt', async () => {
requestPlainTextCompletionMock
.mockRejectedValueOnce(timeoutError)
.mockResolvedValue(
JSON.stringify(
createCustomWorldResponse({
name: '重试世界',
}),
),
);
const profile = await generateCustomWorldProfile('一个生成很慢的世界');
expect(profile.name).toBe('重试世界');
expect(requestPlainTextCompletionMock).toHaveBeenNthCalledWith(
1,
expect.any(String),
expect.any(String),
expect.objectContaining({
timeoutMs: 120000,
debugLabel: 'custom-world-framework',
}),
);
expect(requestPlainTextCompletionMock).toHaveBeenNthCalledWith(
2,
expect.any(String),
expect.any(String),
expect.objectContaining({
timeoutMs: 180000,
debugLabel: 'custom-world-framework-retry-2',
}),
);
});
it('repairs invalid custom world json through a follow-up formatting request', async () => {
requestPlainTextCompletionMock
.mockResolvedValueOnce(
`{
"name": "修复世界",
"subtitle": "副标题",
"summary": "概述",
"tone": "基调",
"playerGoal": "目标",
"templateWorldType": "WUXIA",
"playableNpcs": [{ name: "角色1" }],
"storyNpcs": [],
"landmarks": []
}`,
)
.mockResolvedValue(
JSON.stringify(
createCustomWorldResponse({
name: '修复世界',
}),
),
);
const profile = await generateCustomWorldProfile('一个格式容易损坏的世界');
expect(profile.name).toBe('修复世界');
expect(profile.playableNpcs).toHaveLength(5);
expect(requestPlainTextCompletionMock).toHaveBeenNthCalledWith(
2,
expect.stringContaining('你是 JSON 修复器'),
expect.stringContaining(
'不要输出 playableNpcs、storyNpcs、landmarks、items',
),
expect.objectContaining({
debugLabel: 'custom-world-framework-json-repair',
}),
);
});
it('attaches creator intent and anchor pack when generating from creator cards', async () => {
requestPlainTextCompletionMock.mockResolvedValue(
JSON.stringify(
createCustomWorldResponse({
name: '锚点世界',
}),
),
);
const profile = await generateCustomWorldProfile({
settingText: '世界一句话:一个被灵潮反复改写地形的边境世界。',
creatorIntent: {
sourceMode: 'card',
rawSettingText: '',
worldHook: '一个被灵潮反复改写地形的边境世界。',
themeKeywords: ['边境', '灵潮'],
toneDirectives: ['紧张', '潮湿'],
playerPremise: '玩家是前巡夜人。',
openingSituation: '刚进城就卷入旧案。',
coreConflicts: ['旧案名单再次出现'],
keyFactions: [],
keyCharacters: [
{
id: 'creator-character-1',
name: '沈砺',
role: '灰炬向导',
publicMask: '看起来只是个带路人',
hiddenHook: '一直在查旧撤离线',
relationToPlayer: '会先怀疑玩家身份',
notes: '',
locked: true,
},
],
keyLandmarks: [],
iconicElements: ['裂潮灯塔'],
forbiddenDirectives: ['不要出现现代枪械'],
},
});
expect(profile.name).toBe('锚点世界');
expect(profile.creatorIntent?.sourceMode).toBe('card');
expect(profile.creatorIntent?.keyCharacters[0]?.name).toBe('沈砺');
expect(profile.anchorPack?.keyCharacterAnchors[0]?.name).toBe('沈砺');
expect(profile.anchorPack?.lockedAnchorIds).toContain(
'creator-character-1',
);
});
it('generates a custom world scene image through the local proxy and returns the saved asset path', async () => {
fetchMock.mockResolvedValue({
ok: true,
text: async () =>
JSON.stringify({
ok: true,
data: {
ok: true,
imageSrc: '/generated-custom-world-scenes/world/landmark/scene.png',
assetId: 'custom-scene-1',
model: 'wan2.7-image',
size: '1280*720',
taskId: 'task-123',
prompt: '系统整理后的提示词',
actualPrompt: '扩写后的提示词',
},
error: null,
meta: {
apiVersion: '2026-04-08',
},
}),
} as Response);
const result = await generateCustomWorldSceneImage({
profile: {
id: 'custom-world-1',
name: '测试世界',
subtitle: '副标题',
summary: '世界概述',
tone: '世界基调',
playerGoal: '核心目标',
settingText: '原始设定',
},
landmark: {
id: 'landmark-1',
name: '雾潮码头',
description: '被潮雾与旧升降机包围的码头。',
dangerLevel: 'high',
},
userPrompt: '雨夜的栈桥横跨黑色海沟,塔楼灯火被潮雾吞没。',
size: '1280*720',
referenceImageSrc: '/scene_bg/reference-layout.png',
});
expect(fetchMock).toHaveBeenCalledOnce();
expect(fetchMock).toHaveBeenCalledWith(
'/api/custom-world/scene-image',
expect.objectContaining({
method: 'POST',
headers: expect.objectContaining({
'Content-Type': 'application/json',
}),
}),
);
const [, request] = fetchMock.mock.calls[0] as [string, RequestInit];
const requestBody = JSON.parse(String(request.body)) as {
prompt: string;
referenceImageSrc?: string;
};
expect(requestBody.referenceImageSrc).toBe(
'/scene_bg/reference-layout.png',
);
expect(requestBody.prompt).toContain('像素风场景背景');
expect(requestBody.prompt).toContain('画面构图必须严格按上下 1:1 分区');
expect(requestBody.prompt).toContain('下半部分严格占据整张图的 1/2 高度');
expect(requestBody.prompt).toContain('模拟 3D 游戏视角的地面近景');
expect(requestBody.prompt).toContain(
'下半部分的内容必须是明确可站立的地面本体',
);
expect(requestBody.prompt).toContain('已提供一张自定义参考图');
expect(requestBody.prompt).toContain('雨夜的栈桥横跨黑色海沟');
expect(result).toEqual({
imageSrc: '/generated-custom-world-scenes/world/landmark/scene.png',
assetId: 'custom-scene-1',
model: 'wan2.7-image',
size: '1280*720',
taskId: 'task-123',
prompt: '系统整理后的提示词',
actualPrompt: '扩写后的提示词',
});
});
it('surfaces proxy error messages when scene image generation fails', async () => {
fetchMock.mockResolvedValue({
ok: false,
text: async () =>
JSON.stringify({
error: {
message: 'DashScope API key 无效。',
},
}),
} as Response);
await expect(
generateCustomWorldSceneImage({
profile: {
id: 'custom-world-1',
name: '测试世界',
subtitle: '副标题',
summary: '世界概述',
tone: '世界基调',
playerGoal: '核心目标',
settingText: '原始设定',
},
landmark: {
id: 'landmark-1',
name: '雾潮码头',
description: '被潮雾与旧升降机包围的码头。',
dangerLevel: 'high',
},
}),
).rejects.toThrow('DashScope API key 无效。');
});
});