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)