|
| 1 | +// @vitest-environment node |
| 2 | +// Opt in with BSK_CLICK_CHROME; the harness owns an isolated browser/profile. |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import { SessionManager } from "@/session-manager/manager"; |
| 5 | +import { handleClick } from "../interaction"; |
| 6 | +import { handleObserve, handleSnapshot } from "../observation"; |
| 7 | +import type { CdpRunner } from "../shared"; |
| 8 | + |
| 9 | +type Send = <T = Record<string, unknown>>( |
| 10 | + method: string, |
| 11 | + params?: object, |
| 12 | + sessionId?: string, |
| 13 | +) => Promise<T>; |
| 14 | + |
| 15 | +describe.skipIf(!process.env.BSK_CLICK_CHROME)("click listener observations", () => { |
| 16 | + it.each([ |
| 17 | + { name: "snapshot", observe: handleSnapshot }, |
| 18 | + { name: "observe", observe: handleObserve }, |
| 19 | + ])("$name exposes current dynamic menu items without inline handlers", async ({ observe }) => { |
| 20 | + const { withChrome } = await import( |
| 21 | + new URL( |
| 22 | + "../../../../../evals/browser/cases/regression/snapshot-coordinates/chrome.mjs", |
| 23 | + import.meta.url, |
| 24 | + ).href |
| 25 | + ); |
| 26 | + await withChrome( |
| 27 | + { executable: process.env.BSK_CLICK_CHROME, deviceScale: 1, zoom: 1 }, |
| 28 | + async (send: Send) => { |
| 29 | + const { targetId } = await send<{ targetId: string }>("Target.createTarget", { |
| 30 | + url: "about:blank", |
| 31 | + }); |
| 32 | + const { sessionId } = await send<{ sessionId: string }>("Target.attachToTarget", { |
| 33 | + targetId, |
| 34 | + flatten: true, |
| 35 | + }); |
| 36 | + const local: Send = (method, params) => send(method, params, sessionId); |
| 37 | + const evaluate = async <T>(expression: string) => { |
| 38 | + const reply = await local<{ result: { value: T }; exceptionDetails?: unknown }>( |
| 39 | + "Runtime.evaluate", |
| 40 | + { expression, returnByValue: true, awaitPromise: true }, |
| 41 | + ); |
| 42 | + expect(reply.exceptionDetails).toBeUndefined(); |
| 43 | + return reply.result.value; |
| 44 | + }; |
| 45 | + const cdp: CdpRunner = { |
| 46 | + send: (_tabId, method, params) => local(method, params), |
| 47 | + sendToTarget: (_target, method, params) => local(method, params), |
| 48 | + getAttachmentId: () => sessionId, |
| 49 | + getFrameGraph: async () => { |
| 50 | + const { frameTree } = await local<{ frameTree: { frame: { id: string } } }>( |
| 51 | + "Page.getFrameTree", |
| 52 | + ); |
| 53 | + return { |
| 54 | + rootFrameId: frameTree.frame.id, |
| 55 | + frames: [{ frameId: frameTree.frame.id, target: { tabId: 4 } }], |
| 56 | + }; |
| 57 | + }, |
| 58 | + }; |
| 59 | + const manager = new SessionManager({ |
| 60 | + agentWindow: { |
| 61 | + create: async () => ({ windowId: 100, initialTabIds: [4] }), |
| 62 | + remove: async () => {}, |
| 63 | + ensureActiveTab: async () => 4, |
| 64 | + }, |
| 65 | + }); |
| 66 | + const ctx = await manager.start("click-listener-observation"); |
| 67 | + const tab = { id: 4, windowId: 100, active: true, url: "about:blank" } as chrome.tabs.Tab; |
| 68 | + const deps = { cdp, tabsApi: { get: async () => tab, query: async () => [tab] } }; |
| 69 | + const params = { session_id: ctx.sessionId }; |
| 70 | + const read = async () => { |
| 71 | + const result = await observe(manager, params, deps); |
| 72 | + if (!("text" in result)) throw new Error(JSON.stringify(result)); |
| 73 | + return result.text; |
| 74 | + }; |
| 75 | + const ref = (text: string, label: string) => |
| 76 | + text |
| 77 | + .split("\n") |
| 78 | + .find((line) => line.includes(`"${label}"`) && /@e\d+/.test(line)) |
| 79 | + ?.match(/@e\d+/)?.[0]; |
| 80 | + |
| 81 | + await evaluate(` |
| 82 | + document.body.innerHTML = '<button id="open">Open menu</button><div id="menu"></div>'; |
| 83 | + window.clicks = []; |
| 84 | + window.recordClick = event => clicks.push({ id: event.currentTarget.id, trusted: event.isTrusted }); |
| 85 | + document.addEventListener('click', () => {}); |
| 86 | + document.querySelector('#open').addEventListener('click', async () => { |
| 87 | + await Promise.resolve(); |
| 88 | + document.querySelector('#menu').innerHTML = \` |
| 89 | + <div id="custom-div">Div action</div> |
| 90 | + <ul> |
| 91 | + <li id="custom-li">List action</li> |
| 92 | + <li id="pointer-li" style="cursor:pointer">Pointer action</li> |
| 93 | + <li id="decorative" style="cursor:pointer">Section label</li> |
| 94 | + <li id="semantic" role="menuitem">Semantic action</li> |
| 95 | + </ul> |
| 96 | + <div id="disabled" aria-disabled="true">Disabled action</div> |
| 97 | + <div id="hidden" hidden>Hidden action</div> |
| 98 | + <div id="inert" inert>Inert action</div> |
| 99 | + <div inert><div id="inert-child">Inert child action</div></div> |
| 100 | + <div hidden><div id="hidden-child">Hidden child action</div></div> |
| 101 | + <div aria-hidden="true"><div id="aria-hidden-child">ARIA hidden child action</div></div> |
| 102 | + <div id="transparent" style="pointer-events:none">Transparent action</div> |
| 103 | + <div id="wrapper" aria-label="Wrapper action"><button id="native">Native action</button></div> |
| 104 | + \`; |
| 105 | + for (const id of ['custom-div', 'custom-li', 'pointer-li', 'semantic', 'disabled', 'hidden', 'inert', 'inert-child', 'hidden-child', 'aria-hidden-child', 'transparent', 'wrapper', 'native']) { |
| 106 | + document.getElementById(id).addEventListener('click', recordClick); |
| 107 | + } |
| 108 | + }); |
| 109 | + `); |
| 110 | + const before = await read(); |
| 111 | + expect(ref(before, "Div action")).toBeUndefined(); |
| 112 | + const open = ref(before, "Open menu"); |
| 113 | + expect(open, before).toBeDefined(); |
| 114 | + expect(await handleClick(manager, { ...params, ref: open }, deps)).not.toHaveProperty( |
| 115 | + "code", |
| 116 | + ); |
| 117 | + await expect.poll(() => evaluate("!!document.querySelector('#custom-li')")).toBe(true); |
| 118 | + const after = await read(); |
| 119 | + const actions = [ |
| 120 | + "Div action", |
| 121 | + "List action", |
| 122 | + "Pointer action", |
| 123 | + "Semantic action", |
| 124 | + "Native action", |
| 125 | + ]; |
| 126 | + for (const label of actions) expect(ref(after, label), after).toBeDefined(); |
| 127 | + for (const label of [ |
| 128 | + "Section label", |
| 129 | + "Disabled action", |
| 130 | + "Hidden action", |
| 131 | + "Inert action", |
| 132 | + "Inert child action", |
| 133 | + "Hidden child action", |
| 134 | + "ARIA hidden child action", |
| 135 | + "Transparent action", |
| 136 | + "Wrapper action", |
| 137 | + ]) { |
| 138 | + expect(ref(after, label), after).toBeUndefined(); |
| 139 | + } |
| 140 | + const firstNode = ctx.refStore.resolve(ref(after, "Div action")!); |
| 141 | + for (const label of actions.slice(0, 4)) { |
| 142 | + expect( |
| 143 | + await handleClick(manager, { ...params, ref: ref(after, label) }, deps), |
| 144 | + ).not.toHaveProperty("code"); |
| 145 | + } |
| 146 | + expect(await evaluate("clicks")).toEqual([ |
| 147 | + { id: "custom-div", trusted: true }, |
| 148 | + { id: "custom-li", trusted: true }, |
| 149 | + { id: "pointer-li", trusted: true }, |
| 150 | + { id: "semantic", trusted: true }, |
| 151 | + ]); |
| 152 | + |
| 153 | + await evaluate( |
| 154 | + "document.querySelector('#custom-div').removeEventListener('click', recordClick)", |
| 155 | + ); |
| 156 | + expect(ref(await read(), "Div action")).toBeUndefined(); |
| 157 | + await evaluate(` |
| 158 | + const previous = document.querySelector('#custom-div'); |
| 159 | + const replacement = previous.cloneNode(true); |
| 160 | + replacement.addEventListener('click', recordClick); |
| 161 | + previous.replaceWith(replacement); |
| 162 | + `); |
| 163 | + const refreshed = ref(await read(), "Div action"); |
| 164 | + expect(refreshed).toBeDefined(); |
| 165 | + expect(ctx.refStore.resolve(refreshed!)).not.toBe(firstNode); |
| 166 | + expect(await handleClick(manager, { ...params, ref: refreshed }, deps)).not.toHaveProperty( |
| 167 | + "code", |
| 168 | + ); |
| 169 | + expect(await evaluate("clicks.at(-1)")).toEqual({ id: "custom-div", trusted: true }); |
| 170 | + |
| 171 | + // showModal escapes an inert ancestor; its live AX nodes must remain actionable. |
| 172 | + await evaluate(` |
| 173 | + const container = document.createElement('div'); |
| 174 | + container.inert = true; |
| 175 | + container.innerHTML = '<dialog><div id="modal-item">Modal action</div></dialog>'; |
| 176 | + document.body.append(container); |
| 177 | + document.querySelector('#modal-item').addEventListener('click', recordClick); |
| 178 | + container.querySelector('dialog').showModal(); |
| 179 | + `); |
| 180 | + const modal = ref(await read(), "Modal action"); |
| 181 | + expect(modal).toBeDefined(); |
| 182 | + expect(await handleClick(manager, { ...params, ref: modal }, deps)).not.toHaveProperty( |
| 183 | + "code", |
| 184 | + ); |
| 185 | + expect(await evaluate("clicks.at(-1)")).toEqual({ id: "modal-item", trusted: true }); |
| 186 | + }, |
| 187 | + ); |
| 188 | + }, 30_000); |
| 189 | +}); |
0 commit comments