83 lines
2.9 KiB
TypeScript
83 lines
2.9 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { act, cleanup, renderHook } from '@testing-library/react';
|
|
import { afterEach, expect, it, vi } from 'vitest';
|
|
|
|
import { createGameCreationAppManifest } from '../../../packages/shared/src/contracts/gameCreationApp';
|
|
import { useHomeProjectCreation } from '../src/features/app-shell/useHomeProjectCreation';
|
|
|
|
afterEach(() => {
|
|
cleanup();
|
|
delete window.__TAURI__;
|
|
});
|
|
|
|
const projectPath = 'C:/test/design-project';
|
|
const manifest = createGameCreationAppManifest('test-project', '策划项目');
|
|
|
|
function setup(invoke: ReturnType<typeof vi.fn>) {
|
|
window.__TAURI__ = { core: { invoke } } as unknown as typeof window.__TAURI__;
|
|
return renderHook(() =>
|
|
useHomeProjectCreation({
|
|
setStatus: vi.fn(),
|
|
setLauncherView: vi.fn(),
|
|
setAgentChatProjectPath: vi.fn(),
|
|
rememberRecentWorkspace: vi.fn(),
|
|
}),
|
|
);
|
|
}
|
|
|
|
it.each(['design', 'game', null] as const)(
|
|
'restores %s before publishing the project context',
|
|
async (mode) => {
|
|
let resolveMode!: (value: { activeRuntime: string } | null) => void;
|
|
const modePromise = new Promise<{ activeRuntime: string } | null>(
|
|
(resolve) => {
|
|
resolveMode = resolve;
|
|
},
|
|
);
|
|
const invoke = vi.fn(async (command: string) => {
|
|
if (command === 'inspect_local_project_directory')
|
|
return { exists: true, isDirectory: true, isGameCreatorProject: true };
|
|
if (command === 'get_local_game_manifest') return manifest;
|
|
if (command === 'get_local_game_project_revision') return { revision: 1 };
|
|
if (command === 'get_design_agent_runtime_mode') return modePromise;
|
|
throw new Error(command);
|
|
});
|
|
const { result } = setup(invoke);
|
|
let opening!: Promise<void>;
|
|
await act(async () => {
|
|
opening = result.current.openProject(projectPath, 'open');
|
|
});
|
|
expect(result.current.currentProjectContext).toBeNull();
|
|
await act(async () => {
|
|
resolveMode(mode ? { activeRuntime: mode } : null);
|
|
await opening;
|
|
});
|
|
expect(result.current.currentProjectContext?.startMode).toBe(
|
|
mode === 'design' ? 'planning' : null,
|
|
);
|
|
},
|
|
);
|
|
|
|
it('persists design mode before entering a newly created planning project', async () => {
|
|
const invoke = vi.fn(async (command: string) => {
|
|
if (command === 'create_automatic_local_game_project')
|
|
return { projectPath, manifest };
|
|
if (command === 'set_design_agent_runtime_mode')
|
|
return { activeRuntime: 'design' };
|
|
if (command === 'get_local_game_project_revision') return { revision: 1 };
|
|
throw new Error(command);
|
|
});
|
|
const { result } = setup(invoke);
|
|
await act(async () => {
|
|
await result.current.createHomeDraftAutomatically(
|
|
{ creationType: 'game', prompt: '测试', attachments: [] },
|
|
'planning',
|
|
);
|
|
});
|
|
expect(invoke).toHaveBeenCalledWith('set_design_agent_runtime_mode', {
|
|
projectPath,
|
|
activeRuntime: 'design',
|
|
});
|
|
expect(result.current.currentProjectContext?.startMode).toBe('planning');
|
|
});
|