import { afterEach, describe, expect, it, vi } from 'vitest'; import { setAgcPluginProjectPath, startAvailableAgcPlugin, } from '../src/services/pluginHost'; afterEach(() => vi.unstubAllGlobals()); describe('插件自动启动使用后端能力投影', () => { it.each( [ [], [ { id: 'agc-cocos-editor', enabled: false, hasRuntime: true, status: 'stopped', }, ], [ { id: 'agc-cocos-editor', enabled: true, hasRuntime: false, status: 'package', }, ], [ { id: 'agc-cocos-editor', enabled: true, hasRuntime: true, status: 'invalid', }, ], ].map((plugins) => ({ plugins })), )('隐藏、禁用或不可执行的插件不启动(%j)', async ({ plugins }) => { const invoke = vi.fn(async () => plugins); vi.stubGlobal('window', { __TAURI__: { core: { invoke } } }); await startAvailableAgcPlugin('agc-cocos-editor'); expect(invoke).toHaveBeenCalledTimes(1); expect(invoke).toHaveBeenCalledWith('list_agc_plugins'); }); it.each(['agc-cocos-editor', 'agc-unity-editor', 'agc-godot-editor'])( '支持的编辑器插件按原入口启动:%s', async (pluginId) => { const invoke = vi.fn(async (command: string) => command === 'list_agc_plugins' ? [ { id: pluginId, enabled: true, hasRuntime: true, status: 'stopped', }, ] : {}, ); vi.stubGlobal('window', { __TAURI__: { core: { invoke } } }); await startAvailableAgcPlugin(pluginId); expect(invoke).toHaveBeenLastCalledWith('start_agc_plugin', { id: pluginId, }); }, ); it('快速切换项目时旧 cleanup 不能晚于新项目设置抵达宿主', async () => { const received: Array = []; let finishFirst: () => void = () => undefined; const invoke = vi.fn( async (_command: string, params: { projectPath: string | null }) => { if (params.projectPath === 'C:/A') await new Promise((resolve) => { finishFirst = resolve; }); received.push(params.projectPath); }, ); vi.stubGlobal('window', { __TAURI__: { core: { invoke } } }); const first = setAgcPluginProjectPath('C:/A'); const cleanup = setAgcPluginProjectPath(null); const second = setAgcPluginProjectPath('C:/B'); await vi.waitFor(() => expect(invoke).toHaveBeenCalledTimes(1)); finishFirst(); await Promise.all([first, cleanup, second]); expect(received).toEqual(['C:/A', null, 'C:/B']); }); });