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。

4.3 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { completionSchemasFromTree, completionTablesFromTree } from "@/lib/metadata/completionTreeIndex";
import type { TreeNode } from "@/types/database";

describe("completionTreeIndex", () => {
  it("extracts PrestoSQL schemas and loaded grouped tables from the sidebar tree", () => {
    const tree: TreeNode[] = [
      {
        id: "conn",
        label: "PrestoSQL",
        type: "connection",
        connectionId: "conn",
        children: [
          {
            id: "conn:hive",
            label: "hive",
            type: "database",
            connectionId: "conn",
            database: "hive",
            children: [
              {
                id: "conn:hive:sales_analytics",
                label: "sales_analytics",
                type: "schema",
                connectionId: "conn",
                database: "hive",
                schema: "sales_analytics",
                children: [
                  {
                    id: "conn:hive:sales_analytics:__tables",
                    label: "tree.tables",
                    type: "group-tables",
                    connectionId: "conn",
                    database: "hive",
                    schema: "sales_analytics",
                    children: [
                      {
                        id: "conn:hive:sales_analytics:__tables:daily_revenue",
                        label: "daily_revenue",
                        type: "table",
                        connectionId: "conn",
                        database: "hive",
                        schema: "sales_analytics",
                        comment: "Daily revenue summary",
                      },
                      {
                        id: "conn:hive:sales_analytics:__tables:daily_revenue_view",
                        label: "daily_revenue_view",
                        type: "view",
                        connectionId: "conn",
                        database: "hive",
                        schema: "sales_analytics",
                      },
                      {
                        id: "conn:hive:sales_analytics:__tables:daily_revenue_mv",
                        label: "daily_revenue_mv",
                        type: "materialized_view",
                        connectionId: "conn",
                        database: "hive",
                        schema: "sales_analytics",
                      },
                    ],
                  },
                ],
              },
            ],
          },
        ],
      },
    ];

    expect(completionSchemasFromTree(tree, "conn", "hive")).toEqual(["sales_analytics"]);
    const expected = [
      { name: "daily_revenue", schema: "sales_analytics", type: "table", detail: "→ Daily revenue summary" },
      { name: "daily_revenue_view", schema: "sales_analytics", type: "view" },
      { name: "daily_revenue_mv", schema: "sales_analytics", type: "materialized_view" },
    ];
    expect(completionTablesFromTree(tree, "conn", "hive", "sales_analytics")).toEqual(expected);
    expect(completionTablesFromTree(tree, "conn", "hive")).toEqual(expected);
  });

  it("isolates Doris internal and external catalog tables with the same database name", () => {
    const tree: TreeNode[] = [
      {
        id: "internal-orders",
        label: "orders",
        type: "table",
        connectionId: "doris",
        database: "sales",
      },
      {
        id: "hive-orders",
        label: "orders",
        type: "table",
        connectionId: "doris",
        catalog: "hive_catalog",
        database: "sales",
      },
    ];

    expect(completionTablesFromTree(tree, "doris", "sales")).toEqual([{ name: "orders", schema: undefined, type: "table" }]);
    expect(completionTablesFromTree(tree, "doris", "sales", undefined, "hive_catalog")).toEqual([{ name: "orders", catalog: "hive_catalog", schema: undefined, type: "table" }]);
  });

  it("preserves TDengine stable metadata from the sidebar tree", () => {
    const tree: TreeNode[] = [
      {
        id: "tdengine-stable",
        label: "test_tb",
        type: "table",
        tableType: "STABLE",
        connectionId: "tdengine",
        database: "issue_5685",
      },
    ];

    expect(completionTablesFromTree(tree, "tdengine", "issue_5685")).toEqual([{ name: "test_tb", schema: undefined, type: "table", tableType: "STABLE" }]);
  });
});