fix(drivers): isolate concurrent install progress

This commit is contained in:
t8y2 committed 2026-07-23 14:38:57 +08:00
1 parent e5c436b563
commit 31c7fcc16f
5 files changed
+133 -42

No files matched your search

@@ -0,0 +1,44 @@
import { describe, expect, it } from "vitest";
import { driverInstallProgressChannel, updateDriverInstallProgress, type DriverInstallProgress } from "@/lib/connection/driverInstallProgressUi";
describe("driver install progress channels", () => {
it("classifies agent and JDBC plugin progress independently", () => {
expect(driverInstallProgressChannel({ step: "driver", db_type: "mysql" })).toBe("agent");
expect(driverInstallProgressChannel({ step: "jre-extract", db_type: "oracle" })).toBe("agent");
expect(driverInstallProgressChannel({ step: "jdbc-plugin" })).toBe("jdbc-plugin");
expect(driverInstallProgressChannel({ step: "jdbc-plugin-extract" })).toBe("jdbc-plugin");
});
it("does not let concurrent progress overwrite the other channel", () => {
const agentProgress: DriverInstallProgress = { step: "driver", db_type: "mysql", downloaded: 20, total: 100 };
const jdbcProgress: DriverInstallProgress = { step: "jdbc-plugin", downloaded: 70, total: 100 };
expect(updateDriverInstallProgress(agentProgress, jdbcProgress, "agent")).toBe(agentProgress);
expect(updateDriverInstallProgress(jdbcProgress, agentProgress, "jdbc-plugin")).toBe(jdbcProgress);
});
it("allows a built-in driver backed by JDBC to consume JDBC progress explicitly", () => {
const jdbcProgress: DriverInstallProgress = { step: "jdbc-plugin", downloaded: 40, total: 100 };
expect(updateDriverInstallProgress(null, jdbcProgress, "jdbc-plugin")).toBe(jdbcProgress);
expect(updateDriverInstallProgress(null, jdbcProgress, "agent")).toBeNull();
});
it("clears only the channel identified by a terminal event", () => {
const agentProgress: DriverInstallProgress = { step: "driver", db_type: "mysql", downloaded: 100, total: 100 };
const jdbcProgress: DriverInstallProgress = { step: "jdbc-plugin", downloaded: 50, total: 100 };
const agentDone: DriverInstallProgress = { step: "done", db_type: "mysql" };
expect(updateDriverInstallProgress(agentProgress, agentDone, "agent")).toBeNull();
expect(updateDriverInstallProgress(jdbcProgress, agentDone, "jdbc-plugin")).toBe(jdbcProgress);
});
it("ignores legacy ambiguous done events so operation cleanup owns reset", () => {
const agentProgress: DriverInstallProgress = { step: "driver", db_type: "mysql", downloaded: 80, total: 100 };
const jdbcProgress: DriverInstallProgress = { step: "jdbc-plugin", downloaded: 80, total: 100 };
const ambiguousDone: DriverInstallProgress = { step: "done" };
expect(updateDriverInstallProgress(agentProgress, ambiguousDone, "agent")).toBe(agentProgress);
expect(updateDriverInstallProgress(jdbcProgress, ambiguousDone, "jdbc-plugin")).toBe(jdbcProgress);
});
});
@@ -11,6 +11,23 @@ export interface DriverInstallProgressTargetState {
progress: DriverInstallProgress | null;
}
export type DriverInstallProgressChannel = "agent" | "jdbc-plugin";
const AGENT_PROGRESS_STEPS = new Set(["driver", "jre", "jre-extract", "all-done"]);
export function driverInstallProgressChannel(progress: DriverInstallProgress): DriverInstallProgressChannel | null {
if (progress.step === "jdbc-plugin" || progress.step === "jdbc-plugin-extract") return "jdbc-plugin";
if (progress.db_type || AGENT_PROGRESS_STEPS.has(progress.step)) return "agent";
// Legacy "done" events have no owner, so the operation promise must clear its own channel.
return null;
}
export function updateDriverInstallProgress(current: DriverInstallProgress | null, incoming: DriverInstallProgress, channel: DriverInstallProgressChannel): DriverInstallProgress | null {
if (driverInstallProgressChannel(incoming) !== channel) return current;
if (incoming.step === "done" || incoming.step === "all-done") return null;
return incoming;
}
export function driverInstallProgressPercent(progress: DriverInstallProgress | null): number | null {
if (!progress?.total || progress.total <= 0) return null;
const percent = Math.round(((progress.downloaded ?? 0) / progress.total) * 100);