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。

3.1 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from "vitest";
import { ExternalSqlFileTooLargeError } from "@/lib/sql/sqlFileOpen";

const mocks = vi.hoisted(() => ({
  invoke: vi.fn(),
}));

vi.mock("@tauri-apps/api/core", () => ({
  invoke: mocks.invoke,
}));

import { inspectExternalSqlFile, readExternalSqlFile, readExternalSqlFileSnapshot, writeExternalSqlFile } from "@/lib/backend/tauri";

const version = {
  sizeBytes: 9,
  modifiedNs: "123000000",
  contentHash: "abc123",
};

describe("external SQL file API", () => {
  beforeEach(() => {
    mocks.invoke.mockReset();
  });

  it("returns editor content from the structured backend response", async () => {
    mocks.invoke.mockResolvedValue({ kind: "content", content: "select 1;", version });

    await expect(readExternalSqlFile("/tmp/demo.sql")).resolves.toBe("select 1;");
    expect(mocks.invoke).toHaveBeenCalledWith("read_external_sql_file", { path: "/tmp/demo.sql", maxSizeBytes: undefined, encoding: null });
  });

  it("returns the disk version with an editor snapshot", async () => {
    mocks.invoke.mockResolvedValue({ kind: "content", content: "select 1;", version });

    await expect(readExternalSqlFileSnapshot("/tmp/demo.sql")).resolves.toEqual({ content: "select 1;", version });
  });

  it("inspects metadata without reading editor content", async () => {
    mocks.invoke.mockResolvedValue({ kind: "present", sizeBytes: 9, modifiedNs: "123000000" });

    await expect(inspectExternalSqlFile("/tmp/demo.sql")).resolves.toEqual({ kind: "present", sizeBytes: 9, modifiedNs: "123000000" });
    expect(mocks.invoke).toHaveBeenCalledWith("inspect_external_sql_file", { path: "/tmp/demo.sql" });
  });

  it("passes the expected version to checked writes", async () => {
    mocks.invoke.mockResolvedValue({ kind: "written", version });

    await expect(writeExternalSqlFile("/tmp/demo.sql", "select 2;", { expectedContentHash: "abc123" })).resolves.toEqual({ kind: "written", version });
    expect(mocks.invoke).toHaveBeenCalledWith("write_external_sql_file", {
      path: "/tmp/demo.sql",
      content: "select 2;",
      expectedContentHash: "abc123",
      expectedMissing: false,
      encoding: "utf8",
    });
  });

  it("passes an expected-missing precondition to recreate writes", async () => {
    mocks.invoke.mockResolvedValue({ kind: "written", version });

    await expect(writeExternalSqlFile("/tmp/demo.sql", "select 2;", { expectedMissing: true })).resolves.toEqual({ kind: "written", version });
    expect(mocks.invoke).toHaveBeenCalledWith("write_external_sql_file", {
      path: "/tmp/demo.sql",
      content: "select 2;",
      expectedContentHash: null,
      expectedMissing: true,
      encoding: "utf8",
    });
  });

  it("maps oversized responses to a typed frontend error", async () => {
    mocks.invoke.mockResolvedValue({ kind: "tooLarge", sizeBytes: 50 * 1024 ** 3, maxSizeBytes: 64 * 1024 ** 2 });

    const error = await readExternalSqlFile("/tmp/backup.sql").catch((reason) => reason);

    expect(error).toBeInstanceOf(ExternalSqlFileTooLargeError);
    expect(error).toMatchObject({ sizeBytes: 50 * 1024 ** 3, maxSizeBytes: 64 * 1024 ** 2 });
  });
});