diff --git a/components/terminal/TerminalComposeBar.test.tsx b/components/terminal/TerminalComposeBar.test.tsx index dc03502e6..016aaf052 100644 --- a/components/terminal/TerminalComposeBar.test.tsx +++ b/components/terminal/TerminalComposeBar.test.tsx @@ -2,9 +2,11 @@ import assert from 'node:assert/strict'; import test from 'node:test'; import React from 'react'; import { JSDOM } from 'jsdom'; +import { createRoot } from 'react-dom/client'; import { act, create, type ReactTestRenderer } from 'react-test-renderer'; import { I18nProvider } from '../../application/i18n/I18nProvider'; import { TerminalComposeBar } from './TerminalComposeBar'; +import { TerminalContextMenu } from './TerminalContextMenu'; import { TooltipProvider } from '../ui/tooltip'; import { getComposeBarHistory, pruneComposeBarHistory, recordComposeBarHistory } from '../../application/state/composeBarHistoryStore'; import { terminalReconnectRegistry } from '../../application/state/terminalReconnectRegistry'; @@ -303,3 +305,54 @@ test('compose send target toggle shows all vs active without looking like broadc assert.equal(stillPulsing.length, 0); await act(async () => { root.unmount(); }); }); + + +test('compose right-click never pastes into the terminal behind it', async (t) => { + const dom = new JSDOM('
', { url: 'http://localhost' }); + const globals = ['window', 'document', 'localStorage', 'Element', 'HTMLElement', 'Node'] as const; + const originals = globals.map((name) => [name, Object.getOwnPropertyDescriptor(globalThis, name)] as const); + for (const name of globals) { + Object.defineProperty(globalThis, name, { configurable: true, value: dom.window[name] }); + } + const rootElement = dom.window.document.querySelector('#root')!; + const root = createRoot(rootElement); + t.after(async () => { + await act(async () => root.unmount()); + dom.window.close(); + for (const [name, descriptor] of originals) { + if (descriptor) Object.defineProperty(globalThis, name, descriptor); + else Reflect.deleteProperty(globalThis, name); + } + }); + let pasteCount = 0; + const render = (broadcast: boolean) => ( + + { pasteCount++; }}> +
+
+ true} onClose={() => {}} /> +
+ + + ); + const rightClick = async (target: string) => { + const element = rootElement.querySelector(target); + assert.ok(element, target); + await act(async () => { + element.dispatchEvent(new dom.window.MouseEvent('contextmenu', { bubbles: true, cancelable: true, button: 2 })); + }); + }; + + await act(async () => root.render(render(false))); + await rightClick('textarea'); + assert.equal(pasteCount, 0, 'the solo composer must not paste into the PTY'); + await rightClick('[data-terminal-cell]'); + assert.equal(pasteCount, 1, 'ordinary terminal right-click paste still works'); + + await act(async () => root.render(render(true))); + await rightClick('textarea'); + assert.equal(pasteCount, 1, 'broadcast compose opens only its own menu'); + assert.ok(rootElement.querySelector('[data-compose-broadcast-menu]')); + await rightClick('[data-compose-broadcast-menu]'); + assert.equal(pasteCount, 1, 'right-click on the compose menu must not paste into the PTY'); +}); diff --git a/components/terminal/TerminalComposeBar.tsx b/components/terminal/TerminalComposeBar.tsx index 77a98b049..c2320faaa 100644 --- a/components/terminal/TerminalComposeBar.tsx +++ b/components/terminal/TerminalComposeBar.tsx @@ -360,9 +360,11 @@ export const TerminalComposeBar: React.FC = ({ const sendToAll = composeSendToAll ?? Boolean(isBroadcastEnabled); const handleBroadcastContextMenu = useCallback((event: React.MouseEvent) => { + // A solo composer is inside the terminal's context-menu trigger, but this + // gesture must never run a terminal action (such as paste into the PTY). + event.stopPropagation(); if (!isBroadcastEnabled && !sendToAll) return; event.preventDefault(); - event.stopPropagation(); setBroadcastMenuAnchor({ x: event.clientX, y: event.clientY, diff --git a/components/terminal/runtime/rightClickLongPress.test.ts b/components/terminal/runtime/rightClickLongPress.test.ts index 25c4b48c8..50bf19e5f 100644 --- a/components/terminal/runtime/rightClickLongPress.test.ts +++ b/components/terminal/runtime/rightClickLongPress.test.ts @@ -65,3 +65,24 @@ test('disabled or mouse-owned gestures retain the full original event sequence', dispose(); dom.window.close(); }); + + +test('right-click in the compose bar is never captured or replayed as terminal paste', (t) => { + t.mock.timers.enable({ apis: ['setTimeout'] }); + const dom = new JSDOM('
'); + const root = dom.window.document.querySelector('#root')!; + const compose = root.querySelector('textarea')!; + const dispose = installRightClickLongPress(root, () => true); + const seen: EventTarget[] = []; + root.addEventListener('contextmenu', (event) => seen.push(event.target!)); + const fire = (type: string) => compose.dispatchEvent(new dom.window.MouseEvent(type, { + bubbles: true, cancelable: true, button: 2, buttons: type === 'mouseup' ? 0 : 2, + })); + fire('mousedown'); + fire('contextmenu'); + t.mock.timers.tick(RIGHT_CLICK_LONG_PRESS_MS); + fire('mouseup'); + assert.deepEqual(seen, [compose], 'only the original composer event should be delivered'); + dispose(); + dom.window.close(); +}); diff --git a/components/terminal/runtime/rightClickLongPress.ts b/components/terminal/runtime/rightClickLongPress.ts index 7d0b5429c..88514163c 100644 --- a/components/terminal/runtime/rightClickLongPress.ts +++ b/components/terminal/runtime/rightClickLongPress.ts @@ -37,7 +37,7 @@ export function installRightClickLongPress( suppressContextMenu = false; if (event.button !== 2 || event.shiftKey || event.ctrlKey || event.altKey || event.metaKey || !canStart() - || (event.target as Element | null)?.closest('[data-terminal-history-preview]')) return; + || (event.target as Element | null)?.closest('[data-terminal-history-preview], [data-section="terminal-compose-bar"]')) return; press = event; suppressContextMenu = true; timer = setTimeout(() => {