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。

6.8 KB
Raw Blame
import { test } from "vitest";
import assert from "node:assert/strict";
import { createPinia, setActivePinia } from "pinia";
import { useConnectionStore } from "../../apps/desktop/src/stores/connectionStore.ts";
import type { ConnectionConfig, SidebarLayout, TransportLayerConfig, TreeNode } from "../../apps/desktop/src/types/database.ts";

function installMemoryStorage() {
  const values = new Map<string, string>();
  const original = Object.getOwnPropertyDescriptor(globalThis, "localStorage");
  Object.defineProperty(globalThis, "localStorage", {
    configurable: true,
    value: {
      getItem: (key: string) => values.get(key) ?? null,
      setItem: (key: string, value: string) => values.set(key, value),
      removeItem: (key: string) => values.delete(key),
      clear: () => values.clear(),
    },
  });
  return {
    restore() {
      if (original) Object.defineProperty(globalThis, "localStorage", original);
      else Reflect.deleteProperty(globalThis, "localStorage");
    },
  };
}

function conn(id: string, name: string): ConnectionConfig {
  return {
    id,
    name,
    db_type: "mysql",
    host: "127.0.0.1",
    port: 3306,
    username: "root",
    password: "secret",
  };
}

async function withConnectionStore(initialConnections: ConnectionConfig[], initialLayout: SidebarLayout | null, run: (store: ReturnType<typeof useConnectionStore>) => Promise<void>) {
  const originalFetch = globalThis.fetch;
  const storage = installMemoryStorage();
  let savedConnections = initialConnections;
  let savedLayout = initialLayout;

  globalThis.fetch = (async (input, init) => {
    const url = String(input);
    if (url.split("?")[0] === "/api/connection/list") {
      return new Response(JSON.stringify(savedConnections), { status: 200, headers: { "Content-Type": "application/json" } });
    }
    if (url === "/api/layout/sidebar") {
      if (init?.method === "POST") {
        savedLayout = JSON.parse(String(init.body ?? "null"));
        return new Response("null", { status: 200, headers: { "Content-Type": "application/json" } });
      }
      return new Response(JSON.stringify(savedLayout), { status: 200, headers: { "Content-Type": "application/json" } });
    }
    if (url === "/api/connection/save") {
      savedConnections = JSON.parse(String(init?.body ?? "[]"));
      return new Response("null", { status: 200, headers: { "Content-Type": "application/json" } });
    }
    return new Response("null", { status: 200, headers: { "Content-Type": "application/json" } });
  }) as typeof fetch;

  try {
    setActivePinia(createPinia());
    const store = useConnectionStore();
    await store.initFromDisk();
    await run(store);
  } finally {
    globalThis.fetch = originalFetch;
    storage.restore();
  }
}

function connectionLabels(nodes: TreeNode[]): string[] {
  const labels: string[] = [];
  for (const node of nodes) {
    if (node.type === "connection") labels.push(node.label);
    if (node.children) labels.push(...connectionLabels(node.children));
  }
  return labels;
}

test("copies and pastes a root connection without writing to the OS clipboard", async () => {
  await withConnectionStore([conn("conn-1", "Main MySQL")], { groups: [], order: [{ type: "connection", id: "conn-1" }] }, async (store) => {
    assert.equal(store.copyConnectionsToTreeClipboard(["conn-1"]), 1);
    assert.equal(store.treeClipboard?.kind, "connection-copy");

    const pasted = await store.pasteConnectionClipboard(null);

    assert.equal(pasted, 1);
    assert.equal(store.connections.length, 2);
    assert.deepEqual(connectionLabels(store.treeNodes), ["Main MySQL", "Main MySQL (Copy)"]);
    assert.notEqual(store.connections[0].id, store.connections[1].id);
  });
});

test("pastes a copied grouped connection into the same group when that group is targeted", async () => {
  const originalConnection = conn("conn-1", "Grouped MySQL");
  const layout: SidebarLayout = {
    groups: [{ id: "group-1", name: "Group", collapsed: false }],
    order: [{ type: "group", id: "group-1", children: [{ type: "connection", id: "conn-1" }] }],
  };

  await withConnectionStore([originalConnection], layout, async (store) => {
    assert.equal(store.copyConnectionsToTreeClipboard(["conn-1"]), 1);
    const pasted = await store.pasteConnectionClipboard();

    assert.equal(pasted, 1);
    assert.equal(store.treeNodes[0].type, "connection-group");
    assert.deepEqual(
      store.treeNodes[0].children?.map((node) => node.label),
      ["Grouped MySQL", "Grouped MySQL (Copy)"],
    );
  });
});

test("pastes copied connections into an explicitly selected target group", async () => {
  const layout: SidebarLayout = {
    groups: [
      { id: "group-1", name: "Source", collapsed: false },
      { id: "group-2", name: "Target", collapsed: false },
    ],
    order: [
      { type: "group", id: "group-1", children: [{ type: "connection", id: "conn-1" }] },
      { type: "group", id: "group-2", children: [] },
    ],
  };

  await withConnectionStore([conn("conn-1", "Source MySQL")], layout, async (store) => {
    store.copyConnectionsToTreeClipboard(["conn-1"]);
    await store.pasteConnectionClipboard("group-2");

    assert.equal(store.treeNodes[1].type, "connection-group");
    assert.deepEqual(
      store.treeNodes[1].children?.map((node) => node.label),
      ["Source MySQL (Copy)"],
    );
  });
});

test("pastes multiple copied connections in the copied order", async () => {
  const layout: SidebarLayout = {
    groups: [],
    order: [
      { type: "connection", id: "conn-1" },
      { type: "connection", id: "conn-2" },
    ],
  };

  await withConnectionStore([conn("conn-1", "First"), conn("conn-2", "Second")], layout, async (store) => {
    store.copyConnectionsToTreeClipboard(["conn-2", "conn-1"]);
    await store.pasteConnectionClipboard(null);

    assert.deepEqual(connectionLabels(store.treeNodes), ["First", "Second", "Second (Copy)", "First (Copy)"]);
  });
});

test("pasted connections do not claim the fixed SSH local port of the original", async () => {
  const proxy: TransportLayerConfig = { type: "proxy", id: "proxy", proxy_type: "socks5", host: "proxy.example.com", port: 1080 };
  const tunnel: TransportLayerConfig = { type: "ssh", id: "bastion", host: "bastion.example.com", port: 22, user: "root" };
  const original: ConnectionConfig = { ...conn("conn-1", "Tunnelled MySQL"), transport_layers: [{ ...tunnel, local_port: 4059 }, proxy] };

  await withConnectionStore([original], { groups: [], order: [{ type: "connection", id: "conn-1" }] }, async (store) => {
    store.copyConnectionsToTreeClipboard(["conn-1"]);
    await store.pasteConnectionClipboard(null);

    assert.deepEqual(store.connections.find((connection) => connection.name === "Tunnelled MySQL (Copy)")?.transport_layers, [tunnel, proxy]);
    assert.deepEqual(store.connections.find((connection) => connection.id === "conn-1")?.transport_layers, [{ ...tunnel, local_port: 4059 }, proxy]);
  });
});