diff --git a/packages/hub-ui/src/client/state/context.test.ts b/packages/hub-ui/src/client/state/context.test.ts index 1463ef70..6e5eb95b 100644 --- a/packages/hub-ui/src/client/state/context.test.ts +++ b/packages/hub-ui/src/client/state/context.test.ts @@ -5,7 +5,7 @@ import { HUB_EVENTS } from '@devframes/hub/constants' import { DEVFRAME_EVENTS } from 'devframe/constants' import { createEventEmitter } from 'devframe/utils/events' import { createSharedState } from 'devframe/utils/shared-state' -import { describe, expect, it, vi } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { nextTick, ref, watch } from 'vue' import { createDocksContext } from './context' import { executeSetupScript } from './setup-script' @@ -575,3 +575,90 @@ describe('dock group command activation', () => { ]) }) }) + +describe('dock route persistence', () => { + const iframeEntries = [ + { + id: 'vite', + type: 'iframe', + title: 'Vite', + icon: 'ph:lightning', + url: '/__devtools-vite/', + }, + { + id: 'vite-task', + type: 'iframe', + title: 'Vite Task', + icon: 'ph:list', + url: '/__devtools-vite-task/', + }, + ] satisfies DevframeDockEntry[] + + beforeEach(() => { + vi.stubGlobal('location', new URL('http://localhost:5197/')) + }) + + afterEach(() => { + vi.unstubAllGlobals() + }) + + it('clears the old route before publishing a different dock selection', async () => { + expect.assertions(2) + + const { rpc, sharedStates, trust } = createStubRpc() + const session = ref({ + open: true, + selectedDockId: 'vite', + selectedDockRoute: 'http://localhost:5197/__devtools-vite/home', + }) + const context = await createDocksContext('embedded', rpc, undefined, session) + trust() + sharedStates.get(HUB_EVENTS.sharedState.docks)!.push(iframeEntries) + sharedStates.get(HUB_EVENTS.sharedState.dockRenderers)!.push({}) + await flushRestore() + + const snapshots: DockSessionStorage[] = [] + const stop = watch(() => session.value.selectedDockId, () => { + snapshots.push({ ...session.value }) + }, { flush: 'sync' }) + await context.docks.switchEntry('vite-task') + stop() + + expect(snapshots).toEqual([ + expect.objectContaining({ + selectedDockId: 'vite-task', + selectedDockRoute: null, + }), + ]) + expect(session.value.selectedDockRoute).toBeNull() + }) + + it.each([ + ['http://localhost:5197/__devtools-vite/home', true], + ['/__devtools-vite/home?tab=plugins#details', true], + ['/__devtools-vite/', true], + ['/__devtools-vite', true], + ['/__devtools-vite-task/', false], + ['http://localhost:5198/__devtools-vite/home', false], + ['https://localhost:5197/__devtools-vite/home', false], + ['http://[invalid', false], + ])('restores %s only within the selected dock URL', async (route, valid) => { + expect.assertions(3) + + const { rpc, sharedStates, trust } = createStubRpc() + const session = ref({ + open: true, + selectedDockId: 'vite', + selectedDockRoute: route, + }) + const context = await createDocksContext('embedded', rpc, undefined, session) + trust() + sharedStates.get(HUB_EVENTS.sharedState.docks)!.push(iframeEntries) + sharedStates.get(HUB_EVENTS.sharedState.dockRenderers)!.push({}) + await flushRestore() + + expect(context.panel.consumeBootRoute?.('vite-task')).toBeNull() + expect(context.panel.consumeBootRoute?.('vite')).toBe(valid ? route : null) + expect(context.panel.consumeBootRoute?.('vite')).toBeNull() + }) +}) diff --git a/packages/hub-ui/src/client/state/context.ts b/packages/hub-ui/src/client/state/context.ts index 72a3d042..d9a31396 100644 --- a/packages/hub-ui/src/client/state/context.ts +++ b/packages/hub-ui/src/client/state/context.ts @@ -109,6 +109,9 @@ export async function createDocksContext( const selectedDockId = computed({ get: () => sessionStore.value.selectedDockId, set: (id) => { + // Drop the old dock's route before observers persist the new selection. + if (sessionStore.value.selectedDockId !== id) + sessionStore.value.selectedDockRoute = null sessionStore.value.selectedDockId = id }, }) @@ -755,7 +758,20 @@ export async function createDocksContext( if (bootRoute != null && id === restoreIntent.selectedDockId) { const route = bootRoute bootRoute = null - return route + const entry = entries.value.find(entry => entry.id === id) + if (entry?.type !== 'iframe') + return null + try { + const base = new URL(entry.url, globalThis.location?.href) + const url = new URL(route, globalThis.location?.href) + const path = base.pathname.replace(/\/$/, '') + return url.origin === base.origin && (url.pathname === path || url.pathname.startsWith(`${path}/`)) + ? route + : null + } + catch { + return null + } } return null }