feat(desktop): add Driver Store category navigation

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-07-28 21:56:03 +08:00
1 parent c7e7b7c9d9
commit 06ed8dc5f6
12 files changed
+802 -105

No files matched your search

@@ -0,0 +1,151 @@
import { describe, expect, it } from "vitest";
import { AGENT_DRIVER_CATEGORY_MAP, DRIVER_CATEGORIES, assertAgentDriverCategoriesComplete, getCategoryForAgentDriver } from "@/lib/connection/driver-category-definitions";
describe("getCategoryForAgentDriver", () => {
it("returns correct category for one key from each of the 9 categories", () => {
// sql
expect(getCategoryForAgentDriver("oracle")).toBe("sql");
// analytics
expect(getCategoryForAgentDriver("snowflake")).toBe("analytics");
// domestic
expect(getCategoryForAgentDriver("dameng")).toBe("domestic");
// lightweight
expect(getCategoryForAgentDriver("access")).toBe("lightweight");
// document
expect(getCategoryForAgentDriver("mongodb")).toBe("document");
// graph_ai
expect(getCategoryForAgentDriver("neo4j")).toBe("graph_ai");
// timeseries
expect(getCategoryForAgentDriver("tdengine")).toBe("timeseries");
// mq
expect(getCategoryForAgentDriver("kafka")).toBe("mq");
// registry_config
expect(getCategoryForAgentDriver("etcd")).toBe("registry_config");
});
it('returns "all" for unknown keys', () => {
expect(getCategoryForAgentDriver("")).toBe("all");
expect(getCategoryForAgentDriver("unknown_driver_xyz")).toBe("all");
expect(getCategoryForAgentDriver("nosuchdriver")).toBe("all");
expect(getCategoryForAgentDriver("random-string-123")).toBe("all");
});
});
describe("assertAgentDriverCategoriesComplete", () => {
it("does not throw when all keys mapped", () => {
const mappedKeys = Object.keys(AGENT_DRIVER_CATEGORY_MAP);
expect(() => assertAgentDriverCategoriesComplete(mappedKeys)).not.toThrow();
});
it("throws when a key is missing", () => {
const mappedKeys = Object.keys(AGENT_DRIVER_CATEGORY_MAP);
expect(() => assertAgentDriverCategoriesComplete([...mappedKeys, "no_such_driver"])).toThrow("unmapped=no_such_driver");
});
});
describe("AGENT_DRIVER_CATEGORY_MAP integrity", () => {
it("has no agent driver key mapped to more than one category (i.e. no duplicate keys)", () => {
const entries = Object.entries(AGENT_DRIVER_CATEGORY_MAP);
const keys = entries.map(([key]) => key);
// Each key in a Record is already unique by definition, but verify
// there are no unexpected dupes in the data.
const seen = new Set<string>();
for (const k of keys) {
expect(seen.has(k)).toBe(false);
seen.add(k);
}
});
it("has all category values listed in DRIVER_CATEGORIES", () => {
const validCategoryKeys = new Set(DRIVER_CATEGORIES.map((c) => c.key));
for (const [driverKey, category] of Object.entries(AGENT_DRIVER_CATEGORY_MAP)) {
expect(validCategoryKeys.has(category), `Driver "${driverKey}" maps to unknown category "${category}"`).toBe(true);
}
});
it("covers all known agent driver keys with no stale entries", () => {
// Golden set — mirrors the store-visible entries in agent_catalog.rs
// plus EXTRA_DRIVER_STORE_ENTRIES (kafka, rocketmq, rabbitmq,
// sqlserver-legacy) and the PrestoSQL built-in driver.
// When an Agent driver is added to the catalog its key MUST appear here;
// removing a key from the map without removing it from the list below
// will fail this test.
const expectedKeys = new Set([
// sql
"db2",
"firebird",
"informix",
"iris",
"oracle",
"sqlserver-legacy",
// analytics
"bigquery",
"databend",
"databricks",
"exasol",
"hive",
"kylin",
"prestosql",
"saphana",
"snowflake",
"spark",
"teradata",
"trino",
"vertica",
// domestic
"dameng",
"gbase8a",
"gbase8s",
"goldendb",
"highgo",
"kingbase",
"oceanbase-oracle",
"oscar",
"sundb",
"uxdb",
"vastbase",
"xugu",
"yashandb",
// lightweight
"access",
"h2",
"h2-legacy",
// document
"cassandra",
"mongodb",
// graph_ai
"neo4j",
// timeseries
"influxdb",
"iotdb",
"tdengine",
// mq
"kafka",
"rabbitmq",
"rocketmq",
// registry_config
"etcd",
"zookeeper",
]);
const actualKeys = Object.keys(AGENT_DRIVER_CATEGORY_MAP);
expect(actualKeys).toHaveLength(expectedKeys.size);
const actualSet = new Set(actualKeys);
const missing = [...expectedKeys].filter((k) => !actualSet.has(k));
const extra = actualKeys.filter((k) => !expectedKeys.has(k));
expect(missing, `Missing from AGENT_DRIVER_CATEGORY_MAP: ${missing.join(", ")}`).toEqual([]);
expect(extra, `Extra keys in AGENT_DRIVER_CATEGORY_MAP not in expected set: ${extra.join(", ")}`).toEqual([]);
});
});
describe("prestosql matches ConnectionDialog category", () => {
it("is mapped to analytics (same as ConnectionDialog dbCategoryDefinitions)", () => {
expect(getCategoryForAgentDriver("prestosql")).toBe("analytics");
});
});
@@ -0,0 +1,193 @@
import { describe, expect, it } from "vitest";
import { selectStableDrivers, selectUpdatableDrivers, hasAnyUpdatableDriverMatching } from "@/lib/connection/driverListFilter";
import type { AgentDriverInfo } from "@/lib/backend/api";
function driver(overrides: Partial<AgentDriverInfo> = {}): AgentDriverInfo {
return {
db_type: "test",
label: "Test",
version: "1.0",
size: 0,
installed: true,
installed_version: "1.0",
update_available: false,
jre: "",
jre_installed: false,
...overrides,
};
}
describe("selectUpdatableDrivers", () => {
it("returns only drivers with update_available === true", () => {
const drivers: AgentDriverInfo[] = [driver({ db_type: "mysql", update_available: true }), driver({ db_type: "postgres", update_available: false }), driver({ db_type: "sqlite", update_available: true })];
const result = selectUpdatableDrivers(drivers);
expect(result).toHaveLength(2);
expect(result.map((d) => d.db_type)).toEqual(["mysql", "sqlite"]);
});
it("returns empty array when no drivers have updates available", () => {
const drivers: AgentDriverInfo[] = [driver({ db_type: "mysql", update_available: false }), driver({ db_type: "postgres", update_available: false })];
expect(selectUpdatableDrivers(drivers)).toEqual([]);
});
it("returns empty array for an empty input", () => {
expect(selectUpdatableDrivers([])).toEqual([]);
});
});
describe("selectStableDrivers", () => {
it("returns only drivers with update_available === false", () => {
const drivers: AgentDriverInfo[] = [driver({ db_type: "mysql", update_available: true }), driver({ db_type: "postgres", update_available: false }), driver({ db_type: "sqlite", update_available: false })];
const result = selectStableDrivers(drivers);
expect(result).toHaveLength(2);
expect(result.map((d) => d.db_type)).toEqual(["postgres", "sqlite"]);
});
it("returns all drivers when none have updates", () => {
const drivers: AgentDriverInfo[] = [driver({ db_type: "mysql", update_available: false }), driver({ db_type: "postgres", update_available: false })];
expect(selectStableDrivers(drivers)).toHaveLength(2);
});
it("returns empty array when all drivers have updates", () => {
const drivers: AgentDriverInfo[] = [driver({ db_type: "mysql", update_available: true }), driver({ db_type: "sqlite", update_available: true })];
expect(selectStableDrivers(drivers)).toEqual([]);
});
});
describe("partition invariant", () => {
it("updatable + stable drivers cover the full input with no overlap", () => {
const drivers: AgentDriverInfo[] = [driver({ db_type: "mysql", update_available: true }), driver({ db_type: "postgres", update_available: false }), driver({ db_type: "sqlite", update_available: true }), driver({ db_type: "oracle", update_available: false })];
const updatable = selectUpdatableDrivers(drivers);
const stable = selectStableDrivers(drivers);
// No duplicate db_types between the two sets.
const updatableKeys = new Set(updatable.map((d) => d.db_type));
for (const d of stable) {
expect(updatableKeys.has(d.db_type)).toBe(false);
}
// Union is the full set.
expect(updatable.length + stable.length).toBe(drivers.length);
});
});
describe("search deduplication", () => {
it("updatable drivers are excluded from stable so they only appear in the global update section", () => {
const drivers: AgentDriverInfo[] = [driver({ db_type: "neo4j", label: "Neo4j", update_available: true }), driver({ db_type: "oracle", label: "Oracle", update_available: true }), driver({ db_type: "postgres", label: "PostgreSQL", update_available: false })];
const stable = selectStableDrivers(drivers);
// Searching for "Neo4j" in stable should produce no results — the driver
// already appears in the global update section above and must not be
// duplicated in the category-filtered search results below.
const searchHit = stable.some((d) => d.db_type === "neo4j");
expect(searchHit).toBe(false);
});
});
// ---------------------------------------------------------------------------
// hasAnyUpdatableDriverMatching — regression tests for empty‑state suppression
// (DriverStoreDialog.vue)
// ---------------------------------------------------------------------------
/** Simple matchers mirroring the component's real predicates. */
function labelSearch(d: AgentDriverInfo, q: string) {
return d.label.toLowerCase().includes(q);
}
function categoryOf(d: AgentDriverInfo) {
// Map db_type prefixes to categories — the real mapping lives in
// getCategoryForAgentDriver; this simplified version is sufficient for tests.
if (d.db_type === "neo4j") return "graph";
return "relational";
}
describe("hasAnyUpdatableDriverMatching", () => {
const neo4j = driver({ db_type: "neo4j", label: "Neo4j", update_available: true });
const oracle = driver({ db_type: "oracle", label: "Oracle", update_available: true });
it("returns false when there are no updatable drivers", () => {
expect(
hasAnyUpdatableDriverMatching([], {
searchQuery: "",
selectedCategory: "all",
driverMatchesSearch: labelSearch,
driverCategory: categoryOf,
}),
).toBe(false);
});
it("returns true for 'all' category with no search when updatable drivers exist", () => {
expect(
hasAnyUpdatableDriverMatching([neo4j], {
searchQuery: "",
selectedCategory: "all",
driverMatchesSearch: labelSearch,
driverCategory: categoryOf,
}),
).toBe(true);
});
it("returns true when an updatable driver matches the search", () => {
expect(
hasAnyUpdatableDriverMatching([neo4j, oracle], {
searchQuery: "neo4j",
selectedCategory: "all",
driverMatchesSearch: labelSearch,
driverCategory: categoryOf,
}),
).toBe(true);
});
it("returns false when no updatable driver matches the search (regression: search 'zzzz' with Neo4j update)", () => {
expect(
hasAnyUpdatableDriverMatching([neo4j], {
searchQuery: "zzzz",
selectedCategory: "all",
driverMatchesSearch: labelSearch,
driverCategory: categoryOf,
}),
).toBe(false);
});
it("returns true when an updatable driver belongs to the selected category", () => {
expect(
hasAnyUpdatableDriverMatching([neo4j, oracle], {
searchQuery: "",
selectedCategory: "graph",
driverMatchesSearch: labelSearch,
driverCategory: categoryOf,
}),
).toBe(true);
});
it("returns false when no updatable driver belongs to the selected category (regression: Neo4j update, 'relational' category)", () => {
expect(
hasAnyUpdatableDriverMatching([neo4j], {
searchQuery: "",
selectedCategory: "relational",
driverMatchesSearch: labelSearch,
driverCategory: categoryOf,
}),
).toBe(false);
});
it("search takes precedence over category when both are active", () => {
// Neo4j is in "graph" category; user selected "graph" but searched "oracle".
expect(
hasAnyUpdatableDriverMatching([neo4j, oracle], {
searchQuery: "oracle",
selectedCategory: "graph",
driverMatchesSearch: labelSearch,
driverCategory: categoryOf,
}),
).toBe(true);
});
});
@@ -0,0 +1,116 @@
export const DRIVER_CATEGORIES = [
{ key: "sql", order: 1, titleKey: "connection.databaseCategorySql" },
{ key: "analytics", order: 2, titleKey: "connection.databaseCategoryAnalytics" },
{ key: "domestic", order: 3, titleKey: "connection.databaseCategoryDomestic" },
{ key: "lightweight", order: 4, titleKey: "connection.databaseCategoryLightweight" },
{ key: "document", order: 5, titleKey: "connection.databaseCategoryDocument" },
{ key: "graph_ai", order: 6, titleKey: "connection.databaseCategoryGraphAi" },
{ key: "timeseries", order: 7, titleKey: "connection.databaseCategoryTimeseries" },
{ key: "mq", order: 8, titleKey: "connection.databaseCategoryMq" },
{ key: "registry_config", order: 9, titleKey: "connection.databaseCategoryRegistryConfig" },
] as const;
export type DriverCategoryKey = (typeof DRIVER_CATEGORIES)[number]["key"];
const VALID_CATEGORY_KEYS: ReadonlySet<string> = new Set(DRIVER_CATEGORIES.map((cat) => cat.key));
const EMPTY = 0;
export const AGENT_DRIVER_CATEGORY_MAP: Readonly<Record<string, DriverCategoryKey>> = {
access: "lightweight",
bigquery: "analytics",
cassandra: "document",
dameng: "domestic",
databend: "analytics",
databricks: "analytics",
db2: "sql",
etcd: "registry_config",
exasol: "analytics",
firebird: "sql",
gbase8a: "domestic",
gbase8s: "domestic",
goldendb: "domestic",
h2: "lightweight",
"h2-legacy": "lightweight",
highgo: "domestic",
hive: "analytics",
influxdb: "timeseries",
informix: "sql",
iotdb: "timeseries",
iris: "sql",
kafka: "mq",
kingbase: "domestic",
kylin: "analytics",
mongodb: "document",
neo4j: "graph_ai",
"oceanbase-oracle": "domestic",
oracle: "sql",
oscar: "domestic",
prestosql: "analytics",
rabbitmq: "mq",
rocketmq: "mq",
saphana: "analytics",
snowflake: "analytics",
spark: "analytics",
"sqlserver-legacy": "sql",
sundb: "domestic",
tdengine: "timeseries",
teradata: "analytics",
trino: "analytics",
uxdb: "domestic",
vastbase: "domestic",
vertica: "analytics",
xugu: "domestic",
yashandb: "domestic",
zookeeper: "registry_config",
};
export const getCategoryForAgentDriver = (dbType: string): DriverCategoryKey | "all" => AGENT_DRIVER_CATEGORY_MAP[dbType] ?? "all";
const collectUnmapped = (driverKeys: string[]): string[] => driverKeys.filter((key) => !(key in AGENT_DRIVER_CATEGORY_MAP));
const collectUnknownCategories = (): string[] =>
Object.entries(AGENT_DRIVER_CATEGORY_MAP)
.filter(([, category]) => !VALID_CATEGORY_KEYS.has(category))
.map(([key, category]) => `${key}->${category}`);
const collectDuplicateKeys = (driverKeys: string[]): string[] => driverKeys.filter((key, index) => driverKeys.indexOf(key) !== index).filter((key, index, arr) => arr.indexOf(key) === index);
const formatErrorMessage = (unmapped: string[], unknownCategories: string[], duplicateKeys: string[]): string => {
const parts: string[] = [];
if (unmapped.length > EMPTY) {
parts.push(`unmapped=${unmapped.join(",")}`);
}
if (unknownCategories.length > EMPTY) {
parts.push(`unknownCategories=${unknownCategories.join(",")}`);
}
if (duplicateKeys.length > EMPTY) {
parts.push(`duplicateKeys=${duplicateKeys.join(",")}`);
}
return parts.join("; ");
};
/**
* Validates that every driver key in {@link agentDriverDbTypes} has a category
* mapping and that all mapped categories are valid. Throws if any driver is
* unmapped, duplicates appear, or an unknown category is referenced.
*/
export const assertAgentDriverCategoriesComplete = (agentDriverDbTypes: string[]): void => {
const unmapped = collectUnmapped(agentDriverDbTypes);
const unknownCategories = collectUnknownCategories();
const duplicateKeys = collectDuplicateKeys(agentDriverDbTypes);
if (unmapped.length > EMPTY || unknownCategories.length > EMPTY || duplicateKeys.length > EMPTY) {
throw new Error(formatErrorMessage(unmapped, unknownCategories, duplicateKeys));
}
// Warn in dev if the map has stale entries for drivers not in the catalog.
if (import.meta.env.DEV) {
const driverSet = new Set(agentDriverDbTypes);
const mappedButNotListed = Object.keys(AGENT_DRIVER_CATEGORY_MAP).filter((key) => !driverSet.has(key));
if (mappedButNotListed.length > EMPTY) {
// eslint-disable-next-line no-console
console.warn("[driver-category-definitions] agent driver category map has entries for drivers not in the supplied list:", mappedButNotListed.join(", "));
}
}
};
@@ -0,0 +1,48 @@
import type { AgentDriverInfo } from "@/lib/backend/api";
/**
* Drivers that have an update available — always rendered above category
* navigation so the user can see and act on them regardless of the active
* category filter.
*/
export function selectUpdatableDrivers(drivers: readonly AgentDriverInfo[]): AgentDriverInfo[] {
return drivers.filter((d) => d.update_available);
}
/**
* Category-filtered drivers *excluding* updatable ones — the per‑item
* "Update" action lives in the global update section, so the category
* list only renders stable (non‑updatable) rows.
*/
export function selectStableDrivers(drivers: readonly AgentDriverInfo[]): AgentDriverInfo[] {
return drivers.filter((d) => !d.update_available);
}
export interface UpdatableDriverMatchOptions {
/** Lowercased search query (empty string when search is inactive). */
searchQuery: string;
/** Currently selected category key ("all" when no specific category). */
selectedCategory: string;
/** Predicate: does `driver` match `query` (already lowercased)? */
driverMatchesSearch: (driver: AgentDriverInfo, query: string) => boolean;
/** Returns the category key for `driver`. */
driverCategory: (driver: AgentDriverInfo) => string;
}
/**
* Returns `true` when at least one driver in `updatableDrivers` is relevant to
* the current view (search query or selected category). Used to decide whether
* an empty‑state message should be suppressed: the global update section always
* renders *all* updatable drivers, so the empty‑state must only be hidden when
* at least one of those drivers is actually relevant.
*/
export function hasAnyUpdatableDriverMatching(updatableDrivers: readonly AgentDriverInfo[], opts: UpdatableDriverMatchOptions): boolean {
if (updatableDrivers.length === 0) return false;
if (opts.searchQuery) {
return updatableDrivers.some((d) => opts.driverMatchesSearch(d, opts.searchQuery));
}
if (opts.selectedCategory !== "all") {
return updatableDrivers.some((d) => opts.driverCategory(d) === opts.selectedCategory);
}
return true;
}