Файли
openidea/src/platform/remote.test.ts
T
Vladislav Kuzmin 8fd5ee382d fix(database): isolate PostgreSQL manual transactions by SQL console (#6482)
Two PostgreSQL consoles on the same data source shared one manual transaction, so one console could see and commit the other's pending work. Each console now owns its manual transaction: Commit, Roll Back, Stop, result reads and close act on the originating console, retained results keep their transaction until the last reference closes, and a statement for a lost transaction fails before executing. The pool reserves capacity for ordinary work; up to two manual transactions can hold a connection per target, and a third gets a message on how to free a slot. Auto-commit and the transaction-pooler read-only path are unchanged.

Fixes #6445

Co-authored-by: principalwater <[email protected]>
2026-11-28 07:57:17 +08:00

167 рядки
6.5 KiB
TypeScript

import { Channel } from "@tauri-apps/api/core";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { call, commands, type ChannelFrame, type ExecutionEvent, type ProjectInfo, type Settings } from "./ipc";
import { useProject } from "./project";
import { disableRemote, enableRemote, useRemote } from "./remote";
import { encodeArgs, remoteRouting } from "./remote";
import { scratchesDir } from "./scratchesDir";
describe("remote invoke", () => {
beforeAll(() => {
let next = 1;
Object.assign(window, { __TAURI_INTERNALS__: { transformCallback: () => next++ } });
});
it("replaces channels with ids and keeps the rest", () => {
const events = new Channel<unknown>();
const { json, channels } = encodeArgs({ path: "/a", requestId: 3, onEvent: events });
const parsed = JSON.parse(json) as { path: string; requestId: number; onEvent: { $channel: number } };
expect(parsed.path).toBe("/a");
expect(parsed.requestId).toBe(3);
expect(channels.get(parsed.onEvent.$channel)).toBe(events);
});
it("routes SQL batch progress after releasing the unused local callback", async () => {
let next = 1;
const unregisterCallback = vi.fn();
const done: ExecutionEvent = { type: "done", error: null };
vi.stubGlobal("__TAURI_INTERNALS__", {
transformCallback: () => next++,
unregisterCallback,
async invoke(command: string, call: { cmd: string; args: string; onChannel: Channel<ChannelFrame> }) {
expect(command).toBe("remote_invoke");
expect(call.cmd).toBe("db_execute_stream");
const { onProgress, console, consoleManual } = JSON.parse(call.args) as {
onProgress: { $channel: number };
console: string;
consoleManual: boolean;
};
expect(console).toBe("/remote/console.sql");
expect(consoleManual).toBe(true);
call.onChannel.onmessage({ id: onProgress.$channel, data: JSON.stringify(done) });
return "null";
},
});
enableRemote({
target: { kind: "wsl", distro: "Ubuntu" },
root: "/remote",
host: "Ubuntu",
os: "linux",
commands: ["db_execute_stream"],
});
const progress = vi.fn();
const channel = new Channel<ExecutionEvent>(progress);
try {
expect(
await call(commands.dbExecuteStream(1, "test-db", [], 10, null, null, channel, "/remote/console.sql", true)),
).toBeNull();
expect(unregisterCallback).toHaveBeenCalledExactlyOnceWith(channel.id);
expect(progress).toHaveBeenCalledExactlyOnceWith(done);
} finally {
disableRemote();
vi.unstubAllGlobals();
}
});
it("routes a scratch folder lookup made as soon as the remote session is published", async () => {
const invoke = vi.fn(async (command: string, args: { cmd: string }) => {
expect(command).toBe("remote_invoke");
expect(args.cmd).toBe("fs_scratches_dir");
return JSON.stringify("/home/me/.openidea/data/scratches");
});
vi.stubGlobal("__TAURI_INTERNALS__", { invoke, transformCallback: () => 1 });
const unsubscribe = useRemote.subscribe(({ session }) => {
if (session) void scratchesDir();
});
try {
enableRemote({
target: { kind: "ssh", config: "test" },
root: "/home/me/project",
host: "test",
os: "linux",
commands: ["fs_scratches_dir"],
});
await expect(scratchesDir()).resolves.toBe("/home/me/.openidea/data/scratches");
expect(invoke).toHaveBeenCalledOnce();
} finally {
unsubscribe();
disableRemote();
vi.unstubAllGlobals();
}
});
it("routes served commands, keeps opening / closing local and sets global settings on both sides", () => {
const served = new Set([
"vcs_add_to_ignore",
"fs_read_dir",
"open_project",
"close_project",
"settings_set",
"system_font_families",
]);
expect(remoteRouting("vcs_add_to_ignore", { paths: ["/remote/file"], destination: "localExclude" }, served)).toBe(
"remote",
);
expect(remoteRouting("system_font_families", { refresh: true }, served)).toBe("local");
expect(remoteRouting("fs_read_dir", { path: "/a" }, served)).toBe("remote");
expect(remoteRouting("window_open_settings", {}, served)).toBe("local");
expect(remoteRouting("open_project", { path: "/a" }, served)).toBe("local");
expect(remoteRouting("close_project", {}, served)).toBe("local");
expect(remoteRouting("settings_set", { scope: "project", key: "k", value: "1" }, served)).toBe("remote");
expect(remoteRouting("settings_set", { scope: "global", key: "k", value: "1" }, served)).toBe("both");
});
it.each(["agent_subscribe", "agent_entries", "agent_prompt"])("routes %s to the agent's host", (cmd) => {
expect(remoteRouting(cmd, { task: "saved-codex-task", subagent: null }, new Set([cmd]))).toBe("remote");
});
it("encodes no arguments as an empty object", () => {
expect(encodeArgs(undefined).json).toBe("{}");
});
});
describe("remote session lifetime", () => {
const session = {
target: { kind: "wsl" as const, distro: "Ubuntu" },
root: "/home/me/app",
host: "Ubuntu",
os: "linux",
commands: ["vcs_roots"],
};
const project = {
name: "app",
root: "/home/me/app",
trusted: true,
isGit: true,
settings: {},
} as unknown as ProjectInfo;
afterEach(() => {
disableRemote();
useProject.setState({ project: null, settings: null, opening: false, openingPath: null, lightEdit: false });
});
it("keeps routing when a window with no project of its own updates the settings only", () => {
// The comparison window: it rides its parent's session and keeps just the settings
// (restoring the project would overwrite the main window's editor session).
enableRemote(session);
useProject.setState({ settings: { editor: { fontSize: 12 } } as unknown as Settings });
expect(useRemote.getState().session).not.toBeNull();
});
it("ends routing when the window's project leaves the session's root", () => {
enableRemote(session);
useProject.setState({ project });
// Close Project (or a local project opened over the remote one) — the session goes.
useProject.setState({ project: null });
expect(useRemote.getState().session).toBeNull();
});
it("keeps routing while the project still sits at the session's root", () => {
enableRemote(session);
useProject.setState({ project });
useProject.setState({ settings: { editor: { fontSize: 14 } } as unknown as Settings });
expect(useRemote.getState().session).not.toBeNull();
});
});