feat(plugins): improve connection lifecycle and session tabs
This commit is contained in:
1 parent
6be8d5544e
commit
1321dc9b9d
25 files changed
+849
-44
No files matched your search
@@ -548,6 +548,35 @@ describe("connectionStore plugin password prompt", () => {
|
||||
expect(connectDb).toHaveBeenCalledWith(expect.objectContaining({ id: "ssh-1", external_config: expect.objectContaining({ authentication: "private-key" }) }), expect.any(Number));
|
||||
});
|
||||
|
||||
it("rejects reopening a connection owned by another plugin before prompting or connecting", async () => {
|
||||
const connectDb = vi.fn().mockResolvedValue("ssh-1");
|
||||
installApiMocks({ connectDb, listPlugins: vi.fn().mockResolvedValue([sshInstalledPlugin()]) });
|
||||
installPasswordPromptMock();
|
||||
requestPassword.mockResolvedValue({ password: PROMPT_TYPED_PASSWORD, rememberPassword: false });
|
||||
const { useConnectionStore } = await import("@/stores/connectionStore");
|
||||
const store = useConnectionStore();
|
||||
store.connections = [pluginConnection({ external_config: { authentication: "password" } })];
|
||||
|
||||
await expect(store.reopenPluginConnection("ssh-1", "io.dbx.other")).rejects.toThrow("Connection is owned by another plugin");
|
||||
|
||||
expect(requestPassword).not.toHaveBeenCalled();
|
||||
expect(connectDb).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("allows the owning plugin to reopen its connection", async () => {
|
||||
const connectDb = vi.fn().mockResolvedValue("ssh-1");
|
||||
installApiMocks({ connectDb, listPlugins: vi.fn().mockResolvedValue([sshInstalledPlugin()]) });
|
||||
installPasswordPromptMock();
|
||||
const { useConnectionStore } = await import("@/stores/connectionStore");
|
||||
const store = useConnectionStore();
|
||||
store.connections = [pluginConnection()];
|
||||
|
||||
await expect(store.reopenPluginConnection("ssh-1", "io.dbx.ssh")).resolves.toBeUndefined();
|
||||
|
||||
expect(requestPassword).not.toHaveBeenCalled();
|
||||
expect(connectDb).toHaveBeenCalledWith(expect.objectContaining({ id: "ssh-1" }), expect.any(Number));
|
||||
});
|
||||
|
||||
it("still prompts for a plugin connection whose manifest requires the login password", async () => {
|
||||
installApiMocks({ listPlugins: vi.fn().mockResolvedValue([sshInstalledPlugin()]) });
|
||||
installPasswordPromptMock();
|
||||
|
||||
@@ -95,4 +95,74 @@ describe("queryStore openPluginWorkbench reuse", () => {
|
||||
// Adoption must not replace the live context (same no-deep-reload rule).
|
||||
expect(queryStore.tabs.find((t) => t.id === id)?.pluginWorkbench?.context).toEqual({ connectionId: "conn-1" });
|
||||
});
|
||||
|
||||
it("numbers same-connection session tabs Termius-style and never backfills", async () => {
|
||||
const queryStore = useQueryStore();
|
||||
|
||||
// First tab keeps the bare connection name; numbering starts at (1) from
|
||||
// the second tab on.
|
||||
const firstId = queryStore.openPluginWorkbench("io.dbx.ssh", "workbench", {
|
||||
title: "SSH server222",
|
||||
connectionId: "conn-1",
|
||||
context: { connectionId: "conn-1" },
|
||||
});
|
||||
// forceNew is orthogonal to numbering: it skips dedup, the count still grows.
|
||||
const secondId = queryStore.openPluginWorkbench("io.dbx.ssh", "workbench", {
|
||||
title: "SSH server222",
|
||||
connectionId: "conn-1",
|
||||
context: { connectionId: "conn-1", workbenchId: "wb-2" },
|
||||
forceNew: true,
|
||||
});
|
||||
const thirdId = queryStore.openPluginWorkbench("io.dbx.ssh", "workbench", {
|
||||
title: "SSH server222",
|
||||
connectionId: "conn-1",
|
||||
context: { connectionId: "conn-1", workbenchId: "wb-3" },
|
||||
forceNew: true,
|
||||
});
|
||||
|
||||
const titles = Object.fromEntries(queryStore.tabs.map((tab) => [tab.id, tab.title]));
|
||||
expect([titles[firstId], titles[secondId], titles[thirdId]]).toEqual(["SSH server222", "SSH server222 (1)", "SSH server222 (2)"]);
|
||||
|
||||
// Closing tab (1) must not renumber (2): existing titles stay stable (no
|
||||
// backfill, no drift). The next tab advances beyond the highest live suffix
|
||||
// so two open sessions never share a title. (Removed directly: the stubbed
|
||||
// window in this harness lacks the timers closeTab's cleanup needs.)
|
||||
queryStore.tabs = queryStore.tabs.filter((tab) => tab.id !== secondId);
|
||||
const fourthId = queryStore.openPluginWorkbench("io.dbx.ssh", "workbench", {
|
||||
title: "SSH server222",
|
||||
connectionId: "conn-1",
|
||||
context: { connectionId: "conn-1", workbenchId: "wb-4" },
|
||||
forceNew: true,
|
||||
});
|
||||
expect(queryStore.tabs.find((tab) => tab.id === thirdId)?.title).toBe("SSH server222 (2)");
|
||||
expect(queryStore.tabs.find((tab) => tab.id === fourthId)?.title).toBe("SSH server222 (3)");
|
||||
|
||||
// Closing the highest suffix may reuse that now-free number, but must not
|
||||
// collide with any session that remains open.
|
||||
queryStore.tabs = queryStore.tabs.filter((tab) => tab.id !== fourthId);
|
||||
const fifthId = queryStore.openPluginWorkbench("io.dbx.ssh", "workbench", {
|
||||
title: "SSH server222",
|
||||
connectionId: "conn-1",
|
||||
context: { connectionId: "conn-1", workbenchId: "wb-5" },
|
||||
forceNew: true,
|
||||
});
|
||||
expect(queryStore.tabs.find((tab) => tab.id === fifthId)?.title).toBe("SSH server222 (3)");
|
||||
});
|
||||
|
||||
it("numbers sessions independently per connection and reads bridge-style context-only connection ids", async () => {
|
||||
const queryStore = useQueryStore();
|
||||
|
||||
queryStore.openPluginWorkbench("io.dbx.ssh", "workbench", { title: "A", connectionId: "conn-1", context: { connectionId: "conn-1" }, forceNew: true });
|
||||
const otherConnectionId = queryStore.openPluginWorkbench("io.dbx.ssh", "workbench", { title: "B", connectionId: "conn-2", context: { connectionId: "conn-2" }, forceNew: true });
|
||||
// A different connection keeps its own bare title, unaffected by conn-1's count.
|
||||
expect(queryStore.tabs.find((tab) => tab.id === otherConnectionId)?.title).toBe("B");
|
||||
|
||||
// Bridge-path legacy tab: connection only inside context (tab-level "").
|
||||
// It must join conn-1's numbering (second conn-1 tab → (1)) and its own
|
||||
// tab-level connectionId gets normalized for reconnect/restore logic.
|
||||
const bridgedId = queryStore.openPluginWorkbench("io.dbx.ssh", "workbench", { title: "A", context: { connectionId: "conn-1" }, forceNew: true });
|
||||
const bridged = queryStore.tabs.find((tab) => tab.id === bridgedId);
|
||||
expect(bridged?.title).toBe("A (1)");
|
||||
expect(bridged?.connectionId).toBe("conn-1");
|
||||
});
|
||||
});
|
||||
@@ -4334,8 +4334,8 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
invalidateConnectionMetadataLifetime(connectionId, database);
|
||||
}
|
||||
|
||||
async function ensureConnected(connectionId: string, options: { activate?: boolean; verifyHealth?: boolean } = {}) {
|
||||
if (connectedIds.value.has(connectionId)) {
|
||||
async function ensureConnected(connectionId: string, options: { activate?: boolean; verifyHealth?: boolean; forceReconnect?: boolean } = {}) {
|
||||
if (!options.forceReconnect && connectedIds.value.has(connectionId)) {
|
||||
// Pure navigation can safely trust the existing connected state. Its
|
||||
// destination will perform the real API request, while blocking here on
|
||||
// a health probe makes an otherwise local tab switch take up to 5s.
|
||||
@@ -4450,6 +4450,49 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-push an already-open plugin connection's config (credentials included)
|
||||
* to its sidecar through the same connection/connect path used when opening
|
||||
* from the sidebar. A plugin iframe reload can leave the sidecar's in-memory
|
||||
* connection registry empty while the host still considers the connection
|
||||
* open, and ensureConnected()'s health fast-path would not heal that. Silent
|
||||
* no-op when the connection is not open, is not plugin-backed, or its
|
||||
* credentials are no longer available (save_password=false without a live
|
||||
* session credential): the plugin then keeps its existing "reopen from the
|
||||
* sidebar" guidance instead of triggering an interactive prompt from a
|
||||
* background re-init.
|
||||
*/
|
||||
async function repushPluginConnection(connectionId: string): Promise<void> {
|
||||
const config = getConfig(connectionId);
|
||||
if (!config || config.db_type !== "plugin" || !connectedIds.value.has(connectionId)) return;
|
||||
// A successful connect/health probe within the TTL means the sidebar open
|
||||
// (or a fresh restore connect) pushed the config moments ago and the
|
||||
// sidecar registry cannot plausibly be empty yet — skipping here keeps the
|
||||
// first open from paying a redundant disconnect+connect cycle on the
|
||||
// plugin's first `ready`. The 2s in-memory TTL dies with the frontend, so
|
||||
// every realistic reload path still re-pushes.
|
||||
if (hasRecentConnectionHealthCheck(connectionId)) return;
|
||||
if (connectionNeedsPasswordPrompt(config) && (await pluginConnectionPasswordPromptNeeded(config)) && !(await hasSessionCredential(connectionId))) return;
|
||||
await ensureConnected(connectionId, { activate: false, forceReconnect: true });
|
||||
}
|
||||
|
||||
/**
|
||||
* Explicit, user-triggered reconnect of a plugin connection (the plugin's own
|
||||
* "reconnect" button). Unlike repushPluginConnection — the silent background
|
||||
* heal — this runs the full connect flow and MAY show the interactive
|
||||
* password prompt, which is appropriate for a deliberate user action. Editing
|
||||
* a connection drops it from connectedIds without notifying plugins, so the
|
||||
* sidecar's config goes stale; this is the plugin's way to request a fresh
|
||||
* connection/connect with the updated config.
|
||||
*/
|
||||
async function reopenPluginConnection(connectionId: string, pluginId: string): Promise<void> {
|
||||
const config = getConfig(connectionId);
|
||||
if (!config) throw new Error("Connection config not found");
|
||||
if (config.db_type !== "plugin") throw new Error("Connection is not plugin-backed");
|
||||
if (config.plugin_id !== pluginId) throw new Error("Connection is owned by another plugin");
|
||||
await ensureConnected(connectionId, { activate: false, forceReconnect: true });
|
||||
}
|
||||
|
||||
function setBeforeConnectHandler(handler: BeforeConnectHandler | null) {
|
||||
beforeConnectHandler = handler;
|
||||
}
|
||||
@@ -9334,6 +9377,8 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
startCreatingConnectionInGroup,
|
||||
stopCreatingConnectionInGroup,
|
||||
connect,
|
||||
repushPluginConnection,
|
||||
reopenPluginConnection,
|
||||
cancelConnecting,
|
||||
disconnect,
|
||||
hasDisconnectInFlight,
|
||||
|
||||
@@ -3465,9 +3465,23 @@ export const useQueryStore = defineStore("query", () => {
|
||||
tab.nacosTargetKeyword = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Effective connection binding of a plugin tab: bridge-created tabs carry
|
||||
* the connection only inside context (tab-level connectionId is ""), so
|
||||
* read both. New tabs normalize the context value up to the tab level at
|
||||
* creation, but restored/legacy tabs still need the fallback.
|
||||
*/
|
||||
function pluginTabConnectionId(tab: QueryTab): string {
|
||||
if (tab.connectionId) return tab.connectionId;
|
||||
const contextConnectionId = tab.pluginWorkbench?.context?.connectionId;
|
||||
return typeof contextConnectionId === "string" ? contextConnectionId : "";
|
||||
}
|
||||
|
||||
function openPluginWorkbench(pluginId: string, contributionId: string, options: { title?: string; connectionId?: string; database?: string; context?: Record<string, unknown>; forceNew?: boolean } = {}) {
|
||||
const contextConnectionId = typeof options.context?.connectionId === "string" ? options.context.connectionId : "";
|
||||
const connectionId = options.connectionId || contextConnectionId;
|
||||
if (!options.forceNew) {
|
||||
const existing = tabs.value.find((tab) => tab.mode === "plugin-workbench" && tab.pluginWorkbench?.pluginId === pluginId && tab.pluginWorkbench?.contributionId === contributionId && tab.connectionId === (options.connectionId || ""));
|
||||
const existing = tabs.value.find((tab) => tab.mode === "plugin-workbench" && tab.pluginWorkbench?.pluginId === pluginId && tab.pluginWorkbench?.contributionId === contributionId && pluginTabConnectionId(tab) === connectionId);
|
||||
if (existing) {
|
||||
// Reopening surfaces the existing tab as-is. Replacing the context
|
||||
// here (openPluginConnection mints a fresh workbenchId per click)
|
||||
@@ -3484,11 +3498,23 @@ export const useQueryStore = defineStore("query", () => {
|
||||
}
|
||||
}
|
||||
|
||||
// Termius-style session numbering: the first same-plugin + same-connection
|
||||
// tab keeps the bare connection name; each additional one gets " (n)"
|
||||
// where n advances beyond the highest live suffix (second tab → (1)).
|
||||
// Numbers are assigned at creation and never backfilled after a close —
|
||||
// stable titles beat dense numbering, while live titles stay unique.
|
||||
const siblingTabs = tabs.value.filter((tab) => tab.mode === "plugin-workbench" && tab.pluginWorkbench?.pluginId === pluginId && pluginTabConnectionId(tab) === connectionId);
|
||||
const nextSessionNumber =
|
||||
siblingTabs.reduce((highest, tab) => {
|
||||
const suffix = / \((\d+)\)$/.exec(tab.title);
|
||||
return suffix ? Math.max(highest, Number(suffix[1])) : highest;
|
||||
}, 0) + 1;
|
||||
const baseTitle = options.title || contributionId;
|
||||
const id = uuid();
|
||||
const tab: QueryTab = {
|
||||
id,
|
||||
title: options.title || contributionId,
|
||||
connectionId: options.connectionId || "",
|
||||
title: siblingTabs.length >= 1 ? `${baseTitle} (${nextSessionNumber})` : baseTitle,
|
||||
connectionId,
|
||||
database: options.database || "",
|
||||
sql: "",
|
||||
isExecuting: false,
|
||||
|
||||
Reference in new issue
Block a user