feat: rebuild workspaces as the reference plugin

This commit is contained in:
LukeGus committed 2026-09-23 07:04:03 -05:00
1 parent d774df8b41
commit f217d8db89
79 files changed
+32995 -2184

No files matched your search

@@ -1,4 +1,6 @@
import { afterEach, describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { fireEvent, screen, waitFor } from "@testing-library/react";
import type { PluginApiClient } from "@termix/plugin-sdk/frontend";
import {
renderWithApp,
type RenderedPluginApp,
@@ -51,3 +53,164 @@ describe(`${manifest.id} activate`, () => {
expect(app.registered.actions()).toEqual([]);
});
});
function workspace(overrides: Record<string, unknown> = {}) {
return {
id: 1,
userId: "user-1",
name: "Prod",
color: "#ef4444",
icon: null,
kind: "manual",
isDefault: false,
payload: {
version: 1,
tabs: [{ slotId: "s1", type: "network_graph", label: "Graph" }],
activeSlotId: "s1",
splitMode: "none",
paneTabIds: [],
rowSizes: [],
rowColSizes: [],
},
syncId: "sync-1",
createdAt: "2026-01-01T00:00:00.000Z",
updatedAt: "2026-01-01T00:00:00.000Z",
lastUsedAt: null,
tabCount: 1,
...overrides,
};
}
function stubApi(workspaces: unknown[]) {
return {
get: vi.fn(async () => ({ data: workspaces })),
post: vi.fn(async () => ({ data: workspaces[0] })),
put: vi.fn(async () => ({ data: {} })),
patch: vi.fn(async () => ({ data: {} })),
delete: vi.fn(async () => ({ data: { success: true } })),
};
}
async function applyThroughPanel(app: RenderedPluginApp, name: string) {
app.renderPanel("workspaces", { active: true });
fireEvent.click(await screen.findByText(name));
fireEvent.click(
await screen.findByText("Apply Workspace", { selector: "button" }),
);
await waitFor(() =>
expect(app.shellCalls.some((call) => call.method === "applyLayout")).toBe(
true,
),
);
}
describe("workspaces panel", () => {
it("gates its rail item on workspaces.use", async () => {
rendered = await renderWithApp(plugin, { manifest, locales });
expect(rendered.registered.railItems()).toEqual([
expect.objectContaining({
id: "workspaces",
permission: "workspaces.use",
}),
]);
});
it("lists the user's workspaces", async () => {
const api = stubApi([
workspace(),
workspace({ id: 2, name: "Last Session", kind: "last_session" }),
]);
rendered = await renderWithApp(plugin, {
manifest,
locales,
api: api as unknown as PluginApiClient,
});
rendered.renderPanel("workspaces", { active: true });
expect(await screen.findByText("Prod")).toBeTruthy();
expect(
screen.getByText(locales.newUi.sidebar.workspaces.lastSession),
).toBeTruthy();
expect(api.get).toHaveBeenCalledWith("/");
});
it("applies a workspace to the tabs and marks it used", async () => {
const saved = workspace();
const api = stubApi([saved]);
rendered = await renderWithApp(plugin, {
manifest,
locales,
api: api as unknown as PluginApiClient,
});
await applyThroughPanel(rendered, "Prod");
const call = rendered.shellCalls.find((c) => c.method === "applyLayout");
expect(call?.args[0]).toEqual(saved.payload);
expect(call?.args[1]).toEqual({ name: "Prod" });
await waitFor(() => expect(api.post).toHaveBeenCalledWith("/1/apply"));
});
it("keeps a tab from a plugin that is not running and shows its placeholder", async () => {
const saved = workspace({
payload: {
version: 1,
tabs: [{ slotId: "s1", type: "not-installed-view", label: "Gone" }],
activeSlotId: "s1",
splitMode: "none",
paneTabIds: [],
rowSizes: [],
rowColSizes: [],
},
});
rendered = await renderWithApp(plugin, {
manifest,
locales,
api: stubApi([saved]) as unknown as PluginApiClient,
});
await applyThroughPanel(rendered, "Prod");
expect(rendered.openedTabs()).toEqual([
{ type: "not-installed-view", hostId: undefined, label: "Gone" },
]);
const tab = rendered.renderOpenedTab(0);
expect(
tab.querySelector('[data-testid="plugin-view-placeholder"]'),
).not.toBeNull();
});
it("restores the default workspace once the shell is ready", async () => {
const saved = workspace({ isDefault: true });
rendered = await renderWithApp(plugin, {
manifest,
locales,
permissions: ["workspaces.use"],
ready: true,
api: stubApi([saved]) as unknown as PluginApiClient,
});
await waitFor(() =>
expect(
rendered!.shellCalls.find((c) => c.method === "applyLayout")?.args[0],
).toEqual(saved.payload),
);
});
it("does not restore or autosave for a user without workspaces.use", async () => {
const api = stubApi([workspace({ isDefault: true })]);
rendered = await renderWithApp(plugin, {
manifest,
locales,
permissions: [],
ready: true,
api: api as unknown as PluginApiClient,
});
await new Promise((resolve) => setTimeout(resolve, 20));
expect(api.get).not.toHaveBeenCalled();
expect(rendered.shellCalls.some((c) => c.method === "applyLayout")).toBe(
false,
);
});
});
@@ -1,133 +1,57 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
import { describe, expect, it, vi } from "vitest";
import type { PluginApiClient } from "@termix/plugin-sdk/frontend";
import { createWorkspacesApi } from "../../src/frontend/workspaces-api";
const authApiMock = vi.hoisted(() => ({
get: vi.fn(async () => ({ data: [] })),
post: vi.fn(async () => ({ data: {} })),
patch: vi.fn(async () => ({ data: {} })),
put: vi.fn(async () => ({ data: {} })),
delete: vi.fn(async () => ({ data: { success: true } })),
}));
function fakeClient() {
const reply = (data: unknown) => vi.fn(async () => ({ data }));
return {
get: reply([]),
post: reply({ id: 1 }),
put: reply({ id: 1 }),
patch: reply({ id: 1 }),
delete: reply({ success: true }),
};
}
vi.mock("@/main-axios", () => ({
authApi: authApiMock,
handleApiError: (error: unknown) => error,
}));
const layout = { version: 1, tabs: [] };
import {
listWorkspaces,
createWorkspace,
renameWorkspace,
updateWorkspaceContent,
deleteWorkspace,
duplicateWorkspace,
setDefaultWorkspace,
applyWorkspaceServer,
getLastSessionWorkspace,
saveLastSessionWorkspace,
} from "../../src/frontend/workspaces-api";
import type { WorkspacePayload } from "@/types/ui-types";
describe("workspaces api", () => {
it("calls each route relative to the plugin's own mount point", async () => {
const client = fakeClient();
const api = createWorkspacesApi(client as unknown as PluginApiClient);
beforeEach(() => {
authApiMock.get.mockClear();
authApiMock.post.mockClear();
authApiMock.patch.mockClear();
authApiMock.put.mockClear();
authApiMock.delete.mockClear();
});
await api.list();
await api.create({ name: "A", color: "#fff", payload: layout });
await api.rename(3, { name: "B" });
await api.updateContent(3, layout);
await api.remove(3);
await api.duplicate(3, "C");
await api.setDefault(3);
await api.unsetDefault(3);
await api.apply(3);
await api.getLastSession();
await api.saveLastSession(layout);
const samplePayload: WorkspacePayload = {
version: 1,
tabs: [],
activeSlotId: null,
splitMode: "none",
paneTabIds: [null, null, null, null, null, null],
rowSizes: [100],
rowColSizes: [[100]],
};
describe("workspaces-api", () => {
it("listWorkspaces GETs /plugin-api/workspaces", async () => {
authApiMock.get.mockResolvedValueOnce({ data: [{ id: 1 }] });
const result = await listWorkspaces();
expect(authApiMock.get).toHaveBeenCalledWith("/plugin-api/workspaces");
expect(result).toEqual([{ id: 1 }]);
expect(client.get.mock.calls).toEqual([["/"], ["/last-session"]]);
expect(client.post.mock.calls).toEqual([
["/", { name: "A", color: "#fff", payload: layout }],
["/3/duplicate", { name: "C" }],
["/3/set-default"],
["/3/unset-default"],
["/3/apply"],
]);
expect(client.patch.mock.calls).toEqual([["/3", { name: "B" }]]);
expect(client.put.mock.calls).toEqual([
["/3/content", { payload: layout }],
["/last-session", { payload: layout }],
]);
expect(client.delete.mock.calls).toEqual([["/3"]]);
});
it("createWorkspace POSTs /plugin-api/workspaces with the payload", async () => {
await createWorkspace({
name: "Test A",
color: "#fff",
payload: samplePayload,
});
expect(authApiMock.post).toHaveBeenCalledWith("/plugin-api/workspaces", {
name: "Test A",
color: "#fff",
payload: samplePayload,
});
});
it("renameWorkspace PATCHes /plugin-api/workspaces/:id", async () => {
await renameWorkspace(5, { name: "New Name" });
expect(authApiMock.patch).toHaveBeenCalledWith("/plugin-api/workspaces/5", {
name: "New Name",
});
});
it("updateWorkspaceContent PUTs /plugin-api/workspaces/:id/content", async () => {
await updateWorkspaceContent(5, samplePayload);
expect(authApiMock.put).toHaveBeenCalledWith(
"/plugin-api/workspaces/5/content",
{
payload: samplePayload,
},
);
});
it("deleteWorkspace DELETEs /plugin-api/workspaces/:id", async () => {
await deleteWorkspace(5);
expect(authApiMock.delete).toHaveBeenCalledWith("/plugin-api/workspaces/5");
});
it("duplicateWorkspace POSTs /plugin-api/workspaces/:id/duplicate with a name", async () => {
await duplicateWorkspace(5, "Test A (copy)");
expect(authApiMock.post).toHaveBeenCalledWith(
"/plugin-api/workspaces/5/duplicate",
{
name: "Test A (copy)",
},
);
});
it("setDefaultWorkspace POSTs /plugin-api/workspaces/:id/set-default", async () => {
await setDefaultWorkspace(5);
expect(authApiMock.post).toHaveBeenCalledWith(
"/plugin-api/workspaces/5/set-default",
);
});
it("applyWorkspaceServer POSTs /plugin-api/workspaces/:id/apply", async () => {
await applyWorkspaceServer(5);
expect(authApiMock.post).toHaveBeenCalledWith(
"/plugin-api/workspaces/5/apply",
);
});
it("getLastSessionWorkspace GETs /plugin-api/workspaces/last-session", async () => {
authApiMock.get.mockResolvedValueOnce({ data: null });
const result = await getLastSessionWorkspace();
expect(authApiMock.get).toHaveBeenCalledWith(
"/plugin-api/workspaces/last-session",
);
expect(result).toBeNull();
});
it("saveLastSessionWorkspace PUTs /plugin-api/workspaces/last-session", async () => {
await saveLastSessionWorkspace(samplePayload);
expect(authApiMock.put).toHaveBeenCalledWith(
"/plugin-api/workspaces/last-session",
{
payload: samplePayload,
},
);
it("returns the response body", async () => {
const client = fakeClient();
client.get = vi.fn(async () => ({ data: [{ id: 7 }] }));
const api = createWorkspacesApi(client as unknown as PluginApiClient);
expect(await api.list()).toEqual([{ id: 7 }]);
});
});