import type { SerializedLocator } from '@vitest/browser' import type { Locator, UserEventPointerInputNormalized } from 'vitest/browser' import type { BrowserCommandContext } from 'vitest/node' import type { UserEventCommand } from './utils' import { parseKeyDef } from '@vitest/browser' import { click } from './click' import { hover } from './hover' type SerializedPointerInput = ElementToSerializedLocator interface PointerReturnData extends Pick { unreleased: string[] } type PointerEvent = ( input: readonly SerializedPointerInput[], state: Partial, ) => Promise type ElementToSerializedLocator = T extends Element | Locator ? SerializedLocator : { [K in keyof T]: ElementToSerializedLocator } export const pointer: UserEventCommand = async (context, input, state = {}) => { const pressedKeys = new Set(state.unreleased) let lastTarget: SerializedPointerInput['target'] = state.target let lastCoords: SerializedPointerInput['coords'] = state.coords for (const option of input) { let target = option.target let coords = option.coords if (target || coords) { lastTarget = target lastCoords = coords } else { target = lastTarget coords = lastCoords } const pointerAction = { ...option, target, coords } const keys = 'keys' in option ? option.keys : null const parsedKeys = keys === null ? null : groupKeyDefs(parseKeyDef(keys)) const hasOnlyClickActions = parsedKeys?.every( ({ keyDef: { keyDef: { code }, releasePrevious, releaseSelf, }, }) => code === 'MouseLeft' && !releasePrevious && releaseSelf, ) // click has its own moving logic, no need to move twice if (!hasOnlyClickActions) { if (target) { await hover(context, target, { position: coords, }) } else if (coords) { await context.page.mouse.move(coords.x, coords.y) } } if (parsedKeys) { for (const key of parsedKeys) { await keyDefHandler(key, pointerAction, pressedKeys, context) } } } return { unreleased: Array.from(pressedKeys), target: lastTarget, coords: lastCoords, } } type KeyDefOutput = ReturnType[number] interface GroupedKeyDef { times: number keyDef: KeyDefOutput } function groupKeyDefs(keyDefs: readonly KeyDefOutput[]): GroupedKeyDef[] { const output: GroupedKeyDef[] = [] let last: GroupedKeyDef | undefined for (const keyDef of keyDefs) { if ( last !== undefined && // for our purpose we can ignore the `repeat` property last.keyDef.releasePrevious === keyDef.releasePrevious && last.keyDef.releaseSelf === keyDef.releaseSelf && last.keyDef.keyDef.key === keyDef.keyDef.key && last.keyDef.keyDef.code === keyDef.keyDef.code && last.keyDef.keyDef.location === keyDef.keyDef.location ) { last.times += keyDef.repeat } else { last = { times: keyDef.repeat, keyDef } output.push(last) } } return output } const MOUSE_KEYS = ['MouseLeft', 'MouseRight', 'MouseMiddle'] async function keyDefHandler( { keyDef: { keyDef, releasePrevious, releaseSelf }, times }: GroupedKeyDef, pointerAction: Omit, pressedKeys: Set, context: BrowserCommandContext, ) { const key = keyDef.key! const code = keyDef.code! if (MOUSE_KEYS.includes(code)) { const button = code.replace('Mouse', '').toLowerCase() as 'left' | 'right' | 'middle' const mouseOptions = { button, clickCount: times, } satisfies Parameters<(typeof context)['page']['mouse']['up']>[0] if (releasePrevious) { await context.page.mouse.up(mouseOptions) } else if (releaseSelf) { if (pointerAction.target) { await click(context, pointerAction.target, { ...mouseOptions, position: pointerAction.coords, }) } else { const x = pointerAction.coords?.x ?? 0 const y = pointerAction.coords?.y ?? 0 await context.page.mouse.click(x, y, mouseOptions) } } else { await context.page.mouse.down(mouseOptions) } return } if (key === 'Unknown') { return } if (!releasePrevious) { if (releaseSelf) { for (let count = 0; count < times; count += 1) { await context.page.keyboard.press(key) } } else { await context.page.keyboard.down(key) pressedKeys.add(key) } } else if (pressedKeys.has(key)) { await context.page.keyboard.up(key) pressedKeys.delete(key) } }