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.7 KB
Raw Blame
import { describe, expect, it, vi } from "vitest";
import { copySelectedConnectionsToClipboards, selectedConnectionDeleteTargets, selectedConnectionDisconnectTargets, selectedConnectionDuplicateTargets, selectedConnectionGroupDeleteTargets } from "@/lib/sidebar/sidebarConnectionSelection";
import type { TreeNode } from "@/types/database";

function node(id: string, type: TreeNode["type"] = "connection"): TreeNode {
  return {
    id,
    label: id,
    type,
    connectionId: type === "connection" ? id : undefined,
  };
}

describe("sidebar connection selection", () => {
  it("uses all selected connections when the current connection is selected", () => {
    const current = node("conn-2");
    const selected = [node("conn-1"), current, node("conn-3")];

    expect(selectedConnectionDuplicateTargets(current, selected).map((item) => item.connectionId)).toEqual(["conn-1", "conn-2", "conn-3"]);
  });

  it("falls back to the current connection when the selection is mixed", () => {
    const current = node("conn-1");
    const selected = [current, node("group-1", "connection-group")];

    expect(selectedConnectionDuplicateTargets(current, selected).map((item) => item.connectionId)).toEqual(["conn-1"]);
  });

  it("keeps delete and duplicate target selection aligned", () => {
    const current = node("conn-1");
    const selected = [current, node("conn-2")];

    expect(selectedConnectionDuplicateTargets(current, selected)).toEqual(selectedConnectionDeleteTargets(current, selected));
    expect(selectedConnectionDisconnectTargets(current, selected)).toEqual(selectedConnectionDeleteTargets(current, selected));
  });

  it("disconnects only the right-clicked connection when it is outside the selection", () => {
    const current = node("conn-3");
    const selected = [node("conn-1"), node("conn-2")];

    expect(selectedConnectionDisconnectTargets(current, selected).map((item) => item.connectionId)).toEqual(["conn-3"]);
  });

  it("copies selected connection names to the system clipboard for the sidebar copy shortcut", () => {
    const copyConnectionsToTreeClipboard = vi.fn(() => 2);
    const copyToSystemClipboard = vi.fn(() => Promise.resolve());

    expect(copySelectedConnectionsToClipboards([node("conn-1"), node("conn-2")], copyConnectionsToTreeClipboard, copyToSystemClipboard)).toBe(2);
    expect(copyConnectionsToTreeClipboard).toHaveBeenCalledWith(["conn-1", "conn-2"]);
    expect(copyToSystemClipboard).toHaveBeenCalledWith("conn-1\nconn-2");
  });

  it("keeps the tree clipboard copy when system clipboard access is denied", async () => {
    const copyConnectionsToTreeClipboard = vi.fn(() => 1);
    const copyToSystemClipboard = vi.fn(() => Promise.reject(new Error("denied")));

    expect(copySelectedConnectionsToClipboards([node("conn-1")], copyConnectionsToTreeClipboard, copyToSystemClipboard)).toBe(1);
    await Promise.resolve();
    expect(copyConnectionsToTreeClipboard).toHaveBeenCalledWith(["conn-1"]);
  });
});

describe("sidebar connection group selection", () => {
  it("uses all selected groups when the current group is selected", () => {
    const current = node("group-2", "connection-group");
    const selected = [node("group-1", "connection-group"), current];

    expect(selectedConnectionGroupDeleteTargets(current, selected)).toEqual(selected);
  });

  it("falls back to the current group for mixed, stale, and single selections", () => {
    const current = node("group-current", "connection-group");
    const other = node("group-other", "connection-group");

    expect(selectedConnectionGroupDeleteTargets(current, [other])).toEqual([current]);
    expect(selectedConnectionGroupDeleteTargets(current, [current, node("conn-1")])).toEqual([current]);
    expect(selectedConnectionGroupDeleteTargets(current, [current])).toEqual([current]);
  });
});