Файли
openidea/src/workbench/webSessionGuard.test.ts
T
Luis Felipeтаluisfelipe18 9f643af069 feat(web): openidea-server --web serves the IDE to a browser (#5134)
`openidea-server --web` serves the built frontend and the IDE backend over one HTTP + WebSocket port, directly or behind a reverse proxy that strips or keeps its path prefix.

- The rpc dispatcher gains an async `invoke` with a channel sink, so project-host commands, streaming channels and IDE events run over WebSockets as they do over `--stdio`.
- Token auth is on by default (a random token unless `--token` or `--no-auth`), binds 127.0.0.1 by default, and WebSocket upgrades must be same-origin.
- The browser serves the desktop shell commands with the trust classes the desktop declares (a test keeps them in step), and the desktop menu spec, now in `ide_host::menu`. Recent-project recording and project rename are shared ide-host ops.
- WebSocket output is queued without dropping channel data or events. A reloaded page gets back the project its window still has open; after a server restart the page reloads. One tab per window, with Take Over.
- Browser fallbacks for the title bar menu, path chooser, clipboard, dialogs, Settings and external links; a reconnecting banner and an unsaved-changes guard.
- `scripts/start-web.sh` and the guide (English and Chinese).

Co-authored-by: luisfelipe18 <[email protected]>
2026-11-26 23:00:28 +08:00

93 рядки
3.0 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { useWebConnection } from "@/platform/webBridge";
import * as contributions from "./contributions";
import { installWebSessionGuard } from "./webSessionGuard";
function fireBeforeUnload() {
const event = new Event("beforeunload", { cancelable: true }) as BeforeUnloadEvent;
window.dispatchEvent(event);
return event;
}
describe("webSessionGuard", () => {
let dispose: () => void;
beforeEach(() => {
useWebConnection.setState({
active: false,
status: "idle",
windowLabel: "main",
serverHome: "",
serverOs: "linux",
});
dispose = installWebSessionGuard();
});
afterEach(() => {
dispose();
vi.restoreAllMocks();
useWebConnection.setState({
active: false,
status: "idle",
windowLabel: "main",
serverHome: "",
serverOs: "linux",
});
});
it("is inactive outside web mode even when documents are dirty", () => {
vi.spyOn(contributions, "maybeEditorService").mockReturnValue({
dirtyDocumentIds: () => [1],
untitledDirtyDocumentIds: () => [2],
} as unknown as contributions.EditorService);
const event = fireBeforeUnload();
expect(event.defaultPrevented).toBe(false);
});
it("does not prevent unload in web mode when all documents are clean", () => {
useWebConnection.setState({ active: true, status: "connected" });
vi.spyOn(contributions, "maybeEditorService").mockReturnValue({
dirtyDocumentIds: () => [],
untitledDirtyDocumentIds: () => [],
} as unknown as contributions.EditorService);
const event = fireBeforeUnload();
expect(event.defaultPrevented).toBe(false);
});
it("prevents unload in web mode when dirtyDocumentIds is non-empty", () => {
useWebConnection.setState({ active: true, status: "connected" });
vi.spyOn(contributions, "maybeEditorService").mockReturnValue({
dirtyDocumentIds: () => [42],
untitledDirtyDocumentIds: () => [],
} as unknown as contributions.EditorService);
const event = fireBeforeUnload();
expect(event.defaultPrevented).toBe(true);
});
it("prevents unload in web mode when untitledDirtyDocumentIds is non-empty", () => {
useWebConnection.setState({ active: true, status: "connected" });
vi.spyOn(contributions, "maybeEditorService").mockReturnValue({
dirtyDocumentIds: () => [],
untitledDirtyDocumentIds: () => [7],
} as unknown as contributions.EditorService);
const event = fireBeforeUnload();
expect(event.defaultPrevented).toBe(true);
});
it("removes the beforeunload listener on dispose", () => {
useWebConnection.setState({ active: true, status: "connected" });
vi.spyOn(contributions, "maybeEditorService").mockReturnValue({
dirtyDocumentIds: () => [1],
untitledDirtyDocumentIds: () => [],
} as unknown as contributions.EditorService);
dispose();
const event = fireBeforeUnload();
expect(event.defaultPrevented).toBe(false);
});
});