Files
Genarrative/apps/ai-game-creator-shell/tests/projectCreationDirectory.test.ts
T
k88936 25c5ecdf6e
Project CI / AI game creator shell Rust crates (pull_request) Successful in 2m58s
Project CI / AI game creator shell Rust lane 1/2 (pull_request) Successful in 6m34s
Project CI / AI game creator shell Rust lane 2/2 (pull_request) Successful in 5m29s
Project CI / Backend tests (pull_request) Failing after 4m38s
Project CI / Frontend tests (pull_request) Successful in 3m33s
Project CI / Repository checks (pull_request) Failing after 4m4s
Project CI / AI game creator shell web tests (pull_request) Successful in 3m13s
Project CI / Native shell tests (pull_request) Successful in 6m35s
测试:全仓测试统一 Tauri 运行时替身安装点
- 78 处内联 window.__TAURI__ 赋值改为 installTauriRuntime,覆盖 appSurface 之外的 37 个测试文件
- 31 处 delete window.__TAURI__ / window.__TAURI__ = undefined 复位改为 resetTauriRuntime
- 删除 recentProjectsHook、projectCreationDirectory、projectResourceLiveIntegration、useProjectResourceCardPreviews、resourceVersionReplacementModel、unityProjectOpen 里的本地 asTauriInvoke / asTauriListen 适配器与相关类型别名
- designProjectRestore 的 setup 参数由 ReturnType<typeof vi.fn> 收窄为具体 invoke 签名
- 迁移后仅 tests/tauriRuntimeFake.ts 直接读写 window.__TAURI__
2026-10-03 20:19:45 +08:00

120 lines
3.9 KiB
TypeScript

/** @vitest-environment jsdom */
import { act, renderHook, waitFor } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { normalizeProjectCreationDirectory } from '../src/features/app-shell/model';
import { useProjectCreationDirectory } from '../src/features/app-shell/useProjectCreationDirectory';
import {
readWorkspacePreferences,
resetWorkspacePreferencesForTests,
setProjectCreationDirectory,
} from '../src/features/app-shell/workspacePreferences';
import { installTauriRuntime, resetTauriRuntime } from './tauriRuntimeFake';
describe('项目创建目录偏好', () => {
beforeEach(() => {
resetWorkspacePreferencesForTests();
resetTauriRuntime();
});
it('去掉首尾空白与多余分隔符,并保留盘根', () => {
expect(normalizeProjectCreationDirectory(' ')).toBe('');
expect(normalizeProjectCreationDirectory(' F:\\Projects\\游戏\\ ')).toBe(
'F:\\Projects\\游戏',
);
expect(normalizeProjectCreationDirectory('F:/Projects/游戏/')).toBe(
'F:/Projects/游戏',
);
expect(normalizeProjectCreationDirectory('C:\\')).toBe('C:\\');
// 首尾空白按 trim 处理;目录中间的控制字符必须整条拒绝。
expect(normalizeProjectCreationDirectory('F:\\游戏\n')).toBe('F:\\游戏');
expect(normalizeProjectCreationDirectory('F:\\游\n戏')).toBe('');
});
it('在无 Tauri 时由内存快照承接设置交互', async () => {
await expect(readWorkspacePreferences()).resolves.toMatchObject({
projectCreationDirectory: null,
});
await expect(
setProjectCreationDirectory('C:\\Projects\\games'),
).resolves.toMatchObject({
projectCreationDirectory: 'C:\\Projects\\games',
});
await expect(readWorkspacePreferences()).resolves.toMatchObject({
projectCreationDirectory: 'C:\\Projects\\games',
});
await expect(setProjectCreationDirectory(null)).resolves.toMatchObject({
projectCreationDirectory: null,
});
});
it('非法目录不会进入正式快照', async () => {
await expect(
setProjectCreationDirectory('relative/games'),
).resolves.toMatchObject({
projectCreationDirectory: null,
});
});
it('订阅成功后读取的旧快照不会覆盖较新的 Rust 偏好事件', async () => {
let resolveRead:
| ((snapshot: {
projectCreationDirectory: string | null;
recentWorkspaces: string[];
}) => void)
| null = null;
let eventHandler:
| ((event: {
payload: {
projectCreationDirectory: string | null;
recentWorkspaces: string[];
};
}) => void)
| null = null;
const invoke = vi.fn(async (command: string) => {
if (command === 'read_workspace_preferences') {
return await new Promise<{
projectCreationDirectory: string | null;
recentWorkspaces: string[];
}>((resolve) => {
resolveRead = resolve;
});
}
throw new Error('unexpected invoke ' + command);
});
installTauriRuntime({
core: { invoke },
event: {
listen: vi.fn(async (_event, handler) => {
eventHandler = handler;
return () => {
eventHandler = null;
};
}),
},
});
const { result } = renderHook(() => useProjectCreationDirectory());
await waitFor(() => {
expect(invoke).toHaveBeenCalledWith('read_workspace_preferences');
});
act(() => {
eventHandler?.({
payload: {
projectCreationDirectory: 'F:\\Projects\\new',
recentWorkspaces: [],
},
});
});
await act(async () => {
resolveRead?.({
projectCreationDirectory: null,
recentWorkspaces: [],
});
});
await waitFor(() => {
expect(result.current.projectCreationDirectory).toBe('F:\\Projects\\new');
});
});
});