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.5 KB
Raw Blame
import { strict as assert } from "node:assert";
import { test } from "vitest";
import {
  allCellsSelectionRange,
  columnSelectionRange,
  extractColumnsSelection,
  extractSelection,
  isCellInSelection,
  normalizeSelectionRange,
  parseClipboardTable,
  rowSelectionRange,
} from "../../apps/desktop/src/lib/dataGrid/gridSelection.ts";

test("normalizes a dragged cell range in either direction", () => {
  const range = normalizeSelectionRange({ rowIndex: 4, colIndex: 3 }, { rowIndex: 1, colIndex: 0 });

  assert.deepEqual(range, {
    startRow: 1,
    endRow: 4,
    startCol: 0,
    endCol: 3,
  });
  assert.equal(isCellInSelection(2, 1, range), true);
  assert.equal(isCellInSelection(5, 1, range), false);
});

test("extracts selection rows and columns from a rectangular range", () => {
  const selection = extractSelection(
    ["id", "name", "active"],
    [
      [1, "Ada", true],
      [2, "Linus", false],
      [3, null, true],
    ],
    { startRow: 0, endRow: 1, startCol: 1, endCol: 2 },
  );

  assert.deepEqual(selection.columns, ["name", "active"]);
  assert.deepEqual(selection.rows, [
    ["Ada", true],
    ["Linus", false],
  ]);
});

test("extracts non-contiguous columns in display order", () => {
  const selection = extractColumnsSelection(
    ["id", "name", "active", "email"],
    [
      [1, "Ada", true, "[email protected]"],
      [2, "Linus", false, "[email protected]"],
    ],
    [3, 1, 3],
  );

  assert.deepEqual(selection.columns, ["name", "email"]);
  assert.deepEqual(selection.rows, [
    ["Ada", "[email protected]"],
    ["Linus", "[email protected]"],
  ]);
});

test("builds whole row, column, and table selection ranges", () => {
  assert.deepEqual(rowSelectionRange(2, 4), { startRow: 2, endRow: 2, startCol: 0, endCol: 3 });
  assert.deepEqual(rowSelectionRange(4, 4, 2), { startRow: 2, endRow: 4, startCol: 0, endCol: 3 });
  assert.deepEqual(columnSelectionRange(5, 1), { startRow: 0, endRow: 4, startCol: 1, endCol: 1 });
  assert.deepEqual(columnSelectionRange(5, 3, 1), { startRow: 0, endRow: 4, startCol: 1, endCol: 3 });
  assert.deepEqual(allCellsSelectionRange(3, 2), { startRow: 0, endRow: 2, startCol: 0, endCol: 1 });
  assert.equal(rowSelectionRange(0, 0), null);
  assert.equal(columnSelectionRange(0, 0), null);
  assert.equal(allCellsSelectionRange(0, 2), null);
});

test("parses clipboard table text with spreadsheet line endings", () => {
  assert.deepEqual(parseClipboardTable("0"), [["0"]]);
  assert.deepEqual(parseClipboardTable("a\tb\r\nc\td\r\n"), [
    ["a", "b"],
    ["c", "d"],
  ]);
});