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。

8.5 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { buildDatabaseSavedSqlRootNode, decorateDatabaseSavedSqlTreeNodes, indexSavedSqlFilesByDatabase, savedSqlFilesForDatabase, stripDatabaseSavedSqlTreeNodes, withDatabaseSavedSqlRoot } from "@/lib/savedSql/savedSqlDatabaseTree";
import { expandSidebarLocatePath, findNodePathForTarget, findSidebarNodeForTarget } from "@/lib/sidebar/sidebarActiveTabTarget";
import { flattenTree } from "@/composables/useFlatTree";
import type { SavedSqlFile, TreeNode } from "@/types/database";

function file(input: Partial<SavedSqlFile> & Pick<SavedSqlFile, "id" | "name" | "connectionId" | "database">): SavedSqlFile {
  return {
    sql: "select 1",
    createdAt: "2026-08-12T00:00:00.000Z",
    updatedAt: "2026-08-12T00:00:00.000Z",
    ...input,
  };
}

const files = [
  file({ id: "orders-10", name: "orders-10.sql", connectionId: "conn-1", database: "app", folderId: "reports" }),
  file({ id: "orders-2", name: "orders-2.sql", connectionId: "conn-1", database: "app" }),
  file({ id: "analytics", name: "analytics.sql", connectionId: "conn-1", database: "analytics" }),
  file({ id: "other-connection", name: "orders.sql", connectionId: "conn-2", database: "app" }),
];

describe("savedSqlFilesForDatabase", () => {
  it("filters by the persisted connection and database association across all SQL library folders", () => {
    expect(savedSqlFilesForDatabase(files, { connectionId: "conn-1", database: "app" }).map((item) => item.id)).toEqual(["orders-2", "orders-10"]);
  });

  it("keeps each original SQL before its naturally ordered copies", () => {
    const copiedFiles = [
      file({ id: "query-5-copy-10", name: "query_5_copy10.sql", connectionId: "conn-1", database: "app" }),
      file({ id: "query-10", name: "query_10.sql", connectionId: "conn-1", database: "app" }),
      file({ id: "query-5-copy-1", name: "query_5_copy1.sql", connectionId: "conn-1", database: "app" }),
      file({ id: "query-2", name: "query_2.sql", connectionId: "conn-1", database: "app" }),
      file({ id: "query-5", name: "query_5.sql", connectionId: "conn-1", database: "app" }),
      file({ id: "query-5-copy-2", name: "query_5_copy2.sql", connectionId: "conn-1", database: "app" }),
    ];

    expect(savedSqlFilesForDatabase(copiedFiles, { connectionId: "conn-1", database: "app" }).map((item) => item.name)).toEqual(["query_2.sql", "query_5.sql", "query_5_copy1.sql", "query_5_copy2.sql", "query_5_copy10.sql", "query_10.sql"]);
  });

  it("separates same-named databases by catalog and indexes all files once", () => {
    const catalogFiles = [
      file({ id: "default", name: "default.sql", connectionId: "conn-1", database: "analytics" }),
      file({ id: "hive", name: "hive.sql", connectionId: "conn-1", catalog: "hive", database: "analytics" }),
      file({ id: "iceberg", name: "iceberg.sql", connectionId: "conn-1", catalog: "iceberg", database: "analytics" }),
    ];
    const index = indexSavedSqlFilesByDatabase(catalogFiles);

    expect(savedSqlFilesForDatabase(index, { connectionId: "conn-1", database: "analytics" }).map((item) => item.id)).toEqual(["default"]);
    expect(savedSqlFilesForDatabase(index, { connectionId: "conn-1", catalog: "hive", database: "analytics" }).map((item) => item.id)).toEqual(["hive"]);
    expect(savedSqlFilesForDatabase(index, { connectionId: "conn-1", catalog: "iceberg", database: "analytics" }).map((item) => item.id)).toEqual(["iceberg"]);
  });
});

describe("database saved SQL tree", () => {
  const database: TreeNode = {
    id: "conn-1:app",
    label: "app",
    type: "database",
    connectionId: "conn-1",
    database: "app",
    children: [],
  };

  it("keeps a visible Queries node even when the database has no saved SQL", () => {
    const root = buildDatabaseSavedSqlRootNode(database, []);
    expect(root).toMatchObject({ id: "conn-1:app:__queries", label: "tree.queries", type: "saved-sql-root", children: [] });
  });

  it("preserves database schema on the saved-sql-root node", () => {
    const root = buildDatabaseSavedSqlRootNode({ ...database, schema: "public" }, []);
    expect(root).toMatchObject({
      id: "conn-1:app:__queries",
      type: "saved-sql-root",
      database: "app",
      schema: "public",
    });
  });

  it("collapses the Queries node by default when opening a connection", () => {
    expect(buildDatabaseSavedSqlRootNode(database, files)?.isExpanded).toBe(false);
    expect(buildDatabaseSavedSqlRootNode(database, [])?.isExpanded).toBe(false);
  });

  it("adds Queries after database metadata and preserves its expansion state", () => {
    const existingRoot = { ...buildDatabaseSavedSqlRootNode(database, files)!, isExpanded: false };
    const decorated = decorateDatabaseSavedSqlTreeNodes(
      [
        {
          ...database,
          children: [{ id: "conn-1:app:tables", label: "tree.tables", type: "group-tables" }],
        },
      ],
      files,
      [{ ...database, children: [existingRoot] }],
    );

    expect(decorated[0].children?.map((child) => child.type)).toEqual(["group-tables", "saved-sql-root"]);
    expect(decorated[0].children?.at(-1)).toMatchObject({ isExpanded: false, children: [{ savedSqlId: "orders-2" }, { savedSqlId: "orders-10" }] });
  });

  it("reuses unchanged database children when the saved SQL index changes elsewhere", () => {
    const existingChildren = [{ id: "conn-1:app:tables", label: "tree.tables", type: "group-tables" as const }];
    const existing = { ...database, children: [...existingChildren, buildDatabaseSavedSqlRootNode(database, files)!] };
    const next = withDatabaseSavedSqlRoot({ ...database, children: existing.children }, existing.children, indexSavedSqlFilesByDatabase([...files, file({ id: "analytics-2", name: "analytics-2.sql", connectionId: "conn-1", database: "analytics" })]));

    expect(next).toEqual(existing.children);
    expect(next[0]).toBe(existingChildren[0]);
    expect(next[1]).toBe(existing.children[1]);
  });

  it("removes runtime saved SQL nodes before metadata is cached", () => {
    const decorated = decorateDatabaseSavedSqlTreeNodes([database], files);
    expect(stripDatabaseSavedSqlTreeNodes(decorated)).toEqual([{ ...database, children: [] }]);
  });

  it("renders a saved SQL only below its exact catalog database", () => {
    const catalogFiles = [file({ id: "hive", name: "hive.sql", connectionId: "conn-1", catalog: "hive", database: "app" })];
    const defaultRoot = buildDatabaseSavedSqlRootNode(database, catalogFiles);
    const hiveRoot = buildDatabaseSavedSqlRootNode({ ...database, id: "conn-1:hive:app", catalog: "hive" }, catalogFiles);

    expect(defaultRoot?.children).toEqual([]);
    expect(hiveRoot).toMatchObject({ catalog: "hive", children: [{ catalog: "hive", savedSqlId: "hive" }] });
  });
});

describe("saved SQL locate expansion", () => {
  it.each([undefined, "external"])("reveals a saved file through a collapsed runtime Queries node (catalog: %s)", (catalog) => {
    const saved = file({ id: "saved", name: "report.sql", connectionId: "conn", database: "app", catalog });
    const database: TreeNode = { id: "db", type: "database", label: "app", connectionId: "conn", database: "app", catalog, isExpanded: false };
    const queries = buildDatabaseSavedSqlRootNode(database, [saved])!;
    database.children = [queries];
    const connection: TreeNode = { id: "conn", type: "connection", label: "Connection", connectionId: "conn", isExpanded: false, children: [database] };
    const tree = [connection];
    const target = { type: "saved-sql-file" as const, savedSqlId: saved.id };
    const path = findNodePathForTarget(target, tree)!;
    expect(findSidebarNodeForTarget(target, flattenTree(tree))).toBeNull();
    // Database/connection metadata has loaded; Queries is synthesized locally
    // and intentionally has no metadata load marker.
    const loaded = new Set([connection.id, database.id]);
    expandSidebarLocatePath(path, (node) => loaded.has(node.id));
    expect(findSidebarNodeForTarget(target, flattenTree(tree))?.node.savedSqlId).toBe(saved.id);
    expect(queries.isExpanded).toBe(true);
    expect(path[path.length - 1]!.isExpanded).not.toBe(true);
  });

  it("keeps unloaded object leaves collapsed when revealing their parent path", () => {
    const table: TreeNode = { id: "table", type: "table", label: "orders", isExpanded: false };
    const database: TreeNode = { id: "db", type: "database", label: "app", isExpanded: false, children: [table] };
    expandSidebarLocatePath([database, table], (node) => node.id === database.id);
    expect(flattenTree([database]).map((row) => row.id)).toEqual(["db", "table"]);
    expect(table.isExpanded).toBe(false);
  });
});