penpot/mcp/packages/server/src/PluginBridge.sessions.test.ts
Dominik Jain be17fabc6a 🎉 Introduce global plugins and move MCP to application scope
Global plugin lifecycle:
- Add optional manifest scope "global" for plugins that remain active
  across dashboard and workspace navigation within one browser tab.
  Existing plugins retain workspace scope by default.
- Preserve global sandboxes when a workspace ends or another plugin
  loads, including when that other plugin fails to initialize.
- Avoid duplicate instances of an already-running global plugin.
- Close global plugins on explicit closure or logout, with idempotent
  cleanup of listeners, timers and UI resources. Global scope does not
  imply automatic startup or persistence across a browser reload.

Plugin API and navigation:
- Keep a permanently live penpot facade whose getters follow the current
  workspace, while user, theme and UI facilities remain available on
  the dashboard.
- Expose penpotMgmt to global plugins with workspace metadata,
  none/loading/ready status and workspacechange subscriptions.
- Add awaitable openFile(fileId, {teamId}) navigation in the current tab,
  defaulting to the current team and resolving only once the requested
  file and its active page are ready.
- Reject invalid identifiers, failed or superseded navigation and waits
  exceeding 30 seconds; release navigation watchers on completion.
- Guard workspace-bound operations while no workspace is ready and
  return null from root, currentFile and currentPage in that state.
- Require content:read for the initial management API. Callers must
  reacquire file, page and shape references after changing files.

MCP lifecycle and connection controls:
- Make MCP a global plugin and initialize its integrated controller per
  authenticated application session instead of per workspace.
- Load the enabled integration with a valid token on the dashboard as
  well as in a file, retaining its existing hidden-iframe implementation.
- Keep enabling and connecting separate: an enabled plugin starts idle,
  and each tab connects explicitly through dashboard or workspace
  controls.
- Preserve the iframe, WebSocket, heartbeat and reconnect machinery
  during navigation; disconnect on request and close on disable/logout.

MCP sessions and execution:
- Derive the short MCP session ID solely from currentUser.sessionId,
  excluding the file ID. Retain the SHA-256-based, 50-bit Base32 encoding
  to produce a stable, copyable 10-character identifier.
- Allow sessions without an open file and update workspace metadata over
  the existing connection without replacing its routing identity.
- Refresh context after the connection handshake so initialization does
  not leave stale file metadata.
- Expose penpotMgmt to execute_code, preserve execution storage across
  navigation and skip layout waits when no workspace is ready.
- Update API types, tool guidance, documentation and the changelog.

Validation:
- Cover authenticated startup, workspace survival, logout cleanup,
  readiness, navigation failure, duplicate loads and session routing.
- Verify that short IDs remain stable across files and reconnects and
  that disconnect cancels pending asynchronous ID initialization.
- Pass 16 focused frontend tests, 73 runtime tests, 30 MCP plugin tests,
  builds, TypeScript checks and frontend/runtime lint.
- Pass all 73 MCP server tests with --test-force-exit; the normal runner
  still stays alive after assertions finish.
- Verify a live dashboard-to-file-to-dashboard round trip without a
  document or iframe reload, preserving facade identity and storage.

AI-assisted-by: gpt-6
2026-10-07 14:58:32 +02:00

172 lines
7.1 KiB
TypeScript

import assert from "node:assert/strict";
import { once } from "node:events";
import { afterEach, beforeEach, test } from "node:test";
import { WebSocket } from "ws";
import { PluginBridge } from "./PluginBridge";
import { PluginTask } from "./PluginTask";
import type { PenpotMcpServer } from "./PenpotMcpServer";
let bridge: PluginBridge;
let port = 17_600;
let userToken: string | undefined;
let multiUser: boolean;
let sockets: WebSocket[];
beforeEach(() => {
port++;
userToken = "alice";
multiUser = true;
sockets = [];
bridge = new PluginBridge(
{
host: "127.0.0.1",
isMultiUserMode: () => multiUser,
getSessionContext: () => ({ userToken }),
} as PenpotMcpServer,
port,
1
);
});
afterEach(async () => {
for (const socket of sockets) socket.terminate();
await bridge.close();
});
async function connect(sessionId: string, token = "alice", fileName = "Design", reply = sessionId) {
const socket = new WebSocket(`ws://127.0.0.1:${port}?userToken=${token}`);
sockets.push(socket);
await once(socket, "open");
const initialized = once(socket, "message", { signal: AbortSignal.timeout(1500) });
socket.send(JSON.stringify({ type: "initialize", session: { sessionId, fileId: "file-1", fileName } }));
const [message] = await initialized;
assert.equal(JSON.parse(message.toString()).type, "initialized");
socket.on("message", (raw) => {
const request = JSON.parse(raw.toString());
socket.send(JSON.stringify({ id: request.id, success: true, data: reply }));
});
return socket;
}
test("keeps two tabs for the same file and dispatches to the selected session", async () => {
await connect("tab-1");
await connect("tab-2");
const result = await bridge.executePluginTask(new PluginTask("test", {}), "tab-2");
assert.equal(result.data, "tab-2");
});
test("requires user selection when more than one session is connected", async () => {
await connect("tab-1", "alice", "First design");
await connect("tab-2", "alice", "Second design");
await assert.rejects(bridge.executePluginTask(new PluginTask("test", {})), (error: Error) => {
assert.match(error.message, /Ask the user/);
assert.match(error.message, /tab-1.*First design/);
assert.match(error.message, /tab-2.*Second design/);
return true;
});
});
test("implicitly selects the only session belonging to the requesting user", async () => {
await connect("alice-tab");
await connect("bob-tab", "bob");
const result = await bridge.executePluginTask(new PluginTask("test", {}));
assert.equal(result.data, "alice-tab");
});
test("does not dispatch to another user's session", async () => {
await connect("alice-tab");
await connect("bob-tab", "bob");
await assert.rejects(bridge.executePluginTask(new PluginTask("test", {}), "bob-tab"), /not connected/);
});
test("reports no sessions when no plugin has initialized", async () => {
await assert.rejects(bridge.executePluginTask(new PluginTask("test", {})), /No Penpot.*connected/);
});
test("regular plugins without user tokens can select among multiple sessions", async () => {
multiUser = false;
await connect("first", "");
await connect("second", "");
await assert.rejects(bridge.executePluginTask(new PluginTask("test", {})), /Ask the user/);
assert.equal((await bridge.executePluginTask(new PluginTask("test", {}), "second")).data, "second");
});
test("disconnecting one session leaves its sibling available for implicit selection", async () => {
const first = await connect("first");
await connect("second");
const closed = once(first, "close");
first.close();
await closed;
assert.equal((await bridge.executePluginTask(new PluginTask("test", {}))).data, "second");
await assert.rejects(bridge.executePluginTask(new PluginTask("test", {}), "first"), /not connected/);
});
test("a reconnect with the same session ID replaces the previous connection", async () => {
const previous = await connect("tab", "alice", "Design", "previous");
const closed = once(previous, "close");
await connect("tab", "alice", "Design", "current");
const [code] = await closed;
assert.equal(code, 1008);
assert.equal((await bridge.executePluginTask(new PluginTask("test", {}))).data, "current");
assert.equal((await bridge.executePluginTask(new PluginTask("test", {}), "tab")).data, "current");
});
test("a half-open previous connection does not block a reconnect with the same session ID", async () => {
const previous = await connect("tab", "alice", "Design", "previous");
// stop answering, as a socket whose network path is gone would
previous.pause();
await connect("tab", "alice", "Design", "current");
assert.equal((await bridge.executePluginTask(new PluginTask("test", {}), "tab")).data, "current");
});
test("replacing a session does not affect another user's session with the same ID", async () => {
await connect("tab", "bob", "Design", "bob");
await connect("tab", "alice", "Design", "alice");
userToken = "bob";
assert.equal((await bridge.executePluginTask(new PluginTask("test", {}), "tab")).data, "bob");
});
test("uninitialized sockets are not candidates for discovery", async () => {
await connect("ready");
const pending = new WebSocket(`ws://127.0.0.1:${port}?userToken=alice`);
sockets.push(pending);
await once(pending, "open");
assert.equal((await bridge.executePluginTask(new PluginTask("test", {}))).data, "ready");
});
test("invalid initialization metadata cannot register a session", async () => {
const socket = new WebSocket(`ws://127.0.0.1:${port}?userToken=alice`);
sockets.push(socket);
await once(socket, "open");
const closed = once(socket, "close");
socket.send(JSON.stringify({ type: "initialize", session: { sessionId: "bad", fileId: 42 } }));
assert.equal((await closed)[0], 1008);
await assert.rejects(bridge.executePluginTask(new PluginTask("test", {})), /No Penpot sessions/);
});
test("a dashboard session remains routable while its workspace metadata changes", async () => {
const socket = new WebSocket(`ws://127.0.0.1:${port}?userToken=alice`);
sockets.push(socket);
await once(socket, "open");
const initialized = once(socket, "message");
socket.send(
JSON.stringify({
type: "initialize",
session: { sessionId: "tab", fileId: null, fileName: null, workspaceState: "none" },
})
);
await initialized;
socket.on("message", (raw) => {
const request = JSON.parse(raw.toString());
socket.send(JSON.stringify({ id: request.id, success: true, data: "ok" }));
});
for (const context of [
{ fileId: "file-1", fileName: "Design", workspaceState: "ready" },
{ fileId: null, fileName: null, workspaceState: "none" },
]) {
socket.send(JSON.stringify({ type: "context-update", context }));
assert.equal((await bridge.executePluginTask(new PluginTask("test", {}), "tab")).data, "ok");
assert.deepEqual(bridge.getUserConnections("alice")!.getSessions(), [{ sessionId: "tab", ...context }]);
}
});