feat(plugin): expose stable contribution identity
This commit is contained in:
1 parent
d9338d1a35
commit
4e144035b1
9 files changed
+146
-8
No files matched your search
+60
-1
@@ -1661,11 +1661,16 @@ describe("PluginHostBridge", () => {
|
||||
describe("plugin SDK source", () => {
|
||||
interface SdkWindow {
|
||||
dbxPlugin?: {
|
||||
ready: Promise<unknown>;
|
||||
contributionId?: string;
|
||||
context?: Record<string, unknown>;
|
||||
onInit: (listener: (context: unknown) => void) => () => void;
|
||||
invoke: (method: string, params?: unknown, options?: { timeoutMs?: number }) => Promise<unknown>;
|
||||
getPlanCapabilities: (connectionId: string) => Promise<unknown>;
|
||||
explainPlan: (request: unknown) => Promise<unknown>;
|
||||
getTableMetadata: (context: unknown) => Promise<unknown>;
|
||||
};
|
||||
receiveHostMessage: (message: unknown) => void;
|
||||
}
|
||||
|
||||
function loadSdk(posted: unknown[], initialTheme?: { appearance: "dark" | "light"; tokens: Record<string, string> }): SdkWindow {
|
||||
@@ -1677,10 +1682,19 @@ describe("plugin SDK source", () => {
|
||||
},
|
||||
dispatchEvent: vi.fn(),
|
||||
} as unknown as Document;
|
||||
let onMessage: ((event: MessageEvent) => void) | undefined;
|
||||
const parent = { postMessage: (message: unknown) => posted.push(message) };
|
||||
const addEventListener = (type: string, listener: (event: MessageEvent) => void) => {
|
||||
if (type === "message") onMessage = listener;
|
||||
};
|
||||
// The SDK IIFE only touches window and the bare-global addEventListener at
|
||||
// boot; parent.postMessage is captured for later request() calls, so a
|
||||
// stub window/parent is enough here.
|
||||
new Function("window", "parent", "addEventListener", "document", pluginSdkSource(initialTheme))(sandbox, { postMessage: (message: unknown) => posted.push(message) }, () => {}, document);
|
||||
new Function("window", "parent", "addEventListener", "document", pluginSdkSource(initialTheme))(sandbox, parent, addEventListener, document);
|
||||
sandbox.receiveHostMessage = (message) => {
|
||||
if (!onMessage) throw new Error("SDK did not register its host-message listener");
|
||||
onMessage({ source: parent, data: message } as unknown as MessageEvent);
|
||||
};
|
||||
return sandbox;
|
||||
}
|
||||
|
||||
@@ -1720,6 +1734,51 @@ describe("plugin SDK source", () => {
|
||||
expect(posted[0]).toMatchObject({ source: "dbx-plugin", type: "ready" });
|
||||
});
|
||||
|
||||
it("exposes the opened contribution after ready when init was already delivered and context is empty", async () => {
|
||||
const sdkMessages: unknown[] = [];
|
||||
const hostMessages: unknown[] = [];
|
||||
const sdk = loadSdk(sdkMessages);
|
||||
const probe: PluginWorkbenchContribution = { type: "workbench", id: "sample.probe", label: "Probe" };
|
||||
const generation: PluginWorkbenchContribution = { type: "workbench", id: "sample.generation", label: "Generation" };
|
||||
const target = { postMessage: (message: unknown) => hostMessages.push(message) } as unknown as Window;
|
||||
const bridge = new PluginHostBridge(plugin([], [probe, generation]), generation, {}, () => target, { invoke: vi.fn(), notify: vi.fn(), sendBinary: vi.fn(), readAsset: vi.fn() });
|
||||
|
||||
bridge.sendInit();
|
||||
const init = hostMessages[0] as Record<string, unknown>;
|
||||
expect(init).toMatchObject({ type: "init", contributionId: "sample.generation", context: {} });
|
||||
sdk.receiveHostMessage(init);
|
||||
|
||||
await sdk.dbxPlugin!.ready;
|
||||
|
||||
expect(sdk.dbxPlugin!.contributionId).toBe("sample.generation");
|
||||
expect(sdk.dbxPlugin!.contributionId).not.toBe(probe.id);
|
||||
expect(sdk.dbxPlugin!.context).toEqual({});
|
||||
const lateInit = vi.fn();
|
||||
sdk.dbxPlugin!.onInit(lateInit);
|
||||
expect(lateInit).toHaveBeenCalledWith({});
|
||||
});
|
||||
|
||||
it("sets contribution identity independently of table context before ready and onInit", async () => {
|
||||
const sdk = loadSdk([]);
|
||||
const hostMessages: unknown[] = [];
|
||||
const tableContext = { connectionId: "connection-1", database: "app", schema: "public", table: "orders" };
|
||||
const generation: PluginWorkbenchContribution = { type: "workbench", id: "sample.generation", label: "Generation" };
|
||||
const target = { postMessage: (message: unknown) => hostMessages.push(message) } as unknown as Window;
|
||||
const bridge = new PluginHostBridge(plugin([], [generation]), generation, tableContext, () => target, { invoke: vi.fn(), notify: vi.fn(), sendBinary: vi.fn(), readAsset: vi.fn() });
|
||||
let observed: { context: unknown; contributionId?: string } | undefined;
|
||||
sdk.dbxPlugin!.onInit((context) => {
|
||||
observed = { context, contributionId: sdk.dbxPlugin!.contributionId };
|
||||
});
|
||||
|
||||
bridge.sendInit();
|
||||
sdk.receiveHostMessage(hostMessages[0]);
|
||||
await sdk.dbxPlugin!.ready;
|
||||
|
||||
expect(observed).toEqual({ context: tableContext, contributionId: "sample.generation" });
|
||||
expect(sdk.dbxPlugin!.context).toEqual(tableContext);
|
||||
expect(sdk.dbxPlugin!.contributionId).toBe("sample.generation");
|
||||
});
|
||||
|
||||
it("keeps cloneable values intact and plain scalars by reference", () => {
|
||||
const posted: unknown[] = [];
|
||||
const { dbxPlugin } = loadSdk(posted);
|
||||
|
||||
@@ -1252,6 +1252,7 @@ export function pluginSdkSource(initialTheme?: PluginBridgeTheme): string {
|
||||
const pending = new Map();
|
||||
const listeners = { event: new Set(), binary: new Set(), init: new Set(), context: new Set(), filedrop: new Set(), dragstate: new Set(), close: new Set() };
|
||||
let sequence = 0;
|
||||
let contributionId;
|
||||
let context;
|
||||
let locale = 'en';
|
||||
let theme;
|
||||
@@ -1353,6 +1354,7 @@ export function pluginSdkSource(initialTheme?: PluginBridgeTheme): string {
|
||||
};
|
||||
window.dbxPlugin = Object.freeze({
|
||||
ready,
|
||||
get contributionId() { return contributionId; },
|
||||
get context() { return context; },
|
||||
get locale() { return locale; },
|
||||
get theme() { return theme; },
|
||||
@@ -1511,6 +1513,7 @@ export function pluginSdkSource(initialTheme?: PluginBridgeTheme): string {
|
||||
pending.delete(message.id);
|
||||
if (message.error) handler.reject(new Error(message.error)); else handler.resolve(message.result);
|
||||
} else if (message.type === 'init') {
|
||||
contributionId = message.contributionId;
|
||||
capabilities = message.capabilities || {};
|
||||
context = message.context;
|
||||
locale = typeof message.locale === 'string' ? message.locale : 'en';
|
||||
|
||||
Reference in new issue
Block a user