Files
Genarrative/apps/ai-game-creator-shell/tests/homeWebPreflight.test.tsx
lhk229 1e186369c9
Project CI / AI game creator shell Rust crates (push) Successful in 1m24s
Project CI / AI game creator shell Rust smoke (push) Successful in 1m56s
Project CI / AI game creator shell Rust lane 1/2 (push) Has been cancelled
Project CI / Frontend tests (push) Has been cancelled
Project CI / Backend tests (push) Has been cancelled
Project CI / Repository checks (push) Has been cancelled
Project CI / AI game creator shell web tests (push) Has been cancelled
Project CI / AI game creator shell Rust lane 2/2 (push) Has been cancelled
Project CI / Native shell tests (push) Has been cancelled
客户端埋点设置 (#446)
Reviewed-on: https://git.genarrative.world/git/GenarrativeAI/Genarrative/pulls/446
Co-authored-by: Linghong <ink29535@proton.me>
Co-committed-by: Linghong <ink29535@proton.me>
2026-09-23 00:09:12 +08:00

107 lines
3.8 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 { HOME_WEB_PREFLIGHT_FAILURE } from '../src/features/app-shell/homeWebPreflight';
import { useHomeProjectCreation } from '../src/features/app-shell/useHomeProjectCreation';
afterEach(() => {
cleanup();
delete window.__TAURI__;
});
function mount(preflight: () => Promise<unknown>) {
const calls: string[] = [];
const invoke = vi.fn(async (command: string) => {
if (command !== 'capture_analytics_context') calls.push(command);
if (command === 'preflight_web_game_creation') return preflight();
if (command === 'suggest_automatic_project_name') return '预检项目';
if (command === 'create_automatic_local_game_project')
return {
projectPath: 'C:/test/preflight-game',
manifestPath: 'C:/test/preflight-game/.agent/manifest.json',
manifest: createGameCreationAppManifest('preflight-game', '预检项目'),
};
if (command === 'get_local_game_project_revision') return { revision: 1 };
if (command === 'set_design_agent_runtime_mode')
return { activeRuntime: 'design' };
return null;
});
window.__TAURI__ = { core: { invoke } } as unknown as typeof window.__TAURI__;
const hook = renderHook(() =>
useHomeProjectCreation({
setStatus: vi.fn(),
setLauncherView: vi.fn(),
setAgentChatProjectPath: vi.fn(),
rememberRecentWorkspace: vi.fn(),
}),
);
return { ...hook, calls, invoke };
}
it('awaits the real home preflight before naming or creating a game', async () => {
let ready!: (value: unknown) => void;
const pending = new Promise((resolve) => {
ready = resolve;
});
const { result, calls } = mount(() => pending);
let creation!: Promise<string>;
await act(async () => {
creation = result.current.createHomeDraftAutomatically(
{ creationType: 'game', prompt: '做一个小游戏', attachments: [] },
'direct-build',
);
});
expect(calls).toEqual(['preflight_web_game_creation']);
await act(async () => {
ready({ status: 'ready' });
await creation;
});
expect(calls.slice(0, 3)).toEqual([
'preflight_web_game_creation',
'suggest_automatic_project_name',
'create_automatic_local_game_project',
]);
expect(result.current.currentProjectContext?.creationType).toBe('game');
});
it.each([false, true])(
'blocks naming, creation, and first generation when preflight fails (throws=%s)',
async (throws) => {
const { result, calls } = mount(async () => {
if (throws) throw new Error('private host path');
return { status: 'blocked' };
});
let status = '';
await act(async () => {
status = await result.current.createHomeDraftAutomatically(
{ creationType: 'game', prompt: '做一个小游戏', attachments: [] },
'direct-build',
);
});
expect(status).toBe(HOME_WEB_PREFLIGHT_FAILURE);
expect(calls).toEqual(['preflight_web_game_creation']);
expect(result.current.currentProjectContext).toBeNull();
expect(status).not.toContain('private');
},
);
it.each(['game', 'doc'] as const)(
'does not require the Web preflight for %s planning',
async (creationType) => {
const { result, calls } = mount(async () => {
throw new Error('must not run');
});
await act(async () => {
await result.current.createHomeDraftAutomatically(
{ creationType, prompt: '策划需求', attachments: [] },
'planning',
);
});
expect(calls).not.toContain('preflight_web_game_creation');
expect(calls).not.toContain('suggest_automatic_project_name');
expect(calls).toContain('create_automatic_local_game_project');
},
);