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。

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

import { createApp, defineComponent, h, nextTick, type App } from "vue";
import { afterEach, describe, expect, it, vi } from "vitest";
import i18n, { setLocale } from "@/i18n";
import TreeItem from "@/components/sidebar/TreeItem.vue";
import { createSidebarTreeRuntime, sidebarTreeRuntimeKey, type SidebarTreeRuntimeHost } from "@/lib/sidebar/sidebarTreeRuntime";
import type { TreeNode } from "@/types/database";

const connectionStore = {
  activeConnectionId: "mqtt-1",
  connectedIds: new Set(["mqtt-1"]),
  connectingIds: new Set<string>(),
  connectionMultiSelectActive: false,
  connections: [],
  getConfig: () => ({ id: "mqtt-1", name: "test-mqtt", db_type: "mqtt" }),
  isDefaultDatabase: () => false,
  isDefaultSchema: () => false,
  isPinnedTreeNodeReorderTarget: () => false,
  isTreeNodeChildrenLoaded: () => false,
  isTreeNodePinned: () => false,
  selectedTreeNodeId: null as string | null,
  selectedTreeNodeIds: [] as string[],
  selectedTreeNodeIdsSet: new Set<string>(),
  sidebarTableSearchQueries: {},
  tableNameFilterForScope: () => undefined,
  treeNodes: [],
  treeSelectionAnchorId: null as string | null,
};

vi.mock("@/stores/connectionStore", () => ({
  useConnectionStore: () => connectionStore,
}));

vi.mock("@/stores/queryStore", () => ({
  useQueryStore: () => ({ openDatabaseKeys: new Set<string>() }),
}));

vi.mock("@/stores/settingsStore", () => ({
  useSettingsStore: () => ({
    editorSettings: {
      shortcuts: { openDataInNewTab: "" },
      sidebarActivation: "double",
      sidebarAllowHorizontalScroll: false,
      sidebarHiddenTablePrefixes: [],
      sidebarObjectInfoMode: "none",
    },
  }),
}));

vi.mock("@/composables/useToast", () => ({
  useToast: () => ({ toast: vi.fn() }),
}));

const mountedApps: App[] = [];

function runtimeHost(): SidebarTreeRuntimeHost {
  return {
    buildContextMenu: vi.fn(() => []),
    handleRowClick: vi.fn(),
    handleRowDoubleClick: vi.fn(),
    handleRowKeydown: vi.fn(),
    openPrimaryVisibleFilter: vi.fn(),
    openDataInNewTab: vi.fn(),
    requestPaste: vi.fn(() => false),
    toggleNode: vi.fn(),
  };
}

async function mountMqttConsoleNode(override?: Partial<TreeNode>) {
  const container = document.createElement("div");
  document.body.append(container);
  const node: TreeNode = {
    id: "mqtt-1:mqtt-topic:__console__",
    label: "connection.mqttConsoleTitle",
    type: "mqtt-topic",
    connectionId: "mqtt-1",
    children: [],
    ...override,
  };
  const runtime = createSidebarTreeRuntime();
  runtime.bindHost(runtimeHost());
  const app = createApp(
    defineComponent({
      setup: () => () => h(TreeItem, { node, depth: 1 }),
    }),
  );
  mountedApps.push(app);
  app.use(i18n);
  app.provide(sidebarTreeRuntimeKey, runtime);
  app.mount(container);
  await nextTick();

  const row = container.querySelector<HTMLElement>("[tabindex]");
  if (!row) throw new Error(`MQTT console row was not rendered: ${container.innerHTML}`);
  return row;
}

afterEach(async () => {
  for (const app of mountedApps.splice(0)) app.unmount();
  document.body.replaceChildren();
  await setLocale("en");
  vi.restoreAllMocks();
});

describe("TreeItem MQTT console i18n", () => {
  it("updates the existing synthetic node when the locale changes", async () => {
    await setLocale("en");
    const row = await mountMqttConsoleNode();

    expect(row.textContent).toContain("MQTT Console");

    await setLocale("zh-CN");
    await nextTick();

    expect(row.textContent).toContain("MQTT 控制台");
    expect(row.textContent).not.toContain("MQTT Console");
  });

  it("translates the native Job entry in an existing tree", async () => {
    await setLocale("zh-CN");
    const row = await mountMqttConsoleNode({ id: "ob-1:__native_jobs", type: "database-job-admin", label: "tree.nativeJobs" });
    expect(row.textContent).toContain("原生作业");
    expect(row.textContent).not.toContain("tree.nativeJobs");
    await setLocale("en");
    await nextTick();
    expect(row.textContent).not.toContain("tree.nativeJobs");
    expect(row.textContent).not.toContain("原生作业");
  });
});