refactor(desktop): organize lib modules by domain

This commit is contained in:
t8y2 committed 2026-07-04 13:53:52 +08:00
1 parent 90f0594e93
commit 7e7c4944e9
700 files changed
+1292 -1264

No files matched your search

@@ -0,0 +1,73 @@
import { describe, expect, it } from "vitest";
import { CREATE_DATABASE_CHARSET_OPTIONS, createDatabaseCollationOptionsForCharset, defaultCreateDatabaseCollationForCharset, normalizeCreateDatabaseCharset, parseCreateDatabaseCharsetMetadata, nextCreateDatabaseCollation } from "@/lib/database/createDatabaseCharsetOptions";
describe("createDatabaseCharsetOptions", () => {
it("offers common MySQL character sets with utf8mb4 first", () => {
expect(CREATE_DATABASE_CHARSET_OPTIONS.slice(0, 4)).toEqual(["utf8mb4", "utf8", "gbk", "latin1"]);
expect(CREATE_DATABASE_CHARSET_OPTIONS).toEqual(expect.arrayContaining(["ascii", "big5", "gb2312", "utf16", "utf32"]));
});
it("normalizes typed custom values without changing their case", () => {
expect(normalizeCreateDatabaseCharset(" utf8mb3 ")).toBe("utf8mb3");
expect(normalizeCreateDatabaseCharset("Big5")).toBe("Big5");
});
it("offers common collations for the selected character set", () => {
expect(createDatabaseCollationOptionsForCharset("utf8mb4").slice(0, 3)).toEqual(["utf8mb4_unicode_ci", "utf8mb4_general_ci", "utf8mb4_bin"]);
expect(createDatabaseCollationOptionsForCharset("utf8mb4")).toEqual(expect.arrayContaining(["utf8mb4_bin"]));
expect(createDatabaseCollationOptionsForCharset("utf8mb4").some((collation) => collation.includes("0900"))).toBe(false);
expect(createDatabaseCollationOptionsForCharset("gbk")).toEqual(["gbk_chinese_ci", "gbk_bin"]);
expect(createDatabaseCollationOptionsForCharset("utf8")).toEqual(createDatabaseCollationOptionsForCharset("utf8mb3"));
});
it("parses server-provided character sets and collations", () => {
const metadata = parseCreateDatabaseCharsetMetadata(
{
columns: ["Charset", "Description", "Default collation", "Maxlen"],
rows: [
["utf8mb4", "UTF-8 Unicode", "utf8mb4_general_ci", 4],
["gbk", "GBK Simplified Chinese", "gbk_chinese_ci", 2],
],
},
{
columns: ["Collation", "Charset", "Id", "Default", "Compiled", "Sortlen"],
rows: [
["utf8mb4_general_ci", "utf8mb4", 45, "Yes", "Yes", 1],
["utf8mb4_unicode_ci", "utf8mb4", 224, "", "Yes", 8],
["gbk_chinese_ci", "gbk", 28, "Yes", "Yes", 1],
],
},
);
expect(metadata.charsets).toEqual(["utf8mb4", "gbk"]);
expect(metadata.collationsByCharset.utf8mb4).toEqual(["utf8mb4_general_ci", "utf8mb4_unicode_ci"]);
expect(metadata.collationsByCharset.gbk).toEqual(["gbk_chinese_ci"]);
});
it("keeps server-provided utf8mb4 collations while hiding 0900 entries from the dropdown", () => {
const metadata = parseCreateDatabaseCharsetMetadata(
{
columns: ["Charset", "Description", "Default collation", "Maxlen"],
rows: [["utf8mb4", "UTF-8 Unicode", "utf8mb4_0900_ai_ci", 4]],
},
{
columns: ["Collation", "Charset", "Id", "Default", "Compiled", "Sortlen"],
rows: [
["utf8mb4_0900_ai_ci", "utf8mb4", 255, "Yes", "Yes", 0],
["utf8mb4_general_ci", "utf8mb4", 45, "", "Yes", 1],
["utf8mb4_unicode_ci", "utf8mb4", 224, "", "Yes", 8],
["utf8mb4_unicode_520_ci", "utf8mb4", 246, "", "Yes", 8],
["utf8mb4_bin", "utf8mb4", 46, "", "Yes", 1],
],
},
);
expect(metadata.collationsByCharset.utf8mb4).toEqual(["utf8mb4_general_ci", "utf8mb4_unicode_ci", "utf8mb4_unicode_520_ci", "utf8mb4_bin"]);
});
it("keeps custom collations unless the previous value was the old charset default", () => {
expect(defaultCreateDatabaseCollationForCharset("latin1")).toBe("latin1_swedish_ci");
expect(nextCreateDatabaseCollation("gbk", "utf8mb4", "utf8mb4_unicode_ci")).toBe("gbk_chinese_ci");
expect(nextCreateDatabaseCollation("gbk", "utf8mb4", "custom_ci")).toBe("custom_ci");
});
});
@@ -0,0 +1,12 @@
import { describe, expect, it } from "vitest";
import { databaseRuntimeMode, usesAgentCursorForQuery } from "@/lib/database/databaseDriverManifest";
describe("databaseDriverManifest", () => {
it("uses agent cursor only for agent or external runtimes", () => {
expect(databaseRuntimeMode("gaussdb")).toBe("native");
expect(usesAgentCursorForQuery("gaussdb")).toBe(false);
expect(usesAgentCursorForQuery("mysql")).toBe(false);
expect(usesAgentCursorForQuery("jdbc")).toBe(true);
expect(usesAgentCursorForQuery("prestosql")).toBe(true);
});
});
@@ -0,0 +1,28 @@
import { describe, expect, it } from "vitest";
import { supportsTransaction } from "@/lib/database/databaseFeatureSupport";
describe("supportsTransaction", () => {
it("returns true for supported database types", () => {
expect(supportsTransaction("postgres")).toBe(true);
expect(supportsTransaction("mysql")).toBe(true);
});
it("returns false for unsupported database types", () => {
expect(supportsTransaction("redis")).toBe(false);
expect(supportsTransaction("mongodb")).toBe(false);
expect(supportsTransaction("duckdb")).toBe(false);
expect(supportsTransaction("qdrant")).toBe(false);
expect(supportsTransaction("turso")).toBe(false);
expect(supportsTransaction("sqlite")).toBe(false);
expect(supportsTransaction("clickhouse")).toBe(false);
expect(supportsTransaction("sqlserver")).toBe(false);
expect(supportsTransaction("oracle")).toBe(false);
expect(supportsTransaction("dameng")).toBe(false);
expect(supportsTransaction("rqlite")).toBe(false);
expect(supportsTransaction("agent")).toBe(false);
});
it("returns false for undefined or empty input", () => {
expect(supportsTransaction(undefined)).toBe(false);
});
});
@@ -0,0 +1,12 @@
import { describe, expect, it } from "vitest";
import { normalizeSidebarObjectKind, sidebarObjectKindsForDatabase } from "@/lib/database/databaseObjectCapabilities";
describe("databaseObjectCapabilities", () => {
it("exposes materialized views for Dameng", () => {
expect(sidebarObjectKindsForDatabase("dameng")).toContain("MATERIALIZED_VIEW");
});
it("normalizes space separated materialized view types", () => {
expect(normalizeSidebarObjectKind("MATERIALIZED VIEW")).toBe("MATERIALIZED_VIEW");
});
});
@@ -0,0 +1,19 @@
import { describe, expect, it } from "vitest";
import { decodeSelectableDatabaseValue, EMPTY_DATABASE_SELECT_VALUE, encodeSelectableDatabaseValue, TREE_SCHEMA_DEFAULT_DATABASE_SELECT_VALUE } from "@/lib/database/defaultDatabase";
describe("defaultDatabase selectable values", () => {
it("encodes empty tree-schema databases with the default database sentinel", () => {
expect(encodeSelectableDatabaseValue("postgres", "")).toBe(TREE_SCHEMA_DEFAULT_DATABASE_SELECT_VALUE);
expect(decodeSelectableDatabaseValue("postgres", TREE_SCHEMA_DEFAULT_DATABASE_SELECT_VALUE)).toBe("");
});
it("encodes empty non-tree-schema databases with a non-empty sentinel", () => {
expect(encodeSelectableDatabaseValue("access", "")).toBe(EMPTY_DATABASE_SELECT_VALUE);
expect(decodeSelectableDatabaseValue("access", EMPTY_DATABASE_SELECT_VALUE)).toBe("");
});
it("preserves non-empty database names", () => {
expect(encodeSelectableDatabaseValue("access", "653128SXB.mdb")).toBe("653128SXB.mdb");
expect(decodeSelectableDatabaseValue("access", "653128SXB.mdb")).toBe("653128SXB.mdb");
});
});
@@ -0,0 +1,49 @@
import { describe, expect, it } from "vitest";
import { effectiveDatabaseTypeForConnection, inferJdbcDialect } from "@/lib/database/jdbcDialect";
describe("jdbc dialect inference", () => {
it("detects InterSystems IRIS and Caché JDBC connections", () => {
expect(
inferJdbcDialect({
db_type: "jdbc",
connection_string: "jdbc:IRIS://localhost:1972/USER",
}),
).toBe("iris");
expect(
inferJdbcDialect({
db_type: "jdbc",
connection_string: "jdbc:Cache://localhost:1972/USER",
}),
).toBe("iris");
expect(
inferJdbcDialect({
db_type: "jdbc",
jdbc_driver_class: "com.intersystems.jdbc.IRISDriver",
}),
).toBe("iris");
expect(
inferJdbcDialect({
db_type: "jdbc",
jdbc_driver_paths: ["/drivers/intersystems-jdbc-3.10.5.jar"],
}),
).toBe("iris");
});
it("uses IRIS table preview dialect for generic JDBC IRIS connections", () => {
expect(
effectiveDatabaseTypeForConnection({
db_type: "jdbc",
connection_string: "jdbc:IRIS://localhost:1972/USER",
}),
).toBe("iris");
});
it("uses JDBC driver profiles when inferring dialect", () => {
expect(
inferJdbcDialect({
db_type: "jdbc",
driver_profile: "sqlserver",
}),
).toBe("sqlserver");
});
});
@@ -0,0 +1,19 @@
import { describe, expect, it } from "vitest";
import { mysqlCleartextPasswordAuthEnabled, setMysqlCleartextPasswordAuthEnabled } from "@/lib/database/mysqlConnectionOptions";
describe("mysqlConnectionOptions", () => {
it("detects cleartext password auth aliases", () => {
expect(mysqlCleartextPasswordAuthEnabled("allowCleartextPasswords=true")).toBe(true);
expect(mysqlCleartextPasswordAuthEnabled("enable_cleartext_plugin=true")).toBe(true);
expect(mysqlCleartextPasswordAuthEnabled("?AllowCleartextPasswords=TRUE")).toBe(true);
expect(mysqlCleartextPasswordAuthEnabled("allowCleartextPasswords=false")).toBe(false);
});
it("enables the canonical cleartext password auth param and removes aliases", () => {
expect(setMysqlCleartextPasswordAuthEnabled("allowCleartextPasswords=true&charset=utf8mb4", true)).toBe("charset=utf8mb4&enable_cleartext_plugin=true");
});
it("disables cleartext password auth params while preserving unrelated params", () => {
expect(setMysqlCleartextPasswordAuthEnabled("allowCleartextPasswords=true&enable_cleartext_plugin=true&charset=utf8mb4&connect_timeout=10", false)).toBe("charset=utf8mb4&connect_timeout=10");
});
});
@@ -0,0 +1,50 @@
import { describe, expect, it } from "vitest";
import { PRESTOSQL_JDBC_DRIVER_COORDINATE, prestoSqlBuiltinDriverPaths, prestoSqlBuiltinDriverRow, prestoSqlMavenBundle } from "@/lib/database/prestoSqlBuiltinDriver";
import type { JdbcMavenBundleInfo } from "@/types/database";
function bundle(coordinate: string, version = "350"): JdbcMavenBundleInfo {
return {
id: `bundle:${coordinate}`,
coordinate,
scope: "runtime",
repositories: ["https://repo.maven.apache.org/maven2/"],
installed_at: "2026-06-18T00:00:00Z",
path: "drivers/jdbc",
artifacts: [
{
group_id: "io.prestosql",
artifact_id: "presto-jdbc",
version,
classifier: "",
extension: "jar",
file_name: `presto-jdbc-${version}.jar`,
path: `drivers/jdbc/presto-jdbc-${version}.jar`,
size: 12_345,
sha256: "abc",
},
],
};
}
describe("prestoSqlBuiltinDriver", () => {
it("adds an uninstalled PrestoSQL row when the Maven bundle is absent", () => {
const row = prestoSqlBuiltinDriverRow([]);
expect(row.db_type).toBe("prestosql");
expect(row.label).toBe("PrestoSQL");
expect(row.version).toBe("350");
expect(row.installed).toBe(false);
expect(row.installed_version).toBeNull();
expect(row.jre).toBe("21");
});
it("marks PrestoSQL installed when its Maven coordinate is present", () => {
const installed = bundle(PRESTOSQL_JDBC_DRIVER_COORDINATE);
const row = prestoSqlBuiltinDriverRow([bundle("com.mysql:mysql-connector-j:9.2.0"), installed]);
expect(prestoSqlMavenBundle([installed])?.id).toBe(installed.id);
expect(prestoSqlBuiltinDriverPaths([installed])).toEqual(["drivers/jdbc/presto-jdbc-350.jar"]);
expect(row.installed).toBe(true);
expect(row.installed_version).toBe("350");
});
});