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.0 KB
Raw Blame
// @vitest-environment happy-dom
import { createApp, defineComponent, h, nextTick, reactive } from "vue";
import { createPinia, setActivePinia } from "pinia";
import { createI18n } from "vue-i18n";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { QueryResult, QueryTab } from "@/types/database";

vi.mock("@/components/editor/QueryEditor.vue", () => ({ default: { render: () => null } }));
vi.mock("@/components/transfer/QueryResultTransferDialog.vue", () => ({ __esModule: true, default: { render: () => null } }));
vi.mock("@/components/grid/DataGrid.vue", () => ({ __esModule: true, default: { render: () => h("div", { "data-test": "data-grid" }) } }));

import ContentArea from "../ContentArea.vue";
import { useConnectionStore } from "@/stores/connectionStore";
import { useQueryStore } from "@/stores/queryStore";

const cleanups: Array<() => void> = [];
afterEach(() => {
  cleanups.splice(0).forEach((cleanup) => cleanup());
  document.body.replaceChildren();
  window.localStorage?.clear();
});

async function mountResults(results: QueryResult[], activeIndex: number) {
  const pinia = createPinia();
  setActivePinia(pinia);
  const connection = { id: "sqlserver", name: "SQL Server", db_type: "sqlserver" as const, host: "localhost", port: 1433, username: "", password: "" };
  useConnectionStore().connections = [connection];
  const tab = reactive<QueryTab>({ id: "query", title: "Query", connectionId: connection.id, database: "app", mode: "query", sql: "EXEC demo", isExecuting: false, results, result: results[activeIndex], activeResultIndex: activeIndex });
  useQueryStore().tabs.push(tab);
  const state = reactive({ view: "result" as "result" | "messages" });
  const host = document.createElement("div");
  document.body.appendChild(host);
  const app = createApp(
    defineComponent({
      setup: () => () =>
        h(ContentArea, {
          activeTab: tab,
          activeConnection: connection,
          activeOutputView: state.view,
          executableSql: "",
          formatSqlRequest: null,
          compressSqlRequest: null,
          selectedSql: "",
          cursorPos: 0,
          resultOnly: true,
          blockDangerousRedisCommands: false,
          "onUpdate:activeOutputView": (_tabId: string, view: string) => {
            state.view = view as typeof state.view;
          },
        }),
    }),
  );
  app.use(pinia);
  app.use(createI18n({ legacy: false, locale: "en", messages: { en: { tabs: { resultN: "Result {n}", allResults: "All results ({count})" } } }, missingWarn: false, fallbackWarn: false }));
  app.mount(host);
  cleanups.push(() => app.unmount());
  await nextTick();
  await nextTick();
  return { host, state, tab };
}

describe("SQL Server messages in the result surface", () => {
  const message: QueryResult = { columns: ["Message"], rows: [["before notice"]], affected_rows: 0, execution_time_ms: 1, server_message: true };
  const data: QueryResult = { columns: ["Message"], rows: [["real data"]], affected_rows: 0, execution_time_ms: 1 };

  it("shows only three result tabs and preserves both message blocks in Messages", async () => {
    const { host, state } = await mountResults([message, data, { ...message, rows: [["after notice"]] }, { ...data, rows: [] }, data], 1);
    expect([...host.querySelectorAll(".result-set-scroll button")].map((button) => button.textContent?.trim())).toEqual(["Result 1", "Result 2", "Result 3"]);
    state.view = "messages";
    await nextTick();
    expect(host.textContent).toContain("before notice");
    expect(host.textContent).toContain("after notice");
    expect(host.textContent).not.toContain("real data");
  });

  it.each([false, true])("redirects restored message output without a fake grid (mixed=%s)", async (mixed) => {
    const { host, state } = await mountResults(mixed ? [message, data] : [message], 0);
    expect(state.view).toBe("messages");
    expect(host.textContent).toContain("before notice");
    expect(host.querySelector('[data-test="data-grid"]')).toBeNull();
  });
});

describe("live server output", () => {
  it("opens Messages while running and retains output after cancellation", async () => {
    const { host, state, tab } = await mountResults([], 0);
    tab.isExecuting = true;
    await nextTick();
    tab.liveQueryMessages = [{ severity: "NOTICE", message: "running output" }];
    await nextTick();
    expect(state.view).toBe("messages");
    expect(host.textContent).toContain("running output");
    tab.isExecuting = false;
    await nextTick();
    expect(host.textContent).toContain("running output");
    expect(host.querySelector('[data-test="data-grid"]')).toBeNull();
  });

  it("marks autoSwitchedToMessagesDuringExecution in tab uiState when live messages arrive (#11822)", async () => {
    const { state, tab } = await mountResults([], 0);
    tab.isExecuting = true;
    await nextTick();
    tab.liveQueryMessages = [{ severity: "INFO", message: "function info message" }];
    await nextTick();
    expect(state.view).toBe("messages");
    expect(useQueryStore().tabs.find((t) => t.id === tab.id)?.uiState?.autoSwitchedToMessagesDuringExecution).toBe(true);
  });

  it("clears autoSwitchedToMessagesDuringExecution when user explicitly selects an output view (#11822)", async () => {
    const { host, state, tab } = await mountResults([], 0);
    tab.isExecuting = true;
    await nextTick();
    tab.liveQueryMessages = [{ severity: "INFO", message: "function info message" }];
    await nextTick();
    expect(state.view).toBe("messages");
    expect(useQueryStore().tabs.find((t) => t.id === tab.id)?.uiState?.autoSwitchedToMessagesDuringExecution).toBe(true);

    tab.result = { columns: ["val"], rows: [[1]], affected_rows: 0, execution_time_ms: 1 };
    tab.results = [tab.result];
    await nextTick();

    const buttons = host.querySelectorAll<HTMLButtonElement>("[data-query-result-view-switcher] button");
    buttons[0]?.click();
    await nextTick();
    expect(state.view).toBe("result");
    expect(useQueryStore().tabs.find((t) => t.id === tab.id)?.uiState?.autoSwitchedToMessagesDuringExecution).toBe(false);
  });
});