Files
orca/tests/e2e/session-tabs/mobile-session-legacy-navigation.unit.test.ts
Kelvin Amoaba 805ec4ee21 fix(browser): show site favicons on browser tabs running on a remote host (#40049)
* fix(browser): show site favicons on remote host browser tabs

Host-run browser tabs mirrored to a paired client always showed the
globe: the host's session-tab snapshot carried no favicon. Hosts now
publish the active page's icon URL as an optional faviconUrl, capped at
2 KB and dropped when it points at the host's loopback. Headless hosts
pick it up from page-favicon-updated and publish only when it changes;
desktop hosts include it in the existing graph-sync key. Clients keep
their current icon when an older host omits the field.

* fix(browser): publish a remote tab icon on arrival, not on every swap

Read the headless favicon through the agent browser bridge so the
runtime stays free of Electron imports, publish only when a page gains
an icon so animated icons ride later publishes, and drop IPv4-mapped
loopback icon hosts.

* fix(browser): clear a remote tab icon when a cross-origin load starts

A load that fails on the new origin never commits, so clearing only on
did-navigate kept the old site's icon beside the failed URL. Also give
the runtime tests' bridge fakes the new favicon getter.

* test: give the session-tab bridge fakes the favicon getter
2026-11-09 00:59:11 +00:00

206 lines
8.0 KiB
TypeScript

import '../../../src/main/runtime/orca-runtime-tests/runtime-test-module-registration.spec'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { setRuntimeBrowserCommandsFactory } from '../../../src/main/runtime/browser/installation/runtime-browser-commands-factory'
import { RuntimeBrowserCommands } from '../../../src/main/runtime/browser/commands/browser-commands'
import { withOpenedServeBrowserTab } from '../../../src/main/runtime/browser/headless/serve-browser-tab-layout'
import type { OrcaRuntimeService } from '../../../src/main/runtime/orca-runtime'
import { TEST_WORKTREE_ID as WT } from '../../../src/main/runtime/orca-runtime-tests/orca-runtime-test-fixtures.spec'
import { createRuntimeFixture } from './active-tab-publication.test-fixture'
import type {
RuntimeMobileSessionTabsResult,
RuntimeMobileSessionTabsSnapshot
} from '../../../src/shared/runtime-session-contracts'
import { getDefaultWorkspaceSession } from '../../../src/shared/constants'
const A = 'agent-session:chat-a'
const B = 'agent-session:chat-b'
const list = (runtime: OrcaRuntimeService) => runtime.listMobileSessionTabs(`id:${WT}`)
const createRuntime = () => createRuntimeFixture().runtime
async function publish(runtime: OrcaRuntimeService, id: string, activate = true) {
await runtime.publishStructuredAgentSessionTab({
workspaceId: WT,
sessionId: id.slice('agent-session:'.length),
agent: id === A ? 'claude' : 'codex',
activate
})
}
function accept(runtime: OrcaRuntimeService, frame: RuntimeMobileSessionTabsSnapshot) {
runtime.syncWindowGraph(1, { tabs: [], leaves: [], mobileSessionTabs: [frame] })
}
function expectSelection(
snapshot: RuntimeMobileSessionTabsResult | RuntimeMobileSessionTabsSnapshot | undefined,
id: string | null
) {
expect(snapshot?.activeTabId).toBe(id)
expect(snapshot?.activeTabType).toBe(id ? 'agent-session' : null)
expect(snapshot?.tabs.filter((tab) => tab.isActive).map((tab) => tab.id)).toEqual(id ? [id] : [])
}
afterEach(() => {
setRuntimeBrowserCommandsFactory(null)
vi.useRealTimers()
})
describe('legacy host navigation through inventory reconstruction', () => {
it('activates an existing chat from an admitted legacy snapshot without groups', async () => {
const runtime = createRuntime()
await publish(runtime, A)
await publish(runtime, B, false)
await list(runtime)
const before = runtime.mobileSessionTabsByWorktree.get(WT)!
accept(runtime, {
...before,
publicationEpoch: 'renderer:legacy-explicit',
snapshotVersion: 20,
activeGroupId: null,
tabGroups: undefined
})
expect(runtime.mobileSessionTabsByWorktree.get(WT)?.tabGroups).toBeUndefined()
const seen: RuntimeMobileSessionTabsResult[] = []
runtime.onMobileSessionTabsChanged((snapshot) => seen.push(snapshot))
await publish(runtime, B)
expectSelection(runtime.mobileSessionTabsByWorktree.get(WT), B)
const result = await list(runtime)
expectSelection(result, B)
expectSelection(seen.at(-1), B)
})
it.each([
{ selected: B, saved: A },
{ selected: B, saved: null },
{ selected: null, saved: A }
])(
'retains legacy $selected over saved $saved while reconstructing a split',
async ({ selected, saved }) => {
vi.useFakeTimers()
const { runtime, getSession, setSession } = createRuntimeFixture()
await publish(runtime, A)
await publish(runtime, B, false)
await list(runtime)
const before = runtime.mobileSessionTabsByWorktree.get(WT)!
const layout = {
type: 'split' as const,
direction: 'vertical' as const,
first: { type: 'leaf' as const, groupId: 'left' },
second: { type: 'leaf' as const, groupId: 'right' }
}
setSession({
...getSession(),
tabGroups: {
[WT]: [
{
id: 'left',
worktreeId: WT,
activeTabId: saved,
tabOrder: ['serve-tab', A],
recentTabIds: []
},
{ id: 'right', worktreeId: WT, activeTabId: B, tabOrder: [B], recentTabIds: [] }
]
},
activeGroupIdByWorktree: { [WT]: 'left' },
tabGroupLayouts: { [WT]: layout }
})
accept(runtime, {
...before,
publicationEpoch: 'renderer:legacy-split',
snapshotVersion: 30,
activeGroupId: null,
activeTabId: selected,
activeTabType: selected ? 'agent-session' : null,
tabGroups: undefined,
tabGroupLayout: undefined,
tabs: before.tabs.map((tab) => ({ ...tab, isActive: tab.id === selected }))
})
const result = await list(runtime)
expectSelection(result, selected)
expect(result.activeGroupId).toBe(selected ? 'right' : 'left')
const stored = runtime.mobileSessionTabsByWorktree.get(WT)!
expect(stored.tabGroupLayout).toEqual(layout)
expect(stored.tabGroups?.map((group) => [group.id, group.tabOrder])).toEqual([
['left', ['serve-tab', A]],
['right', [B]]
])
await vi.runAllTimersAsync()
const seen: RuntimeMobileSessionTabsResult[] = []
runtime.onMobileSessionTabsChanged((snapshot) => seen.push(snapshot))
expectSelection(await list(runtime), selected)
await vi.runAllTimersAsync()
expect(runtime.mobileSessionTabsByWorktree.get(WT)).toBe(stored)
expect(seen).toEqual([])
}
)
it('publishes an explicit browser switch from a legacy graph to list and live readers', async () => {
vi.useFakeTimers()
setRuntimeBrowserCommandsFactory((host) => new RuntimeBrowserCommands(host))
const { runtime, setSession } = createRuntimeFixture()
let saved = getDefaultWorkspaceSession()
for (const pageId of ['page-a', 'page-b']) {
saved = withOpenedServeBrowserTab('local', saved, {
worktreeId: WT,
pageId,
url: 'https://example.com/',
title: pageId
})!
}
setSession(saved)
runtime.setOffscreenBrowserBackend({
createTab: vi.fn(async () => ({ browserPageId: 'unused' })),
closeTab: vi.fn(async () => {}),
ownsTab: () => true
})
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: External browser operations; runtime selection and publication remain real.
runtime.setAgentBrowserBridge({
tabList: () => ({
tabs: ['page-a', 'page-b'].map((browserPageId, index) => ({
browserPageId,
index,
url: 'https://example.com/',
title: browserPageId,
active: index === 0
}))
}),
getRemoteTabFaviconUrl: () => null,
tabSwitch: vi.fn(async () => ({ switched: 1, browserPageId: 'page-b' })),
getRegisteredTabs: () =>
new Map([
['page-a', 1],
['page-b', 2]
])
} as never)
const tabs: RuntimeMobileSessionTabsSnapshot['tabs'] = ['page-a', 'page-b'].map((id) => ({
type: 'browser',
id,
browserWorkspaceId: id,
browserPageId: id,
title: id,
url: 'https://example.com/',
loading: false,
canGoBack: false,
canGoForward: false,
isActive: id === 'page-a'
}))
accept(runtime, {
worktree: WT,
publicationEpoch: 'renderer:legacy-browser',
snapshotVersion: 10,
activeGroupId: null,
activeTabId: 'page-a',
activeTabType: 'browser',
tabs
})
expect(runtime.mobileSessionTabsByWorktree.get(WT)?.tabGroups).toBeUndefined()
const seen: RuntimeMobileSessionTabsResult[] = []
runtime.onMobileSessionTabsChanged((snapshot) => seen.push(snapshot))
await runtime.browserTabSwitch({ worktree: `id:${WT}`, page: 'page-b', focus: true })
await vi.runAllTimersAsync()
const result = await list(runtime)
expect(runtime.mobileSessionTabsByWorktree.get(WT)?.activeTabId).toBe('page-b')
expect(result.activeTabId).toBe('page-b')
expect(result.activeTabType).toBe('browser')
expect(result.tabs.filter((tab) => tab.isActive).map((tab) => tab.id)).toEqual(['page-b'])
expect(seen.at(-1)?.activeTabId).toBe('page-b')
})
})