-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(server): close pages by refreshed target id #981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Nikhil (shadowfax92)
wants to merge
2
commits into
dev
Choose a base branch
from
polecat/garnet/bosmain-wcw@moxnktij
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
packages/browseros-agent/apps/server/tests/browser/browser.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| import { describe, it } from 'bun:test' | ||
| import assert from 'node:assert' | ||
| import type { | ||
| CloseTabParams, | ||
| GetTabInfoParams, | ||
| TabInfo, | ||
| } from '@browseros/cdp-protocol/domains/browser' | ||
| import type { ProtocolApi } from '@browseros/cdp-protocol/protocol-api' | ||
| import type { CdpBackend } from '../../src/browser/backends/types' | ||
| import { Browser } from '../../src/browser/browser' | ||
|
|
||
| class FakeCdpBackend { | ||
| readonly closeTabCalls: CloseTabParams[] = [] | ||
| private nextSessionId = 1 | ||
| private readonly protocolSessions = new Map<string, ProtocolApi>() | ||
| tabs: TabInfo[] | ||
|
|
||
| Browser = { | ||
| getTabs: async () => ({ tabs: this.tabs }), | ||
| getTabInfo: async (params: GetTabInfoParams) => { | ||
| const tab = this.tabs.find( | ||
| (t) => t.tabId === params.tabId || t.targetId === params.targetId, | ||
| ) | ||
| if (!tab) throw new Error('Unknown page') | ||
| return { tab } | ||
| }, | ||
| closeTab: async (params: CloseTabParams) => { | ||
| this.closeTabCalls.push(params) | ||
| const index = this.tabs.findIndex( | ||
| (t) => t.tabId === params.tabId || t.targetId === params.targetId, | ||
| ) | ||
| if (index === -1) throw new Error('Unknown page') | ||
| this.tabs.splice(index, 1) | ||
| }, | ||
| } | ||
|
|
||
| Target = { | ||
| on: () => () => {}, | ||
| attachToTarget: async () => { | ||
| const sessionId = `session-${this.nextSessionId++}` | ||
| this.protocolSessions.set(sessionId, createProtocolSession()) | ||
| return { sessionId } | ||
| }, | ||
| } | ||
|
|
||
| constructor(tabs: TabInfo[]) { | ||
| this.tabs = tabs | ||
| } | ||
|
|
||
| async connect(): Promise<void> {} | ||
| async disconnect(): Promise<void> {} | ||
| isConnected(): boolean { | ||
| return true | ||
| } | ||
| async getTargets(): Promise<[]> { | ||
| return [] | ||
| } | ||
| session(sessionId: string): ProtocolApi { | ||
| const session = this.protocolSessions.get(sessionId) | ||
| if (!session) throw new Error(`Unknown session ${sessionId}`) | ||
| return session | ||
| } | ||
| onSessionEvent(): () => void { | ||
| return () => {} | ||
| } | ||
| } | ||
|
|
||
| function createProtocolSession(): ProtocolApi { | ||
| return { | ||
| Page: { enable: async () => {} }, | ||
| DOM: { enable: async () => {} }, | ||
| Runtime: { enable: async () => {} }, | ||
| Log: { enable: async () => {} }, | ||
| Accessibility: { enable: async () => {} }, | ||
| } as unknown as ProtocolApi | ||
| } | ||
|
|
||
| function createTab(overrides: Partial<TabInfo>): TabInfo { | ||
| return { | ||
| tabId: 101, | ||
| targetId: 'target-101', | ||
| url: 'https://example.com', | ||
| title: 'Example', | ||
| isActive: false, | ||
| isLoading: false, | ||
| loadProgress: 1, | ||
| isPinned: false, | ||
| isHidden: false, | ||
| windowId: 1, | ||
| index: 0, | ||
| ...overrides, | ||
| } | ||
| } | ||
|
|
||
| function createBrowser(tabs: TabInfo[]): { | ||
| browser: Browser | ||
| cdp: FakeCdpBackend | ||
| } { | ||
| const cdp = new FakeCdpBackend(tabs) | ||
| return { | ||
| browser: new Browser(cdp as unknown as CdpBackend), | ||
| cdp, | ||
| } | ||
| } | ||
|
|
||
| function getSessionCache(browser: Browser): Map<string, string> { | ||
| return (browser as unknown as { sessions: Map<string, string> }).sessions | ||
| } | ||
|
|
||
| async function attachSession( | ||
| browser: Browser, | ||
| targetId: string, | ||
| pageId: number, | ||
| ): Promise<void> { | ||
| await ( | ||
| browser as unknown as { | ||
| attachToPage(targetId: string, pageId: number): Promise<string> | ||
| } | ||
| ).attachToPage(targetId, pageId) | ||
| } | ||
|
|
||
| describe('Browser', () => { | ||
| it('closes listed pages by target ID', async () => { | ||
| const { browser, cdp } = createBrowser([ | ||
| createTab({ tabId: 401, targetId: 'target-401' }), | ||
| ]) | ||
|
|
||
| const pages = await browser.listPages() | ||
| assert.strictEqual(pages.length, 1) | ||
|
|
||
| await browser.closePage(pages[0].pageId) | ||
|
|
||
| assert.deepStrictEqual(cdp.closeTabCalls, [{ targetId: 'target-401' }]) | ||
| assert.strictEqual(cdp.tabs.length, 0) | ||
| }) | ||
|
|
||
| it('refreshes pages before treating closePage page IDs as unknown', async () => { | ||
| const { browser, cdp } = createBrowser([ | ||
| createTab({ tabId: 402, targetId: 'target-402' }), | ||
| ]) | ||
|
|
||
| await browser.closePage(1) | ||
|
|
||
| assert.deepStrictEqual(cdp.closeTabCalls, [{ targetId: 'target-402' }]) | ||
| assert.strictEqual(cdp.tabs.length, 0) | ||
| }) | ||
|
|
||
| it('retries closePage when the tab target changes before close', async () => { | ||
| const { browser, cdp } = createBrowser([ | ||
| createTab({ tabId: 403, targetId: 'target-403-old' }), | ||
| ]) | ||
|
|
||
| const pages = await browser.listPages() | ||
| cdp.tabs[0].targetId = 'target-403-new' | ||
|
|
||
| await browser.closePage(pages[0].pageId) | ||
|
|
||
| assert.deepStrictEqual(cdp.closeTabCalls, [ | ||
| { targetId: 'target-403-old' }, | ||
| { targetId: 'target-403-new' }, | ||
| ]) | ||
| assert.strictEqual(cdp.tabs.length, 0) | ||
| }) | ||
|
|
||
| it('clears stale session cache when closePage retries with a refreshed target ID', async () => { | ||
| const { browser, cdp } = createBrowser([ | ||
| createTab({ tabId: 404, targetId: 'target-404-old' }), | ||
| ]) | ||
|
|
||
| const pages = await browser.listPages() | ||
| await attachSession(browser, 'target-404-old', pages[0].pageId) | ||
| assert.deepStrictEqual( | ||
| [...getSessionCache(browser).keys()], | ||
| ['target-404-old'], | ||
| ) | ||
|
|
||
| cdp.tabs[0].targetId = 'target-404-new' | ||
|
|
||
| await browser.closePage(pages[0].pageId) | ||
|
|
||
| assert.deepStrictEqual(cdp.closeTabCalls, [ | ||
| { targetId: 'target-404-old' }, | ||
| { targetId: 'target-404-new' }, | ||
| ]) | ||
| assert.deepStrictEqual([...getSessionCache(browser).keys()], []) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
targetIdANDtabIdto decide whether to retry. This means if onlytabIdchanges (whiletargetIdstays the same) the method retriescloseTabwith the sametargetIdthat just failed, then surfaces the error from the second attempt rather than the original one. SincecloseTabis called bytargetId, only a changedtargetIdcan make the retry meaningful. ThetabIdcondition should be dropped.Prompt To Fix With AI