mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
* fix(remote): restore WSL projects onto a live connection A reopened WSL window kept sending commands to a remote host it was no longer connected to. Remember the remote location, and do not drop a connection that a later open or a superseded attempt still needs. Co-authored-by: Cursor <cursoragent@cursor.com> * Use ? for the last remote location prefix (clippy::question_mark) * fix(remote): save window session after connecting --------- Co-authored-by: 胡长贵 <hucg13@chinaunicom.cn> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com>
323 lines
13 KiB
TypeScript
323 lines
13 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { CLIENT_SETTINGS_KEYS } from "./clientSettings";
|
|
import { commands, call, type Settings } from "./ipc";
|
|
import { loadSettings, useProject } from "./project";
|
|
import {
|
|
connectRemote,
|
|
disableRemote,
|
|
enableRemote,
|
|
isRemoteWindow,
|
|
localGlobalSettings,
|
|
remoteRouting,
|
|
useRemote,
|
|
useRemoteProgress,
|
|
} from "./remote";
|
|
import { invoke as routedInvoke } from "./tauriCore";
|
|
|
|
const handlers = vi.hoisted(() => new Map<string, () => void>());
|
|
vi.mock("./ipc", async (original) => ({
|
|
...(await original<typeof import("./ipc")>()),
|
|
onIdeEvent: (event: string, handler: () => void) => {
|
|
handlers.set(event, handler);
|
|
return () => handlers.delete(event);
|
|
},
|
|
}));
|
|
import { trackRuntimeSettings } from "@/features/settings/runtime";
|
|
|
|
const local = {
|
|
keymap: "my-map",
|
|
keymapOverrides: { "editor.copy": ["Meta+J"] },
|
|
customKeymaps: {
|
|
"my-map": { name: "Mine", parent: "vscode-macos", overrides: { "editor.paste": ["Meta+K"] } },
|
|
},
|
|
appearance: { theme: "dark" },
|
|
editor: { fontSize: 19, tabSize: 8 },
|
|
terminal: { fontSize: 18, shellPath: "/local" },
|
|
agents: { fitWindowWidth: true, newTaskApprovals: "ask" },
|
|
};
|
|
const remote = {
|
|
keymap: "intellij-linux",
|
|
keymapOverrides: {},
|
|
customKeymaps: {},
|
|
appearance: { theme: "light" },
|
|
editor: { fontSize: 13, tabSize: 2 },
|
|
terminal: { fontSize: 13, shellPath: "/remote" },
|
|
agents: { fitWindowWidth: false, newTaskApprovals: "bypass", mcpServers: { docs: { command: "npx" } } },
|
|
};
|
|
const served = new Set(["settings_get", "settings_set", "project_current"]);
|
|
let invoke: ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(() => {
|
|
useProject.setState({ opening: true });
|
|
invoke = vi.fn(async (cmd: string, _args?: unknown, _options?: unknown) =>
|
|
cmd === "remote_invoke" ? JSON.stringify(remote) : local,
|
|
);
|
|
Object.assign(window, { __TAURI_INTERNALS__: { invoke, transformCallback: () => 1 } });
|
|
enableRemote({
|
|
target: { kind: "ssh", config: "test" },
|
|
root: "/remote",
|
|
host: "test",
|
|
os: "linux",
|
|
commands: [...served],
|
|
});
|
|
});
|
|
afterEach(() => {
|
|
disableRemote();
|
|
useProject.setState({ opening: false, settings: null });
|
|
useRemoteProgress.setState({ progress: null, location: null });
|
|
});
|
|
|
|
describe("remote client settings", () => {
|
|
it("routes window reports through the commands advertised by the remote host", async () => {
|
|
enableRemote({
|
|
target: { kind: "ssh", config: "test" },
|
|
root: "/remote",
|
|
host: "test",
|
|
os: "linux",
|
|
commands: ["mcp_set_open_editors", "mcp_set_focused_editor", "mcp_report_problems"],
|
|
});
|
|
invoke.mockResolvedValue("null");
|
|
await call(commands.mcpSetOpenEditors(["/remote/a.txt"]));
|
|
await call(commands.mcpSetFocusedEditor(null, true));
|
|
const files = [{ path: "/remote/a.txt", items: [] }];
|
|
await call(commands.mcpReportProblems(true, files));
|
|
await call(commands.mcpReportProblems(false, files));
|
|
const reports = invoke.mock.calls.map(([cmd, args]) => [
|
|
cmd,
|
|
(args as { cmd: string }).cmd,
|
|
JSON.parse((args as { args: string }).args),
|
|
]);
|
|
expect(reports).toEqual([
|
|
["remote_invoke", "mcp_set_open_editors", { paths: ["/remote/a.txt"] }],
|
|
["remote_invoke", "mcp_set_focused_editor", { editor: null, focused: true }],
|
|
["remote_invoke", "mcp_report_problems", { reset: true, files }],
|
|
["remote_invoke", "mcp_report_problems", { reset: false, files }],
|
|
]);
|
|
});
|
|
|
|
it.each([
|
|
{ gradle: { dir: "app", projects: [] }, expected: [{ dir: "app", projects: [] }] },
|
|
{
|
|
gradle: [
|
|
{ dir: "one", projects: [] },
|
|
{ dir: "two", projects: [] },
|
|
],
|
|
expected: [
|
|
{ dir: "one", projects: [] },
|
|
{ dir: "two", projects: [] },
|
|
],
|
|
},
|
|
{ gradle: null, expected: null },
|
|
])("accepts the released and current remote Gradle build models: $gradle", async ({ gradle, expected }) => {
|
|
for (const command of ["build_model", "build_sync", "gradle_link_projects", "gradle_unlink_projects"]) {
|
|
enableRemote({
|
|
target: { kind: "wsl", distro: "Ubuntu" },
|
|
root: "/remote",
|
|
host: "Ubuntu",
|
|
os: "linux",
|
|
commands: [command],
|
|
});
|
|
invoke.mockResolvedValue(JSON.stringify({ gradle, maven: null }));
|
|
expect(await routedInvoke(command)).toEqual({ gradle: expected, maven: null });
|
|
}
|
|
});
|
|
|
|
it("ends remote routing before a successful local open is published, even at the same path", async () => {
|
|
invoke.mockResolvedValueOnce({ root: "/remote", settings: local });
|
|
await call(commands.openProject("/remote"));
|
|
expect(isRemoteWindow()).toBe(false);
|
|
await loadSettings();
|
|
expect(useProject.getState().settings).toEqual(local);
|
|
expect(invoke.mock.calls.map(([cmd]) => cmd)).toEqual(["open_project", "settings_get"]);
|
|
});
|
|
|
|
it("preserves remote routing when opening a local project fails", async () => {
|
|
invoke.mockRejectedValueOnce({ kind: "notFound", message: "missing folder" });
|
|
await expect(call(commands.openProject("/missing"))).rejects.toThrow("missing folder");
|
|
expect(isRemoteWindow()).toBe(true);
|
|
await loadSettings();
|
|
expect(useProject.getState().settings?.editor?.tabSize).toBe(2);
|
|
});
|
|
|
|
it.each([...CLIENT_SETTINGS_KEYS, "customKeymaps.mine", "keymapOverrides.editor.copy", "appearance.theme"])(
|
|
"keeps %s writes local in either scope",
|
|
(key) => {
|
|
for (const scope of ["global", "project"])
|
|
expect(remoteRouting("settings_set", { scope, key, value: "null" }, served)).toBe("local");
|
|
},
|
|
);
|
|
it.each([
|
|
"editor.tabSize",
|
|
"editor.formatOnSave",
|
|
"editor.codeStyle.languages",
|
|
"terminal.shellPath",
|
|
"terminal.env",
|
|
"tools.node",
|
|
"languages.python.interpreter",
|
|
"agents.newTaskApprovals",
|
|
"agents.defaults",
|
|
"agents.mcpServers",
|
|
"keymapExtra",
|
|
])("preserves routing for host setting %s", (key) => {
|
|
expect(remoteRouting("settings_set", { scope: "project", key }, served)).toBe("remote");
|
|
expect(remoteRouting("settings_set", { scope: "global", key }, served)).toBe("both");
|
|
});
|
|
it("routes mcpServer settings and MCP commands to the remote host without mutating local MCP settings", async () => {
|
|
const withMcp = new Set([...served, "settings_get_global", "mcp_server_status", "mcp_ui_ready", "mcp_respond"]);
|
|
enableRemote({
|
|
target: { kind: "ssh", config: "test" },
|
|
root: "/remote",
|
|
host: "test",
|
|
os: "linux",
|
|
commands: [...withMcp],
|
|
});
|
|
expect(remoteRouting("settings_set", { scope: "global", key: "mcpServer.enabled", value: "true" }, withMcp)).toBe(
|
|
"remote",
|
|
);
|
|
expect(
|
|
remoteRouting("settings_set", { scope: "global", key: "mcpServer.address", value: '"0.0.0.0"' }, withMcp),
|
|
).toBe("remote");
|
|
expect(remoteRouting("mcp_server_status", undefined, withMcp)).toBe("remote");
|
|
expect(remoteRouting("mcp_ui_ready", { ready: true }, withMcp)).toBe("remote");
|
|
expect(remoteRouting("mcp_respond", { id: 1, allow: true, error: null }, withMcp)).toBe("remote");
|
|
|
|
expect(await commands.settingsGetGlobal()).toMatchObject({
|
|
keymap: "my-map",
|
|
editor: { fontSize: 19, tabSize: 2 },
|
|
});
|
|
expect(await localGlobalSettings()).toMatchObject({
|
|
keymap: "my-map",
|
|
editor: { fontSize: 19, tabSize: 8 },
|
|
});
|
|
});
|
|
it("merges settings before publishing a newly connected project", async () => {
|
|
disableRemote();
|
|
invoke.mockImplementation(async (cmd: string) =>
|
|
cmd === "remote_connect"
|
|
? {
|
|
target: { kind: "ssh", config: "test" },
|
|
root: "/remote",
|
|
host: "test",
|
|
commands: [...served],
|
|
project: { root: "/remote", name: "remote", settings: remote },
|
|
}
|
|
: cmd === "remote_invoke"
|
|
? "null"
|
|
: local,
|
|
);
|
|
await connectRemote({ kind: "ssh", config: "test" }, "/remote");
|
|
expect(useProject.getState().settings).toMatchObject({ keymap: "my-map", editor: { tabSize: 2, fontSize: 19 } });
|
|
});
|
|
it("does not disconnect a newer connect when an older one finishes", async () => {
|
|
disableRemote();
|
|
useRemoteProgress.setState({ progress: null, location: null });
|
|
let releaseFirst: (value: unknown) => void = () => {};
|
|
const first = new Promise((resolve) => {
|
|
releaseFirst = resolve;
|
|
});
|
|
let connects = 0;
|
|
invoke.mockImplementation(async (cmd: string) => {
|
|
if (cmd !== "remote_connect") return cmd === "remote_invoke" ? "null" : local;
|
|
connects += 1;
|
|
const root = connects === 1 ? "/home/me/old" : "/home/me/new";
|
|
const project = {
|
|
target: { kind: "wsl", distro: "Ubuntu" },
|
|
root,
|
|
host: "Ubuntu",
|
|
os: "linux",
|
|
commands: [...served],
|
|
project: { root, name: "app", displayName: "app", settings: remote },
|
|
};
|
|
return connects === 1 ? first.then(() => project) : project;
|
|
});
|
|
const older = connectRemote({ kind: "wsl", distro: "Ubuntu" }, "/home/me/old");
|
|
const newer = connectRemote({ kind: "wsl", distro: "Ubuntu" }, "/home/me/new");
|
|
await newer;
|
|
releaseFirst(undefined);
|
|
await expect(older).rejects.toMatchObject({ kind: "cancelled" });
|
|
expect(invoke.mock.calls.filter(([cmd]) => cmd === "remote_disconnect")).toHaveLength(0);
|
|
expect(useRemote.getState().session?.root).toBe("/home/me/new");
|
|
});
|
|
it("overlays the project snapshot used by the settings window", async () => {
|
|
invoke.mockImplementation(async (cmd: string) =>
|
|
cmd === "remote_invoke" ? JSON.stringify({ root: "/remote", settings: remote }) : local,
|
|
);
|
|
expect((await commands.projectCurrent())?.settings).toMatchObject({
|
|
keymap: "my-map",
|
|
editor: { tabSize: 2, fontSize: 19 },
|
|
});
|
|
});
|
|
it("keeps client preferences after a remote tooling write", async () => {
|
|
expect(await call(commands.settingsSet("project", "editor.tabSize", "2"))).toMatchObject({
|
|
keymap: "my-map",
|
|
editor: { fontSize: 19, tabSize: 2 },
|
|
});
|
|
expect(invoke.mock.calls.some(([cmd]) => cmd === "settings_set")).toBe(false);
|
|
});
|
|
it("keeps local overrides through a settingsChanged runtime reload", async () => {
|
|
vi.useFakeTimers();
|
|
await loadSettings();
|
|
const stop = trackRuntimeSettings();
|
|
try {
|
|
handlers.get("settingsChanged")!();
|
|
await vi.advanceTimersByTimeAsync(40);
|
|
expect(useProject.getState().settings?.keymapOverrides).toEqual(local.keymapOverrides);
|
|
expect(useProject.getState().settings?.keymap).toBe("my-map");
|
|
} finally {
|
|
stop();
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
it("reloads local preferences instead of retaining stale or remote overrides", async () => {
|
|
await loadSettings();
|
|
invoke.mockImplementation(async (cmd: string) =>
|
|
cmd === "remote_invoke" ? JSON.stringify(remote) : { ...local, keymapOverrides: {} },
|
|
);
|
|
await loadSettings();
|
|
expect(useProject.getState().settings?.keymapOverrides).toEqual({});
|
|
});
|
|
it("does not inherit remote UI values when a local field is absent", async () => {
|
|
invoke.mockImplementation(async (cmd: string) => (cmd === "remote_invoke" ? JSON.stringify(remote) : {}));
|
|
await loadSettings();
|
|
expect(useProject.getState().settings?.keymap).toBeUndefined();
|
|
expect(useProject.getState().settings?.editor?.tabSize).toBe(2);
|
|
});
|
|
it("leaves local windows unchanged", async () => {
|
|
disableRemote();
|
|
await loadSettings();
|
|
expect(useProject.getState().settings).toEqual(local);
|
|
expect(invoke).toHaveBeenCalledTimes(1);
|
|
});
|
|
it("preserves client keymaps and UI settings when loading remote settings", async () => {
|
|
await loadSettings();
|
|
const settings = useProject.getState().settings as Settings;
|
|
expect(settings).toMatchObject({
|
|
...local,
|
|
editor: { fontSize: 19, tabSize: 2 },
|
|
terminal: { fontSize: 18, shellPath: "/remote" },
|
|
agents: { fitWindowWidth: true, newTaskApprovals: "bypass", mcpServers: { docs: { command: "npx" } } },
|
|
});
|
|
expect(invoke).toHaveBeenCalledWith("settings_get_global", {}, undefined);
|
|
});
|
|
it("shows client preferences in the global settings dialog snapshot", async () => {
|
|
expect(await commands.settingsGetGlobal()).toMatchObject({
|
|
keymap: "my-map",
|
|
editor: { fontSize: 19, tabSize: 8 },
|
|
});
|
|
});
|
|
it("writes a client preference only to local global settings and returns merged settings", async () => {
|
|
const settings = await call(commands.settingsSet("project", "editor.fontSize", "19"));
|
|
expect(invoke).toHaveBeenCalledWith(
|
|
"settings_set",
|
|
{ scope: "global", key: "editor.fontSize", value: "19" },
|
|
undefined,
|
|
);
|
|
expect(
|
|
invoke.mock.calls.filter(
|
|
([cmd, args]) => cmd === "remote_invoke" && (args as { cmd?: string })?.cmd === "settings_set",
|
|
),
|
|
).toHaveLength(0);
|
|
expect(settings).toMatchObject({ keymap: "my-map", editor: { fontSize: 19, tabSize: 2 } });
|
|
});
|
|
});
|