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。

22.5 KB
Raw Blame
import assert from "node:assert/strict";
import { computed, toRaw } from "vue";
import { createPinia, setActivePinia } from "pinia";
import { describe, test, vi } from "vitest";
import { useConnectionStore } from "../../apps/desktop/src/stores/connectionStore.ts";
import { useQueryStore } from "../../apps/desktop/src/stores/queryStore.ts";
import { useSettingsStore } from "../../apps/desktop/src/stores/settingsStore.ts";
import type { ColumnInfo, ConnectionConfig } from "../../apps/desktop/src/types/database.ts";

vi.mock("vue-i18n", async () => {
  const actual = await vi.importActual<typeof import("vue-i18n")>("vue-i18n");
  return {
    ...actual,
    useI18n: () => ({ t: (key: string) => key }),
  };
});

vi.mock("@/composables/useToast", () => ({
  useToast: () => ({ toast: vi.fn() }),
}));

function installMemoryStorage() {
  const values = new Map<string, string>();
  const original = Object.getOwnPropertyDescriptor(globalThis, "localStorage");
  Object.defineProperty(globalThis, "localStorage", {
    configurable: true,
    value: {
      getItem: (key: string) => values.get(key) ?? null,
      setItem: (key: string, value: string) => values.set(key, value),
      removeItem: (key: string) => values.delete(key),
      clear: () => values.clear(),
    },
  });
  return () => {
    if (original) Object.defineProperty(globalThis, "localStorage", original);
    else Reflect.deleteProperty(globalThis, "localStorage");
  };
}

function conn(id: string): ConnectionConfig {
  return {
    id,
    name: id,
    db_type: "mysql",
    host: "localhost",
    port: 3306,
    username: "root",
    password: "",
  };
}

async function waitFor(predicate: () => boolean, timeoutMs = 1000) {
  const started = Date.now();
  while (!predicate()) {
    if (Date.now() - started > timeoutMs) throw new Error("timed out waiting for condition");
    await new Promise((resolve) => setTimeout(resolve, 5));
  }
}

test("data reload executes before slow metadata refresh completes", async () => {
  const restoreStorage = installMemoryStorage();
  const originalFetch = globalThis.fetch;
  const { useDataGridActions } = await import("../../apps/desktop/src/composables/useDataGridActions.ts");
  let executeBody: any;
  let resolveColumns!: (columns: ColumnInfo[]) => void;

  globalThis.fetch = (async (input, init) => {
    const url = new URL(String(input), "http://localhost");
    if (url.pathname === "/api/connection/check-health") {
      return Response.json(null);
    }
    if (url.pathname === "/api/schema/columns") {
      return Response.json(await new Promise<ColumnInfo[]>((resolve) => (resolveColumns = resolve)));
    }
    if (url.pathname === "/api/schema/indexes") {
      return Response.json([]);
    }
    if (url.pathname === "/api/query/build-table-select-sql") {
      return Response.json("SELECT * FROM `users` LIMIT 50 OFFSET 0");
    }
    if (url.pathname === "/api/query/prepare-pagination-plan") {
      const { options } = JSON.parse(String(init?.body ?? "{}"));
      assert.equal(options.sql, "SELECT * FROM `users`");
      assert.deepEqual(options.pagination, { limit: 50, offset: 0 });
      return Response.json({
        sqlToExecute: "SELECT * FROM `users` LIMIT 50;",
        pageProbeSql: "SELECT * FROM `users` LIMIT 51;",
        useAgentResultSession: false,
      });
    }
    if (url.pathname === "/api/query/execute-multi") {
      executeBody = JSON.parse(String(init?.body ?? "{}"));
      return Response.json([{ columns: ["id"], rows: [[1]], affected_rows: 0, execution_time_ms: 1 }]);
    }
    return new Response(`unexpected ${url.pathname}`, { status: 500 });
  }) as typeof fetch;

  try {
    setActivePinia(createPinia());
    const connectionStore = useConnectionStore();
    const queryStore = useQueryStore();
    connectionStore.addEphemeralConnection(conn("mysql-1"));
    const tabId = queryStore.createTab("mysql-1", "app", "users", "data");
    queryStore.setTableMeta(tabId, { tableName: "users", tableType: "TABLE", columns: [], primaryKeys: [] });
    const tab = queryStore.tabs.find((item) => item.id === tabId);
    assert.ok(tab);

    const actions = useDataGridActions(computed(() => tab));
    const reload = actions.onReloadData(tabId, undefined, undefined, undefined, undefined, 50, 0);

    await waitFor(() => !!executeBody, 5_000);
    assert.equal(executeBody.clientSessionId, tabId);
    assert.equal(executeBody.sql, "SELECT * FROM `users` LIMIT 51;");
    assert.equal(executeBody.maxRows, 51);
    assert.deepEqual(tab.result?.rows, [[1]]);

    await waitFor(() => typeof resolveColumns === "function");
    resolveColumns([
      {
        name: "id",
        data_type: "int",
        is_nullable: false,
        column_default: null,
        is_primary_key: true,
        extra: null,
      },
    ]);
    await reload;
    await waitFor(() => tab.tableMeta?.columns.length === 1);
    assert.equal(tab.tableMeta?.primaryKeys[0], "id");
  } finally {
    globalThis.fetch = originalFetch;
    restoreStorage();
  }
});

test("data reload preserves current page offset instead of resetting to page 1", async () => {
  // Verifies the fix: onReloadData called with offset=400 (page 5, pageSize=100)
  // should pass offset=400 to build-table-select-sql and store it in resultPageOffset,
  // rather than defaulting to offset=0.
  const restoreStorage = installMemoryStorage();
  const originalFetch = globalThis.fetch;
  const { useDataGridActions } = await import("../../apps/desktop/src/composables/useDataGridActions.ts");
  let buildSqlOptions: any;
  let executePagination: any;

  globalThis.fetch = (async (input, init) => {
    const url = new URL(String(input), "http://localhost");
    if (url.pathname === "/api/connection/check-health") return Response.json(null);
    if (url.pathname === "/api/schema/columns") return Response.json([]);
    if (url.pathname === "/api/schema/indexes") return Response.json([]);
    if (url.pathname === "/api/query/build-table-select-sql") {
      buildSqlOptions = JSON.parse(String(init?.body ?? "{}"))?.options;
      return Response.json(`SELECT * FROM \`orders\` LIMIT 100 OFFSET ${buildSqlOptions?.offset ?? 0}`);
    }
    if (url.pathname === "/api/query/execute-multi") {
      const body = JSON.parse(String(init?.body ?? "{}"));
      executePagination = body?.page_offset;
      return Response.json([{ columns: ["id"], rows: [[42]], affected_rows: 0, execution_time_ms: 1 }]);
    }
    return new Response(`unexpected ${url.pathname}`, { status: 500 });
  }) as typeof fetch;

  try {
    setActivePinia(createPinia());
    const connectionStore = useConnectionStore();
    const queryStore = useQueryStore();
    connectionStore.addEphemeralConnection(conn("mysql-1"));
    const tabId = queryStore.createTab("mysql-1", "app", "orders", "data");
    queryStore.setTableMeta(tabId, { tableName: "orders", tableType: "TABLE", columns: [], primaryKeys: [] });
    const tab = queryStore.tabs.find((item) => item.id === tabId);
    assert.ok(tab);

    const actions = useDataGridActions(computed(() => tab));
    // Simulate refresh from page 5 with pageSize=100 → offset=400
    await actions.onReloadData(tabId, undefined, undefined, undefined, undefined, 100, 400);

    assert.equal(buildSqlOptions?.offset, 400, "build-table-select-sql must receive offset=400 to stay on page 5");
    assert.equal(buildSqlOptions?.limit, 100, "limit must be 100");
    assert.equal(tab.resultPageOffset, 400, "resultPageOffset must be 400 after reload to keep DataGrid on page 5");
  } finally {
    globalThis.fetch = originalFetch;
    restoreStorage();
  }
});

test("explicit load-all pagination fetches and appends only the missing table segment", async () => {
  const restoreStorage = installMemoryStorage();
  const originalFetch = globalThis.fetch;
  const { useDataGridActions } = await import("../../apps/desktop/src/composables/useDataGridActions.ts");
  let buildSqlOptions: any;
  let executeBody: any;

  globalThis.fetch = (async (input, init) => {
    const url = new URL(String(input), "http://localhost");
    if (url.pathname === "/api/connection/check-health") return Response.json(null);
    if (url.pathname === "/api/query/build-table-select-sql") {
      buildSqlOptions = JSON.parse(String(init?.body ?? "{}"))?.options;
      return Response.json("SELECT * FROM `orders` LIMIT 100 OFFSET 100");
    }
    if (url.pathname === "/api/query/execute-multi") {
      executeBody = JSON.parse(String(init?.body ?? "{}"));
      return Response.json([{ columns: ["id"], rows: [[101], [102]], affected_rows: 0, execution_time_ms: 2 }]);
    }
    return new Response(`unexpected ${url.pathname}`, { status: 500 });
  }) as typeof fetch;

  try {
    setActivePinia(createPinia());
    const connectionStore = useConnectionStore();
    const queryStore = useQueryStore();
    const settingsStore = useSettingsStore();
    settingsStore.updateEditorSettings({ infiniteScroll: false, queryResultMaxRowsEnabled: true, queryResultMaxRows: 5000 });
    connectionStore.addEphemeralConnection(conn("mysql-1"));
    const tabId = queryStore.createTab("mysql-1", "app", "orders", "data");
    queryStore.setTableMeta(tabId, { tableName: "orders", tableType: "TABLE", columns: [], primaryKeys: [] });
    const tab = queryStore.tabs.find((item) => item.id === tabId);
    assert.ok(tab);
    const firstRow = [1] as (string | number | boolean | null)[];
    tab.result = { columns: ["id"], rows: [firstRow], affected_rows: 0, execution_time_ms: 1 };
    tab.resultPageLimit = 100;
    tab.resultPageOffset = 0;

    const actions = useDataGridActions(computed(() => tab));
    await actions.onPaginate(tabId, 1, 100, undefined, undefined, true);

    assert.equal(buildSqlOptions?.offset, 1);
    assert.equal(buildSqlOptions?.limit, 100);
    assert.equal(executeBody.maxRows, 100, "the backend receives one segment, not a cumulative limit");
    assert.equal(tab.result?.rows.length, 3);
    assert.equal(toRaw(tab.result?.rows[0]), firstRow, "existing row identity must survive append");
    assert.deepEqual(tab.result?.rows.slice(1), [[101], [102]]);
  } finally {
    globalThis.fetch = originalFetch;
    restoreStorage();
  }
});

test("query toolbar refresh reruns the complete multi-result SQL and keeps the active result", async () => {
  const restoreStorage = installMemoryStorage();
  const { useDataGridActions } = await import("../../apps/desktop/src/composables/useDataGridActions.ts");

  try {
    setActivePinia(createPinia());
    const queryStore = useQueryStore();
    const tabId = queryStore.createTab("mysql-1", "app", "query", "query");
    const tab = queryStore.tabs.find((item) => item.id === tabId);
    assert.ok(tab);

    const batchSql = "select * from users; select * from orders";
    const refreshedResults = [
      { columns: ["id"], rows: [[11]], affected_rows: 0, execution_time_ms: 1, sourceStatement: "select * from users" },
      { columns: ["id"], rows: [[22]], affected_rows: 0, execution_time_ms: 1, sourceStatement: "select * from orders" },
    ];
    tab.sql = batchSql;
    tab.lastExecutedSql = batchSql;
    tab.resultBaseSql = batchSql;
    tab.results = refreshedResults.map((result) => ({ ...result, rows: result.rows.map((row) => [...row]) }));
    tab.activeResultIndex = 1;
    tab.result = tab.results[1];
    tab.resultSortColumn = "id";
    tab.resultSortColumnIndex = 0;
    tab.resultSortDirection = "desc";
    tab.resultSortMode = "database";
    tab.resultSortedSql = "select * from orders order by id desc";

    const executeTabSql = vi.spyOn(queryStore, "executeTabSql").mockImplementation(async (_tabId, _sql, options) => {
      assert.equal(tab.resultSortColumn, undefined);
      assert.equal(tab.resultSortColumnIndex, undefined);
      assert.equal(tab.resultSortDirection, undefined);
      assert.equal(tab.resultSortMode, undefined);
      assert.equal(tab.resultSortedSql, undefined);
      tab.results = refreshedResults;
      if (!options?.preserveActiveResultIndex) tab.activeResultIndex = 0;
      tab.result = refreshedResults[tab.activeResultIndex ?? 0];
    });
    const actions = useDataGridActions(computed(() => tab));

    await actions.onReloadData(tabId, "select * from orders", undefined, undefined, undefined, 100, 0, "refresh");
    await actions.onReloadData(tabId, "select * from orders", undefined, undefined, undefined, 100, 0, "refresh");

    assert.equal(executeTabSql.mock.calls.length, 2);
    for (const call of executeTabSql.mock.calls) {
      assert.equal(call[1], batchSql);
      assert.deepEqual(call[2], {
        resultBaseSql: batchSql,
        resultSortedSql: undefined,
        preserveResultDuringExecution: true,
        preserveActiveResultIndex: true,
        refreshActiveResultRun: true,
      });
    }
    assert.equal(tab.results?.length, 2);
    assert.equal(tab.activeResultIndex, 1);
    assert.deepEqual(tab.result, refreshedResults[1]);
  } finally {
    restoreStorage();
  }
});

describe("multi-result preservation after submit", () => {
  function withConnectionHealthMock(
    handler: typeof fetch,
  ): typeof fetch {
    return async (input, init) => {
      const url = String(input);
      if (url === "/api/connection/check-health") {
        return Response.json(null);
      }
      return handler(input, init);
    };
  }

  test("submit edit on first result preserves the second result", async () => {
    const restoreStorage = installMemoryStorage();
    const originalFetch = globalThis.fetch;
    const { useDataGridActions } = await import(
      "../../apps/desktop/src/composables/useDataGridActions.ts",
    );

    globalThis.fetch = withConnectionHealthMock(async (input, init) => {
      const url = String(input);
      if (url === "/api/query/prepare-pagination-plan") {
        const body = JSON.parse(String(init?.body ?? "{}"));
        return new Response(
          JSON.stringify({
            sqlToExecute: body.options.sql,
            useAgentResultSession: false,
          }),
          {
            status: 200,
            headers: { "Content-Type": "application/json" },
          },
        );
      }
      if (url === "/api/query/execute-multi") {
        return new Response(
          JSON.stringify([
            {
              columns: ["id"],
              rows: [[11]],
              affected_rows: 0,
              execution_time_ms: 1,
              sourceStatement: "select * from users",
            },
          ]),
          {
            status: 200,
            headers: { "Content-Type": "application/json" },
          },
        );
      }
      if (url === "/api/query/analyze-editability") {
        return new Response(
          JSON.stringify({ editable: false, reason: "complex-source" }),
          {
            status: 200,
            headers: { "Content-Type": "application/json" },
          },
        );
      }
      return new Response("unexpected request: " + url, {
        status: 500,
      });
    });

    try {
      setActivePinia(createPinia());
      const connectionStore = useConnectionStore();
      const queryStore = useQueryStore();
      connectionStore.addEphemeralConnection(conn("mysql-1"));

      const tabId = queryStore.createTab("mysql-1", "app", "query", "query");
      const tab = queryStore.tabs.find((item) => item.id === tabId);
      assert.ok(tab);

      // Simulate prior multi-statement execution with two results
      const batchSql = "select * from users; select * from orders";
      const resultA = {
        columns: ["id"],
        rows: [[1]],
        affected_rows: 0,
        execution_time_ms: 1,
        sourceStatement: "select * from users",
      };
      const resultB = {
        columns: ["id"],
        rows: [[2]],
        affected_rows: 0,
        execution_time_ms: 1,
        sourceStatement: "select * from orders",
      };

      tab.sql = batchSql;
      tab.lastExecutedSql = batchSql;
      tab.resultBaseSql = batchSql;
      tab.results = [resultA, resultB];
      tab.activeResultIndex = 0;
      tab.result = resultA;

      const actions = useDataGridActions(computed(() => tab));

      // Simulate DataGrid submit reload: onReloadData with the current
      // result's sourceStatement (single statement, not the full batch SQL)
      await actions.onReloadData(tabId, "select * from users");

      // The second result must be preserved
      assert.equal(tab.results?.length, 2, "both results must survive after submit");
      assert.equal(tab.activeResultIndex, 0, "activeResultIndex must stay on the first result");
      assert.equal(tab.results![0].rows[0][0], 11, "first result must be refreshed");
      assert.equal(tab.results![1].rows[0][0], 2, "second result must be the original resultB");
      assert.equal(tab.result, tab.results![0], "tab.result must point to the refreshed first result");
    } finally {
      globalThis.fetch = originalFetch;
      restoreStorage();
    }
  });

  test("submit edit on second result preserves the first result", async () => {
    const restoreStorage = installMemoryStorage();
    const originalFetch = globalThis.fetch;
    const { useDataGridActions } = await import(
      "../../apps/desktop/src/composables/useDataGridActions.ts",
    );

    globalThis.fetch = withConnectionHealthMock(async (input, init) => {
      const url = String(input);
      if (url === "/api/query/prepare-pagination-plan") {
        const body = JSON.parse(String(init?.body ?? "{}"));
        return new Response(
          JSON.stringify({
            sqlToExecute: body.options.sql,
            useAgentResultSession: false,
          }),
          {
            status: 200,
            headers: { "Content-Type": "application/json" },
          },
        );
      }
      if (url === "/api/query/execute-multi") {
        return new Response(
          JSON.stringify([
            {
              columns: ["id"],
              rows: [[22]],
              affected_rows: 0,
              execution_time_ms: 1,
              sourceStatement: "select * from orders",
            },
          ]),
          {
            status: 200,
            headers: { "Content-Type": "application/json" },
          },
        );
      }
      if (url === "/api/query/analyze-editability") {
        return new Response(
          JSON.stringify({ editable: false, reason: "complex-source" }),
          {
            status: 200,
            headers: { "Content-Type": "application/json" },
          },
        );
      }
      return new Response("unexpected request: " + url, {
        status: 500,
      });
    });

    try {
      setActivePinia(createPinia());
      const connectionStore = useConnectionStore();
      const queryStore = useQueryStore();
      connectionStore.addEphemeralConnection(conn("mysql-1"));

      const tabId = queryStore.createTab("mysql-1", "app", "query", "query");
      const tab = queryStore.tabs.find((item) => item.id === tabId);
      assert.ok(tab);

      // Simulate prior multi-statement execution with two results
      const batchSql = "select * from users; select * from orders";
      const resultA = {
        columns: ["id"],
        rows: [[1]],
        affected_rows: 0,
        execution_time_ms: 1,
        sourceStatement: "select * from users",
      };
      const resultB = {
        columns: ["id"],
        rows: [[2]],
        affected_rows: 0,
        execution_time_ms: 1,
        sourceStatement: "select * from orders",
      };

      tab.sql = batchSql;
      tab.lastExecutedSql = batchSql;
      tab.resultBaseSql = batchSql;
      tab.results = [resultA, resultB];
      tab.activeResultIndex = 1;
      tab.result = resultB;

      const actions = useDataGridActions(computed(() => tab));

      // Simulate DataGrid submit reload on the SECOND result
      await actions.onReloadData(tabId, "select * from orders");

      // The first result must be preserved
      assert.equal(tab.results?.length, 2, "both results must survive after submit on second result");
      assert.equal(tab.activeResultIndex, 1, "activeResultIndex must stay on the second result");
      assert.equal(tab.results![0].rows[0][0], 1, "first result must be the original resultA");
      assert.equal(tab.results![1].rows[0][0], 22, "second result must be refreshed");
      assert.equal(tab.result, tab.results![1], "tab.result must point to the refreshed second result");
    } finally {
      globalThis.fetch = originalFetch;
      restoreStorage();
    }
  });

  test("onPaginate uses MAX_QUERY_RESULT_MAX_ROWS when appendRequested is true", async () => {
    const restoreStorage = installMemoryStorage();
    const originalFetch = globalThis.fetch;
    const { useDataGridActions } = await import("../../apps/desktop/src/composables/useDataGridActions.ts");
    const { MAX_QUERY_RESULT_MAX_ROWS } = await import("../../apps/desktop/src/lib/dataGrid/queryResultRowLimit.ts");

    globalThis.fetch = (async (input) => {
      const url = new URL(String(input), "http://localhost");
      if (url.pathname === "/api/connection/check-health") return Response.json(null);
      if (url.pathname === "/api/query/build-table-select-sql") {
        return Response.json("SELECT * FROM `orders` LIMIT 100 OFFSET 100");
      }
      return new Response(`unexpected ${url.pathname}`, { status: 500 });
    }) as typeof fetch;

    try {
      setActivePinia(createPinia());
      const connectionStore = useConnectionStore();
      const queryStore = useQueryStore();
      const settingsStore = useSettingsStore();
      settingsStore.updateEditorSettings({
        queryResultMaxRows: 100_000,
        queryResultMaxRowsEnabled: true,
      });

      connectionStore.addEphemeralConnection(conn("mysql-1"));
      const tabId = queryStore.createTab("mysql-1", "app", "orders", "data");
      queryStore.setTableMeta(tabId, { tableName: "orders", tableType: "TABLE", columns: [], primaryKeys: [] });
      const tab = queryStore.tabs.find((item) => item.id === tabId);
      assert.ok(tab);
      tab.result = {
        columns: ["id"],
        rows: Array.from({ length: 100 }, (_, i) => [i + 1]),
        affected_rows: 0,
        execution_time_ms: 1,
      };

      let capturedOptions: any;
      vi.spyOn(queryStore, "executeTabSql").mockImplementation(async (_tabId, _sql, options) => {
        capturedOptions = options;
        return true;
      });

      const actions = useDataGridActions(computed(() => tab));

      // Normal infinite scroll append
      settingsStore.updateEditorSettings({ infiniteScroll: true });
      await actions.onPaginate(tabId, 100, 100, undefined, undefined, false);
      assert.equal(capturedOptions?.appendResult?.maxRows, 100_000);

      // Explicit load-all append
      await actions.onPaginate(tabId, 100, 99_900, undefined, undefined, true);
      assert.equal(capturedOptions?.appendResult?.maxRows, MAX_QUERY_RESULT_MAX_ROWS);
    } finally {
      globalThis.fetch = originalFetch;
      restoreStorage();
    }
  });
});