diff --git a/apps/desktop/src/components/plugins/PluginFilesystemTab.spec.ts b/apps/desktop/src/components/plugins/PluginFilesystemTab.spec.ts new file mode 100644 index 0000000000..77d8df3220 --- /dev/null +++ b/apps/desktop/src/components/plugins/PluginFilesystemTab.spec.ts @@ -0,0 +1,88 @@ +// @vitest-environment happy-dom + +import { createApp, nextTick, ref, type App } from "vue"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +const mocks = vi.hoisted(() => ({ + listPlugins: vi.fn(), + ensureConnected: vi.fn(), + repushPluginConnection: vi.fn(), + connectedIds: new Set(["conn-1"]), +})); + +vi.mock("@/lib/backend/api", () => ({ + invokePlugin: vi.fn(), + notifyPlugin: vi.fn(), + sendPluginBinary: vi.fn(), + listPlugins: mocks.listPlugins, + readPluginUiEntry: vi.fn(), + readPluginUiAsset: vi.fn(), + subscribePluginEvents: vi.fn(), +})); +vi.mock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false })); +vi.mock("@/stores/connectionStore", () => ({ + useConnectionStore: () => ({ + connectedIds: mocks.connectedIds, + getConfig: () => undefined, + ensureConnected: mocks.ensureConnected, + repushPluginConnection: mocks.repushPluginConnection, + }), +})); +vi.mock("vue-i18n", () => ({ + useI18n: () => ({ locale: ref("en"), t: (key: string) => key }), +})); + +import PluginFilesystemTab from "./PluginFilesystemTab.vue"; + +describe("PluginFilesystemTab restore self-heal", () => { + let app: App | undefined; + let root: HTMLDivElement; + + beforeEach(() => { + vi.clearAllMocks(); + mocks.listPlugins.mockResolvedValue([]); + mocks.ensureConnected.mockResolvedValue(undefined); + mocks.repushPluginConnection.mockResolvedValue(undefined); + mocks.connectedIds.clear(); + root = document.createElement("div"); + document.body.appendChild(root); + }); + + afterEach(() => { + app?.unmount(); + root.remove(); + }); + + async function mountTab() { + app = createApp(PluginFilesystemTab, { pluginId: "io.dbx.files", providerId: "files.main", connectionId: "conn-1" }); + app.mount(root); + await nextTick(); + await new Promise((resolve) => setTimeout(resolve, 0)); + await nextTick(); + } + + it("self-heals a restored tab: silent ensureConnected instead of the reload prompt", async () => { + await mountTab(); + + expect(mocks.ensureConnected).toHaveBeenCalledWith("conn-1", { allowPasswordPrompt: false }); + expect(root.textContent).not.toContain("pluginPlatform.reloadRequired"); + }); + + it("re-pushes credentials for an open connection when refreshed", async () => { + mocks.connectedIds.add("conn-1"); + await mountTab(); + expect(mocks.repushPluginConnection).not.toHaveBeenCalled(); + + const exposed = (app as unknown as { _instance?: { exposed?: Record unknown> } })._instance?.exposed; + await exposed?.refresh?.(); + expect(mocks.repushPluginConnection).toHaveBeenCalledWith("conn-1", { ignoreRecentHealthCheck: false }); + expect(mocks.ensureConnected).not.toHaveBeenCalled(); + }); + + it("parks on the reload prompt when silent recovery fails", async () => { + mocks.ensureConnected.mockRejectedValue(new Error("interactive password required")); + await mountTab(); + + expect(root.textContent).toContain("pluginPlatform.reloadRequired"); + }); +}); diff --git a/apps/desktop/src/components/plugins/PluginFilesystemTab.vue b/apps/desktop/src/components/plugins/PluginFilesystemTab.vue index 7367b57e60..02e0bed309 100644 --- a/apps/desktop/src/components/plugins/PluginFilesystemTab.vue +++ b/apps/desktop/src/components/plugins/PluginFilesystemTab.vue @@ -50,12 +50,31 @@ async function load() { } } -async function refresh() { +async function refresh(options: { silent?: boolean } = {}) { + const silent = options.silent === true; deferred.value = false; loading.value = true; error.value = ""; try { - if (props.connectionId) await connectionStore.ensureConnected(props.connectionId); + if (props.connectionId) { + if (connectionStore.connectedIds.has(props.connectionId)) { + // Mirror PluginWorkbenchTab: an open connection's sidecar registry can + // still be wiped (iframe reload, tab restore) and ensureConnected's + // health fast-path would not heal that — force a re-push; it no-ops + // when a push just happened. Silent restore overrides the recent-health + // TTL (the SPA's boot restore can mark a connection healthy without + // delivering credentials to the sidecar) and parks on the prompt when + // the push cannot run (dbx-plugin-ssh#144). + const pushed = await connectionStore.repushPluginConnection(props.connectionId, { ignoreRecentHealthCheck: silent }); + if (silent && pushed === false) { + loading.value = false; + deferred.value = true; + return; + } + } else { + await connectionStore.ensureConnected(props.connectionId, silent ? { allowPasswordPrompt: false } : undefined); + } + } if (fileManagerRef.value?.refresh) { loading.value = false; return await fileManagerRef.value.refresh(); @@ -63,15 +82,30 @@ async function refresh() { await load(); } catch (cause) { loading.value = false; + if (silent) { + // Silent recovery (tab restore after a page refresh, or a filesystem + // tab opened for a not-yet-connected connection) must not surface an + // error card nor trigger an interactive credential prompt from the + // background: park on the explicit reload prompt so the user can retry + // interactively (dbx-plugin-ssh#144). + error.value = ""; + deferred.value = !!props.connectionId; + return; + } error.value = cause instanceof Error ? cause.message : String(cause); } } function start() { - deferred.value = !!props.connectionId && !connectionStore.connectedIds.has(props.connectionId); - if (deferred.value) { - loading.value = false; - error.value = ""; + if (props.connectionId && !connectionStore.connectedIds.has(props.connectionId)) { + // A restored tab (page refresh) or a link for a closed connection used to + // park here from a one-shot snapshot: even when the SPA's boot connect + // replay landed a moment later nothing re-checked, so the tab dead-ended + // on the reload prompt (dbx-plugin-ssh#144, same reproduction as the + // workbench tab). Self-heal silently first and only park when silent + // recovery cannot proceed (interactive credentials required, connection + // gone). + void refresh({ silent: true }); return; } void load(); diff --git a/apps/desktop/src/components/plugins/PluginWorkbenchTab.spec.ts b/apps/desktop/src/components/plugins/PluginWorkbenchTab.spec.ts index f3b3207b56..06f3d9f7f9 100644 --- a/apps/desktop/src/components/plugins/PluginWorkbenchTab.spec.ts +++ b/apps/desktop/src/components/plugins/PluginWorkbenchTab.spec.ts @@ -12,6 +12,7 @@ const mocks = vi.hoisted(() => ({ repushPluginConnection: vi.fn(), ensureConnected: vi.fn(), openPluginWorkbench: vi.fn(), + connectedIds: new Set(["conn-1"]), })); vi.mock("@/lib/backend/api", () => ({ @@ -29,7 +30,7 @@ vi.mock("@/composables/useTheme", () => ({ useTheme: () => ({ isDark: ref(false) vi.mock("@/stores/settingsStore", () => ({ useSettingsStore: () => ({ editorSettings: { uiFontFamily: "", fontFamily: "", fontSize: 14 } }) })); vi.mock("@/stores/connectionStore", () => ({ useConnectionStore: () => ({ - connectedIds: new Set(["conn-1"]), + connectedIds: mocks.connectedIds, getConfig: () => undefined, ensureConnected: mocks.ensureConnected, repushPluginConnection: mocks.repushPluginConnection, @@ -154,3 +155,64 @@ describe("PluginWorkbenchTab contribution resolution", () => { expect(root.textContent).toContain("pluginPlatform.workbenchUnavailable"); }); }); + +// Restored-tab self-heal (dbx-plugin-ssh#144): a page refresh used to park the +// tab on the reload prompt from a one-shot `start()` snapshot, so the plugin +// could never re-establish its session even after the SPA's boot connect +// replay landed. `start()` now recovers silently and only parks when silent +// recovery cannot proceed. +describe("PluginWorkbenchTab restore self-heal", () => { + let app: App | undefined; + let root: HTMLDivElement; + + beforeEach(() => { + vi.clearAllMocks(); + vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => window.setTimeout(() => callback(0), 0)); + vi.stubGlobal("getComputedStyle", () => Object.assign([], { getPropertyValue: () => "" })); + mocks.listPlugins.mockResolvedValue([plugin]); + mocks.readPluginUiEntry.mockResolvedValue({ dataBase64: btoa(""), contentType: "text/html" }); + mocks.subscribePluginEvents.mockResolvedValue(vi.fn()); + mocks.repushPluginConnection.mockResolvedValue(undefined); + mocks.ensureConnected.mockResolvedValue(undefined); + mocks.connectedIds.clear(); + root = document.createElement("div"); + document.body.appendChild(root); + }); + + afterEach(() => { + app?.unmount(); + root.remove(); + vi.unstubAllGlobals(); + }); + + it("self-heals a restored tab: silent ensureConnected, then the workbench loads", async () => { + app = createApp(PluginWorkbenchTab, { pluginId: "com.example.graph", contributionId: "graph.main", connectionId: "conn-1" }); + app.mount(root); + + await vi.waitFor(() => expect(mocks.ensureConnected).toHaveBeenCalledWith("conn-1", { allowPasswordPrompt: false })); + await vi.waitFor(() => expect(root.querySelector("iframe")).toBeInstanceOf(HTMLIFrameElement)); + expect(root.textContent).not.toContain("pluginPlatform.reloadRequired"); + }); + + it("keeps an already-connected restore on the direct load path", async () => { + mocks.connectedIds.add("conn-1"); + app = createApp(PluginWorkbenchTab, { pluginId: "com.example.graph", contributionId: "graph.main", connectionId: "conn-1" }); + app.mount(root); + + await vi.waitFor(() => expect(root.querySelector("iframe")).toBeInstanceOf(HTMLIFrameElement)); + // The direct load path must not reconnect behind the user's back. (The + // host's PluginWorkbenchHost re-pushes credentials on its own mount; that + // is not start()'s concern here.) + expect(mocks.ensureConnected).not.toHaveBeenCalled(); + expect(root.textContent).not.toContain("pluginPlatform.reloadRequired"); + }); + + it("parks on the reload prompt without an error card when silent recovery fails", async () => { + mocks.ensureConnected.mockRejectedValue(new Error("interactive password required")); + app = createApp(PluginWorkbenchTab, { pluginId: "com.example.graph", contributionId: "graph.main", connectionId: "conn-1" }); + app.mount(root); + + await vi.waitFor(() => expect(root.textContent).toContain("pluginPlatform.reloadRequired")); + expect(root.querySelector("iframe")).toBeNull(); + }); +}); diff --git a/apps/desktop/src/components/plugins/PluginWorkbenchTab.vue b/apps/desktop/src/components/plugins/PluginWorkbenchTab.vue index 1f930a7279..1aa47071b2 100644 --- a/apps/desktop/src/components/plugins/PluginWorkbenchTab.vue +++ b/apps/desktop/src/components/plugins/PluginWorkbenchTab.vue @@ -56,7 +56,8 @@ async function load() { } } -async function refresh() { +async function refresh(options: { silent?: boolean } = {}) { + const silent = options.silent === true; deferred.value = false; loading.value = true; error.value = ""; @@ -67,25 +68,51 @@ async function refresh() { // The connection is open but the plugin sidecar's registry may have // been wiped (iframe reload, tab restore): ensureConnected's health // fast-path only probes the sidecar process and would not heal that. - // Force a re-push instead; it no-ops when a push just happened. - await connectionStore.repushPluginConnection(connectionId); + // Force a re-push instead; it no-ops when a push just happened. A + // silent restore additionally overrides the recent-health TTL: the + // SPA's boot restore can mark the connection healthy without ever + // delivering credentials to the sidecar, and a skipped push here + // strands the restored plugin with an empty registry + // (dbx-plugin-ssh#144). + const pushed = await connectionStore.repushPluginConnection(connectionId, { ignoreRecentHealthCheck: silent }); + if (silent && pushed === false) { + loading.value = false; + deferred.value = true; + return; + } } else { - await connectionStore.ensureConnected(connectionId); + await connectionStore.ensureConnected(connectionId, silent ? { allowPasswordPrompt: false } : undefined); } } await load(); } catch (cause) { loading.value = false; + if (silent) { + // Silent recovery (tab restore after a page refresh, or a workbench + // opened for a not-yet-connected connection) must not surface an error + // card nor trigger an interactive credential prompt from the background: + // park on the explicit reload prompt so the user can retry + // interactively (dbx-plugin-ssh#144). + error.value = ""; + deferred.value = !!resolvedConnectionId.value; + return; + } error.value = cause instanceof Error ? cause.message : String(cause); } } function start() { const connectionId = resolvedConnectionId.value; - deferred.value = !!connectionId && !connectionStore.connectedIds.has(connectionId); - if (deferred.value) { - loading.value = false; - error.value = ""; + if (connectionId && !connectionStore.connectedIds.has(connectionId)) { + // A restored tab (page refresh) or a link for a closed connection used to + // park here from a one-shot snapshot: even when the SPA's boot connect + // replay landed a moment later nothing re-checked, so the tab dead-ended + // on the reload prompt and the plugin could never re-establish its + // session (dbx-plugin-ssh#144). Self-heal silently instead — the Refresh + // button's path minus interactive prompts — and only park when silent + // recovery cannot proceed (interactive credentials required, connection + // gone). + void refresh({ silent: true }); return; } void load(); diff --git a/apps/desktop/src/stores/connectionStore.ts b/apps/desktop/src/stores/connectionStore.ts index a7dc278a3a..292607c13d 100644 --- a/apps/desktop/src/stores/connectionStore.ts +++ b/apps/desktop/src/stores/connectionStore.ts @@ -5057,18 +5057,22 @@ export const useConnectionStore = defineStore("connection", () => { * sidebar" guidance instead of triggering an interactive prompt from a * background re-init. */ - async function repushPluginConnection(connectionId: string): Promise { + async function repushPluginConnection(connectionId: string, options: { ignoreRecentHealthCheck?: boolean } = {}): Promise { const config = getConfig(connectionId); - if (!config || config.db_type !== "plugin" || !connectedIds.value.has(connectionId)) return; + if (!config || config.db_type !== "plugin" || !connectedIds.value.has(connectionId)) return false; // 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 (!(await canReconnectPluginConnectionWithoutPrompt(config))) return; + // every realistic reload path still re-pushes. The restore path overrides + // the skip: the SPA's boot restore can mark a connection healthy without + // ever delivering credentials to the sidecar, so trusting the TTL there + // strands the plugin with an empty registry (dbx-plugin-ssh#144). + if (!options.ignoreRecentHealthCheck && hasRecentConnectionHealthCheck(connectionId)) return false; + if (!(await canReconnectPluginConnectionWithoutPrompt(config))) return false; await ensureConnected(connectionId, { activate: false, forceReconnect: true, allowPasswordPrompt: false }); + return true; } /**