import { afterEach, describe, expect, it, vi } from 'vitest'; import { putCharacterRoleAssetWorkflow, resolveCharacterRoleAssetWorkflow, } from './characterAssetWorkflowPersistence'; afterEach(() => { vi.unstubAllGlobals(); }); describe('角色资产工坊 workflow client', () => { it('通过后端 workflow 接口解析默认 prompt 和缓存合并结果', async () => { const fetchMock = vi.fn().mockResolvedValue({ ok: true, text: async () => JSON.stringify({ ok: true, cache: null, workflow: { defaultPromptBundle: { visualPromptText: '默认视觉', animationPromptText: '默认动作', scenePromptText: '默认场景', }, visualPromptText: '默认视觉', animationPromptText: '默认动作', animationPromptTextByKey: { run: '默认动作' }, visualDrafts: [], selectedVisualDraftId: '', selectedAnimation: 'run', }, }), }); vi.stubGlobal('fetch', fetchMock); const result = await resolveCharacterRoleAssetWorkflow({ characterId: 'role 01', cacheScopeId: 'world-01', role: { id: 'role 01', name: '沈砺', title: '灰炬向导', role: '边路同行者', }, }); expect(result.workflow.visualPromptText).toBe('默认视觉'); expect(fetchMock).toHaveBeenCalledWith( '/api/runtime/custom-world/asset-studio/role/role%2001/workflow', expect.objectContaining({ method: 'POST', body: JSON.stringify({ cacheScopeId: 'world-01', role: { id: 'role 01', name: '沈砺', title: '灰炬向导', role: '边路同行者', }, }), }), ); }); it('使用 PUT 保存用户当前工坊草稿缓存', async () => { const fetchMock = vi.fn().mockResolvedValue({ ok: true, text: async () => JSON.stringify({ ok: true, cache: { characterId: 'role-01' }, saveMessage: '已保存', }), }); vi.stubGlobal('fetch', fetchMock); await putCharacterRoleAssetWorkflow({ characterId: 'role-01', cacheScopeId: 'world-01', visualPromptText: '视觉草稿', animationPromptText: '动作草稿', animationPromptTextByKey: { run: '奔跑草稿' }, visualDrafts: [], selectedVisualDraftId: '', selectedAnimation: 'run', }); expect(fetchMock).toHaveBeenCalledWith( '/api/runtime/custom-world/asset-studio/role/role-01/workflow', expect.objectContaining({ method: 'PUT', body: JSON.stringify({ characterId: 'role-01', cacheScopeId: 'world-01', visualPromptText: '视觉草稿', animationPromptText: '动作草稿', animationPromptTextByKey: { run: '奔跑草稿' }, visualDrafts: [], selectedVisualDraftId: '', selectedAnimation: 'run', }), }), ); }); });