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。

4.0 KB
Raw Blame
import assert from "node:assert/strict";
import { beforeEach, test, vi } from "vitest";

const { beginDatabaseBackupSnapshot, exportDatabaseSql, rollbackManualTransaction } = vi.hoisted(() => ({
  beginDatabaseBackupSnapshot: vi.fn(),
  exportDatabaseSql: vi.fn(),
  rollbackManualTransaction: vi.fn(),
}));

vi.mock("@/lib/backend/api", () => ({
  beginDatabaseBackupSnapshot,
  exportDatabaseSql,
  rollbackManualTransaction,
}));

import { runDatabaseExportUntilTerminal, runWithDatabaseBackupSnapshot, shouldUseDatabaseBackupSnapshot } from "../../apps/desktop/src/lib/export/databaseExport.ts";

beforeEach(() => {
  beginDatabaseBackupSnapshot.mockReset();
  exportDatabaseSql.mockReset();
  rollbackManualTransaction.mockReset();
  beginDatabaseBackupSnapshot.mockResolvedValue({ sessionId: "snapshot-1" });
  rollbackManualTransaction.mockResolvedValue({});
});

test("database export snapshots are limited to supported desktop data exports", () => {
  assert.equal(shouldUseDatabaseBackupSnapshot("mysql", true, true), true);
  assert.equal(shouldUseDatabaseBackupSnapshot("postgres", true, true), true);
  assert.equal(shouldUseDatabaseBackupSnapshot("oracle", true, true), false);
  assert.equal(shouldUseDatabaseBackupSnapshot("sqlserver", true, true), false);
  assert.equal(shouldUseDatabaseBackupSnapshot("mysql", false, true), false);
  assert.equal(shouldUseDatabaseBackupSnapshot("postgres", true, false), false);
});

test("database export waits for terminal progress before releasing its snapshot", async () => {
  let emitProgress: ((progress: any) => void) | undefined;
  exportDatabaseSql.mockImplementation(async (_request, onProgress) => {
    emitProgress = onProgress;
  });

  const operation = runDatabaseExportUntilTerminal({ exportId: "export-1" } as any, () => {});
  let settled = false;
  void operation.then(() => {
    settled = true;
  });
  await Promise.resolve();

  assert.equal(settled, false);
  emitProgress?.({ exportId: "export-1", status: "Running" });
  await Promise.resolve();
  assert.equal(settled, false);

  emitProgress?.({ exportId: "export-1", status: "Done" });
  assert.equal((await operation).status, "Done");
});

test("database export snapshot is released after success", async () => {
  const operation = vi.fn(async (sessionId: string | undefined) => `done:${sessionId}`);

  const result = await runWithDatabaseBackupSnapshot({ connectionId: "conn-1", database: "app", enabled: true }, operation);

  assert.equal(result, "done:snapshot-1");
  assert.deepEqual(operation.mock.calls, [["snapshot-1"]]);
  assert.deepEqual(rollbackManualTransaction.mock.calls, [["snapshot-1"]]);
});

test("database export snapshot is released without masking an export error", async () => {
  const exportError = new Error("export failed");
  const cleanupError = new Error("cleanup failed");
  rollbackManualTransaction.mockRejectedValue(cleanupError);

  await assert.rejects(
    runWithDatabaseBackupSnapshot({ connectionId: "conn-1", database: "app", enabled: true }, async () => {
      throw exportError;
    }),
    exportError,
  );

  assert.deepEqual(rollbackManualTransaction.mock.calls, [["snapshot-1"]]);
});

test("database export snapshot is released after cancellation", async () => {
  const cleanupError = new Error("cleanup failed");
  rollbackManualTransaction.mockRejectedValue(cleanupError);

  const result = await runWithDatabaseBackupSnapshot(
    { connectionId: "conn-1", database: "app", enabled: true },
    async () => ({ status: "Cancelled" as const }),
    (terminal) => terminal.status === "Done",
  );

  assert.equal(result.status, "Cancelled");
  assert.deepEqual(rollbackManualTransaction.mock.calls, [["snapshot-1"]]);
});

test("database export snapshot cleanup failure is reported after success", async () => {
  const cleanupError = new Error("cleanup failed");
  rollbackManualTransaction.mockRejectedValue(cleanupError);

  await assert.rejects(
    runWithDatabaseBackupSnapshot({ connectionId: "conn-1", database: "app", enabled: true }, async () => "done"),
    cleanupError,
  );
});