// @vitest-environment jsdom import { act, renderHook } from '@testing-library/react'; import { afterEach, expect, test, vi } from 'vitest'; import { createGameCreationAppManifest } from '../../../packages/shared/src/contracts/gameCreationApp'; import { buildRecentProjectRows } from '../src/features/app-shell/model'; import { useHomeProjectCreation } from '../src/features/app-shell/useHomeProjectCreation'; afterEach(() => { delete window.__TAURI__; }); test.each([false, true])( 'Unity 项目通过现有工作台打开,已有元数据=%s', async (initialized) => { const projectPath = 'C:/projects/unity'; const manifest = createGameCreationAppManifest( 'unity-project', 'Unity 项目', ); const directory = { projectPath, exists: true, isDirectory: true, isGameCreatorProject: initialized, isGodotProject: false, godotProjectRoot: null, isCocosProject: false, isUnityProject: true, unityProjectRoot: '.', projectName: 'Unity 项目', recentRunStatus: null, recentRunStopReason: null, }; const invoke = vi.fn(async (command: string) => { if (command === 'inspect_local_project_directory') return directory; if (command === 'import_local_unity_project') return { projectPath, manifest }; if (command === 'get_local_game_manifest') return manifest; if (command === 'get_local_game_project_revision') return { revision: 0 }; if (command === 'get_design_agent_runtime_mode') return { activeRuntime: 'game' }; throw new Error(`意外调用:${command}`); }); window.__TAURI__ = { core: { invoke } }; const setLauncherView = vi.fn(); const { result } = renderHook(() => useHomeProjectCreation({ setStatus: vi.fn(), setLauncherView, setAgentChatProjectPath: vi.fn(), rememberRecentWorkspace: vi.fn(), }), ); await act(() => result.current.openProject(projectPath, 'open')); expect(result.current.currentProjectContext).toMatchObject({ projectPath, projectKind: 'unity', manifest, startMode: null, initialPrompt: '', }); expect(setLauncherView).toHaveBeenCalledWith('project-development'); expect( invoke.mock.calls.some( ([command]) => command === 'init_local_game_project', ), ).toBe(false); expect( invoke.mock.calls.some( ([command]) => command === 'import_local_unity_project', ), ).toBe(!initialized); expect( buildRecentProjectRows( [projectPath], { [projectPath]: directory }, false, )[0], ).toMatchObject({ projectKind: 'unity', canOpen: true, status: initialized ? '本地项目' : '可导入', }); }, );