feat(driver-store): filter built-in drivers by install status

This commit is contained in:
onenewcode authored and GitHub committed 2026-09-16 20:16:29 +08:00
1 parent 888f37c3c8
commit a57906ed7e
19 files changed
+727 -320

No files matched your search

@@ -5,6 +5,7 @@ const tauri = readFileSync(new URL("../tauri.ts", import.meta.url), "utf8");
const http = readFileSync(new URL("../http.ts", import.meta.url), "utf8");
const api = readFileSync(new URL("../api.ts", import.meta.url), "utf8");
const driverStore = readFileSync(new URL("../../../components/config/DriverStoreDialog.vue", import.meta.url), "utf8");
const driverStoreAgentRow = readFileSync(new URL("../../../components/config/DriverStoreAgentRow.vue", import.meta.url), "utf8");
const tauriRegistry = readFileSync(new URL("../../../../../../src-tauri/src/lib.rs", import.meta.url), "utf8");
const webRegistry = readFileSync(new URL("../../../../../../crates/dbx-web/src/main.rs", import.meta.url), "utf8");
@@ -91,10 +92,19 @@ describe("offline Agent export transport contract", () => {
expect(busyContract).toContain(dependency);
}
for (const handler of ["upgradeAll", "installDriver(driver.db_type)", "importDriverFile(driver)", "uninstallDriver(driver.db_type)", "reinstallJre(jre.key)", "uninstallJre(jre.key)"]) {
for (const handler of ["upgradeAll", "reinstallJre(jre.key)", "uninstallJre(jre.key)"]) {
for (const button of buttonTagsForClick(handler)) expect(button).toContain("agentPackageBusy");
}
expect(driverStore).toContain("packageBusy: agentPackageBusy.value");
expect(driverStore).toContain('@install="installDriver(driver.db_type)"');
expect(driverStore).toContain('@uninstall="uninstallDriver(driver.db_type)"');
expect(driverStore).toContain('@import-file="importDriverFile(driver)"');
expect(driverStoreAgentRow).toContain("packageBusy");
expect(driverStoreAgentRow).toContain(':disabled="queueOrUpgradeBusy"');
expect(driverStoreAgentRow).toContain(':disabled="preparingUpgradeAll || upgradingAll || installing || packageBusy"');
expect(driverStoreAgentRow).toContain(':disabled="uninstallBusy"');
expect(sourceSection(driverStore, "async function importOfflineZip", "\n\nasync function importDriverFile")).toContain("if (agentExportImportBlocked.value) return;");
});
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { agentDriverInstallKey, driverStoreFocusForInstallError, hasInstalledAgentVersion, showAgentDriverInstallHint } from "@/lib/connection/agentDriverInstallHint";
import { agentDriverInstallKey, driverStoreFocusElementKey, driverStoreFocusForInstallError, driverStoreFocusRowIsRenderable, hasInstalledAgentVersion, shouldApplyDriverStoreFocus, showAgentDriverInstallHint } from "@/lib/connection/agentDriverInstallHint";
describe("DuckDB driver installation", () => {
it("requires the downloadable DuckDB driver before connecting", () => {
@@ -28,6 +28,49 @@ describe("HiveServer2-compatible driver installation", () => {
});
});
describe("shouldApplyDriverStoreFocus", () => {
it("applies when the driver first appears after a list load", () => {
expect(shouldApplyDriverStoreFocus(null, "driver:mysql", false)).toBe(true);
});
it("does not re-apply on inventory refresh of the same focus", () => {
expect(shouldApplyDriverStoreFocus("driver:mysql", "driver:mysql", false)).toBe(false);
});
it("re-applies when the parent sends a new focus object for the same driver", () => {
expect(shouldApplyDriverStoreFocus("driver:mysql", "driver:mysql", true)).toBe(true);
});
it("applies when the focused driver changes", () => {
expect(shouldApplyDriverStoreFocus("driver:mysql", "driver:postgres", true)).toBe(true);
});
});
describe("driverStoreFocusElementKey", () => {
it("keys driver and jre focus targets", () => {
expect(driverStoreFocusElementKey({ target: "driver", driver: "mysql" })).toBe("driver:mysql");
expect(driverStoreFocusElementKey({ target: "jre" })).toBe("jre");
});
});
describe("driverStoreFocusRowIsRenderable", () => {
it("does not treat managed JDBC as renderable while the agent list is still loading", () => {
expect(driverStoreFocusRowIsRenderable({ target: "driver", driver: "phoenix" }, 0, [{ db_type: "phoenix" }])).toBe(false);
});
it("allows focus once the agent list has loaded and the row exists", () => {
expect(driverStoreFocusRowIsRenderable({ target: "driver", driver: "phoenix" }, 1, [{ db_type: "phoenix" }])).toBe(true);
});
it("waits when the focused driver is not in the builtin rows yet", () => {
expect(driverStoreFocusRowIsRenderable({ target: "driver", driver: "mysql" }, 3, [{ db_type: "phoenix" }])).toBe(false);
});
it("treats JRE focus as renderable during agent list loading", () => {
expect(driverStoreFocusRowIsRenderable({ target: "jre" }, 0, [])).toBe(true);
});
});
describe("driverStoreFocusForInstallError", () => {
it("focuses the SQLite SSH worker for remote SQLite driver errors", () => {
expect(driverStoreFocusForInstallError("sqlite-worker driver is not installed. Please install it from the Driver Manager.", "sqlite", undefined)).toEqual({
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { selectStableDrivers, selectUpdatableDrivers, hasAnyUpdatableDriverMatching } from "@/lib/connection/driverListFilter";
import { selectStableDrivers, selectUpdatableDrivers, hasAnyUpdatableDriverMatching, selectDriversByInstallStatus, countInstalledDrivers, countAvailableDrivers, partitionDriversByInstallStatus, upgradeAllDriverTypes, upgradeAllMatchesFullUpdateSet } from "@/lib/connection/driverListFilter";
import type { AgentDriverInfo } from "@/lib/backend/api";
function driver(overrides: Partial<AgentDriverInfo> = {}): AgentDriverInfo {
@@ -17,6 +17,123 @@ function driver(overrides: Partial<AgentDriverInfo> = {}): AgentDriverInfo {
};
}
describe("selectDriversByInstallStatus", () => {
const mysql = driver({ db_type: "mysql", installed: true });
const postgres = driver({ db_type: "postgres", installed: false });
const sqlite = driver({ db_type: "sqlite", installed: true, update_available: true });
it("returns a copy of all drivers for 'all'", () => {
const drivers = [mysql, postgres, sqlite];
const result = selectDriversByInstallStatus(drivers, "all");
expect(result.map((d) => d.db_type)).toEqual(["mysql", "postgres", "sqlite"]);
expect(result).not.toBe(drivers);
});
it("returns only installed drivers, including those with updates", () => {
expect(selectDriversByInstallStatus([mysql, postgres, sqlite], "installed").map((d) => d.db_type)).toEqual(["mysql", "sqlite"]);
});
it("returns only drivers that are not installed", () => {
expect(selectDriversByInstallStatus([mysql, postgres, sqlite], "available").map((d) => d.db_type)).toEqual(["postgres"]);
});
it("returns empty arrays when nothing matches", () => {
expect(selectDriversByInstallStatus([postgres], "installed")).toEqual([]);
expect(selectDriversByInstallStatus([mysql], "available")).toEqual([]);
expect(selectDriversByInstallStatus([], "all")).toEqual([]);
});
});
describe("install-status counts", () => {
it("counts installed and available drivers without double-counting updates", () => {
const drivers = [driver({ db_type: "mysql", installed: true }), driver({ db_type: "postgres", installed: false }), driver({ db_type: "sqlite", installed: true, update_available: true })];
expect(countInstalledDrivers(drivers)).toBe(2);
expect(countAvailableDrivers(drivers)).toBe(1);
});
it("returns zeros for an empty list", () => {
expect(countInstalledDrivers([])).toBe(0);
expect(countAvailableDrivers([])).toBe(0);
});
});
describe("partitionDriversByInstallStatus", () => {
const mysql = driver({ db_type: "mysql", installed: true, update_available: false });
const postgres = driver({ db_type: "postgres", installed: false, update_available: false });
const sqlite = driver({ db_type: "sqlite", installed: true, update_available: true });
// Stale registry / missing jar: not installed, but update_available is true.
const staleOracle = driver({ db_type: "oracle", installed: false, update_available: true });
it("keeps uninstalled update_available drivers in the available list and hides the banner", () => {
const { updatable, stable } = partitionDriversByInstallStatus([mysql, postgres, sqlite, staleOracle], "available");
expect(updatable).toEqual([]);
expect(stable.map((d) => d.db_type)).toEqual(["postgres", "oracle"]);
});
it("does not put uninstalled update_available drivers in the installed banner", () => {
const { updatable, stable } = partitionDriversByInstallStatus([mysql, postgres, sqlite, staleOracle], "installed");
expect(updatable.map((d) => d.db_type)).toEqual(["sqlite"]);
expect(stable.map((d) => d.db_type)).toEqual(["mysql"]);
});
it("keeps the all-status partition aligned with updatable vs stable", () => {
const drivers = [mysql, postgres, sqlite, staleOracle];
const { updatable, stable } = partitionDriversByInstallStatus(drivers, "all");
expect(updatable.map((d) => d.db_type)).toEqual(["sqlite", "oracle"]);
expect(stable.map((d) => d.db_type)).toEqual(["mysql", "postgres"]);
expect(updatable.length + stable.length).toBe(drivers.length);
});
it("returns empty sides when the status filter matches nothing", () => {
expect(partitionDriversByInstallStatus([postgres], "installed")).toEqual({ updatable: [], stable: [] });
expect(partitionDriversByInstallStatus([mysql], "available")).toEqual({ updatable: [], stable: [] });
});
it("does not copy the naive available composition that drops stale updatables", () => {
expect(selectStableDrivers(selectDriversByInstallStatus([staleOracle], "available"))).toEqual([]);
expect(partitionDriversByInstallStatus([staleOracle], "available").stable.map((d) => d.db_type)).toEqual(["oracle"]);
});
it("installed banner+list cover every installed driver exactly once", () => {
const drivers = [mysql, postgres, sqlite, staleOracle];
const { updatable, stable } = partitionDriversByInstallStatus(drivers, "installed");
expect(updatable.length + stable.length).toBe(countInstalledDrivers(drivers));
});
it("available list covers every uninstalled driver exactly once", () => {
const drivers = [mysql, postgres, sqlite, staleOracle];
const { updatable, stable } = partitionDriversByInstallStatus(drivers, "available");
expect(updatable).toEqual([]);
expect(stable.length).toBe(countAvailableDrivers(drivers));
});
it("Upgrade All keys match the visible banner, not hidden stale uninstalled rows", () => {
const drivers = [mysql, postgres, sqlite, staleOracle];
expect(upgradeAllDriverTypes(drivers, "installed")).toEqual(["sqlite"]);
expect(upgradeAllMatchesFullUpdateSet(drivers, "installed")).toBe(false);
expect(upgradeAllDriverTypes(drivers, "all")).toEqual(["sqlite", "oracle"]);
expect(upgradeAllMatchesFullUpdateSet(drivers, "all")).toBe(true);
expect(upgradeAllDriverTypes(drivers, "available")).toEqual([]);
expect(upgradeAllMatchesFullUpdateSet(drivers, "available")).toBe(false);
});
it("allows the batch Upgrade All button when every updatable is visible", () => {
expect(upgradeAllMatchesFullUpdateSet([sqlite], "installed")).toBe(true);
expect(upgradeAllDriverTypes([sqlite], "installed")).toEqual(["sqlite"]);
});
it("does not treat equal-sized but different updatable sets as a safe batch", () => {
const installedOther = driver({ db_type: "mysql", installed: true, update_available: false });
const visibleInstalled = driver({ db_type: "sqlite", installed: true, update_available: true });
const hiddenStale = driver({ db_type: "oracle", installed: false, update_available: true });
expect(upgradeAllDriverTypes([installedOther, visibleInstalled, hiddenStale], "installed")).toEqual(["sqlite"]);
expect(upgradeAllMatchesFullUpdateSet([installedOther, visibleInstalled, hiddenStale], "installed")).toBe(false);
});
});
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 })];
@@ -190,4 +307,28 @@ describe("hasAnyUpdatableDriverMatching", () => {
}),
).toBe(true);
});
it("returns false when the install-status filter is 'available' (update banner is hidden)", () => {
expect(
hasAnyUpdatableDriverMatching([neo4j], {
searchQuery: "",
selectedCategory: "all",
driverMatchesSearch: labelSearch,
driverCategory: categoryOf,
installStatus: "available",
}),
).toBe(false);
});
it("still matches updatable drivers when the install-status filter is 'installed'", () => {
expect(
hasAnyUpdatableDriverMatching([neo4j], {
searchQuery: "",
selectedCategory: "all",
driverMatchesSearch: labelSearch,
driverCategory: categoryOf,
installStatus: "installed",
}),
).toBe(true);
});
});
@@ -80,6 +80,31 @@ export type DriverStoreTab = "agent" | "jdbc" | "storage" | "runtime";
export type DriverStoreFocus = { target: "driver"; driver?: string } | { target: "jre" } | { target: "tab"; tab: DriverStoreTab };
export function driverStoreFocusElementKey(focus: DriverStoreFocus): string {
return focus.target === "driver" ? `driver:${focus.driver ?? ""}` : "jre";
}
/**
* Apply search/category/status reset + scroll only once per focus key, or when
* the parent hands us a new focus object (user re-triggered the same hint).
* Inventory refreshes must not clobber the status filter.
*/
export function shouldApplyDriverStoreFocus(lastAppliedKey: string | null, nextKey: string, focusChanged: boolean): boolean {
if (focusChanged) return true;
return lastAppliedKey !== nextKey;
}
/**
* Agent list `v-if="drivers.length === 0"` is a loading placeholder that hides
* every row, including managed JDBC rows already present in `builtinDriverRows`.
* Committing focus before that would mark the key applied and skip the later scroll.
*/
export function driverStoreFocusRowIsRenderable(focus: DriverStoreFocus, agentDriverCount: number, builtinRows: readonly { db_type: string }[]): boolean {
if (focus.target !== "driver") return true;
if (agentDriverCount === 0) return false;
return builtinRows.some((row) => row.db_type === focus.driver);
}
/** Maps a backend connect error to the Driver Store item that can fix it. */
export function driverStoreFocusForInstallError(message: string, dbType?: DatabaseType, driverProfile?: string): DriverStoreFocus | null {
if (message.includes("JRE") && message.includes("not installed")) return { target: "jre" };
@@ -1,5 +1,66 @@
import type { AgentDriverInfo } from "@/lib/backend/api";
/** Orthogonal to category: inventory vs catalog. Default is `"all"`. */
export type DriverInstallStatusFilter = "all" | "installed" | "available";
export function selectDriversByInstallStatus(drivers: readonly AgentDriverInfo[], status: DriverInstallStatusFilter): AgentDriverInfo[] {
if (status === "installed") return drivers.filter((d) => d.installed);
if (status === "available") return drivers.filter((d) => !d.installed);
return [...drivers];
}
export function countInstalledDrivers(drivers: readonly AgentDriverInfo[]): number {
return drivers.reduce((count, driver) => count + (driver.installed ? 1 : 0), 0);
}
export function countAvailableDrivers(drivers: readonly AgentDriverInfo[]): number {
return drivers.length - countInstalledDrivers(drivers);
}
export interface PartitionedDriverList {
updatable: AgentDriverInfo[];
stable: AgentDriverInfo[];
}
/**
* Banner vs list partition for the current install-status filter.
*
* The update banner is hidden on `"available"`. Uninstalled rows with
* `update_available` (stale local registry / missing jar) must stay in the
* list, not disappear from both surfaces. On `"installed"` the banner only
* includes installed updatables.
*/
export function partitionDriversByInstallStatus(drivers: readonly AgentDriverInfo[], status: DriverInstallStatusFilter): PartitionedDriverList {
const statusFiltered = selectDriversByInstallStatus(drivers, status);
if (status === "available") {
return { updatable: [], stable: statusFiltered };
}
const updatable = selectUpdatableDrivers(statusFiltered);
const updatableKeys = new Set(updatable.map((driver) => driver.db_type));
return {
updatable,
stable: statusFiltered.filter((driver) => !updatableKeys.has(driver.db_type)),
};
}
/** Driver keys the visible update banner would upgrade. */
export function upgradeAllDriverTypes(drivers: readonly AgentDriverInfo[], status: DriverInstallStatusFilter): string[] {
return partitionDriversByInstallStatus(drivers, status).updatable.map((driver) => driver.db_type);
}
/**
* True when a batch `upgradeAllAgents` (every `update_available` row) would
* match the visible banner. False on Installed when a stale uninstalled
* updatable exists — per-row Update stays, the global batch button does not.
*/
export function upgradeAllMatchesFullUpdateSet(drivers: readonly AgentDriverInfo[], status: DriverInstallStatusFilter): boolean {
const visible = upgradeAllDriverTypes(drivers, status);
const all = selectUpdatableDrivers(drivers).map((driver) => driver.db_type);
if (visible.length !== all.length) return false;
const visibleKeys = new Set(visible);
return all.every((key) => visibleKeys.has(key));
}
/**
* Drivers that have an update available — always rendered above category
* navigation so the user can see and act on them regardless of the active
@@ -27,6 +88,11 @@ export interface UpdatableDriverMatchOptions {
driverMatchesSearch: (driver: AgentDriverInfo, query: string) => boolean;
/** Returns the category key for `driver`. */
driverCategory: (driver: AgentDriverInfo) => string;
/**
* Current install-status filter. Updatable drivers are already installed, so
* `"available"` hides the update banner and must not suppress the empty state.
*/
installStatus?: DriverInstallStatusFilter;
}
/**
@@ -38,6 +104,7 @@ export interface UpdatableDriverMatchOptions {
*/
export function hasAnyUpdatableDriverMatching(updatableDrivers: readonly AgentDriverInfo[], opts: UpdatableDriverMatchOptions): boolean {
if (updatableDrivers.length === 0) return false;
if (opts.installStatus === "available") return false;
if (opts.searchQuery) {
return updatableDrivers.some((d) => opts.driverMatchesSearch(d, opts.searchQuery));
}