дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 18:17:15 +02:00
The remote status shows the dev container's CPU and memory limits and live usage, and says when the container hit its memory limit, using Docker's explicit OOM status rather than the exit code. Stale responses after a disconnect are discarded and host round-trip time is measured independently of Docker polling; MCP approval flow and tool registrations are kept. Closes #7467 Co-authored-by: OnurrUgur <[email protected]>
241 рядки
8.1 KiB
TypeScript
241 рядки
8.1 KiB
TypeScript
import { afterEach, beforeEach, expect, it, vi } from "vitest";
|
|
import type { RemoteSession } from "@/platform/ipc";
|
|
|
|
const listeners = vi.hoisted(
|
|
() =>
|
|
new Map<
|
|
string,
|
|
(event: { host: string; takenOver: boolean; oomMessage?: string | null; oomConfigPath?: string | null }) => void
|
|
>(),
|
|
);
|
|
const reconnectRemote = vi.hoisted(() => vi.fn<() => Promise<void>>());
|
|
const notify = vi.hoisted(() => vi.fn());
|
|
const dirty = vi.hoisted(() => ({ ids: [] as number[] }));
|
|
const confirm = vi.hoisted(() => vi.fn(async () => true));
|
|
|
|
vi.mock("@/platform/ipc", async (orig) => ({
|
|
...(await orig<typeof import("@/platform/ipc")>()),
|
|
onIdeEvent: (
|
|
type: string,
|
|
handler: (event: {
|
|
host: string;
|
|
takenOver: boolean;
|
|
oomMessage?: string | null;
|
|
oomConfigPath?: string | null;
|
|
}) => void,
|
|
) => {
|
|
listeners.set(type, handler);
|
|
return () => listeners.delete(type);
|
|
},
|
|
}));
|
|
vi.mock("@/platform/remote", async (orig) => ({
|
|
...(await orig<typeof import("@/platform/remote")>()),
|
|
reconnectRemote,
|
|
}));
|
|
vi.mock("@/workbench/contributions", async (orig) => ({
|
|
...(await orig<typeof import("@/workbench/contributions")>()),
|
|
notifications: () => ({ notify, expire: vi.fn() }),
|
|
maybeEditorService: () => ({ dirtyDocumentIds: () => dirty.ids }),
|
|
}));
|
|
vi.mock("@/workbench/messages", () => ({ confirm }));
|
|
|
|
import { getContext } from "@/platform/context";
|
|
import { enableRemote, useRemote } from "@/platform/remote";
|
|
import { installReconnect, PASSIVE_RECONNECT_MS, reconnectNow, RETRY_DELAYS_MS } from "./reconnect";
|
|
|
|
const session: RemoteSession = {
|
|
target: { kind: "ssh", config: "box" },
|
|
root: "/srv/app",
|
|
host: "box",
|
|
os: "linux",
|
|
commands: [],
|
|
};
|
|
|
|
let waits: number[];
|
|
let uninstall: () => void;
|
|
|
|
beforeEach(() => {
|
|
waits = [];
|
|
dirty.ids = [];
|
|
useRemote.setState({ session, lost: null, takenOver: false, reconnecting: false });
|
|
uninstall = installReconnect(async (ms) => {
|
|
waits.push(ms);
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
uninstall();
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
const lose = (takenOver = false) => listeners.get("remoteConnectionLost")!({ host: "box", takenOver });
|
|
const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
|
|
it("marks the window lost and reconnects in the background", async () => {
|
|
reconnectRemote.mockRejectedValueOnce(new Error("Network is unreachable")).mockResolvedValueOnce();
|
|
lose();
|
|
expect(useRemote.getState().lost).toBe("box");
|
|
expect(getContext().remoteConnectionLost).toBe(true);
|
|
await settle();
|
|
expect(waits).toEqual(RETRY_DELAYS_MS.slice(0, 2));
|
|
expect(reconnectRemote).toHaveBeenCalledTimes(2);
|
|
expect(notify).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("reports a takeover without automatically disconnecting the other window in return", async () => {
|
|
lose(true);
|
|
lose(); // A subsequent transport-loss event must not turn a takeover into a retry.
|
|
await settle();
|
|
expect(waits).toEqual([]);
|
|
expect(reconnectRemote).not.toHaveBeenCalled();
|
|
expect(notify).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
level: "warning",
|
|
title: "Another window took over this remote session",
|
|
body: "Your project is still running on box. This window will not reconnect automatically.",
|
|
}),
|
|
);
|
|
confirm.mockResolvedValueOnce(false);
|
|
await reconnectNow();
|
|
expect(confirm).toHaveBeenCalledOnce();
|
|
expect(reconnectRemote).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("keeps a restored, displaced window disconnected until an explicit, confirmed reconnect", async () => {
|
|
enableRemote({ ...session, takenOver: true });
|
|
expect(getContext().remoteConnectionLost).toBe(true);
|
|
expect(notify).toHaveBeenCalledOnce();
|
|
lose();
|
|
await settle();
|
|
expect(waits).toEqual([]);
|
|
expect(reconnectRemote).not.toHaveBeenCalled();
|
|
reconnectRemote.mockResolvedValueOnce();
|
|
await reconnectNow();
|
|
expect(confirm).toHaveBeenCalledOnce();
|
|
expect(reconnectRemote).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("offers Reconnect once every attempt failed", async () => {
|
|
reconnectRemote.mockRejectedValue(new Error("Connection refused"));
|
|
lose();
|
|
await settle();
|
|
expect(reconnectRemote).toHaveBeenCalledTimes(RETRY_DELAYS_MS.length);
|
|
expect(notify).toHaveBeenCalledOnce();
|
|
const n = notify.mock.calls[0]![0];
|
|
expect(n).toMatchObject({ level: "error", title: "Lost the connection to box", body: "Connection refused" });
|
|
|
|
reconnectRemote.mockResolvedValueOnce();
|
|
n.actions[0].run();
|
|
await settle();
|
|
expect(reconnectRemote).toHaveBeenCalledTimes(RETRY_DELAYS_MS.length + 1);
|
|
expect(confirm).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("leaves the reload to the user while edits are unsaved, and asks first", async () => {
|
|
dirty.ids = [3, 4];
|
|
lose();
|
|
await settle();
|
|
expect(reconnectRemote).not.toHaveBeenCalled();
|
|
expect(notify).toHaveBeenCalledWith(expect.objectContaining({ title: "Lost the connection to box" }));
|
|
|
|
confirm.mockResolvedValueOnce(false);
|
|
await reconnectNow();
|
|
expect(confirm).toHaveBeenCalledOnce();
|
|
expect(reconnectRemote).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("stops reconnecting on its own once a file is edited while it waits", async () => {
|
|
uninstall();
|
|
reconnectRemote.mockRejectedValueOnce(new Error("Network is unreachable"));
|
|
uninstall = installReconnect(async (ms) => {
|
|
waits.push(ms);
|
|
// Edited during the second wait.
|
|
if (waits.length === 2) dirty.ids = [7];
|
|
});
|
|
lose();
|
|
await settle();
|
|
expect(reconnectRemote).toHaveBeenCalledOnce();
|
|
expect(notify).toHaveBeenCalledWith(expect.objectContaining({ title: "Lost the connection to box" }));
|
|
});
|
|
|
|
it("ignores a lost connection of a window that is no longer remote", async () => {
|
|
useRemote.setState({ session: null });
|
|
lose();
|
|
await settle();
|
|
expect(useRemote.getState().lost).toBeNull();
|
|
expect(reconnectRemote).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("auto-reconnects after a long Wi-Fi outage on browser online or passive poll unless dirty or taken over", async () => {
|
|
vi.useFakeTimers();
|
|
try {
|
|
uninstall();
|
|
reconnectRemote.mockRejectedValue(new Error("Network down"));
|
|
uninstall = installReconnect(async (ms) => {
|
|
waits.push(ms);
|
|
});
|
|
lose();
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(reconnectRemote).toHaveBeenCalledTimes(RETRY_DELAYS_MS.length);
|
|
expect(notify).toHaveBeenCalledOnce();
|
|
|
|
// Unsaved edits block automatic passive reconnect on `online`.
|
|
dirty.ids = [1];
|
|
window.dispatchEvent(new Event("online"));
|
|
expect(reconnectRemote).toHaveBeenCalledTimes(RETRY_DELAYS_MS.length);
|
|
|
|
// Clean state: passive 60s timer tries once, then `online` succeeds when Wi-Fi returns!
|
|
dirty.ids = [];
|
|
await vi.advanceTimersByTimeAsync(PASSIVE_RECONNECT_MS);
|
|
expect(reconnectRemote).toHaveBeenCalledTimes(RETRY_DELAYS_MS.length + 1);
|
|
|
|
reconnectRemote.mockImplementationOnce(async () => {
|
|
useRemote.setState({ lost: null, reconnecting: false });
|
|
});
|
|
window.dispatchEvent(new Event("online"));
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(reconnectRemote).toHaveBeenCalledTimes(RETRY_DELAYS_MS.length + 2);
|
|
expect(useRemote.getState().lost).toBeNull();
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("reports an OOM-killed dev container in plain words with a link to its config and skips auto-retry", async () => {
|
|
useRemote.setState({
|
|
session: {
|
|
target: { kind: "devContainer", folder: "/Users/me/app", config: "" },
|
|
root: "/workspaces/app",
|
|
host: "app",
|
|
os: "linux",
|
|
commands: [],
|
|
},
|
|
lost: null,
|
|
takenOver: false,
|
|
reconnecting: false,
|
|
});
|
|
|
|
listeners.get("remoteConnectionLost")!({
|
|
host: "app",
|
|
takenOver: false,
|
|
oomMessage: "The dev container ran out of its 4 GB memory limit.",
|
|
oomConfigPath: "/Users/me/app/.devcontainer/devcontainer.json",
|
|
});
|
|
await settle();
|
|
|
|
expect(waits).toEqual([]);
|
|
expect(reconnectRemote).not.toHaveBeenCalled();
|
|
expect(notify).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
level: "error",
|
|
title: "Dev container ran out of memory",
|
|
body: "The dev container ran out of its 4 GB memory limit.",
|
|
actions: expect.arrayContaining([
|
|
expect.objectContaining({ label: "Open Configuration" }),
|
|
expect.objectContaining({ label: "Open Services" }),
|
|
expect.objectContaining({ label: "Reconnect" }),
|
|
]),
|
|
}),
|
|
);
|
|
});
|