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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
import {
groupMcpScopeConnections,
isMcpPolicyMutationBlocked,
matchesMcpSearchQuery,
MCP_CAPABILITY_ROWS,
MCP_EXECUTION_MODE_COLUMNS,
MCP_TOOL_OPTIONS,
mcpExecutionModeFromPolicy,
mcpPolicyFieldsForExecutionMode,
toggleMcpAllowedConnectionId,
toggleMcpAllowedToolName,
updateMcpAllowedConnectionIds,
} from "@/lib/mcp/mcpPolicySelection";
const mcpServerSource = readFileSync(new URL("../../../../../../crates/dbx-mcp/src/server.rs", import.meta.url), "utf8");
describe("MCP execution permission selection", () => {
it("maps the persisted policy to the three UI modes", () => {
expect(mcpExecutionModeFromPolicy({ readOnly: true, allowDangerousSql: false })).toBe("read_only");
expect(mcpExecutionModeFromPolicy({ readOnly: false, allowDangerousSql: false })).toBe("safe_write");
expect(mcpExecutionModeFromPolicy({ readOnly: false, allowDangerousSql: true })).toBe("high_risk_write");
});
it("treats read-only as authoritative when legacy state also allows dangerous SQL", () => {
expect(mcpExecutionModeFromPolicy({ readOnly: true, allowDangerousSql: true })).toBe("read_only");
});
it("maps every UI mode to a complete atomic policy update", () => {
expect(mcpPolicyFieldsForExecutionMode("read_only")).toEqual({ readOnly: true, allowDangerousSql: false });
expect(mcpPolicyFieldsForExecutionMode("safe_write")).toEqual({ readOnly: false, allowDangerousSql: false });
expect(mcpPolicyFieldsForExecutionMode("high_risk_write")).toEqual({ readOnly: false, allowDangerousSql: true });
});
it("presents the stable internal modes as three user-facing columns", () => {
expect(MCP_EXECUTION_MODE_COLUMNS.map((column) => column.mode)).toEqual(["read_only", "safe_write", "high_risk_write"]);
});
it("shows the risk-based capability boundary without changing enforcement semantics", () => {
expect(MCP_CAPABILITY_ROWS).toEqual([
{ labelKey: "settings.mcpCapabilityRead", read_only: true, safe_write: true, high_risk_write: true },
{ labelKey: "settings.mcpCapabilityScopedMutation", read_only: false, safe_write: true, high_risk_write: true },
{ labelKey: "settings.mcpCapabilityBroadMutation", read_only: false, safe_write: false, high_risk_write: true },
{ labelKey: "settings.mcpCapabilitySchemaAdmin", read_only: false, safe_write: false, high_risk_write: true },
{ labelKey: "settings.mcpCapabilityConnectionCreate", read_only: false, safe_write: true, high_risk_write: true },
{ labelKey: "settings.mcpCapabilityConnectionRemove", read_only: false, safe_write: false, high_risk_write: true },
]);
});
});
describe("MCP policy connection selection", () => {
it("turns allow-all into an explicit list when one connection is removed", () => {
expect(toggleMcpAllowedConnectionId(null, ["one", "two", "three"], "two", false)).toEqual(["one", "three"]);
});
it("updates an existing explicit allowlist", () => {
expect(toggleMcpAllowedConnectionId(["one"], ["one", "two"], "two", true)).toEqual(["one", "two"]);
expect(toggleMcpAllowedConnectionId(["one", "two"], ["one", "two"], "one", false)).toEqual(["two"]);
});
it("groups live and unavailable connections without losing source order", () => {
const one = { id: "one", name: "One" };
const two = { id: "two", name: "Two" };
const three = { id: "three", name: "Three" };
expect(groupMcpScopeConnections([one, two, three], ["three", "missing", "one"])).toEqual({
allowed: [one, three],
available: [two],
unavailableAllowedIds: ["missing"],
});
expect(groupMcpScopeConnections([one, two], null)).toEqual({
allowed: [one, two],
available: [],
unavailableAllowedIds: [],
});
});
it("applies batch additions and removals while preserving unavailable IDs", () => {
expect(updateMcpAllowedConnectionIds(["one", "missing"], ["one", "two", "three"], ["two", "three"], true)).toEqual(["one", "missing", "two", "three"]);
expect(updateMcpAllowedConnectionIds(null, ["one", "two", "three"], ["one", "three"], false)).toEqual(["two"]);
});
});
describe("MCP tool permission selection", () => {
it("lists every tool registered by the MCP server", () => {
const registeredToolNames = [...mcpServerSource.matchAll(/name\s*=\s*"(dbx_[^"]+)"/g)].map((match) => match[1]).sort();
expect(MCP_TOOL_OPTIONS.map((tool) => tool.name).sort()).toEqual(registeredToolNames);
});
it("keeps the Salesforce tools individually switchable, writes included", () => {
expect(MCP_TOOL_OPTIONS.filter((tool) => tool.name.startsWith("dbx_salesforce_")).map((tool) => [tool.name, tool.labelKey])).toEqual([
["dbx_salesforce_current_user", "settings.mcpToolSalesforceCurrentUser"],
["dbx_salesforce_prepare_write", "settings.mcpToolSalesforcePrepareWrite"],
["dbx_salesforce_apply_write", "settings.mcpToolSalesforceApplyWrite"],
]);
// An admin can expose reading (identity + SOQL) without handing over the write path.
const readOnlySfdc = toggleMcpAllowedToolName(null, "dbx_salesforce_apply_write", false);
expect(toggleMcpAllowedToolName(readOnlySfdc, "dbx_salesforce_prepare_write", false)).not.toContain("dbx_salesforce_prepare_write");
expect(readOnlySfdc).toContain("dbx_salesforce_current_user");
});
it("keeps batch execution allowed when allow-all becomes an explicit allowlist", () => {
const next = toggleMcpAllowedToolName(null, "dbx_send_message", false);
expect(next).toContain("dbx_execute_batch");
expect(next).not.toContain("dbx_send_message");
});
it("keeps Kafka reading independent from message sending", () => {
const next = toggleMcpAllowedToolName(null, "dbx_send_message", false);
expect(next).toContain("dbx_peek_messages");
expect(toggleMcpAllowedToolName(next, "dbx_peek_messages", false)).not.toContain("dbx_peek_messages");
expect(toggleMcpAllowedToolName([], "dbx_peek_messages", true)).toEqual(["dbx_peek_messages"]);
});
it("lets batch execution be enabled and disabled independently", () => {
expect(toggleMcpAllowedToolName(["dbx_execute_query"], "dbx_execute_batch", true)).toEqual(["dbx_execute_query", "dbx_execute_batch"]);
expect(toggleMcpAllowedToolName(["dbx_execute_query", "dbx_execute_batch"], "dbx_execute_batch", false)).toEqual(["dbx_execute_query"]);
expect(MCP_TOOL_OPTIONS.find((tool) => tool.name === "dbx_execute_batch")?.labelKey).toBe("settings.mcpToolExecuteBatch");
});
});
describe("MCP policy settings state", () => {
it("blocks mutations while loading, saving, or displaying a load error", () => {
expect(isMcpPolicyMutationBlocked({ loading: true, saving: false, loadError: "" })).toBe(true);
expect(isMcpPolicyMutationBlocked({ loading: false, saving: true, loadError: "" })).toBe(true);
expect(isMcpPolicyMutationBlocked({ loading: false, saving: false, loadError: "unavailable" })).toBe(true);
expect(isMcpPolicyMutationBlocked({ loading: false, saving: false, loadError: "" })).toBe(false);
});
});
describe("MCP connection search", () => {
it("matches case-insensitively across text, numeric fields, and connection IDs", () => {
const values = ["MySQL Local", "mysql", "127.0.0.1", 3306, "app_db", "connection-ABC"];
expect(matchesMcpSearchQuery(" mysql ", values)).toBe(true);
expect(matchesMcpSearchQuery("3306", values)).toBe(true);
expect(matchesMcpSearchQuery("connection-abc", values)).toBe(true);
expect(matchesMcpSearchQuery("postgres", values)).toBe(false);
});
it("treats an empty query as a match and can search unavailable IDs", () => {
expect(matchesMcpSearchQuery(" ", [null, undefined])).toBe(true);
expect(matchesMcpSearchQuery("missing-id", ["missing-id-123", "Previously selected connection (unavailable)"])).toBe(true);
});
});