Files
Genarrative/apps/ai-game-creator-shell/tests/cocosProjectOpen.test.tsx
T
k88936 594063a1e0 测试:修正 AGC 测试文件的类型错误
- 按 vitest 0.34 的参数元组口径改写 mock 泛型
- 修正 noUncheckedIndexedAccess 下的下标访问
- 补齐与当前类型脱节的测试桩字段
- 按 scripts/*.mjs 的运行时契约补局部类型声明
2026-10-03 17:41:30 +08:00

78 lines
2.7 KiB
TypeScript

// @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 { useHomeProjectCreation } from '../src/features/app-shell/useHomeProjectCreation';
afterEach(() => {
delete window.__TAURI__;
});
test('打开 Cocos 目录走导入入口,不创建 Web 脚手架或发送初始消息', async () => {
const manifest = {
...createGameCreationAppManifest('cocos-project', 'Cocos 项目'),
cocosProjectRoot: '.',
};
const invoke = vi.fn(async (command: string) => {
if (command === 'inspect_local_project_directory') {
return {
exists: true,
isDirectory: true,
isGameCreatorProject: false,
isGodotProject: false,
godotProjectRoot: null,
isCocosProject: true,
cocosProjectRoot: '.',
projectName: 'Cocos 项目',
recentRunStatus: null,
recentRunStopReason: null,
};
}
if (command === 'import_local_cocos_project') {
return { projectPath: 'C:/projects/cocos', 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}`);
});
// TauriInvoke 是泛型函数签名,vi.fn 的具体返回类型无法直接赋给它,这里只在装配处桥接。
window.__TAURI__ = { core: { invoke } } as unknown as typeof window.__TAURI__;
const setLauncherView = vi.fn();
const { result } = renderHook(() =>
useHomeProjectCreation({
setStatus: vi.fn(),
// 现行 UseHomeProjectCreationOptions 必填 showTransientNotice,旧的 setAgentChatProjectPath 已移除。
showTransientNotice: vi.fn(),
setLauncherView,
rememberRecentWorkspace: vi.fn(),
}),
);
await act(() => result.current.openProject('C:/projects/cocos', 'open'));
expect(invoke).toHaveBeenCalledWith('import_local_cocos_project', {
projectPath: 'C:/projects/cocos',
projectId: expect.any(String),
name: 'cocos',
});
expect(result.current.currentProjectContext).toMatchObject({
projectKind: 'cocos',
projectPath: 'C:/projects/cocos',
initialPrompt: '',
startMode: null,
manifest,
});
expect(setLauncherView).toHaveBeenCalledWith('project-development');
expect(invoke).toHaveBeenCalledWith('get_design_agent_runtime_mode', {
projectPath: 'C:/projects/cocos',
});
expect(
invoke.mock.calls.some(
([command]) => command === 'init_local_game_project',
),
).toBe(false);
});