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。

2.9 KB
Raw Blame
import assert from "node:assert/strict";
import { test } from "vitest";
import { buildTreeNodesFromLayout, deleteGroup, moveConnectionToGroup, reconcileLayout, reorderEntry } from "../../apps/desktop/src/lib/sidebar/sidebarLayout.ts";
import type { ConnectionConfig, SidebarLayout } from "../../apps/desktop/src/types/database.ts";

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

test("reconciles legacy grouped layout into nested children", () => {
  const layout: SidebarLayout = {
    groups: [{ id: "g1", name: "Group", collapsed: false }],
    order: [{ type: "group", id: "g1", connectionIds: ["c1"] }],
  };

  const reconciled = reconcileLayout(["c1", "c2"], layout);

  assert.deepEqual(reconciled.order, [
    { type: "group", id: "g1", children: [{ type: "connection", id: "c1" }] },
    { type: "connection", id: "c2" },
  ]);
});

test("builds nested connection group tree nodes", () => {
  const layout: SidebarLayout = {
    groups: [
      { id: "g1", name: "Parent", collapsed: false },
      { id: "g2", name: "Child", collapsed: false },
    ],
    order: [
      {
        type: "group",
        id: "g1",
        children: [{ type: "group", id: "g2", children: [{ type: "connection", id: "c1" }] }],
      },
    ],
  };

  const nodes = buildTreeNodesFromLayout(layout, [conn("c1")], new Set());

  assert.deepEqual(
    nodes.map((node) => [node.type, node.label, node.children?.[0]?.type, node.children?.[0]?.children?.[0]?.id]),
    [["connection-group", "Parent", "connection-group", "c1"]],
  );
});

test("moves connections and groups into nested groups", () => {
  const layout: SidebarLayout = {
    groups: [
      { id: "g1", name: "Parent", collapsed: false },
      { id: "g2", name: "Child", collapsed: false },
    ],
    order: [
      { type: "group", id: "g1", children: [] },
      { type: "group", id: "g2", children: [] },
      { type: "connection", id: "c1" },
    ],
  };

  const withNestedGroup = reorderEntry(layout, "g2", "g1", "inside");
  const withNestedConnection = moveConnectionToGroup(withNestedGroup, "c1", "g2");

  assert.deepEqual(withNestedConnection.order, [
    {
      type: "group",
      id: "g1",
      children: [{ type: "group", id: "g2", children: [{ type: "connection", id: "c1" }] }],
    },
  ]);
});

test("deleting a parent group removes nested child groups and promotes their connections", () => {
  const layout: SidebarLayout = {
    groups: [
      { id: "g1", name: "Parent", collapsed: false },
      { id: "g2", name: "Child", collapsed: false },
    ],
    order: [{ type: "group", id: "g1", children: [{ type: "group", id: "g2", children: [{ type: "connection", id: "c1" }] }] }],
  };

  const next = deleteGroup(layout, "g1");

  assert.deepEqual(next.groups, []);
  assert.deepEqual(next.order, [{ type: "connection", id: "c1" }]);
});