{{ message }}
Repository navigation
fix(desktop-browser): ask the user before a page's alert, confirm, or leave-site prompt is answered #8432
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
Merged
Merged
fix(desktop-browser): ask the user before a page's alert, confirm, or leave-site prompt is answered #8432
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
be9ba65
fix(desktop-browser): ask the user before a page's alert, confirm, or…
waleedlatif1 1a4ef00
fix(desktop-browser): ask only about the user's own on-screen page, n…
waleedlatif1 464ed21
fix(desktop-browser): label frame dialogs with their own origin and c…
waleedlatif1 41040be
fix(desktop-browser): preserve native dialog ownership and keyboard f…
waleedlatif1 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
Next
Next commit
fix(desktop-browser): ask the user before a page's alert, confirm, or…
… leave-site prompt is answered
- Loading branch information
commit be9ba650e053dc3de647939ee2bbc0efee03595b
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,271 @@ | ||
| import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' | ||
| import { createServer, type Server } from 'node:http' | ||
| import { tmpdir } from 'node:os' | ||
| import { dirname, join } from 'node:path' | ||
| import { fileURLToPath } from 'node:url' | ||
| import { | ||
| type ElectronApplication, | ||
| _electron as electron, | ||
| expect, | ||
| type Page, | ||
| test, | ||
| } from '@playwright/test' | ||
| import type { BrowserPageDialog, BrowserToolName } from '@sim/browser-protocol' | ||
| import type { SimDesktopApi } from '@sim/desktop-bridge' | ||
| import { getErrorMessage } from '@sim/utils/errors' | ||
|
|
||
| const DESKTOP_DIR = fileURLToPath(new URL('..', import.meta.url)) | ||
| const SCOPE = 'browser-page-dialogs-e2e' | ||
| const SHELL_FIXTURE = '<!doctype html><title>Sim fixture</title><h1>Browser dialogs fixture</h1>' | ||
| const FORM_FIXTURE = `<!doctype html><title>form</title> | ||
| <input id="draft" aria-label="Draft"> | ||
| <button id="delete" onclick="document.title = 'confirm:' + confirm('Delete the report?')">Delete</button> | ||
| <script>addEventListener('beforeunload', (event) => { | ||
| if (document.getElementById('draft').value) { event.preventDefault(); event.returnValue = '' } | ||
| })</script>` | ||
|
|
||
| type Bridge = typeof globalThis & { | ||
| simDesktop: SimDesktopApi | ||
| pageDialog?: BrowserPageDialog | null | ||
| } | ||
|
|
||
| /** | ||
| * A page's alert, confirm, and leave-site question belong to whoever is using | ||
| * the page: the user on the tab they are looking at, the agent during its own | ||
| * action. These checks drive the real shell and native tab views. | ||
| */ | ||
| test('page dialogs wait for the user on their page and stay automatic for the agent', async () => { | ||
| const reportPath = | ||
| process.env.DESKTOP_BROWSER_DIALOGS_REPORT_PATH ?? | ||
| test.info().outputPath('browser-page-dialogs.json') | ||
| const checks: { | ||
| name: string | ||
| status: 'passed' | 'failed' | ||
| durationMs: number | ||
| error?: string | ||
| }[] = [] | ||
| const check = async (name: string, run: () => Promise<void>) => { | ||
| const started = Date.now() | ||
| try { | ||
| await test.step(name, run) | ||
| checks.push({ name, status: 'passed', durationMs: Date.now() - started }) | ||
| } catch (error) { | ||
| checks.push({ | ||
| name, | ||
| status: 'failed', | ||
| durationMs: Date.now() - started, | ||
| error: getErrorMessage(error), | ||
| }) | ||
| throw error | ||
| } | ||
| } | ||
| const calls = new Map<string, { chatId: string; toolName: BrowserToolName; args: unknown }>() | ||
| const server: Server = createServer(async (request, response) => { | ||
| const path = new URL(request.url ?? '/', 'http://localhost').pathname | ||
| if (path === '/api/desktop/tool/authorize') { | ||
| let body = '' | ||
| for await (const chunk of request) body += chunk.toString() | ||
| const authorization = calls.get(JSON.parse(body).toolCallId) | ||
| response.writeHead(authorization ? 200 : 403, { 'Content-Type': 'application/json' }) | ||
| response.end(JSON.stringify(authorization ?? {})) | ||
| return | ||
| } | ||
| // The app origin serves the shell; the same server on localhost is the web. | ||
| const isSite = request.headers.host?.startsWith('localhost') === true | ||
| response.writeHead(200, { | ||
| 'Content-Type': 'text/html', | ||
| ...(isSite ? {} : { 'Set-Cookie': 'better-auth.session_token=fixture; HttpOnly; Path=/' }), | ||
| }) | ||
| response.end( | ||
| !isSite | ||
| ? SHELL_FIXTURE | ||
| : path === '/form' | ||
| ? FORM_FIXTURE | ||
| : '<!doctype html><title>next</title>' | ||
| ) | ||
| }) | ||
| const userData = mkdtempSync(join(tmpdir(), 'sim-browser-dialogs-e2e-')) | ||
| let app: ElectronApplication | undefined | ||
| let passed = false | ||
| try { | ||
| await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve)) | ||
| const address = server.address() | ||
| if (!address || typeof address === 'string') throw new Error('Missing fixture address') | ||
| const origin = `http://127.0.0.1:${address.port}` | ||
| const site = origin.replace('127.0.0.1', 'localhost') | ||
| const shellApp = await electron.launch({ | ||
| args: [process.env.SIM_DESKTOP_E2E_MAIN ?? '.'], | ||
| cwd: DESKTOP_DIR, | ||
| env: { ...process.env, SIM_DESKTOP_ORIGIN: origin, SIM_DESKTOP_USER_DATA: userData }, | ||
| }) | ||
| app = shellApp | ||
| // A dialog listener stops Playwright auto-dismissing page dialogs, so the desktop's own | ||
| // handling decides their outcome exactly as it does in production. | ||
| const leaveDialogsToDesktop = (page: Page) => page.on('dialog', () => {}) | ||
| shellApp.context().pages().forEach(leaveDialogsToDesktop) | ||
| shellApp.context().on('page', leaveDialogsToDesktop) | ||
| const shell = await shellApp.firstWindow() | ||
| await expect(shell.getByRole('heading')).toHaveText('Browser dialogs fixture') | ||
| await shell.evaluate(async (scope) => { | ||
| const bridge = globalThis as Bridge | ||
| const api = bridge.simDesktop.browserAgent | ||
| await api.activateScope(scope) | ||
| const updateBounds = () => | ||
| api.setPanelBounds( | ||
| { x: 0, y: 80, width: innerWidth, height: innerHeight - 80 }, | ||
| null, | ||
| scope | ||
| ) | ||
| updateBounds() | ||
| window.setInterval(updateBounds, 200) | ||
| bridge.pageDialog = null | ||
| api.onPageState((state) => { | ||
| bridge.pageDialog = state.dialog ?? null | ||
| }) | ||
| }, SCOPE) | ||
|
|
||
| let callCount = 0 | ||
| const execute = async (tool: BrowserToolName, args: Record<string, unknown>) => { | ||
| const callId = `browser-dialogs-${++callCount}` | ||
| calls.set(callId, { chatId: SCOPE, toolName: tool, args }) | ||
| const result = await shell.evaluate( | ||
| ({ callId, tool, args, scope }) => | ||
| (globalThis as Bridge).simDesktop.browserAgent.executeTool(callId, tool, args, scope), | ||
| { callId, tool, args, scope: SCOPE } | ||
| ) | ||
| expect(result.ok).toBe(true) | ||
| return result.ok ? result.result : undefined | ||
| } | ||
| /** Browser-chrome actions are user gestures, so each follows a real click in Sim. */ | ||
| const panelAction = async (action: Record<string, unknown>) => { | ||
| await shell.getByRole('heading').click() | ||
| await shell.evaluate( | ||
| ({ action, scope }) => | ||
| (globalThis as Bridge).simDesktop.browserAgent.panelAction( | ||
| action as unknown as Parameters<SimDesktopApi['browserAgent']['panelAction']>[0], | ||
| scope | ||
| ), | ||
| { action, scope: SCOPE } | ||
| ) | ||
| } | ||
| const pageDialog = () => shell.evaluate(() => (globalThis as Bridge).pageDialog ?? null) | ||
| const inPage = <T>(script: string) => | ||
| shellApp.evaluate( | ||
| async ({ webContents }, { script, url }) => | ||
| (await webContents | ||
| .getAllWebContents() | ||
| .find((contents) => contents.getURL().startsWith(url)) | ||
| ?.executeJavaScript(script)) as T, | ||
| { script, url: site } | ||
| ) | ||
| /** Read from the shell: a script cannot run in a page while its dialog is open. */ | ||
| const pageTitle = () => | ||
| shellApp.evaluate( | ||
| ({ webContents }, url) => | ||
| webContents | ||
| .getAllWebContents() | ||
| .find((contents) => contents.getURL().startsWith(url)) | ||
| ?.getTitle() ?? null, | ||
| site | ||
| ) | ||
| const pageUrl = () => | ||
| shellApp.evaluate( | ||
| ({ webContents }, url) => | ||
| webContents | ||
| .getAllWebContents() | ||
| .find((contents) => contents.getURL().startsWith(url)) | ||
| ?.getURL() ?? null, | ||
| site | ||
| ) | ||
| /** Trusted input into the page, the way the user's own mouse and keys arrive. */ | ||
| const userInput = (selector: string, text = '') => | ||
| shellApp.evaluate( | ||
| async ({ webContents }, { selector, text, url }) => { | ||
| const contents = webContents | ||
| .getAllWebContents() | ||
| .find((candidate) => candidate.getURL().startsWith(url)) | ||
| if (!contents) throw new Error('No page') | ||
| const rect = JSON.parse( | ||
| await contents.executeJavaScript( | ||
| `JSON.stringify(document.querySelector(${JSON.stringify(selector)}).getBoundingClientRect())` | ||
| ) | ||
| ) | ||
| const point = { x: Math.round(rect.x + 5), y: Math.round(rect.y + 5) } | ||
| contents.sendInputEvent({ type: 'mouseDown', ...point, button: 'left', clickCount: 1 }) | ||
| contents.sendInputEvent({ type: 'mouseUp', ...point, button: 'left', clickCount: 1 }) | ||
| for (const character of text) | ||
| contents.sendInputEvent({ type: 'char', keyCode: character }) | ||
| }, | ||
| { selector, text, url: site } | ||
| ) | ||
|
|
||
| await check('without a renderer that shows dialogs, the shell still answers them', async () => { | ||
| await execute('browser_open_url', { url: `${site}/form` }) | ||
| await panelAction({ action: 'switch-tab', tabId: '1' }) | ||
| await userInput('#delete') | ||
| await expect.poll(() => pageTitle()).toBe('confirm:false') | ||
| expect(await pageDialog()).toBeNull() | ||
| }) | ||
|
|
||
| await panelAction({ action: 'enable-page-dialogs' }) | ||
| await inPage("document.title = 'form'") | ||
|
|
||
| await check("the user's confirm waits for their answer", async () => { | ||
| await userInput('#delete') | ||
| await expect | ||
| .poll(pageDialog) | ||
| .toMatchObject({ kind: 'confirm', message: 'Delete the report?' }) | ||
| expect(await pageTitle()).toBe('form') | ||
| const dialog = await pageDialog() | ||
| await panelAction({ action: 'respond-dialog', requestId: dialog?.requestId, allowed: true }) | ||
| await expect.poll(() => pageTitle()).toBe('confirm:true') | ||
|
waleedlatif1 marked this conversation as resolved.
Outdated
|
||
| await expect.poll(pageDialog).toBeNull() | ||
| }) | ||
|
|
||
| await check("the agent's dialogs never wait on the user", async () => { | ||
| await inPage("document.title = 'form'") | ||
| const snapshot = await execute('browser_snapshot', {}) | ||
| const ref = /button "Delete" \[ref=(\d+)\]/.exec( | ||
| String((snapshot as { outline?: string }).outline) | ||
| )?.[1] | ||
| expect(ref, 'snapshot lists the Delete button').toBeTruthy() | ||
| await execute('browser_click', { elementId: Number(ref) }) | ||
| await expect.poll(() => pageTitle()).toBe('confirm:false') | ||
| expect(await pageDialog()).toBeNull() | ||
| await inPage("document.title = 'form'") | ||
| await execute('browser_click', { elementId: Number(ref), dialog: { accept: true } }) | ||
| await expect.poll(() => pageTitle()).toBe('confirm:true') | ||
| expect(await pageDialog()).toBeNull() | ||
| }) | ||
|
|
||
| await check('leaving a draft from the URL bar asks, and Stay keeps it', async () => { | ||
| await userInput('#draft', 'draft') | ||
| await expect | ||
| .poll(() => inPage<string>("document.getElementById('draft').value")) | ||
| .toBe('draft') | ||
| await panelAction({ action: 'navigate', url: `${site}/next` }) | ||
| await expect.poll(pageDialog).toMatchObject({ kind: 'beforeunload' }) | ||
| const dialog = await pageDialog() | ||
| await panelAction({ action: 'respond-dialog', requestId: dialog?.requestId, allowed: false }) | ||
| await expect.poll(pageDialog).toBeNull() | ||
| expect(await pageUrl()).toBe(`${site}/form`) | ||
| expect(await inPage<string>("document.getElementById('draft').value")).toBe('draft') | ||
| }) | ||
|
|
||
| await check('Leave lets the navigation through without asking again', async () => { | ||
| await panelAction({ action: 'navigate', url: `${site}/next` }) | ||
| await expect.poll(pageDialog).toMatchObject({ kind: 'beforeunload' }) | ||
| const dialog = await pageDialog() | ||
| await panelAction({ action: 'respond-dialog', requestId: dialog?.requestId, allowed: true }) | ||
| await expect.poll(pageUrl).toBe(`${site}/next`) | ||
| expect(await pageDialog()).toBeNull() | ||
| }) | ||
| passed = true | ||
| } finally { | ||
| mkdirSync(dirname(reportPath), { recursive: true }) | ||
| writeFileSync(reportPath, JSON.stringify({ passed, checks }, null, 2)) | ||
| await app?.close() | ||
| await new Promise<void>((resolve) => server.close(() => resolve())) | ||
| rmSync(userData, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
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
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.
You can’t perform that action at this time.

Uh oh!
There was an error while loading. Please reload this page.