feat: move session sharing and collab rooms into a plugin (B12)

This commit is contained in:
LukeGus committed 2026-09-23 23:42:46 -05:00
1 parent 791d8656a1
commit a276f6898c
207 files changed
+35771 -11072

No files matched your search

@@ -0,0 +1,113 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import type { ComponentType } from "react";
import type { PluginApiClient } from "@termix/plugin-sdk/frontend";
import {
renderWithApp,
type RenderedPluginApp,
} from "@termix/plugin-sdk/testing";
import { PluginComponent } from "@termix/plugin-sdk/ui";
import type { PluginManifest } from "@termix/plugin-sdk/manifest";
import * as plugin from "../../src/frontend/index";
import SharedSessionView from "../../src/frontend/SharedSessionView";
import manifestJson from "../../manifest.json";
import locales from "../../locales/en.json";
vi.mock("react-xtermjs", () => ({
useXTerm: () => ({
instance: {
options: {},
loadAddon: vi.fn(),
open: vi.fn(),
write: vi.fn(),
onData: vi.fn(),
},
ref: { current: document.createElement("div") },
}),
}));
vi.mock("@xterm/addon-fit", () => ({
FitAddon: class FitAddon {
fit() {}
},
}));
const manifest = manifestJson as unknown as PluginManifest;
const guestView = locales.sessionSharing.guestView;
let rendered: RenderedPluginApp | null = null;
afterEach(async () => {
await rendered?.deactivate();
rendered = null;
});
function httpError(status: number) {
return Object.assign(new Error(`HTTP ${status}`), { response: { status } });
}
/** The shared-link page, rendered as a guest inside the plugin's scope. */
async function renderShared(search: string, get: PluginApiClient["get"]) {
window.history.pushState({}, "", `/?${search}`);
rendered = await renderWithApp(plugin, {
manifest,
locales,
guest: true,
api: { get } as unknown as PluginApiClient,
});
rendered.app.registerComponent(
"session-sharing.testSharedView",
SharedSessionView as ComponentType<Record<string, unknown>>,
);
rendered.app.registerSlotContribution("session.remoteDisplay", {
actionId: "fixture.display",
titleKey: "nav.collab",
kind: "component",
component: () => <div data-testid="remote-display" />,
});
render(<PluginComponent id="session-sharing.testSharedView" />);
}
describe("SharedSessionView", () => {
it("says the link is invalid when there is no token", async () => {
const get = vi.fn();
await renderShared("view=shared", get as never);
expect(await screen.findByText(guestView.linkInvalid)).toBeTruthy();
expect(get).not.toHaveBeenCalled();
});
it("says the link is invalid for a revoked or expired link", async () => {
await renderShared(
"view=shared&token=gone",
vi.fn(async () => {
throw httpError(404);
}) as never,
);
expect(await screen.findByText(guestView.linkInvalid)).toBeTruthy();
});
it("asks the guest to wait when rate limited", async () => {
await renderShared(
"view=shared&token=busy",
vi.fn(async () => {
throw httpError(429);
}) as never,
);
expect(await screen.findByText(guestView.rateLimited)).toBeTruthy();
});
it("draws a remote desktop share through the display slot", async () => {
const get = vi.fn(async () => ({
data: {
protocol: "rdp",
permissionLevel: "read-only",
wsPath: "/plugin-ws/remote-desktop/display",
connectParams: { token: "t" },
},
}));
await renderShared("view=shared&token=ok", get as never);
expect(await screen.findByTestId("remote-display")).toBeTruthy();
expect(screen.getByText(guestView.readOnlyBadge)).toBeTruthy();
expect(get).toHaveBeenCalledWith("/resolve/ok");
});
});
@@ -0,0 +1,196 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { act, fireEvent, screen, waitFor } from "@testing-library/react";
import {
invokeAction,
type PluginApiClient,
} from "@termix/plugin-sdk/frontend";
import {
renderWithApp,
type RenderedPluginApp,
} from "@termix/plugin-sdk/testing";
import type { PluginManifest } from "@termix/plugin-sdk/manifest";
import * as plugin from "../../src/frontend/index";
import manifestJson from "../../manifest.json";
import locales from "../../locales/en.json";
const manifest = manifestJson as unknown as PluginManifest;
let rendered: RenderedPluginApp | null = null;
afterEach(async () => {
await rendered?.deactivate();
rendered = null;
});
function stubApi(overrides: Partial<PluginApiClient> = {}): PluginApiClient {
return {
get: vi.fn(async (url: string) => ({
data:
url === "/directory"
? { users: [{ id: "bob", username: "bob" }], roles: [] }
: url.includes("/active")
? { shares: [] }
: url === "/rooms"
? { rooms: [] }
: {},
})),
post: vi.fn(async () => ({
data: { shareId: "s1", linkToken: "tok", expiresAt: "2999-01-01" },
})),
put: vi.fn(async () => ({ data: {} })),
patch: vi.fn(async () => ({ data: {} })),
delete: vi.fn(async () => ({ data: {} })),
...overrides,
} as PluginApiClient;
}
describe("session-sharing activate", () => {
it("registers the rooms rail item, panel and tab, and the share buttons", async () => {
rendered = await renderWithApp(plugin, {
manifest,
locales,
api: stubApi(),
});
expect(rendered.registered.railItems()).toEqual([
expect.objectContaining({
id: "collab",
permission: "session-sharing.use",
}),
]);
expect(rendered.registered.panels()).toEqual(["collab"]);
expect(rendered.registered.tabs()).toEqual(["collab"]);
expect(rendered.registered.slot("terminal.toolbar")).toEqual([
"session-sharing.share",
]);
expect(rendered.registered.slot("remote-desktop.toolbar")).toEqual([
"session-sharing.share",
]);
expect(rendered.registered.slot("shell.overlay")).toHaveLength(2);
expect(rendered.registered.actions().sort()).toEqual([
"session-sharing.share",
"sessions.sharedWithMe",
]);
});
it("registers only the guest views on a guest page", async () => {
rendered = await renderWithApp(plugin, {
manifest,
locales,
api: stubApi(),
guest: true,
});
expect(rendered.registered.tabs()).toEqual(["collab"]);
expect(rendered.registered.railItems()).toEqual([]);
expect(rendered.registered.actions()).toEqual([]);
});
it("opens the share dialog for a terminal's session", async () => {
const api = stubApi();
rendered = await renderWithApp(plugin, { manifest, locales, api });
rendered.renderSlot("shell.overlay");
await act(() =>
invokeAction("session-sharing.share", {
getShareTarget: () => ({
hostId: 4,
sessionId: "sess-9",
protocol: "ssh",
}),
}),
);
expect(
await screen.findByText(locales.sessionSharing.modalTitle),
).toBeTruthy();
await waitFor(() => expect(api.get).toHaveBeenCalledWith("/host/4/active"));
});
it("opens the dialog for a remote desktop session once it is up", async () => {
rendered = await renderWithApp(plugin, {
manifest,
locales,
api: stubApi(),
});
rendered.renderSlot("shell.overlay");
await act(() =>
invokeAction("session-sharing.share", {
hostId: 2,
sessionId: null,
protocol: "rdp",
}),
);
expect(screen.queryByText(locales.sessionSharing.modalTitle)).toBeNull();
await act(() =>
invokeAction("session-sharing.share", {
hostId: 2,
sessionId: "guac-1",
protocol: "rdp",
}),
);
expect(
await screen.findByText(locales.sessionSharing.modalTitle),
).toBeTruthy();
});
it("creates a link share from the dialog", async () => {
const api = stubApi();
rendered = await renderWithApp(plugin, { manifest, locales, api });
rendered.renderSlot("shell.overlay");
await act(() =>
invokeAction("session-sharing.share", {
getShareTarget: () => ({
hostId: 1,
sessionId: "sess-1",
protocol: "ssh",
}),
}),
);
fireEvent.click(
await screen.findByRole("button", {
name: locales.sessionSharing.createLinkButton,
}),
);
await waitFor(() =>
expect(api.post).toHaveBeenCalledWith(
"/create",
expect.objectContaining({
hostId: 1,
sessionId: "sess-1",
shareType: "link",
permissionLevel: "read-only",
}),
),
);
});
it("answers sessions shared with the user through the api", async () => {
const shared = [{ sessionId: "s", isOwnSession: false }];
rendered = await renderWithApp(plugin, {
manifest,
locales,
api: stubApi({
get: vi.fn(async () => ({ data: shared })) as never,
}),
});
await expect(invokeAction("sessions.sharedWithMe")).resolves.toEqual(
shared,
);
});
it("removes everything it registered on deactivate", async () => {
const app = await renderWithApp(plugin, {
manifest,
locales,
api: stubApi(),
});
await app.deactivate();
expect(app.registered.tabs()).toEqual([]);
expect(app.registered.panels()).toEqual([]);
expect(app.registered.slot("terminal.toolbar")).toEqual([]);
expect(app.registered.actions()).toEqual([]);
});
});