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。

9.7 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { orderItemsByPinnedTreeNodeOrder, removePinnedTreeNodesFromOrder, treeNodePinIdentity, treeNodePinKey } from "@/lib/app/pinnedItems";
import { buildMongoObjectBrowserRows, buildObjectBrowserRows, canonicalizeObjectBrowserPinnedTreeNodeIdentity, objectBrowserRowLegacyPinnedTreeNodeIds, objectBrowserRowMatchesPinnedTreeNode, sortObjectBrowserRows, type ObjectBrowserRow } from "@/lib/table/objectBrowserRows";
import type { TreeNode } from "@/types/database";

describe("buildObjectBrowserRows", () => {
  it("keeps a schema index distinct from a table and preserves its exact parent identity", () => {
    const rows = buildObjectBrowserRows({
      objects: [{ name: " Ix (a) ", object_type: "INDEX", schema: "Index Owner", parent_schema: "Table Owner", parent_name: " T(a) " }],
      database: "database",
      fallbackSchema: "Index Owner",
    });

    expect(rows).toHaveLength(1);
    expect(rows[0]).toMatchObject({ name: " Ix (a) ", displayName: " Ix (a) ", type: "INDEX", schema: "Index Owner", parentSchema: "Table Owner", parentName: " T(a) " });
    expect(rows[0]?.partitionParentSchema).toBeUndefined();
    expect(rows[0]?.partitionParentName).toBeUndefined();
  });

  it("preserves a resolved SQLite attached-database alias on every row", () => {
    const rows = buildObjectBrowserRows({
      objects: [{ name: "events", object_type: "TABLE" }],
      database: "analytics",
      fallbackSchema: "analytics",
      rowSchema: "analytics",
    });

    expect(rows).toHaveLength(1);
    expect(rows[0]?.schema).toBe("analytics");
  });

  it("keeps non-schema database rows unqualified when no row namespace is resolved", () => {
    const rows = buildObjectBrowserRows({
      objects: [{ name: "events", object_type: "TABLE" }],
      database: "app",
      fallbackSchema: "app",
    });

    expect(rows[0]?.schema).toBeUndefined();
  });

  it("preserves nullable view validity in browser rows", () => {
    const rows = buildObjectBrowserRows({
      objects: [
        { name: "valid_view", object_type: "VIEW", valid: true },
        { name: "invalid_view", object_type: "VIEW", valid: false },
        { name: "unknown_view", object_type: "VIEW", valid: null },
      ],
      database: "app",
      fallbackSchema: "app",
    });

    expect(rows.map((row) => row.valid)).toEqual([true, false, null]);
  });

  it("preserves routine and package validity in browser rows", () => {
    const rows = buildObjectBrowserRows({
      objects: [
        { name: "valid_proc", object_type: "PROCEDURE", valid: true },
        { name: "invalid_proc", object_type: "PROCEDURE", valid: false },
        { name: "invalid_func", object_type: "FUNCTION", valid: false },
        { name: "invalid_pkg", object_type: "PACKAGE", valid: false },
      ],
      database: "app",
      fallbackSchema: "app",
    });

    expect(rows.map((row) => row.valid)).toEqual([true, false, false, false]);
  });
});

describe("Object Browser pinned ordering", () => {
  const context = { connectionId: "conn", database: "app", schema: "public" };

  function tableNode(name: string, schema = "public"): TreeNode {
    return {
      id: `conn:app:${schema}:tables:${name}`,
      label: name,
      type: "table",
      connectionId: "conn",
      database: "app",
      schema,
    };
  }

  function orderRows(rows: ObjectBrowserRow[], pinnedNodes: TreeNode[], extraPinnedKeys: string[] = []): ObjectBrowserRow[] {
    const pinnedOrder = [...extraPinnedKeys, ...pinnedNodes.map(treeNodePinKey)];
    return orderItemsByPinnedTreeNodeOrder(rows, pinnedOrder, (row, identity) => objectBrowserRowMatchesPinnedTreeNode(row, identity, context));
  }

  it("uses the sidebar's persisted custom pin order before the selected name sort", () => {
    const rows = buildObjectBrowserRows({
      objects: ["a1", "a2", "a3", "a4", "a5", "aaa"].map((name) => ({ name, object_type: "TABLE", schema: "public" })),
      database: "app",
      fallbackSchema: "public",
      rowSchema: "public",
    });
    const sorted = sortObjectBrowserRows(rows, "name", "asc");
    const pinnedNodes = ["aaa", "a5", "a4", "a3", "a2", "a1"].map((name) => tableNode(name));

    const ordered = orderRows(sorted, pinnedNodes, [treeNodePinKey({ ...tableNode("other"), connectionId: "other-connection" })]);

    expect(ordered.map((row) => row.name)).toEqual(["aaa", "a5", "a4", "a3", "a2", "a1"]);
  });

  it("keeps unpinned rows in the Object Browser's selected column order", () => {
    const rows: ObjectBrowserRow[] = [
      { id: "a1", name: "a1", displayName: "a1", schema: "public", type: "TABLE", totalBytes: 30 },
      { id: "a2", name: "a2", displayName: "a2", schema: "public", type: "TABLE", totalBytes: 40 },
      { id: "a3", name: "a3", displayName: "a3", schema: "public", type: "TABLE", totalBytes: 10 },
      { id: "a4", name: "a4", displayName: "a4", schema: "public", type: "TABLE", totalBytes: 20 },
    ];
    const sorted = sortObjectBrowserRows(rows, "totalBytes", "desc");

    const ordered = orderRows(sorted, [tableNode("a3"), tableNode("a1")]);

    expect(ordered.map((row) => row.name)).toEqual(["a3", "a1", "a2", "a4"]);
  });

  it("matches a sidebar database child when Object Browser reports the database name as its schema", () => {
    const row: ObjectBrowserRow = { id: "a1", name: "a1", displayName: "a1", schema: "app", type: "TABLE" };
    const sidebarNode = tableNode("a1", "");

    expect(objectBrowserRowMatchesPinnedTreeNode(row, treeNodePinIdentity(sidebarNode), { connectionId: "conn", database: "app", schema: "app" })).toBe(true);
  });

  it("clears the database-as-schema alias when an Object Browser object is deleted", () => {
    const sidebarNode = tableNode("events", "");
    const objectBrowserNode: TreeNode = { ...sidebarNode, id: "object-browser:app:events", schema: "app" };
    const canonicalize = canonicalizeObjectBrowserPinnedTreeNodeIdentity({ connectionId: "conn", database: "app" });

    const remainingOrder = removePinnedTreeNodesFromOrder([treeNodePinKey(sidebarNode)], [objectBrowserNode], canonicalize);

    expect(remainingOrder).toEqual([]);
  });

  it("reconstructs legacy sidebar IDs for an unloaded Object Browser row", () => {
    const row: ObjectBrowserRow = { id: "object-browser:events:0", name: "events", displayName: "events", schema: "public", type: "TABLE" };
    const ids = objectBrowserRowLegacyPinnedTreeNodeIds(row, { connectionId: "conn", database: "app", schema: "public", sidebarParentId: "conn:app:public" });

    expect(ids).toEqual(expect.arrayContaining(["conn:app:public:events", "conn:app:public:__tables:public:events"]));
  });

  it("does not confuse same-name objects across schemas or routine overloads", () => {
    const tableRow: ObjectBrowserRow = { id: "orders", name: "orders", displayName: "orders", schema: "public", type: "TABLE" };
    expect(objectBrowserRowMatchesPinnedTreeNode(tableRow, treeNodePinIdentity(tableNode("orders", "archive")), context)).toBe(false);
    expect(objectBrowserRowMatchesPinnedTreeNode(tableRow, treeNodePinIdentity(tableNode("orders", "public")), context)).toBe(true);

    const routineRow: ObjectBrowserRow = { id: "run-int", name: "run", displayName: "run(integer)", schema: "public", type: "FUNCTION", signature: "integer" };
    const routineNode: TreeNode = {
      id: "conn:app:public:functions:run:text",
      label: "run(text)",
      objectName: "run",
      signature: "text",
      type: "function",
      connectionId: "conn",
      database: "app",
      schema: "public",
    };
    expect(objectBrowserRowMatchesPinnedTreeNode(routineRow, treeNodePinIdentity(routineNode), context)).toBe(false);
    expect(objectBrowserRowMatchesPinnedTreeNode(routineRow, treeNodePinIdentity({ ...routineNode, id: "conn:app:public:functions:run:integer", label: "run(integer)", signature: "integer" }), context)).toBe(true);
  });
});

describe("buildMongoObjectBrowserRows", () => {
  it("maps collection kinds and keeps collection names searchable", () => {
    const rows = buildMongoObjectBrowserRows({
      collections: [
        { name: "users", kind: "collection" },
        { name: "active_users", kind: "view" },
        { name: "metrics", kind: "timeseries" },
      ],
      database: "app",
    });

    expect(rows.map((row) => ({ name: row.name, type: row.type, collectionKind: row.collectionKind }))).toEqual([
      { name: "users", type: "TABLE", collectionKind: "collection" },
      { name: "active_users", type: "VIEW", collectionKind: "view" },
      { name: "metrics", type: "TABLE", collectionKind: "timeseries" },
    ]);
  });

  it("sorts collections by document count and on-disk bytes once stats are merged", () => {
    const stats = [
      { name: "stores", estimatedRows: 50, totalBytes: 40_960 },
      { name: "customer_events", estimatedRows: 30_000, totalBytes: 1_912_832 },
      { name: "orders_10k", estimatedRows: 10_000, totalBytes: 901_120 },
    ];
    const rows = buildMongoObjectBrowserRows({ collections: stats.map(({ name }) => ({ name, kind: "collection" })), database: "app" }).map((row, index) => ({
      ...row,
      estimatedRows: stats[index]!.estimatedRows,
      totalBytes: stats[index]!.totalBytes,
    }));

    expect(sortObjectBrowserRows(rows, "totalBytes", "desc").map((row) => row.name)).toEqual(["customer_events", "orders_10k", "stores"]);
    expect(sortObjectBrowserRows(rows, "estimatedRows", "desc").map((row) => row.name)).toEqual(["customer_events", "orders_10k", "stores"]);
  });

  it("preserves distinct MongoDB collection identifiers verbatim", () => {
    const rows = buildMongoObjectBrowserRows({
      collections: [{ name: " users " }, { name: "users" }, { name: " " }, { name: "" }],
      database: "app",
    });

    expect(rows.map((row) => row.name)).toEqual([" users ", "users", " "]);
    expect(new Set(rows.map((row) => row.id))).toHaveLength(3);
  });
});