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.6 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { canCancelQueryExecution, isActiveResultLoading, shouldShowCancelAction, type QueryResultLoadingStateLike } from "@/lib/sql/queryExecutionState";

function state(overrides: Partial<QueryResultLoadingStateLike> = {}): QueryResultLoadingStateLike {
  return {
    isExecuting: false,
    ...overrides,
  };
}

describe("active query result loading", () => {
  it("keeps the single-result execution loading", () => {
    expect(isActiveResultLoading(state({ isExecuting: true, executingResultRunId: null }))).toBe(true);
  });

  it("keeps the result run being updated loading", () => {
    expect(isActiveResultLoading(state({ isExecuting: true, activeResultRunId: "run-b", executingResultRunId: "run-b" }))).toBe(true);
  });

  it("does not load a completed result while a new run is executing", () => {
    expect(isActiveResultLoading(state({ isExecuting: true, activeResultRunId: "run-a", executingResultRunId: null }))).toBe(false);
  });

  it("loads again when the user switches back to the executing run", () => {
    expect(isActiveResultLoading(state({ isExecuting: true, activeResultRunId: "run-b", executingResultRunId: "run-b" }))).toBe(true);
  });

  it("clears loading after execution finishes", () => {
    expect(isActiveResultLoading(state({ isExecuting: false, activeResultRunId: "run-b", executingResultRunId: "run-b" }))).toBe(false);
  });

  it("keeps legacy unscoped executions conservative", () => {
    expect(isActiveResultLoading(state({ isExecuting: true, activeResultRunId: "run-a" }))).toBe(true);
  });
});

describe("stop action visibility", () => {
  it("hides the action when nothing is running", () => {
    expect(shouldShowCancelAction(state())).toBe(false);
    expect(shouldShowCancelAction(state({ executionId: "exec-1" }))).toBe(false);
  });

  it("hides the action while an execution has no cancellable id yet", () => {
    expect(shouldShowCancelAction(state({ isExecuting: true }))).toBe(false);
  });

  it("keeps the action visible while a cancel request is in flight", () => {
    const cancelling = state({ isExecuting: true, executionId: "exec-1", isCancelling: true });
    expect(shouldShowCancelAction(cancelling)).toBe(true);
    // The button must switch to its cancelling state instead of being pressable again.
    expect(canCancelQueryExecution(cancelling)).toBe(false);
  });

  it("allows only one cancel request for an idle execution", () => {
    expect(shouldShowCancelAction(state({ isExecuting: true, executionId: "exec-1" }))).toBe(true);
    expect(canCancelQueryExecution(state({ isExecuting: true, executionId: "exec-1" }))).toBe(true);
  });
});