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

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(),
showTransientNotice: vi.fn(),
setLauncherView: 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');
});