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.8 KB
Raw Blame
// @vitest-environment happy-dom

import { createApp, defineComponent, h, inject, provide, 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(), invokePlugin: vi.fn(), useConnectionStore: vi.fn() }));

vi.mock("@/lib/backend/api", () => ({ listPlugins: mocks.listPlugins, invokePlugin: mocks.invokePlugin }));
vi.mock("vue-i18n", () => ({ useI18n: () => ({ locale: ref("en"), t: (key: string) => key }) }));
vi.mock("@/stores/connectionStore", () => ({ useConnectionStore: mocks.useConnectionStore }));
vi.mock("@/stores/queryStore", () => ({ useQueryStore: () => ({}) }));
vi.mock("@/components/ui/button", () => ({
  Button: defineComponent(
    (_, { slots }) =>
      () =>
        h("button", slots.default?.()),
  ),
}));
vi.mock("@/components/ui/tooltip", () => ({
  Tooltip: defineComponent(
    (_, { slots }) =>
      () =>
        h("div", slots.default?.()),
  ),
  TooltipTrigger: defineComponent(
    (_, { slots }) =>
      () =>
        h("div", slots.default?.()),
  ),
  TooltipContent: defineComponent(
    (_, { slots }) =>
      () =>
        h("div", slots.default?.()),
  ),
}));
vi.mock("./PluginIcon.vue", () => ({ default: defineComponent(() => () => h("span")) }));
vi.mock("@/components/icons/ConnectionIcon.vue", () => ({ default: defineComponent(() => () => h("span", { "data-connection-icon-stub": "" })) }));
// DropdownMenu harness: content renders unconditionally and the trigger's
// click forwards `update:open`, so picker tests assert menu content and item
// selection without depending on reka-ui's floating/portal behavior.
vi.mock("@/components/ui/dropdown-menu", () => ({
  DropdownMenu: defineComponent({
    props: { open: { type: Boolean, default: false } },
    emits: ["update:open"],
    setup(_, { slots, emit }) {
      provide("dbxDropdownTestSetOpen", (value: boolean) => emit("update:open", value));
      return () => h("div", { "data-dropdown-menu-stub": "" }, slots.default?.());
    },
  }),
  DropdownMenuTrigger: defineComponent({
    setup(_, { slots }) {
      const setOpen = inject<(value: boolean) => void>("dbxDropdownTestSetOpen")!;
      return () => h("div", { onClick: () => setOpen(true) }, slots.default?.());
    },
  }),
  DropdownMenuContent: defineComponent(
    (_, { slots }) =>
      () =>
        h("div", { "data-plugin-dock-plus-menu": "" }, slots.default?.()),
  ),
  DropdownMenuItem: defineComponent({
    emits: ["select"],
    setup(_, { slots, emit }) {
      return () => h("div", { "data-dropdown-item-stub": "", onClick: () => emit("select", {}) }, slots.default?.());
    },
  }),
}));
// Mirrors the real PluginWorkbenchHost, whose title computed dereferences
// props.contribution.label unconditionally — an undefined contribution must
// never reach it, otherwise the whole dock subtree crashes during render.
vi.mock("./PluginWorkbenchHost.vue", () => ({
  default: defineComponent({
    props: {
      plugin: { type: Object, required: true },
      contribution: { type: Object, required: true },
      context: { type: Object, default: undefined },
    },
    emits: ["open-workbench"],
    setup(props, { emit }) {
      return () => h("div", { "data-workbench-host-stub": "", onClick: () => emit("open-workbench", props.contribution.id, { commandId: "untrusted" }) }, `${props.plugin.manifest.name} · ${props.contribution.label}`);
    },
  }),
}));

import PluginBottomDock from "./PluginBottomDock.vue";
import * as dock from "@/lib/plugins/pluginBottomDock";

function installedPlugin(contributionId: string): InstalledPlugin {
  return {
    manifest: { id: "io.dbx.sample", name: "Sample", version: "1.0.0", permissions: [], drivers: [], contributions: [{ type: "workbench", id: contributionId, label: "Sample panel" }] },
    compatibility: { compatible: true },
  };
}

describe("PluginBottomDock workbench contribution guard", () => {
  let app: App<Element> | undefined;
  let root: HTMLDivElement;
  let renderError: unknown;
  const entryIds: string[] = [];

  beforeEach(() => {
    vi.clearAllMocks();
    mocks.useConnectionStore.mockReturnValue({ connections: [], sidebarLayout: { groups: [], order: [] }, ensureConnected: vi.fn(() => Promise.resolve()) });
    root = document.createElement("div");
    document.body.appendChild(root);
  });

  afterEach(() => {
    app?.unmount();
    root.remove();
    for (const id of entryIds.splice(0)) dock.closePluginDockEntry(id);
    dock.setDockVisible(false);
  });

  async function mountDock() {
    dock.setDockVisible(true);
    const application = createApp(PluginBottomDock);
    renderError = undefined;
    application.config.errorHandler = (error) => {
      renderError = error;
    };
    app = application;
    application.mount(root);
    await new Promise((resolve) => setTimeout(resolve, 0));
  }

  it("renders the workbench host when the entry's contribution is still live", async () => {
    mocks.listPlugins.mockResolvedValue([installedPlugin("sample.panel")]);
    entryIds.push(dock.addPluginDockEntry({ pluginId: "io.dbx.sample", workbenchContributionId: "sample.panel", kind: "command", commandId: "sample.open", title: "Sample terminal" }));

    await mountDock();

    expect(renderError).toBeUndefined();
    expect(root.querySelector("[data-workbench-host-stub]")?.textContent).toBe("Sample · Sample panel");
  });

  it("preserves host command provenance when a plugin opens a second session", async () => {
    mocks.listPlugins.mockResolvedValue([installedPlugin("sample.panel")]);
    const first = dock.addPluginDockEntry({ pluginId: "io.dbx.sample", workbenchContributionId: "sample.panel", kind: "command", commandId: "sample.open", instanceKey: "local", icon: "terminal", title: "Terminal" });
    entryIds.push(first);
    await mountDock();
    (root.querySelector("[data-workbench-host-stub]") as HTMLElement).click();
    const state = dock.usePluginBottomDock();
    const second = state.entries.value.find((entry) => entry.id === state.activeEntryId.value)!;
    entryIds.push(second.id);
    expect(second.id).not.toBe(first);
    expect(second).toMatchObject({ commandId: "sample.open", instanceKey: "local", icon: "terminal" });
  });

  it("skips the launch options fetch when the panel command has no options_action", async () => {
    // options_action is optional on open-workbench panel commands; without the
    // guard the dock still fired a doomed invokePlugin(undefined) round trip
    // and warned on every picker open / entry switch.
    const plugin = installedPlugin("sample.panel");
    plugin.manifest.contributions!.push({
      type: "command",
      id: "sample.open",
      label: "Open sample",
      action: { type: "open-workbench", workbench: "sample.panel", presentation: "panel" },
    });
    mocks.listPlugins.mockResolvedValue([plugin]);
    const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
    try {
      entryIds.push(dock.addPluginDockEntry({ pluginId: "io.dbx.sample", workbenchContributionId: "sample.panel", kind: "command", commandId: "sample.open", title: "Sample terminal" }));
      await mountDock();
      await new Promise((resolve) => setTimeout(resolve, 0));

      expect(renderError).toBeUndefined();
      expect(mocks.invokePlugin).not.toHaveBeenCalled();
      expect(warnSpy.mock.calls.filter((call) => String(call[0]).includes("[DBX][plugin:dock]"))).toEqual([]);
      // Without options_action the command doesn't own the picker, so the
      // generic replay item stays visible.
      expect(root.querySelector("[data-plugin-dock-plus-menu]")?.textContent).toContain("pluginDock.newTerminal");
    } finally {
      warnSpy.mockRestore();
    }
  });

  it("passes the current UI locale to the options_action fetch", async () => {
    // Sidecars need the UI language to localize launch-option labels; the host
    // used to call options_action with an empty payload.
    const plugin = installedPlugin("sample.panel");
    plugin.manifest.contributions!.push({
      type: "command",
      id: "sample.open",
      label: "Open sample",
      action: { type: "open-workbench", workbench: "sample.panel", presentation: "panel", options_action: "local/terminal/launch-options" },
    });
    mocks.listPlugins.mockResolvedValue([plugin]);
    mocks.invokePlugin.mockResolvedValue({ entries: [{ label: "Local terminal (auto-detect)", description: "Default shell: /bin/zsh", context: {} }] });
    entryIds.push(dock.addPluginDockEntry({ pluginId: "io.dbx.sample", workbenchContributionId: "sample.panel", kind: "command", commandId: "sample.open", title: "Sample terminal" }));
    await mountDock();
    await new Promise((resolve) => setTimeout(resolve, 0));

    expect(mocks.invokePlugin).toHaveBeenCalledWith("io.dbx.sample", "local/terminal/launch-options", { locale: "en" });
    const menu = root.querySelector("[data-plugin-dock-plus-menu]");
    expect(menu?.textContent).toContain("Local terminal (auto-detect)");
    expect(menu?.textContent).toContain("Default shell: /bin/zsh");
    // The plugin owns the picker now: the generic replay item hides.
    expect(menu?.textContent).not.toContain("pluginDock.newTerminal");
  });

  it("renders the + picker connection targets as sidebar-grouped rows and opens them on pick", async () => {
    const plugin = installedPlugin("sample.panel");
    plugin.manifest.contributions!.push(
      { type: "connection-provider", id: "ssh", label: "SSH", database_type: "plugin", fields: [] },
      {
        type: "command",
        id: "sample.open",
        label: "Open sample",
        action: { type: "open-workbench", workbench: "sample.panel", presentation: "panel", connection_targets: true },
      },
    );
    mocks.listPlugins.mockResolvedValue([plugin]);
    mocks.useConnectionStore.mockReturnValue({
      connections: [{ id: "conn-1", name: "my-server", plugin_connection_provider: "ssh" }],
      sidebarLayout: {
        groups: [{ id: "g1", name: "Prod", collapsed: false }],
        order: [{ type: "group", id: "g1", children: [{ type: "connection", id: "conn-1" }] }],
      },
      ensureConnected: vi.fn(() => Promise.resolve()),
    });
    entryIds.push(dock.addPluginDockEntry({ pluginId: "io.dbx.sample", workbenchContributionId: "sample.panel", kind: "command", commandId: "sample.open", title: "Sample terminal" }));
    await mountDock();
    await new Promise((resolve) => setTimeout(resolve, 0));

    const menu = root.querySelector("[data-plugin-dock-plus-menu]")!;
    expect(menu.textContent).toContain("Prod");
    expect(menu.textContent).toContain("my-server");
    // The flat "connection · name" prefix is gone; rows carry icons instead.
    expect(menu.textContent).not.toContain("pluginDock.connectionTerminal");

    const row = [...menu.querySelectorAll("[data-dropdown-item-stub]")].find((element) => element.textContent?.includes("my-server")) as HTMLElement;
    row.click();
    const state = dock.usePluginBottomDock();
    const picked = state.entries.value[state.entries.value.length - 1]!;
    entryIds.push(picked.id);
    expect(picked).toMatchObject({ kind: "connection", context: { connectionId: "conn-1" } });
  });

  it("degrades to an empty frame instead of crashing when the plugin no longer declares the entry's workbench contribution", async () => {
    // Plugin upgrade/downgrade replaced the workbench id the entry was opened with.
    mocks.listPlugins.mockResolvedValue([installedPlugin("sample.renamed")]);
    entryIds.push(dock.addPluginDockEntry({ pluginId: "io.dbx.sample", workbenchContributionId: "sample.panel", kind: "command", commandId: "sample.open", title: "Sample terminal" }));

    await mountDock();

    expect(renderError).toBeUndefined();
    expect(root.querySelector("[data-workbench-host-stub]")).toBeNull();
    // The tab strip (and its close affordance) survives so the stale entry stays closable.
    expect(root.querySelector("[data-plugin-dock-tabs]")?.textContent).toContain("Sample terminal");
  });
});