Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

11.2 KB
Raw Blame
// @vitest-environment happy-dom

import { createApp, nextTick, ref, type App } from "vue";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { InstalledPlugin } from "@/types/database";

const mocks = vi.hoisted(() => ({
  listPlugins: vi.fn(),
  readPluginUiEntry: vi.fn(),
  readPluginUiAsset: vi.fn(),
  subscribePluginEvents: vi.fn(),
  repushPluginConnection: vi.fn(),
  ensureConnected: vi.fn(),
  openPluginWorkbench: vi.fn(),
  setPluginWorkbenchTabTitle: vi.fn(),
  getPluginWorkbenchTabTitle: vi.fn(),
  connectedIds: new Set<string>(["conn-1"]),
}));

vi.mock("@/lib/backend/api", () => ({
  invokePlugin: vi.fn(),
  notifyPlugin: vi.fn(),
  sendPluginBinary: vi.fn(),
  listPlugins: mocks.listPlugins,
  readPluginUiEntry: mocks.readPluginUiEntry,
  readPluginUiAsset: mocks.readPluginUiAsset,
  subscribePluginEvents: mocks.subscribePluginEvents,
}));
vi.mock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.mock("@/lib/common/clipboard", () => ({ copyToClipboard: vi.fn() }));
vi.mock("@/composables/useTheme", () => ({ useTheme: () => ({ isDark: ref(false), themeRevision: ref(0) }) }));
vi.mock("@/stores/settingsStore", () => ({ useSettingsStore: () => ({ editorSettings: { uiFontFamily: "", fontFamily: "", fontSize: 14 } }) }));
vi.mock("@/stores/connectionStore", () => ({
  useConnectionStore: () => ({
    connectedIds: mocks.connectedIds,
    getConfig: () => undefined,
    ensureConnected: mocks.ensureConnected,
    repushPluginConnection: mocks.repushPluginConnection,
    reopenPluginConnection: vi.fn(),
  }),
}));
vi.mock("@/stores/queryStore", () => ({
  useQueryStore: () => ({
    openPluginWorkbench: mocks.openPluginWorkbench,
    setPluginWorkbenchTabTitle: mocks.setPluginWorkbenchTabTitle,
    getPluginWorkbenchTabTitle: mocks.getPluginWorkbenchTabTitle,
  }),
}));
vi.mock("vue-i18n", () => ({
  useI18n: () => ({ locale: ref("en"), t: (key: string, params?: Record<string, unknown>) => (params ? `${key} ${JSON.stringify(params)}` : key) }),
}));

import PluginWorkbenchTab from "./PluginWorkbenchTab.vue";

const plugin: InstalledPlugin = {
  manifest: {
    id: "com.example.graph",
    name: "Graph plugin",
    version: "1.0.0",
    permissions: ["host.workbench"],
    drivers: [],
    entrypoints: { ui: { root: "ui", entry: "ui/index.html" } },
    contributions: [
      { type: "workbench", id: "graph.main", label: "Graph workbench" },
      { type: "result-view", id: "graph.chart", label: "Chart" },
      { type: "context-menu", id: "graph.inspect", label: "Inspect", menu: "connection" },
    ],
  },
  compatibility: { compatible: true },
};

/** The bounded snapshot `ContentArea.openPluginResultView` hands to the plugin UI. */
const resultContext = {
  connectionId: "conn-1",
  database: "dbx_test",
  sql: "SELECT id, name FROM dbx_9597_orders",
  result: { columns: ["id", "name"], rows: [[1, "alpha"]], truncated: false },
};

describe("PluginWorkbenchTab contribution resolution", () => {
  let app: App<Element> | undefined;
  let root: HTMLDivElement;

  beforeEach(() => {
    vi.clearAllMocks();
    vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => window.setTimeout(() => callback(0), 0));
    vi.stubGlobal("getComputedStyle", () => Object.assign([], { getPropertyValue: () => "" }));
    mocks.listPlugins.mockResolvedValue([plugin]);
    mocks.readPluginUiEntry.mockResolvedValue({ dataBase64: btoa("<!doctype html><html><body></body></html>"), contentType: "text/html" });
    mocks.subscribePluginEvents.mockResolvedValue(vi.fn());
    mocks.repushPluginConnection.mockResolvedValue(undefined);
    mocks.ensureConnected.mockResolvedValue(undefined);
    root = document.createElement("div");
    document.body.appendChild(root);
  });

  afterEach(() => {
    app?.unmount();
    root.remove();
    vi.unstubAllGlobals();
  });

  async function mountTab(contributionId: string, context?: Record<string, unknown>) {
    app = createApp(PluginWorkbenchTab, { pluginId: "com.example.graph", contributionId, connectionId: "conn-1", context });
    app.mount(root);
    await nextTick();
    await new Promise((resolve) => setTimeout(resolve, 0));
    await nextTick();
  }

  /** Mount, reveal the sandbox iframe, and capture the bridge init payload. */
  async function mountLoadedTab(contributionId: string, context?: Record<string, unknown>) {
    await mountTab(contributionId, context);
    const frame = root.querySelector("iframe");
    expect(frame).toBeInstanceOf(HTMLIFrameElement);
    const target = frame!.contentWindow!;
    const postMessage = vi.spyOn(target, "postMessage").mockImplementation(() => {});
    frame!.dispatchEvent(new Event("load"));
    await vi.waitFor(() => expect(postMessage).toHaveBeenCalled());
    return postMessage;
  }

  function initPayload(postMessage: { mock: { calls: unknown[][] } }) {
    const init = postMessage.mock.calls.map((call) => call[0] as Record<string, unknown>).find((message) => message.type === "init");
    expect(init).toBeDefined();
    return init!;
  }

  it("renders a declared result-view instead of reporting it unavailable", async () => {
    const postMessage = await mountLoadedTab("graph.chart", resultContext);

    // The regression behind #9597: the toolbar opens the tab with the
    // result-view contribution id, which a workbench-only lookup never resolves.
    expect(root.textContent).not.toContain("pluginPlatform.workbenchUnavailable");
    expect(initPayload(postMessage)).toMatchObject({ type: "init", pluginId: "com.example.graph", contributionId: "graph.chart" });
  });

  it("delivers the query result snapshot to a result-view plugin UI", async () => {
    const postMessage = await mountLoadedTab("graph.chart", resultContext);

    expect(initPayload(postMessage).context).toEqual(resultContext);
  });

  it("still renders a declared workbench", async () => {
    const postMessage = await mountLoadedTab("graph.main");

    expect(root.textContent).not.toContain("pluginPlatform.workbenchUnavailable");
    expect(initPayload(postMessage)).toMatchObject({ contributionId: "graph.main" });
  });

  it("reports an unavailable contribution when the id matches no renderable plugin UI", async () => {
    await mountTab("graph.missing");

    expect(root.querySelector("iframe")).toBeNull();
    expect(root.textContent).toContain("pluginPlatform.workbenchUnavailable");

    // A declared contribution that owns another host surface stays unavailable
    // here: resolving plugin UI must not turn the lookup into a wildcard.
    app?.unmount();
    await mountTab("graph.inspect");

    expect(root.querySelector("iframe")).toBeNull();
    expect(root.textContent).toContain("pluginPlatform.workbenchUnavailable");
  });

  it("forwards setTitle and getTitle from the bridge to queryStore for the tab", async () => {
    mocks.getPluginWorkbenchTabTitle.mockReturnValue("Tab Original Title");
    app = createApp(PluginWorkbenchTab, { tabId: "tab-test-1", pluginId: "com.example.graph", contributionId: "graph.main", connectionId: "conn-1" });
    app.mount(root);
    await nextTick();
    await new Promise((resolve) => setTimeout(resolve, 0));
    await nextTick();
    const frame = root.querySelector("iframe");
    expect(frame).toBeInstanceOf(HTMLIFrameElement);
    const target = frame!.contentWindow!;
    const postMessage = vi.spyOn(target, "postMessage").mockImplementation(() => {});
    frame!.dispatchEvent(new Event("load"));
    await vi.waitFor(() => expect(postMessage).toHaveBeenCalled());

    // Send host.setTitle from the iframe
    window.dispatchEvent(
      new MessageEvent("message", {
        source: target,
        data: { source: "dbx-plugin", version: 1, type: "request", id: "t-set", method: "host.setTitle", params: { title: "Custom Title" } },
      }),
    );
    await vi.waitFor(() => expect(mocks.setPluginWorkbenchTabTitle).toHaveBeenCalledWith("tab-test-1", "Custom Title"));

    // Send host.getTitle from the iframe
    window.dispatchEvent(
      new MessageEvent("message", {
        source: target,
        data: { source: "dbx-plugin", version: 1, type: "request", id: "t-get", method: "host.getTitle" },
      }),
    );
    await vi.waitFor(() => expect(mocks.getPluginWorkbenchTabTitle).toHaveBeenCalledWith("tab-test-1"));
  });
});

// Restored-tab self-heal (dbx-plugin-ssh#144): a page refresh used to park the
// tab on the reload prompt from a one-shot `start()` snapshot, so the plugin
// could never re-establish its session even after the SPA's boot connect
// replay landed. `start()` now recovers silently and only parks when silent
// recovery cannot proceed.
describe("PluginWorkbenchTab restore self-heal", () => {
  let app: App<Element> | undefined;
  let root: HTMLDivElement;

  beforeEach(() => {
    vi.clearAllMocks();
    vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => window.setTimeout(() => callback(0), 0));
    vi.stubGlobal("getComputedStyle", () => Object.assign([], { getPropertyValue: () => "" }));
    mocks.listPlugins.mockResolvedValue([plugin]);
    mocks.readPluginUiEntry.mockResolvedValue({ dataBase64: btoa("<!doctype html><html><body></body></html>"), contentType: "text/html" });
    mocks.subscribePluginEvents.mockResolvedValue(vi.fn());
    mocks.repushPluginConnection.mockResolvedValue(undefined);
    mocks.ensureConnected.mockResolvedValue(undefined);
    mocks.connectedIds.clear();
    root = document.createElement("div");
    document.body.appendChild(root);
  });

  afterEach(() => {
    app?.unmount();
    root.remove();
    vi.unstubAllGlobals();
  });

  it("self-heals a restored tab: silent ensureConnected, then the workbench loads", async () => {
    app = createApp(PluginWorkbenchTab, { pluginId: "com.example.graph", contributionId: "graph.main", connectionId: "conn-1" });
    app.mount(root);

    await vi.waitFor(() => expect(mocks.ensureConnected).toHaveBeenCalledWith("conn-1", { allowPasswordPrompt: false, skipIfClosedByUser: true }));
    await vi.waitFor(() => expect(root.querySelector("iframe")).toBeInstanceOf(HTMLIFrameElement));
    expect(root.textContent).not.toContain("pluginPlatform.reloadRequired");
  });

  it("keeps an already-connected restore on the direct load path", async () => {
    mocks.connectedIds.add("conn-1");
    app = createApp(PluginWorkbenchTab, { pluginId: "com.example.graph", contributionId: "graph.main", connectionId: "conn-1" });
    app.mount(root);

    await vi.waitFor(() => expect(root.querySelector("iframe")).toBeInstanceOf(HTMLIFrameElement));
    // The direct load path must not reconnect behind the user's back. (The
    // host's PluginWorkbenchHost re-pushes credentials on its own mount; that
    // is not start()'s concern here.)
    expect(mocks.ensureConnected).not.toHaveBeenCalled();
    expect(root.textContent).not.toContain("pluginPlatform.reloadRequired");
  });

  it("parks on the reload prompt without an error card when silent recovery fails", async () => {
    mocks.ensureConnected.mockRejectedValue(new Error("interactive password required"));
    app = createApp(PluginWorkbenchTab, { pluginId: "com.example.graph", contributionId: "graph.main", connectionId: "conn-1" });
    app.mount(root);

    await vi.waitFor(() => expect(root.textContent).toContain("pluginPlatform.reloadRequired"));
    expect(root.querySelector("iframe")).toBeNull();
  });
});