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。

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

import { createApp, nextTick } from "vue";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import ConnectionIcon from "./ConnectionIcon.vue";
import { clearPluginIconCache } from "@/lib/plugins/pluginIconResolver";
import type { ConnectionConfig } from "@/types/database";

const { listPlugins, readPluginAsset } = vi.hoisted(() => ({ listPlugins: vi.fn(), readPluginAsset: vi.fn() }));
vi.mock("@/lib/backend/api", () => ({ listPlugins, readPluginAsset }));

const cleanups: Array<() => void> = [];
const connection: ConnectionConfig = { id: "c1", name: "Connection", db_type: "mysql", host: "localhost", port: 3306, username: "", password: "" };

async function mountIcon(config?: ConnectionConfig) {
  const container = document.createElement("div");
  document.body.append(container);
  const app = createApp(ConnectionIcon, { connection: config, class: "h-3 w-3" });
  app.mount(container);
  cleanups.push(() => {
    app.unmount();
    container.remove();
  });
  await new Promise((resolve) => setTimeout(resolve, 0));
  await nextTick();
  return container;
}

beforeEach(() => {
  vi.clearAllMocks();
  clearPluginIconCache();
  listPlugins.mockResolvedValue([
    {
      manifest: {
        id: "sample.plugin",
        icon: "assets/plugin.svg",
        contributions: [{ type: "connection-provider", id: "sample.connection", icon: "assets/connection.svg" }],
      },
    },
  ]);
  readPluginAsset.mockResolvedValue({ contentType: "image/svg+xml", dataBase64: "PHN2Zy8+" });
  vi.stubGlobal("URL", { ...URL, createObjectURL: () => "blob:connection-icon", revokeObjectURL: vi.fn() });
});

afterEach(() => {
  while (cleanups.length) cleanups.pop()?.();
  vi.unstubAllGlobals();
});

describe("ConnectionIcon", () => {
  it("uses the plugin connection-provider logo and preserves sizing", async () => {
    const container = await mountIcon({ ...connection, db_type: "plugin", plugin_id: "sample.plugin", plugin_connection_provider: "sample.connection" });
    expect(readPluginAsset).toHaveBeenCalledWith("sample.plugin", "assets/connection.svg");
    expect(container.querySelector("img")?.getAttribute("src")).toBe("blob:connection-icon");
    expect(container.firstElementChild?.classList.contains("h-3")).toBe(true);
  });

  it("falls back to the plugin logo without a provider logo", async () => {
    await mountIcon({ ...connection, db_type: "plugin", plugin_id: "sample.plugin" });
    expect(readPluginAsset).toHaveBeenCalledWith("sample.plugin", "assets/plugin.svg");
  });

  it("keeps native database logos unchanged", async () => {
    const container = await mountIcon(connection);
    expect(container.querySelector("img")?.getAttribute("src")).toBe("/icons/database/mysql.svg");
    expect(listPlugins).not.toHaveBeenCalled();
  });

  it("shows the Inceptor logo for a saved Transwarp connection", async () => {
    const container = await mountIcon({ ...connection, db_type: "transwarp", driver_profile: "transwarp-inceptor" });
    expect(container.querySelector("img")?.getAttribute("src")).toBe("/icons/database/transwarp-inceptor.png");
  });

  it("renders a safe fallback for missing connection metadata", async () => {
    const container = await mountIcon();
    expect(container.querySelector("img")?.getAttribute("src")).toBe("/icons/database/postgres.svg");
    expect(listPlugins).not.toHaveBeenCalled();
  });
});