Fix parked browser wakeups and daemon PTY inspection (#4420)
This commit is contained in:
@@ -1,16 +1,28 @@
|
||||
/* eslint-disable max-lines -- Why: browser runtime command tests share one
|
||||
mocked BrowserManager/agent-browser bridge so page wake and registration
|
||||
ordering regressions stay in one boundary-focused suite. */
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { AgentBrowserBridge } from '../browser/agent-browser-bridge'
|
||||
import type { RuntimeBrowserCommandHost } from './orca-runtime-browser'
|
||||
|
||||
const { webContentsFromIdMock, startBrowserScreencastMock, waitForWorktreeTabRegistrationMock } =
|
||||
vi.hoisted(() => ({
|
||||
webContentsFromIdMock: vi.fn(),
|
||||
startBrowserScreencastMock: vi.fn(),
|
||||
waitForWorktreeTabRegistrationMock: vi.fn()
|
||||
}))
|
||||
const {
|
||||
ipcMainOnMock,
|
||||
ipcMainRemoveListenerMock,
|
||||
webContentsFromIdMock,
|
||||
startBrowserScreencastMock,
|
||||
waitForTabRegistrationMock,
|
||||
waitForWorktreeTabRegistrationMock
|
||||
} = vi.hoisted(() => ({
|
||||
ipcMainOnMock: vi.fn(),
|
||||
ipcMainRemoveListenerMock: vi.fn(),
|
||||
webContentsFromIdMock: vi.fn(),
|
||||
startBrowserScreencastMock: vi.fn(),
|
||||
waitForTabRegistrationMock: vi.fn(),
|
||||
waitForWorktreeTabRegistrationMock: vi.fn()
|
||||
}))
|
||||
|
||||
vi.mock('electron', () => ({
|
||||
ipcMain: { on: vi.fn(), removeListener: vi.fn() },
|
||||
ipcMain: { on: ipcMainOnMock, removeListener: ipcMainRemoveListenerMock },
|
||||
webContents: { fromId: webContentsFromIdMock }
|
||||
}))
|
||||
|
||||
@@ -19,7 +31,7 @@ vi.mock('../browser/browser-screencast-stream', () => ({
|
||||
}))
|
||||
|
||||
vi.mock('../ipc/browser', () => ({
|
||||
waitForTabRegistration: vi.fn(),
|
||||
waitForTabRegistration: waitForTabRegistrationMock,
|
||||
waitForWorktreeTabRegistration: waitForWorktreeTabRegistrationMock
|
||||
}))
|
||||
|
||||
@@ -62,8 +74,12 @@ function createHost(overrides: Partial<RuntimeBrowserCommandHost> = {}): Runtime
|
||||
|
||||
describe('RuntimeBrowserCommands browser screencast', () => {
|
||||
beforeEach(() => {
|
||||
ipcMainOnMock.mockReset()
|
||||
ipcMainRemoveListenerMock.mockReset()
|
||||
webContentsFromIdMock.mockReset()
|
||||
startBrowserScreencastMock.mockReset()
|
||||
waitForTabRegistrationMock.mockReset()
|
||||
waitForTabRegistrationMock.mockResolvedValue(undefined)
|
||||
waitForWorktreeTabRegistrationMock.mockReset()
|
||||
waitForWorktreeTabRegistrationMock.mockResolvedValue(undefined)
|
||||
})
|
||||
@@ -89,6 +105,28 @@ describe('RuntimeBrowserCommands browser screencast', () => {
|
||||
expect(bridge.tabList).toHaveBeenCalledWith('wt-1')
|
||||
})
|
||||
|
||||
it('re-wakes an explicit worktree when the only registered browser tab is stale', async () => {
|
||||
const { RuntimeBrowserCommands } = await import('./orca-runtime-browser')
|
||||
webContentsFromIdMock.mockReturnValue({ isDestroyed: () => true })
|
||||
const send = vi.fn()
|
||||
const bridge = {
|
||||
getRegisteredTabs: vi.fn(() => new Map([['page-stale', 404]])),
|
||||
tabList: vi.fn(() => ({ tabs: [] }))
|
||||
} as unknown as AgentBrowserBridge
|
||||
const commands = new RuntimeBrowserCommands(
|
||||
createHost({
|
||||
getAgentBrowserBridge: () => bridge,
|
||||
getAuthoritativeWindow: vi.fn(() => ({ webContents: { send } }) as never)
|
||||
})
|
||||
)
|
||||
|
||||
await commands.browserTabList({ worktree: 'id:wt-1' })
|
||||
|
||||
expect(send).toHaveBeenCalledWith('browser:activateView', { worktreeId: 'wt-1' })
|
||||
expect(waitForWorktreeTabRegistrationMock).toHaveBeenCalledWith('wt-1')
|
||||
expect(bridge.tabList).toHaveBeenCalledWith('wt-1')
|
||||
})
|
||||
|
||||
it('waits for any browser registration after requesting a hidden mount without worktree scope', async () => {
|
||||
const { RuntimeBrowserCommands } = await import('./orca-runtime-browser')
|
||||
const send = vi.fn()
|
||||
@@ -110,6 +148,204 @@ describe('RuntimeBrowserCommands browser screencast', () => {
|
||||
expect(bridge.tabList).toHaveBeenCalledWith(undefined)
|
||||
})
|
||||
|
||||
it('creates the first explicit-worktree browser tab without waiting for an existing registration', async () => {
|
||||
const { RuntimeBrowserCommands } = await import('./orca-runtime-browser')
|
||||
const send = vi.fn((channel: string, data: { requestId: string }) => {
|
||||
expect(channel).toBe('browser:requestTabCreate')
|
||||
const handler = ipcMainOnMock.mock.calls.find(
|
||||
([eventName]) => eventName === 'browser:tabCreateReply'
|
||||
)?.[1] as
|
||||
| ((
|
||||
event: unknown,
|
||||
reply: { requestId: string; browserPageId?: string; error?: string }
|
||||
) => void)
|
||||
| undefined
|
||||
handler?.({} as never, { requestId: data.requestId, browserPageId: 'page-new' })
|
||||
})
|
||||
const bridge = {
|
||||
getRegisteredTabs: vi.fn(() => new Map([['page-new', 101]])),
|
||||
getActivePageId: vi.fn(() => 'page-new'),
|
||||
setActiveTab: vi.fn(),
|
||||
tabList: vi.fn(() => ({ tabs: [] }))
|
||||
} as unknown as AgentBrowserBridge
|
||||
const commands = new RuntimeBrowserCommands(
|
||||
createHost({
|
||||
getAgentBrowserBridge: () => bridge,
|
||||
getAvailableAuthoritativeWindow: vi.fn(() => ({}) as never),
|
||||
getAuthoritativeWindow: vi.fn(() => ({ webContents: { send } }) as never)
|
||||
})
|
||||
)
|
||||
|
||||
await expect(
|
||||
commands.browserTabCreate({ worktree: 'id:wt-1', url: 'about:blank' })
|
||||
).resolves.toEqual({ browserPageId: 'page-new' })
|
||||
|
||||
expect(waitForWorktreeTabRegistrationMock).not.toHaveBeenCalled()
|
||||
expect(send).toHaveBeenCalledWith(
|
||||
'browser:requestTabCreate',
|
||||
expect.objectContaining({ url: 'about:blank', worktreeId: 'wt-1' })
|
||||
)
|
||||
expect(waitForTabRegistrationMock).toHaveBeenCalledWith('page-new')
|
||||
expect(bridge.setActiveTab).toHaveBeenCalledWith(101, 'wt-1')
|
||||
})
|
||||
|
||||
it('wakes the requested page instead of the first worktree tab for page-scoped commands', async () => {
|
||||
const { RuntimeBrowserCommands } = await import('./orca-runtime-browser')
|
||||
webContentsFromIdMock.mockReturnValue({ isDestroyed: () => true })
|
||||
const send = vi.fn()
|
||||
const snapshot = vi.fn(() => ({
|
||||
origin: 'about:blank',
|
||||
refs: {},
|
||||
snapshot: '(empty page)',
|
||||
browserPageId: 'page-target'
|
||||
}))
|
||||
const bridge = {
|
||||
getRegisteredTabs: vi.fn(() => new Map([['page-target', 101]])),
|
||||
getActivePageId: vi.fn(() => 'page-other'),
|
||||
snapshot
|
||||
} as unknown as AgentBrowserBridge
|
||||
const commands = new RuntimeBrowserCommands(
|
||||
createHost({
|
||||
getAgentBrowserBridge: () => bridge,
|
||||
getAuthoritativeWindow: vi.fn(() => ({ webContents: { send } }) as never)
|
||||
})
|
||||
)
|
||||
|
||||
await expect(
|
||||
commands.browserSnapshot({ worktree: 'id:wt-1', page: 'page-target' })
|
||||
).resolves.toEqual({
|
||||
origin: 'about:blank',
|
||||
refs: {},
|
||||
snapshot: '(empty page)',
|
||||
browserPageId: 'page-target'
|
||||
})
|
||||
|
||||
expect(send).toHaveBeenCalledWith('browser:activateView', {
|
||||
worktreeId: 'wt-1',
|
||||
browserPageId: 'page-target'
|
||||
})
|
||||
expect(waitForTabRegistrationMock).toHaveBeenCalledWith('page-target')
|
||||
expect(waitForWorktreeTabRegistrationMock).not.toHaveBeenCalled()
|
||||
expect(snapshot).toHaveBeenCalledWith('wt-1', 'page-target')
|
||||
})
|
||||
|
||||
it('wakes the requested page before showing page-scoped tab metadata', async () => {
|
||||
const { RuntimeBrowserCommands } = await import('./orca-runtime-browser')
|
||||
webContentsFromIdMock.mockReturnValue({ isDestroyed: () => true })
|
||||
const send = vi.fn()
|
||||
const bridge = {
|
||||
getRegisteredTabs: vi.fn(() => new Map([['page-target', 101]])),
|
||||
getActivePageId: vi.fn(() => 'page-other'),
|
||||
tabList: vi.fn(() => ({
|
||||
tabs: [
|
||||
{
|
||||
browserPageId: 'page-target',
|
||||
index: 1,
|
||||
url: 'about:blank',
|
||||
title: 'Target',
|
||||
active: false
|
||||
}
|
||||
]
|
||||
}))
|
||||
} as unknown as AgentBrowserBridge
|
||||
const commands = new RuntimeBrowserCommands(
|
||||
createHost({
|
||||
getAgentBrowserBridge: () => bridge,
|
||||
getAuthoritativeWindow: vi.fn(() => ({ webContents: { send } }) as never)
|
||||
})
|
||||
)
|
||||
|
||||
const result = await commands.browserTabShow({ worktree: 'id:wt-1', page: 'page-target' })
|
||||
|
||||
expect(send).toHaveBeenCalledWith('browser:activateView', {
|
||||
worktreeId: 'wt-1',
|
||||
browserPageId: 'page-target'
|
||||
})
|
||||
expect(waitForTabRegistrationMock).toHaveBeenCalledWith('page-target')
|
||||
expect(waitForWorktreeTabRegistrationMock).not.toHaveBeenCalled()
|
||||
expect(result.tab.browserPageId).toBe('page-target')
|
||||
})
|
||||
|
||||
it('wakes the requested page before showing page-scoped profile metadata', async () => {
|
||||
const { RuntimeBrowserCommands } = await import('./orca-runtime-browser')
|
||||
webContentsFromIdMock.mockReturnValue({ isDestroyed: () => true })
|
||||
const send = vi.fn()
|
||||
const bridge = {
|
||||
getRegisteredTabs: vi.fn(() => new Map([['page-target', 101]])),
|
||||
getActivePageId: vi.fn(() => 'page-other'),
|
||||
tabList: vi.fn(() => ({
|
||||
tabs: [
|
||||
{
|
||||
browserPageId: 'page-target',
|
||||
index: 1,
|
||||
url: 'about:blank',
|
||||
title: 'Target',
|
||||
active: false
|
||||
}
|
||||
]
|
||||
}))
|
||||
} as unknown as AgentBrowserBridge
|
||||
const commands = new RuntimeBrowserCommands(
|
||||
createHost({
|
||||
getAgentBrowserBridge: () => bridge,
|
||||
getAuthoritativeWindow: vi.fn(() => ({ webContents: { send } }) as never)
|
||||
})
|
||||
)
|
||||
|
||||
const result = await commands.browserTabProfileShow({
|
||||
worktree: 'id:wt-1',
|
||||
page: 'page-target'
|
||||
})
|
||||
|
||||
expect(send).toHaveBeenCalledWith('browser:activateView', {
|
||||
worktreeId: 'wt-1',
|
||||
browserPageId: 'page-target'
|
||||
})
|
||||
expect(waitForTabRegistrationMock).toHaveBeenCalledWith('page-target')
|
||||
expect(waitForWorktreeTabRegistrationMock).not.toHaveBeenCalled()
|
||||
expect(result.browserPageId).toBe('page-target')
|
||||
})
|
||||
|
||||
it('wakes the requested page before closing a page-scoped tab', async () => {
|
||||
const { RuntimeBrowserCommands } = await import('./orca-runtime-browser')
|
||||
webContentsFromIdMock.mockReturnValue({ isDestroyed: () => true })
|
||||
const send = vi.fn((channel: string, data: { requestId?: string }) => {
|
||||
if (channel !== 'browser:requestTabClose') {
|
||||
return
|
||||
}
|
||||
const handler = ipcMainOnMock.mock.calls.find(
|
||||
([eventName]) => eventName === 'browser:tabCloseReply'
|
||||
)?.[1] as ((event: unknown, reply: { requestId: string; error?: string }) => void) | undefined
|
||||
handler?.({} as never, { requestId: data.requestId ?? '' })
|
||||
})
|
||||
const bridge = {
|
||||
getRegisteredTabs: vi.fn(() => new Map([['page-target', 101]])),
|
||||
getActivePageId: vi.fn(() => 'page-other'),
|
||||
getActiveWebContentsId: vi.fn(() => 101)
|
||||
} as unknown as AgentBrowserBridge
|
||||
const commands = new RuntimeBrowserCommands(
|
||||
createHost({
|
||||
getAgentBrowserBridge: () => bridge,
|
||||
getAuthoritativeWindow: vi.fn(() => ({ webContents: { send } }) as never)
|
||||
})
|
||||
)
|
||||
|
||||
await expect(
|
||||
commands.browserTabClose({ worktree: 'id:wt-1', page: 'page-target' })
|
||||
).resolves.toEqual({ closed: true })
|
||||
|
||||
expect(send).toHaveBeenCalledWith('browser:activateView', {
|
||||
worktreeId: 'wt-1',
|
||||
browserPageId: 'page-target'
|
||||
})
|
||||
expect(waitForTabRegistrationMock).toHaveBeenCalledWith('page-target')
|
||||
expect(waitForWorktreeTabRegistrationMock).not.toHaveBeenCalled()
|
||||
expect(send).toHaveBeenCalledWith(
|
||||
'browser:requestTabClose',
|
||||
expect.objectContaining({ tabId: 'page-target', worktreeId: 'wt-1' })
|
||||
)
|
||||
})
|
||||
|
||||
it('lets a new same-page stream take over an active stale stream', async () => {
|
||||
const { RuntimeBrowserCommands } = await import('./orca-runtime-browser')
|
||||
webContentsFromIdMock.mockReturnValue({ isDestroyed: () => false })
|
||||
|
||||
Reference in New Issue
Block a user