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

98 lines
3.1 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 { buildRecentProjectRows } from '../src/features/app-shell/model';
import { useHomeProjectCreation } from '../src/features/app-shell/useHomeProjectCreation';
afterEach(() => {
delete window.__TAURI__;
});
type TauriInvoke = NonNullable<
NonNullable<NonNullable<Window['__TAURI__']>['core']>['invoke']
>;
function asTauriInvoke(
invoke: (command: string, args?: Record<string, unknown>) => Promise<unknown>,
): TauriInvoke {
// 泛型 invoke 与具体的非泛型 mock 结构上不可兼容,只能用 as unknown as 收敛。
return invoke as unknown as TauriInvoke;
}
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: asTauriInvoke(invoke) } };
const setLauncherView = vi.fn();
const { result } = renderHook(() =>
useHomeProjectCreation({
setStatus: vi.fn(),
showTransientNotice: vi.fn(),
setLauncherView,
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 ? '本地项目' : '可导入',
});
},
);