910862fd06
稳定工作台与窗口标题栏状态同步,消除重复更新循环 支持画布右键平移并保留左键框选及资源拖动,完善中断清理 解耦运行不可用提示与资源选中状态 复用原生UI状态校验区分UI设计JSON和普通JSON,接入现有编辑器与代码预览 补充前端和原生回归测试、规范及待验收记录,明确对话历史分页尚未修复
112 lines
3.5 KiB
TypeScript
112 lines
3.5 KiB
TypeScript
/** @vitest-environment jsdom */
|
|
import type { ReactNode } from 'react';
|
|
import { useCallback } from 'react';
|
|
import { vi } from 'vitest';
|
|
|
|
import { WindowChrome } from '../src/components/WindowChrome';
|
|
import {
|
|
useWindowChrome,
|
|
type WindowChromeActiveProjectRuns,
|
|
WindowChromeContext,
|
|
} from '../src/components/windowChromeContext';
|
|
import { WorkspaceLauncherShell } from '../src/features/app-shell/WorkspaceLauncher';
|
|
import {
|
|
act,
|
|
expect,
|
|
it,
|
|
React,
|
|
render,
|
|
testAuthUser,
|
|
} from './appSurface/harness';
|
|
|
|
const homeProjectOverride = vi.hoisted(() => ({
|
|
openProject: null as
|
|
| null
|
|
| ((path: string, mode: 'open' | 'create') => Promise<void>),
|
|
}));
|
|
|
|
vi.mock('../src/features/app-shell/useHomeProjectCreation', async () => {
|
|
const actual = await vi.importActual<
|
|
typeof import('../src/features/app-shell/useHomeProjectCreation')
|
|
>('../src/features/app-shell/useHomeProjectCreation');
|
|
return {
|
|
...actual,
|
|
useHomeProjectCreation(
|
|
...args: Parameters<typeof actual.useHomeProjectCreation>
|
|
) {
|
|
const result = actual.useHomeProjectCreation(...args);
|
|
return homeProjectOverride.openProject
|
|
? { ...result, openProject: homeProjectOverride.openProject }
|
|
: result;
|
|
},
|
|
};
|
|
});
|
|
|
|
it('真实窗口与工作台状态同步收敛,回调读取最新处理器且卸载才清理', async () => {
|
|
const publications: WindowChromeActiveProjectRuns[] = [];
|
|
let cleanups = 0;
|
|
// 仍经过真实 WindowChrome 的 setState/Context;上限只防止回归时测试无限循环。
|
|
function BoundedWindowBridge({ children }: { children: ReactNode }) {
|
|
const chrome = useWindowChrome();
|
|
const { setActiveProjectRuns } = chrome;
|
|
const publish = useCallback(
|
|
(next: WindowChromeActiveProjectRuns | null) => {
|
|
if (next) publications.push(next);
|
|
else cleanups += 1;
|
|
if (publications.length < 12) setActiveProjectRuns(next);
|
|
},
|
|
[setActiveProjectRuns],
|
|
);
|
|
return (
|
|
<WindowChromeContext.Provider
|
|
value={{ ...chrome, setActiveProjectRuns: publish }}
|
|
>
|
|
{children}
|
|
</WindowChromeContext.Provider>
|
|
);
|
|
}
|
|
const supervisor = () => null;
|
|
const view = (displayName = '测试用户') => (
|
|
<WindowChrome>
|
|
<BoundedWindowBridge>
|
|
<WorkspaceLauncherShell
|
|
currentUser={{ ...testAuthUser, displayName }}
|
|
onLogout={() => undefined}
|
|
initialView="projects"
|
|
ProjectSupervisor={supervisor}
|
|
/>
|
|
</BoundedWindowBridge>
|
|
</WindowChrome>
|
|
);
|
|
delete window.__TAURI__;
|
|
const rendered = render(view());
|
|
try {
|
|
await act(async () => {
|
|
await Promise.resolve();
|
|
});
|
|
// 无原生 invoke 时 active-turn Hook 会把初始快照归一为空数组一次。
|
|
expect(publications).toHaveLength(2);
|
|
expect(cleanups).toBe(0);
|
|
expect(new Set(publications.map((item) => item.onOpenProject)).size).toBe(
|
|
1,
|
|
);
|
|
const openProject = publications[0]!.onOpenProject!;
|
|
const latestOpen = vi.fn(async () => undefined);
|
|
homeProjectOverride.openProject = latestOpen;
|
|
rendered.rerender(view('更改显示名'));
|
|
expect(publications).toHaveLength(2);
|
|
act(() => openProject('/tmp/window-latest-project'));
|
|
expect(latestOpen).toHaveBeenCalledWith(
|
|
'/tmp/window-latest-project',
|
|
'open',
|
|
);
|
|
expect(publications).toHaveLength(2);
|
|
expect(cleanups).toBe(0);
|
|
rendered.unmount();
|
|
expect(cleanups).toBe(1);
|
|
} finally {
|
|
homeProjectOverride.openProject = null;
|
|
rendered.unmount();
|
|
}
|
|
});
|