From bf15aa34d1f04018e6b551baec4e0d381e2cc02f Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Sun, 8 Nov 2026 15:06:25 -0800 Subject: [PATCH] fix(browser): open website links in new tabs --- .../navigation/browser-link-destination.ts | 25 +++---- .../navigation/browser-link-open.test.ts | 73 +++---------------- .../browser-link-profile-isolation.test.ts | 15 +++- .../http-link-routing-store.test.ts | 18 +++-- .../web-runtime-browser-link-session.test.ts | 44 +++++++---- 5 files changed, 74 insertions(+), 101 deletions(-) diff --git a/src/renderer/src/lib/browser/navigation/browser-link-destination.ts b/src/renderer/src/lib/browser/navigation/browser-link-destination.ts index b8e8fe4068c..3dadc8a3fdd 100644 --- a/src/renderer/src/lib/browser/navigation/browser-link-destination.ts +++ b/src/renderer/src/lib/browser/navigation/browser-link-destination.ts @@ -130,11 +130,8 @@ export function browserOpenResultForPage( workspaceId: string, pageId: string ): BrowserOpenResult | null { - const matchesHost = createBrowserLinkHostMatcher( - state, - destination.worktreeId, - destination.executionHostId - ) + const { worktreeId, executionHostId } = destination + const matchesHost = createBrowserLinkHostMatcher(state, worktreeId, executionHostId) const wrapper = (state.unifiedTabsByWorktree[destination.worktreeId] ?? []).find( (tab) => tab.contentType === 'browser' && @@ -209,6 +206,12 @@ export function selectBrowserLinkDestination( ) { return { status: 'refused' } } + const reusePage = + destination.resource.kind !== 'website' || + (destination.browserRuntimeEnvironmentId !== null && !!intent?.targetGroupId) + if (intent?.targetGroupId && !reusePage) { + return { status: 'ready', groupId: intent.targetGroupId, match: null } + } const workspaces = new Map( (state.browserTabsByWorktree[worktreeId] ?? []).map((tab) => [tab.id, tab]) ) @@ -226,12 +229,7 @@ export function selectBrowserLinkDestination( const matchingGroups = new Set() const liveGroups = liveLinkOpenGroupIds(state, worktreeId) for (const tab of wrappersByWorkspace.values()) { - if ( - !tab || - tab.contentType !== 'browser' || - !matchesHost(tab.executionHostId) || - !liveGroups.has(tab.groupId) - ) { + if (!tab || !matchesHost(tab.executionHostId) || !liveGroups.has(tab.groupId)) { continue } const workspace = workspaces.get(tab.entityId) @@ -300,10 +298,9 @@ export function selectBrowserLinkDestination( return { status: 'ready', ...placement, match: null } } const group = groups.find((item) => item.id === groupId) - const ids = group ? [group.activeTabId, ...group.tabOrder] : [] - for (const id of ids) { + for (const id of group ? [group.activeTabId, ...group.tabOrder] : []) { const match = id ? matchesByTab.get(id) : undefined - if (match && match.groupId === groupId) { + if (reusePage && match && match.groupId === groupId) { return { status: 'ready', groupId, match } } } diff --git a/src/renderer/src/lib/browser/navigation/browser-link-open.test.ts b/src/renderer/src/lib/browser/navigation/browser-link-open.test.ts index c9e36260a6c..c3fcc1583a4 100644 --- a/src/renderer/src/lib/browser/navigation/browser-link-open.test.ts +++ b/src/renderer/src/lib/browser/navigation/browser-link-open.test.ts @@ -1,5 +1,4 @@ import type * as BrowserFocusModule from '@/components/browser-pane/host-guest/browser-focus' -import { requestBrowserFocus } from '@/components/browser-pane/host-guest/browser-focus' import { beforeEach, describe, expect, it, vi } from 'vitest' import { createStoreCascadesMockApi } from '@/store/testing/app-store-cascade-api.test-fixture' import '@/store/testing/app-store-test-fixture' @@ -66,71 +65,17 @@ function browser(groupId: string, url = URL_TO_OPEN) { } describe('browser link placement and exact page activation', () => { - it('prefers an old inactive nonadjacent match over a nearby browser without navigation or creation', async () => { - store.getState().createBrowserTab(WORKSPACE, 'https://nearby.example/', { - targetGroupId: 'b', - executionHostId: 'local', - browserRuntimeEnvironmentId: null, - activate: true - }) - const existing = browser('c') - store.getState().createBrowserPage(existing.id, 'https://other.example/', { - activate: true, - browserRuntimeEnvironmentId: null - }) - const navigate = vi.spyOn(store.getState(), 'setBrowserPageUrl') - const create = vi.spyOn(store.getState(), 'createBrowserTab') - const result = await open() - expect(result).toMatchObject({ - status: 'opened', - result: { - browserWorkspaceId: existing.id, - browserPageId: existing.activePageId, - groupId: 'c' - } - }) - expect( - store.getState().browserTabsByWorktree[WORKSPACE]?.find((tab) => tab.id === existing.id) - ?.activePageId - ).toBe(existing.activePageId) - expect(navigate).not.toHaveBeenCalled() - expect(create).not.toHaveBeenCalled() - }) - - it('reusing a page clears a pending address-bar request and focuses the page', async () => { + it('opens a new website tab when the same URL is already open', async () => { const existing = browser('b') - const pageId = existing.activePageId - if (!pageId) { - throw new Error('Expected page') + const result = await open(context(true)) + expect(result.status).toBe('opened') + if (result.status !== 'opened') { + throw new Error('Expected opened link') } - store.setState({ - pendingAddressBarFocusByPageId: { [pageId]: true }, - pendingAddressBarFocusByTabId: { [existing.id]: true, [pageId]: true } - }) - expect(await open()).toMatchObject({ status: 'opened', result: { browserPageId: pageId } }) - expect(store.getState().pendingAddressBarFocusByPageId).toEqual({}) - expect(store.getState().pendingAddressBarFocusByTabId).toEqual({}) - expect(requestBrowserFocus).toHaveBeenLastCalledWith({ pageId, target: 'webview' }) - }) - - it('rechecks a reused page after workspace activation without navigating it back', async () => { - const existing = browser('b') - const pageId = existing.activePageId - if (!pageId) { - throw new Error('Expected page') - } - const outcome = await openLocalBrowserLink({ - destination: browserLinkTestDestination(), - context: context(), - url: URL_TO_OPEN, - ownsRoute: () => true, - reveal: () => store.getState().setBrowserPageUrl(pageId, 'https://navigated-away.example/') - }) - expect(outcome).toEqual({ status: 'refused' }) - expect(store.getState().browserPagesByWorkspace[existing.id]?.[0]?.url).toBe( - 'https://navigated-away.example/' - ) - expect(store.getState().activeGroupIdByWorktree[WORKSPACE]).toBe('c') + expect(result.result.browserWorkspaceId).not.toBe(existing.id) + expect(result.result.groupId).toBe('a') + expect(store.getState().browserTabsByWorktree[WORKSPACE]).toHaveLength(2) + expect(store.getState().browserPagesByWorkspace[existing.id]?.[0]?.url).toBe(URL_TO_OPEN) }) it('removes a newly created tab after an activation error while preserving existing tabs', async () => { diff --git a/src/renderer/src/lib/browser/navigation/browser-link-profile-isolation.test.ts b/src/renderer/src/lib/browser/navigation/browser-link-profile-isolation.test.ts index fe7f2f5f6c8..d15daee5e3d 100644 --- a/src/renderer/src/lib/browser/navigation/browser-link-profile-isolation.test.ts +++ b/src/renderer/src/lib/browser/navigation/browser-link-profile-isolation.test.ts @@ -120,8 +120,19 @@ describe.each(['repo', 'worktree'] as const)('%s browser profile isolation', (sc (workspace) => workspace.id === opened.result.browserWorkspaceId ) ).toMatchObject({ sessionProfileId: profile.id, sessionPartition: profile.partition }) - expect(await open(surface)).toEqual(opened) - expect(store.getState().browserTabsByWorktree[WORKSPACE]).toHaveLength(2) + const reopened = await open(surface) + if (surface === 'html') { + expect(reopened).toEqual(opened) + } else { + expect(reopened.status).toBe('opened') + if (reopened.status !== 'opened') { + throw new Error('Expected opened link') + } + expect(reopened.result.browserWorkspaceId).not.toBe(opened.result.browserWorkspaceId) + } + expect(store.getState().browserTabsByWorktree[WORKSPACE]).toHaveLength( + surface === 'http' ? 3 : 2 + ) } ) }) diff --git a/src/renderer/src/lib/browser/navigation/http-link-routing-store.test.ts b/src/renderer/src/lib/browser/navigation/http-link-routing-store.test.ts index f85bba2193c..b7e481258ed 100644 --- a/src/renderer/src/lib/browser/navigation/http-link-routing-store.test.ts +++ b/src/renderer/src/lib/browser/navigation/http-link-routing-store.test.ts @@ -248,7 +248,7 @@ it.each(['workspace', 'view'] as const)( ) it.each([false, true])( - 'reveals the tab layout for a foreground source-aware HTTP link (reuse: %s)', + 'reveals the tab layout for a foreground source-aware HTTP link (already open: %s)', async (reuse) => { if (reuse) { useAppStore.getState().createBrowserTab(WORKSPACE, URL_TO_OPEN, { @@ -275,7 +275,7 @@ it.each([false, true])( } expect(useAppStore.getState().activeView).toBe('terminal') expect(useAppStore.getState().activeGroupIdByWorktree[WORKSPACE]).toBe(outcome.result.groupId) - expect(useAppStore.getState().browserTabsByWorktree[WORKSPACE]).toHaveLength(1) + expect(useAppStore.getState().browserTabsByWorktree[WORKSPACE]).toHaveLength(reuse ? 2 : 1) } ) @@ -341,7 +341,7 @@ it('keeps the main view and workspace selected when revealing a floating HTTP li expect(useAppStore.getState().groupsByWorktree[floating]).toHaveLength(3) }) -it('reuses an exact page for an HTTP link without source context', async () => { +it('opens a new page for an HTTP link without source context', async () => { const browser = useAppStore.getState().createBrowserTab(WORKSPACE, URL_TO_OPEN, { targetGroupId: 'b', browserRuntimeEnvironmentId: null, @@ -350,9 +350,11 @@ it('reuses an exact page for an HTTP link without source context', async () => { }) const completed = observeBrowserLinkOpen() openHttpLink(URL_TO_OPEN, { worktreeId: WORKSPACE, sourceOwner: { kind: 'local' } }) - expect(await completed).toMatchObject({ - status: 'opened', - result: { browserWorkspaceId: browser.id, browserPageId: browser.activePageId } - }) - expect(useAppStore.getState().browserTabsByWorktree[WORKSPACE]).toHaveLength(1) + const outcome = await completed + expect(outcome.status).toBe('opened') + if (outcome.status !== 'opened') { + throw new Error('Expected opened link') + } + expect(outcome.result.browserWorkspaceId).not.toBe(browser.id) + expect(useAppStore.getState().browserTabsByWorktree[WORKSPACE]).toHaveLength(2) }) diff --git a/src/renderer/src/runtime/browser/creation/web-runtime-browser-link-session.test.ts b/src/renderer/src/runtime/browser/creation/web-runtime-browser-link-session.test.ts index c8037a92e82..3e193b641ff 100644 --- a/src/renderer/src/runtime/browser/creation/web-runtime-browser-link-session.test.ts +++ b/src/renderer/src/runtime/browser/creation/web-runtime-browser-link-session.test.ts @@ -93,7 +93,7 @@ function setDefaultProfiles( useAppStore.setState({ settings: { ...settings, browserDefaultProfileIdByHostId } }) } -function open() { +function open(explicit = false) { return openWorkspaceBrowserTabForLink({ workspaceId: WT, url: URL_TO_OPEN, @@ -102,7 +102,9 @@ function open() { placementPreference: 'server', selectWorktree: false, linkContext: { - linkIntent: captureLinkOpenIntent(useAppStore.getState(), WT, SOURCE.unifiedTabId) + linkIntent: captureLinkOpenIntent(useAppStore.getState(), WT, OTHER.unifiedTabId, { + explicitSourceGroup: explicit + }) } }) } @@ -141,22 +143,38 @@ function selectDestination() { ENV, HOST ), - captureLinkOpenIntent(state, WT, SOURCE.unifiedTabId) + captureLinkOpenIntent(state, WT, OTHER.unifiedTabId, { explicitSourceGroup: true }) ) } -it.each([null, 'remote-profile'])( - 'reuses the host-confirmed default or custom profile (%s)', - async (profileId) => { +it.each([ + { profileId: null, explicit: false }, + { profileId: 'remote-profile', explicit: false }, + { profileId: null, explicit: true }, + { profileId: 'remote-profile', explicit: true } +])( + 'opens with the host-confirmed profile ($profileId, explicit target: $explicit)', + async ({ profileId, explicit }) => { setDefaultProfiles(profileId ? { [HOST]: profileId } : {}) publishProfileSnapshot(profileId ?? 'default') - const creating = open() - pendingCreates[0]?.resolve('unexpected-new-page') - expect(await creating).toMatchObject({ - status: 'opened', - result: { browserPageId: OTHER.pageId } - }) - expect(pendingCreates).toHaveLength(0) + const creating = open(explicit) + pendingCreates[0]?.resolve('new-host-page') + const outcome = await creating + if (explicit) { + expect(outcome).toMatchObject({ + status: 'opened', + result: { browserPageId: OTHER.pageId } + }) + return + } + expect(outcome.status).toBe('opened') + if (outcome.status !== 'opened') { + throw new Error('Expected opened link') + } + expect(outcome.result.browserPageId).not.toBe(OTHER.pageId) + expect( + useAppStore.getState().remoteBrowserPageHandlesByPageId[outcome.result.browserPageId] + ).toMatchObject({ remotePageId: 'new-host-page', environmentId: ENV }) } )