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。

6.3 KB
Raw Blame
import { createPinia, setActivePinia } from "pinia";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { ConnectionConfig } from "@/types/database";

function installLocalStorage() {
  const data = new Map<string, string>();
  vi.stubGlobal("localStorage", {
    getItem: vi.fn((key: string) => data.get(key) ?? null),
    setItem: vi.fn((key: string, value: string) => data.set(key, value)),
    removeItem: vi.fn((key: string) => data.delete(key)),
  });
}

function postgresConnection(overrides: Partial<ConnectionConfig> = {}): ConnectionConfig {
  return {
    id: "pg-1",
    name: "Reporting",
    db_type: "postgres",
    host: "127.0.0.1",
    port: 5432,
    username: "postgres",
    password: "secret",
    database: "testdb",
    read_only: false,
    ...overrides,
  } as ConnectionConfig;
}

async function createStores() {
  vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
  vi.doMock("@/lib/backend/api", () => ({
    checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
    disconnectDb: vi.fn().mockResolvedValue(undefined),
    deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
    loadSchemaCache: vi.fn().mockResolvedValue(null),
    saveConnections: vi.fn().mockResolvedValue(undefined),
    saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
    connectionDatabaseInfo: vi.fn().mockResolvedValue(undefined),
  }));
  const { useConnectionStore } = await import("@/stores/connectionStore");
  const { useQueryStore } = await import("@/stores/queryStore");
  return { connectionStore: useConnectionStore(), queryStore: useQueryStore() };
}

describe("connectionStore updateConnection re-syncs open query tabs' database", () => {
  beforeEach(() => {
    vi.resetModules();
    vi.unstubAllGlobals();
    installLocalStorage();
    setActivePinia(createPinia());
  });

  it("disconnects a changed MySQL codec before publishing config and preserves SQL drafts", async () => {
    const { connectionStore, queryStore } = await createStores();
    const api = await import("@/lib/backend/api");
    const connection = postgresConnection({ db_type: "mysql", url_params: "dataCharset=GBK" });
    connectionStore.connections = [connection];
    const tabId = queryStore.createTab(connection.id, "testdb", "draft");
    queryStore.updateSql(tabId, "SELECT 'unsaved';");
    vi.mocked(api.disconnectDb).mockImplementation(async () => {
      expect(connectionStore.getConfig(connection.id)?.url_params).toBe("dataCharset=GBK");
    });
    await connectionStore.updateConnection({ ...connection, url_params: "" });
    expect(api.disconnectDb).toHaveBeenCalled();
    expect(connectionStore.getConfig(connection.id)?.url_params || "").toBe("");
    expect(queryStore.tabs.find((tab) => tab.id === tabId)?.sql).toBe("SELECT 'unsaved';");
  });

  it("does not publish writable settings after codec disconnect fails", async () => {
    const { connectionStore } = await createStores();
    const api = await import("@/lib/backend/api");
    const connection = postgresConnection({ db_type: "mysql", url_params: "dataCharset=GBK" });
    connectionStore.connections = [connection];
    vi.mocked(api.disconnectDb).mockRejectedValue(new Error("disconnect failed"));
    await expect(connectionStore.updateConnection({ ...connection, url_params: "" })).rejects.toThrow("disconnect failed");
    expect(connectionStore.getConfig(connection.id)?.url_params).toBe("dataCharset=GBK");
    expect(api.saveConnections).not.toHaveBeenCalled();
  });

  it("rejects codec changes with old results without discarding them", async () => {
    const { connectionStore, queryStore } = await createStores();
    const api = await import("@/lib/backend/api");
    const connection = postgresConnection({ db_type: "mysql", url_params: "dataCharset=GBK" });
    connectionStore.connections = [connection];
    const tabId = queryStore.createTab(connection.id, "testdb", "old result");
    const tab = queryStore.tabs.find((item) => item.id === tabId)!;
    tab.result = { columns: ["text"], rows: [["缤纷彩虹"]], affected_rows: 0, execution_time_ms: 0 };
    await expect(connectionStore.updateConnection({ ...connection, url_params: "" })).rejects.toThrow();
    expect(tab.result.rows).toEqual([["缤纷彩虹"]]);
    expect(api.disconnectDb).not.toHaveBeenCalled();
    expect(api.saveConnections).not.toHaveBeenCalled();
  });

  it("re-points a tab still on the connection's old default database, but leaves a tab the user pointed elsewhere alone", async () => {
    const { connectionStore, queryStore } = await createStores();
    const connection = postgresConnection();
    connectionStore.connections = [connection];

    // Opened before the edit, still on the connection's original default database (#7905 repro).
    const staleTabId = queryStore.createTab("pg-1", "testdb", "stale query");
    queryStore.setTableMeta(staleTabId, { schema: "public", database: "testdb", tableName: "users", tableType: "TABLE", columns: [], primaryKeys: [] });
    // A tab the user explicitly pointed at a different database via the tab's own
    // connection/database switcher — must not be silently reassigned.
    const explicitTabId = queryStore.createTab("pg-1", "reporting_archive", "explicit query", "query", undefined, undefined, undefined, { forceNew: true });

    await connectionStore.updateConnection({ ...connection, database: "hive313" });

    expect(connectionStore.getConfig("pg-1")?.database).toBe("hive313");
    const staleTab = queryStore.tabs.find((tab) => tab.id === staleTabId);
    expect(staleTab?.database).toBe("hive313");
    // The stale snapshot's cached metadata/result state must not survive the switch.
    expect(staleTab?.tableMeta).toBeUndefined();

    const explicitTab = queryStore.tabs.find((tab) => tab.id === explicitTabId);
    expect(explicitTab?.database).toBe("reporting_archive");
  });

  it("does nothing to open tabs when the edited connection's database is unchanged", async () => {
    const { connectionStore, queryStore } = await createStores();
    const connection = postgresConnection();
    connectionStore.connections = [connection];
    const tabId = queryStore.createTab("pg-1", "testdb", "unaffected query");

    await connectionStore.updateConnection({ ...connection, host: "new-host.internal" });

    expect(queryStore.tabs.find((tab) => tab.id === tabId)?.database).toBe("testdb");
  });
});