From cb233d9c83e54a92ca715eca0ce9e364f4b1bd1a Mon Sep 17 00:00:00 2001 From: "buf0-bot[bot]" <252831055+buf0-bot[bot]@users.noreply.github.com> Date: Sun, 31 May 2026 02:13:36 -0700 Subject: [PATCH] fix: pr-bug-scan validated finding from #3552 (#3857) * fix: address pr-bug-scan validated finding from #3552 Clear actionPending in setOverlayRootRef when node attaches, mirroring sibling BrowserMobileDriverOverlay; blocks stale-disabled-button on re-mount. * test: cover mobile driver overlay pending reset --------- Co-authored-by: orca-bug-scan-bot Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com> --- .../MobileDriverOverlay.test.tsx | 96 +++++++++++++++++++ .../terminal-pane/MobileDriverOverlay.tsx | 5 + 2 files changed, 101 insertions(+) create mode 100644 src/renderer/src/components/terminal-pane/MobileDriverOverlay.test.tsx diff --git a/src/renderer/src/components/terminal-pane/MobileDriverOverlay.test.tsx b/src/renderer/src/components/terminal-pane/MobileDriverOverlay.test.tsx new file mode 100644 index 000000000..f9f5ea9ba --- /dev/null +++ b/src/renderer/src/components/terminal-pane/MobileDriverOverlay.test.tsx @@ -0,0 +1,96 @@ +import type React from 'react' +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { MobileDriverOverlay } from './MobileDriverOverlay' + +type OverlayProps = { + actionPending: boolean + onAction: () => void | Promise + rootRef: (node: HTMLDivElement | null) => void +} + +type OverlayElement = React.ReactElement + +const hookRuntime = vi.hoisted(() => ({ + states: [] as unknown[], + stateIndex: 0, + refs: [] as { current: unknown }[], + refIndex: 0, + id: 0 +})) + +vi.mock('react', async () => { + const actual = await vi.importActual('react') // eslint-disable-line @typescript-eslint/consistent-type-imports -- vi.importActual requires inline import() + return { + ...actual, + useCallback unknown>(callback: T): T { + return callback + }, + useId(): string { + hookRuntime.id += 1 + return `test-id-${hookRuntime.id}` + }, + useRef(initial: T) { + const refIndex = hookRuntime.refIndex++ + if (!(refIndex in hookRuntime.refs)) { + hookRuntime.refs[refIndex] = { current: initial } + } + return hookRuntime.refs[refIndex] as { current: T } + }, + useState(initial: T | (() => T)) { + const stateIndex = hookRuntime.stateIndex++ + if (!(stateIndex in hookRuntime.states)) { + hookRuntime.states[stateIndex] = + typeof initial === 'function' ? (initial as () => T)() : initial + } + const setState = (next: T | ((previous: T) => T)): void => { + hookRuntime.states[stateIndex] = + typeof next === 'function' + ? (next as (previous: T) => T)(hookRuntime.states[stateIndex] as T) + : next + } + return [hookRuntime.states[stateIndex] as T, setState] as const + } + } +}) + +function renderOverlay(onAction: () => void | Promise): OverlayElement { + hookRuntime.stateIndex = 0 + hookRuntime.refIndex = 0 + return MobileDriverOverlay({ + driver: { kind: 'mobile', clientId: 'phone-1' } as never, + hasFitOverride: false, + onAction + }) as OverlayElement +} + +describe('MobileDriverOverlay', () => { + beforeEach(() => { + hookRuntime.states = [] + hookRuntime.stateIndex = 0 + hookRuntime.refs = [] + hookRuntime.refIndex = 0 + hookRuntime.id = 0 + }) + + it('clears stale pending action state when the overlay root reattaches', async () => { + let currentRootRef: OverlayProps['rootRef'] | null = null + const onAction = vi.fn(async () => { + currentRootRef?.(null) + }) + + let overlay = renderOverlay(onAction) + currentRootRef = overlay.props.rootRef + currentRootRef({} as HTMLDivElement) + + await overlay.props.onAction() + + overlay = renderOverlay(onAction) + expect(overlay.props.actionPending).toBe(true) + + currentRootRef = overlay.props.rootRef + currentRootRef({} as HTMLDivElement) + overlay = renderOverlay(onAction) + + expect(overlay.props.actionPending).toBe(false) + }) +}) diff --git a/src/renderer/src/components/terminal-pane/MobileDriverOverlay.tsx b/src/renderer/src/components/terminal-pane/MobileDriverOverlay.tsx index 49c762947..681031879 100644 --- a/src/renderer/src/components/terminal-pane/MobileDriverOverlay.tsx +++ b/src/renderer/src/components/terminal-pane/MobileDriverOverlay.tsx @@ -37,6 +37,11 @@ export function MobileDriverOverlay({ const setOverlayRootRef = useCallback((node: HTMLDivElement | null): void => { mountedRef.current = node !== null + if (node) { + // Why: take-back can resolve after the overlay renders null; a later + // mobile session must not inherit the stale disabled state. + setActionPending(false) + } }, []) const currentCollapseState = getMobileDriverOverlayCollapseState(collapseState, driverClientId)