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.4 KB
Raw Blame
import { strict as assert } from "node:assert";
import { beforeEach, test, vi } from "vitest";

const apiMock = vi.hoisted(() => ({
  findStatementAtCursor: vi.fn(),
}));

vi.mock("@/lib/backend/api", () => apiMock);

const { resolveExecutableSqlWithBackend } = await import("../../apps/desktop/src/lib/sql/sqlExecutionTarget.ts");

beforeEach(() => {
  apiMock.findStatementAtCursor.mockReset();
});

test("mongodb backend resolution uses the current command when configured", async () => {
  apiMock.findStatementAtCursor.mockResolvedValue("db.users.insertOne({ name: 'ignored' })");

  const fullSql = 'db.users.find({ name: "Ada" });\ndb.users.find({ name: "Grace" });\ndb.users.find({ name: "Linus" });';
  const resolved = await resolveExecutableSqlWithBackend(fullSql, "", {
    mode: "current",
    cursorPos: fullSql.indexOf("Grace"),
    databaseType: "mongodb",
  });

  assert.equal(resolved, 'db.users.find({ name: "Grace" })');
  assert.equal(apiMock.findStatementAtCursor.mock.calls.length, 0);
});

test("mongodb backend resolution keeps the full text in all mode", async () => {
  const fullSql = 'db.users.find({ name: "Ada" });\ndb.users.find({ name: "Grace" });';
  const resolved = await resolveExecutableSqlWithBackend(fullSql, "", {
    mode: "all",
    cursorPos: fullSql.indexOf("Grace"),
    databaseType: "mongodb",
  });

  assert.equal(resolved, fullSql);
  assert.equal(apiMock.findStatementAtCursor.mock.calls.length, 0);
});

test("mongodb backend resolution prefers the manual selection", async () => {
  const fullSql = 'db.users.find({ name: "Ada" });\ndb.users.find({ name: "Grace" });';
  const selectedSql = 'db.users.find({ name: "Ada" })';
  const resolved = await resolveExecutableSqlWithBackend(fullSql, selectedSql, {
    mode: "current",
    cursorPos: fullSql.indexOf("Grace"),
    databaseType: "mongodb",
  });

  assert.equal(resolved, selectedSql);
  assert.equal(apiMock.findStatementAtCursor.mock.calls.length, 0);
});

test("non-mongodb backend resolution still asks the backend for the current statement", async () => {
  apiMock.findStatementAtCursor.mockResolvedValue("SELECT 2");

  const fullSql = "SELECT 1;\nSELECT 2;";
  const resolved = await resolveExecutableSqlWithBackend(fullSql, "", {
    mode: "current",
    cursorPos: fullSql.indexOf("2"),
    databaseType: "postgres",
  });

  assert.equal(resolved, "SELECT 2");
  assert.deepEqual(apiMock.findStatementAtCursor.mock.calls[0], [fullSql, fullSql.indexOf("2"), "postgres"]);
});