fix(plugin): resolve result-view by UI contribution

This commit is contained in:
jearhe authored and GitHub committed 2026-09-20 01:32:23 +08:00
1 parent dc6c9b5c7c
commit 4f3be8ccfb
14 files changed
+337 -16

No files matched your search

+68
View File
@@ -148,6 +148,68 @@ describe("FrontendPluginRegistry", () => {
expect(views[0]?.contribution.label).toBe("Graph");
});
it("resolves the plugin UI contribution behind a workbench or result-view tab", () => {
const registry = createFrontendPluginRegistry([
installedPlugin("com.example.plugin", [
{ type: "workbench", id: "example.main", label: "Example Workbench", icon: "assets/main.svg" },
{ type: "result-view", id: "example.graph", label: "Graph", icon: "assets/graph.svg" },
]),
]);
const workbench = registry.findUiContribution("com.example.plugin", "example.main");
expect(workbench?.contribution).toMatchObject({ type: "workbench", id: "example.main", label: "Example Workbench", icon: "assets/main.svg" });
// The result-view keeps its own id and display metadata: the plugin UI is
// told which declared contribution the user opened, and it is not a workbench.
const resultView = registry.findUiContribution("com.example.plugin", "example.graph");
expect(resultView?.contribution).toMatchObject({ type: "result-view", id: "example.graph", label: "Graph", icon: "assets/graph.svg" });
});
it("keeps workbench lookups scoped to workbench contributions", () => {
const registry = createFrontendPluginRegistry([
installedPlugin("com.example.plugin", [
{ type: "workbench", id: "example.main", label: "Example Workbench" },
{ type: "result-view", id: "example.graph", label: "Graph" },
]),
]);
// `findWorkbench` answers `host.openWorkbench` and `connection-provider.workbench`:
// a result-view id must never satisfy it.
expect(registry.findWorkbench("com.example.plugin", "example.graph")).toBeUndefined();
expect(registry.findWorkbench("com.example.plugin", "example.main")?.contribution.id).toBe("example.main");
});
it("does not resolve unknown, foreign, or non-UI contributions as plugin UI", () => {
const registry = createFrontendPluginRegistry([
installedPlugin("com.example.plugin", [
{ type: "result-view", id: "example.graph", label: "Graph" },
{ type: "context-menu", id: "example.inspect", label: "Inspect", menu: "connection" },
{ type: "filesystem-provider", id: "example.files", label: "Files", schemes: ["example"] },
]),
installedPlugin("com.example.other", []),
]);
expect(registry.findUiContribution("com.example.plugin", "example.graph")?.contribution.type).toBe("result-view");
expect(registry.findUiContribution("com.example.plugin", "example.missing")).toBeUndefined();
expect(registry.findUiContribution("com.example.other", "example.graph")).toBeUndefined();
// Context menus render natively and filesystem providers own their tab mode.
expect(registry.findUiContribution("com.example.plugin", "example.inspect")).toBeUndefined();
expect(registry.findUiContribution("com.example.plugin", "example.files")).toBeUndefined();
});
it("does not resolve plugin UI contributions of incompatible plugins", () => {
const plugin = installedPlugin("com.example.plugin", [
{ type: "workbench", id: "example.main", label: "Example Workbench" },
{ type: "result-view", id: "example.graph", label: "Graph" },
]);
plugin.compatibility = { compatible: false, errors: ["Unsupported host API"] };
const registry = createFrontendPluginRegistry([plugin]);
expect(registry.findUiContribution("com.example.plugin", "example.main")).toBeUndefined();
expect(registry.findUiContribution("com.example.plugin", "example.graph")).toBeUndefined();
});
it("indexes context-menu contributions per menu surface", () => {
const registry = createFrontendPluginRegistry([
installedPlugin("com.example.plugin", [
@@ -226,6 +288,10 @@ describe("FrontendPluginRegistry", () => {
database_type: "unsafe",
fields: [],
},
// Every contribution the host renders through the plugin UI entrypoint
// resolves its icon asset path the same way.
{ type: "workbench", id: "unsafe.main", label: "Unsafe workbench", icon: "../outside.svg" },
{ type: "result-view", id: "unsafe.graph", label: "Unsafe graph", icon: "/outside.svg" },
]);
plugin.manifest.icon = "\\outside.svg";
@@ -234,6 +300,8 @@ describe("FrontendPluginRegistry", () => {
expect(definition.plugin.manifest.icon).toBeUndefined();
expect(pluginConnectionProviderIcon(entry)).toBeUndefined();
expect(createFrontendPluginRegistry([plugin]).listWorkbenches()[0]?.contribution.icon).toBeUndefined();
expect(createFrontendPluginRegistry([plugin]).listResultViews()[0]?.contribution.icon).toBeUndefined();
});
it("localizes plugin metadata, contributions, and form fields", () => {
+16 -2
View File
@@ -12,6 +12,7 @@ import type {
PluginFormFieldLocalization,
PluginFormFieldValue,
PluginManifestLocalization,
PluginUiContribution,
PluginWorkbenchContribution,
} from "@/types/database";
import { uuid } from "@/lib/common/utils";
@@ -71,6 +72,17 @@ export class FrontendPluginRegistry {
return this.listWorkbenches().find((entry) => entry.plugin.manifest.id === pluginId && entry.contribution.id === contributionId);
}
/**
* Resolve any contribution the plugin UI entrypoint can render, whichever host
* surface opened the tab — a `workbench` opened from the sidebar or a
* `result-view` opened from the query-result toolbar. Lookups stay scoped to
* the renderable contribution types, so an id owned by a native context menu
* or a filesystem provider is not a plugin UI surface.
*/
findUiContribution(pluginId: string, contributionId: string): PluginContributionEntry<PluginUiContribution> | undefined {
return [...this.listWorkbenches(), ...this.listResultViews()].find((entry) => entry.plugin.manifest.id === pluginId && entry.contribution.id === contributionId);
}
private listContributions<T extends PluginContribution["type"]>(type: T): Array<PluginContributionEntry<Extract<PluginContribution, { type: T }>>> {
return this.definitions
.filter((definition) => definition.plugin.compatibility.compatible)
@@ -278,7 +290,9 @@ function localizeContribution(contribution: PluginContribution, localization: Pl
label: localizedRequiredText(action.label, localization?.actions?.[action.id]?.label),
description: localizedOptionalText(action.description, localization?.actions?.[action.id]?.description),
}));
} else if (localized.type === "workbench") {
} else if (localized.type === "workbench" || localized.type === "result-view") {
// Both render through the plugin UI entrypoint, so both resolve their icon
// asset path the same way.
localized.icon = optionalPluginAssetPath(localized.icon);
}
return localized;
@@ -324,6 +338,6 @@ function isPluginFormFieldValue(value: unknown): value is PluginFormFieldValue {
return value === undefined || typeof value === "string" || typeof value === "number" || typeof value === "boolean";
}
function isRecord(value: unknown): value is Record<string, any> {
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
+19 -1
View File
@@ -1,7 +1,7 @@
import { describe, expect, it, vi } from "vitest";
import { reactive, readonly } from "vue";
import { PluginHostBridge, pluginSandboxDocument, pluginSdkSource } from "./pluginHostBridge";
import type { InstalledPlugin, PluginWorkbenchContribution } from "@/types/database";
import type { InstalledPlugin, PluginResultViewContribution, PluginWorkbenchContribution } from "@/types/database";
function plugin(permissions: string[] = []): InstalledPlugin {
return {
@@ -11,6 +11,7 @@ function plugin(permissions: string[] = []): InstalledPlugin {
}
const workbench: PluginWorkbenchContribution = { type: "workbench", id: "sample.main", label: "Sample" };
const resultView: PluginResultViewContribution = { type: "result-view", id: "sample.graph", label: "Graph" };
describe("PluginHostBridge", () => {
it("binds backend calls to the owning plugin identity", async () => {
@@ -166,6 +167,23 @@ describe("PluginHostBridge", () => {
).toThrow("exceeds");
});
it("sends the opened contribution id for a result-view surface in the init message", () => {
const messages: unknown[] = [];
const target = { postMessage: (message: unknown) => messages.push(message) } as unknown as Window;
// The plugin has a single UI entrypoint, so the contribution id is the only
// signal telling its UI which declared surface was opened.
const bridge = new PluginHostBridge(plugin(), resultView, { connectionId: "connection", sql: "SELECT 1" }, () => target, {
invoke: vi.fn(),
notify: vi.fn(),
sendBinary: vi.fn(),
readAsset: vi.fn(),
});
bridge.sendInit();
expect(messages[0]).toMatchObject({ source: "dbx-host", type: "init", pluginId: "sample", contributionId: "sample.graph", context: { connectionId: "connection", sql: "SELECT 1" } });
});
it("rejects privileged host calls without manifest permission", async () => {
const messages: unknown[] = [];
const target = { postMessage: (message: unknown) => messages.push(message) } as unknown as Window;
@@ -1,4 +1,4 @@
import type { InstalledPlugin, PluginBinaryEvent, PluginEvent, PluginUiAssetPayload, PluginWorkbenchContribution } from "@/types/database";
import type { InstalledPlugin, PluginBinaryEvent, PluginEvent, PluginUiAssetPayload, PluginUiContribution } from "@/types/database";
import { clonePluginData, snapshotPluginWorkbenchContext } from "./pluginData";
const PLUGIN_MESSAGE_SOURCE = "dbx-plugin";
@@ -81,7 +81,7 @@ export class PluginHostBridge {
constructor(
private readonly plugin: InstalledPlugin,
private readonly workbench: PluginWorkbenchContribution,
private readonly contribution: PluginUiContribution,
context: PluginWorkbenchContext,
private readonly targetWindow: () => Window | null,
private readonly api: PluginHostBridgeApi,
@@ -161,7 +161,7 @@ export class PluginHostBridge {
version: BRIDGE_VERSION,
type: "init",
pluginId: this.plugin.manifest.id,
contributionId: this.workbench.id,
contributionId: this.contribution.id,
locale: this.locale,
theme: this.theme ? clonePluginData(this.theme) : undefined,
permissions: [...(this.plugin.manifest.permissions || [])],
@@ -684,6 +684,6 @@ function enforcePayloadLimit(value: unknown): void {
if (bytes > MAX_BRIDGE_PAYLOAD_BYTES) throw new Error("Plugin bridge request is too large");
}
function isRecord(value: unknown): value is Record<string, any> {
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}