Skip to content

The agent that grows with you

30.1 KB
Raw Blame
import { cleanup, createEvent, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { MemoryRouter } from 'react-router'
import { afterEach, describe, expect, it, vi } from 'vitest'

import { FileEntryContextMenu, InlineRenameInput } from '@/app/right-sidebar/file-actions'
import { registerTerminalContextMenu } from '@/app/right-sidebar/terminal/terminal-context-menu'
import { DirectiveContent } from '@/components/assistant-ui/directive-text'
import { ContextMenu, ContextMenuTrigger, HERMES_CONTEXT_MENU_TRIGGER_ATTR } from '@/components/ui/context-menu'
import { Dialog, DialogContent, DialogTitle } from '@/components/ui/dialog'
import { $previewTabs, closeRightRail } from '@/store/preview'
import { $connection } from '@/store/session'

import { AppContextMenu } from './app-context-menu'
import {
  $contextMenu,
  augmentSpellcheck,
  type GuestMenuHandle,
  type GuestMenuParams,
  openGuestContextMenu
} from './store'
import { resolveDomTarget } from './target'

const desktopWindow = window as unknown as { hermesDesktop?: Window['hermesDesktop'] }

function installBridge(partial: Partial<Window['hermesDesktop']> = {}) {
  desktopWindow.hermesDesktop = {
    openExternal: vi.fn().mockResolvedValue(undefined),
    writeClipboard: vi.fn().mockResolvedValue(true),
    ...partial
  } as unknown as Window['hermesDesktop']
}

function mountMenu() {
  return render(
    <MemoryRouter>
      <AppContextMenu />
    </MemoryRouter>
  )
}

function attach(html: string): HTMLElement {
  const host = document.createElement('div')

  host.innerHTML = html
  document.body.appendChild(host)

  return host
}

afterEach(() => {
  $contextMenu.set(null)
  $connection.set(null)
  closeRightRail()
  cleanup()
  vi.restoreAllMocks()
  vi.unstubAllGlobals()
  document.body.innerHTML = ''
  delete window.document.documentElement.dataset.hermesDesktopHost
  delete desktopWindow.hermesDesktop
})

describe('resolveDomTarget', () => {
  it('resolves an anchor and its href', () => {
    const host = attach('<a href="https://example.com/x">link</a>')

    expect(resolveDomTarget(host.querySelector('a')).linkUrl).toBe('https://example.com/x')
  })

  it('ignores hash-only placeholder anchors', () => {
    const host = attach('<a href="#">stub</a>')

    expect(resolveDomTarget(host.querySelector('a')).linkUrl).toBe('')
  })

  it('resolves an image, and the anchor wrapping it', () => {
    const host = attach('<a href="https://example.com/page"><img src="https://example.com/pic.png"></a>')
    const target = resolveDomTarget(host.querySelector('img'))

    expect(target.onImage).toBe(true)
    expect(target.imageUrl).toContain('pic.png')
    expect(target.linkUrl).toBe('https://example.com/page')
  })

  it('resolves editables, skipping disabled and readonly fields', () => {
    const host = attach('<textarea></textarea><input readonly><div contenteditable="true"><span>x</span></div>')

    expect(resolveDomTarget(host.querySelector('textarea')).editable).toBeTruthy()
    expect(resolveDomTarget(host.querySelector('input')).editable).toBeNull()
  })

  it('resolves the enclosing dialog as the menu portal container', () => {
    const host = attach('<div data-slot="dialog-content"><a href="https://example.com">link</a></div>')
    const dialog = host.firstElementChild

    expect(resolveDomTarget(host.querySelector('a')).dialogPortalContainer).toBe(dialog)
  })
})

describe('AppContextMenu', () => {
  it('leaves explicit row menus available on touch devices', () => {
    installBridge()
    window.document.documentElement.dataset.hermesDesktopHost = 'browser'
    vi.stubGlobal('matchMedia', vi.fn().mockReturnValue({ matches: true }))
    mountMenu()
    const host = attach('<div data-slot="context-menu-trigger">Profile row</div>')
    const rowMenu = vi.fn()
    host.addEventListener('contextmenu', rowMenu)
    fireEvent.contextMenu(host.firstElementChild!)
    expect(rowMenu).toHaveBeenCalledTimes(1)
    expect($contextMenu.get()).toBeNull()
  })
  it.each([
    ['message text', '<div data-slot="aui_assistant-message-content"><p>selectable content</p></div>', 'p'],
    ['a link', '<a href="https://example.com">selectable content</a>', 'a'],
    ['an editable', '<textarea>selectable content</textarea>', 'textarea']
  ])('preserves native touch long-press on %s before text selection exists', (_name, content, tag) => {
    installBridge()
    window.document.documentElement.dataset.hermesDesktopHost = 'browser'
    vi.stubGlobal('matchMedia', vi.fn().mockReturnValue({ matches: true }))
    mountMenu()

    const host = attach(`<div data-zone-body="test" data-slot="context-menu-trigger">${content}</div>`)

    const fallback = vi.fn()
    host.addEventListener('contextmenu', fallback)
    const event = createEvent.contextMenu(host.querySelector(tag)!, { bubbles: true, cancelable: true })
    fireEvent(host.querySelector(tag)!, event)
    expect(event.defaultPrevented).toBe(false)
    expect(fallback).not.toHaveBeenCalled()
    expect($contextMenu.get()).toBeNull()
  })
  describe.each(['browser', 'electron'])('%s host gesture ownership', hostKind => {
    it.each([
      ['chrome', '<p>plain chrome</p>', 'Settings'],
      ['terminal', '<div data-terminal=""><canvas></canvas></div>', 'Select all']
    ])('opens only the app menu for %s, preserving Electron native facts', async (_kind, html, label) => {
      installBridge()
      window.document.documentElement.dataset.hermesDesktopHost = hostKind
      mountMenu()
      const host = attach(html)
      const terminal = host.querySelector('[data-terminal]')

      const unregister = terminal
        ? registerTerminalContextMenu(terminal, {
            getSelection: () => '',
            paste: null,
            reload: vi.fn(),
            selectAll: vi.fn(),
            wordErase: null
          })
        : undefined

      const target = host.querySelector('canvas') ?? host.firstElementChild!
      const onTarget = vi.fn()

      target.addEventListener('contextmenu', onTarget)
      const event = createEvent.contextMenu(target)

      try {
        fireEvent(target, event)

        expect(await screen.findByText(label)).toBeTruthy()
        expect(onTarget).not.toHaveBeenCalled()
        expect(event.defaultPrevented).toBe(hostKind === 'browser')
      } finally {
        unregister?.()
      }
    })
  })

  // The skip-marked and radix-surface tests below cover these markers in
  // Electron; in the Webapp a late marker check would also cancel the native menu.
  it.each([HERMES_CONTEXT_MENU_TRIGGER_ATTR, 'data-slot="context-menu-trigger"', 'data-context-menu-skip'])(
    'leaves the gesture untouched in the browser Webapp for a surface owning %s',
    marker => {
      installBridge()
      window.document.documentElement.dataset.hermesDesktopHost = 'browser'
      mountMenu()
      const host = attach(`<div ${marker}><span>owned surface</span></div>`)
      const target = host.querySelector('span')!
      const onTarget = vi.fn()

      target.addEventListener('contextmenu', onTarget)
      const event = createEvent.contextMenu(target)

      fireEvent(target, event)

      expect($contextMenu.get()).toBeNull()
      expect(onTarget).toHaveBeenCalledOnce()
      expect(event.defaultPrevented).toBe(false)
    }
  )

  it('leaves native media context menus untouched in the browser Webapp', () => {
    installBridge()
    window.document.documentElement.dataset.hermesDesktopHost = 'browser'
    mountMenu()
    const host = attach('<audio controls></audio>')
    const target = host.firstElementChild!
    const onTarget = vi.fn()

    target.addEventListener('contextmenu', onTarget)
    const event = createEvent.contextMenu(target)

    fireEvent(target, event)

    expect($contextMenu.get()).toBeNull()
    expect(onTarget).toHaveBeenCalledOnce()
    expect(event.defaultPrevented).toBe(false)
  })

  it('detects native media through the composed path', () => {
    installBridge()
    window.document.documentElement.dataset.hermesDesktopHost = 'browser'
    mountMenu()
    const host = attach('<div><video controls></video></div>')
    const target = host.firstElementChild!
    const video = target.firstElementChild!
    const event = createEvent.contextMenu(target)

    Object.defineProperty(event, 'composedPath', { value: () => [video, target, host, document.body, document] })
    fireEvent(target, event)

    expect($contextMenu.get()).toBeNull()
    expect(event.defaultPrevented).toBe(false)
  })

  // The browser bridge has no contextMenuEdit (our Cut/Copy/Paste) and no spellcheck feed:
  // only the browser's own menu has working edit verbs and spelling suggestions there.
  it.each([
    [
      'a field in a pane body',
      '<div data-zone-body="t" data-slot="context-menu-trigger"><textarea>teh</textarea></div>',
      'textarea'
    ],
    ['the rich composer', '<div contenteditable="true" data-slot="composer-rich-input"><span>teh</span></div>', 'span'],
    ['a link inside an editable', '<div contenteditable="true"><a href="https://example.com">link</a></div>', 'a']
  ])('leaves the native menu on %s in the browser Webapp', (_name, html, selector) => {
    installBridge()
    window.document.documentElement.dataset.hermesDesktopHost = 'browser'
    mountMenu()
    const host = attach(html)
    // jsdom lacks the browser's isContentEditable.
    host
      .querySelectorAll('[contenteditable]')
      .forEach(node => Object.defineProperty(node, 'isContentEditable', { value: true }))
    const fallback = vi.fn()
    host.addEventListener('contextmenu', fallback)
    const field = host.querySelector(selector)!
    const event = createEvent.contextMenu(field, { bubbles: true, cancelable: true })

    fireEvent(field, event)

    expect(event.defaultPrevented).toBe(false)
    expect(fallback).not.toHaveBeenCalled()
    expect($contextMenu.get()).toBeNull()
  })

  it('leaves the native menu on an inline file rename in the browser Webapp', () => {
    installBridge()
    window.document.documentElement.dataset.hermesDesktopHost = 'browser'
    render(
      <MemoryRouter>
        <AppContextMenu />
        <FileEntryContextMenu isDirectory={false} name="notes.txt" path="/work/notes.txt">
          <div>
            <InlineRenameInput name="notes.txt" path="/work/notes.txt" />
          </div>
        </FileEntryContextMenu>
      </MemoryRouter>
    )
    const input = screen.getByRole('textbox', { name: 'New name' })
    const event = createEvent.contextMenu(input, { bubbles: true, cancelable: true })

    fireEvent(input, event)

    expect(event.defaultPrevented).toBe(false)
    expect(screen.queryByText('Copy path')).toBeNull()
  })

  // Only a field the user types in keeps the browser's menu; Electron resolves nothing for a row.
  it.each([
    ['browser', 'checkbox'],
    ['electron', 'text']
  ])('%s: a %s input inside a row still opens the row menu', async (hostKind, type) => {
    installBridge()
    window.document.documentElement.dataset.hermesDesktopHost = hostKind
    render(
      <MemoryRouter>
        <AppContextMenu />
        <FileEntryContextMenu isDirectory={false} name="notes.txt" path="/work/notes.txt">
          <div>
            <input aria-label="Pick" type={type} />
          </div>
        </FileEntryContextMenu>
      </MemoryRouter>
    )
    const input = screen.getByLabelText('Pick')
    const selectionText = vi.spyOn(Selection.prototype, 'toString')
    const event = createEvent.contextMenu(input, { bubbles: true, cancelable: true })

    fireEvent(input, event)

    expect(event.defaultPrevented).toBe(true)
    expect(await screen.findByText('Copy path')).toBeTruthy()
    expect(selectionText).not.toHaveBeenCalled()
  })

  it('keeps native media gestures unprevented in Electron', async () => {
    installBridge()
    window.document.documentElement.dataset.hermesDesktopHost = 'electron'
    mountMenu()
    const electronHost = attach('<video controls></video>')
    const electronEvent = createEvent.contextMenu(electronHost.firstElementChild!)

    fireEvent(electronHost.firstElementChild!, electronEvent)

    expect(await screen.findByText('Settings')).toBeTruthy()
    expect(electronEvent.defaultPrevented).toBe(false)
  })

  it('opens the link menu on a chat link right-click', async () => {
    installBridge()
    mountMenu()
    const host = attach('<a href="https://example.com/docs">Docs</a>')

    fireEvent.contextMenu(host.querySelector('a')!)

    expect(await screen.findByText('Open in in-app browser')).toBeTruthy()
    expect(screen.getByText('Open in external browser')).toBeTruthy()
    expect(screen.getByText('Copy URL')).toBeTruthy()
    expect(screen.queryByText('Copy resolved URL')).toBeNull()
  })

  // The url chip used to be a `<button>`: `resolveDomTarget` only knows
  // `a[href]`, so the right-click fell through to the shell fallback menu.
  it('offers the link verbs on a message url chip right-click', async () => {
    installBridge()
    mountMenu()
    // The coordinator binds to window in the capture phase, so a second
    // render alongside the menu is fine — same as a real transcript.
    render(<DirectiveContent text="@url:`https://example.com/pr/1`" />)
    const chip = document.querySelector('[data-slot="aui_directive-chip"]')!

    fireEvent.contextMenu(chip)

    expect(await screen.findByText('Open in in-app browser')).toBeTruthy()
    expect(screen.getByText('Open in external browser')).toBeTruthy()
    expect(screen.getByText('Copy URL')).toBeTruthy()
  })

  it('opens the in-app browser from the link menu', async () => {
    installBridge()
    mountMenu()
    const host = attach('<a href="https://example.com/docs">Docs</a>')

    fireEvent.contextMenu(host.querySelector('a')!)
    fireEvent.click(await screen.findByText('Open in in-app browser'))

    await waitFor(() => expect($previewTabs.get().at(-1)?.target.url).toBe('https://example.com/docs'))
  })

  it('skips Open in in-app browser on the HUD — that window has no browser pane', async () => {
    const originalLocation = window.location

    Object.defineProperty(window, 'location', {
      configurable: true,
      value: { ...originalLocation, search: '?win=hud' }
    })

    try {
      installBridge()
      mountMenu()
      const host = attach('<a href="https://accounts.google.com/o/oauth2/auth">Sign in</a>')

      fireEvent.contextMenu(host.querySelector('a')!)

      expect(await screen.findByText('Open in external browser')).toBeTruthy()
      expect(screen.queryByText('Open in in-app browser')).toBeNull()
    } finally {
      Object.defineProperty(window, 'location', { configurable: true, value: originalLocation })
    }
  })

  it('offers the resolved copy only for loopback links on a remote gateway', async () => {
    $connection.set({ mode: 'remote' } as never)
    const reachPreviewUrl = vi.fn(async () => 'http://127.0.0.1:45173/')
    const writeClipboard = vi.fn().mockResolvedValue(true)

    installBridge({
      reachPreviewUrl: reachPreviewUrl as unknown as Window['hermesDesktop']['reachPreviewUrl'],
      writeClipboard: writeClipboard as unknown as Window['hermesDesktop']['writeClipboard']
    })
    mountMenu()
    const host = attach('<a href="http://localhost:5173/">Dev</a>')

    fireEvent.contextMenu(host.querySelector('a')!)
    fireEvent.click(await screen.findByText('Copy resolved URL'))

    await waitFor(() => expect(writeClipboard).toHaveBeenCalledWith('http://127.0.0.1:45173/'))
  })

  it('opens the image menu with copy, address, and save', async () => {
    installBridge()
    mountMenu()
    const host = attach('<img src="https://example.com/pic.png" alt="pic">')

    fireEvent.contextMenu(host.querySelector('img')!)

    expect(await screen.findByText('Copy image')).toBeTruthy()
    expect(screen.getByText('Copy image address')).toBeTruthy()
    expect(screen.getByText('Save image as…')).toBeTruthy()
  })

  it('opens the edit menu in an editable and augments it with spellcheck', async () => {
    installBridge()
    mountMenu()
    const host = attach('<textarea></textarea>')

    fireEvent.contextMenu(host.querySelector('textarea')!)

    expect(await screen.findByText('Select all')).toBeTruthy()
    expect(screen.getByText('Paste')).toBeTruthy()
    expect(screen.queryByText('Add to dictionary')).toBeNull()

    // The main-process forward lands after the menu opened.
    augmentSpellcheck({ misspelledWord: 'teh', suggestions: ['the', 'ten'] })

    expect(await screen.findByText('Add to dictionary')).toBeTruthy()
    expect(screen.getByText('the')).toBeTruthy()
  })

  // A browser host whose bridge can run the edit verbs keeps the app menu too, cancelling its own.
  it.each(['electron', 'browser'])('%s: edit verbs run after the menu closed, field refocused', async hostKind => {
    const contextMenuEdit = vi.fn().mockResolvedValue(undefined)

    installBridge({ contextMenuEdit: contextMenuEdit as unknown as Window['hermesDesktop']['contextMenuEdit'] })
    window.document.documentElement.dataset.hermesDesktopHost = hostKind
    mountMenu()
    const host = attach('<textarea>some draft text</textarea>')
    const textarea = host.querySelector('textarea')!

    // Cut/copy act on the selection, so give the field one.
    textarea.setSelectionRange(0, 4)
    const event = createEvent.contextMenu(textarea, { bubbles: true, cancelable: true })
    fireEvent(textarea, event)
    fireEvent.click(await screen.findByText('Copy'))
    expect(event.defaultPrevented).toBe(hostKind === 'browser')

    // The verb waits a frame so the radix focus trap unmounts first —
    // dispatching while the trap holds focus sent the command to `body`.
    expect(contextMenuEdit).not.toHaveBeenCalled()

    await waitFor(() => expect(contextMenuEdit).toHaveBeenCalledWith('copy'))
    expect($contextMenu.get()).toBeNull()
    expect(document.activeElement).toBe(textarea)
  })

  it('keeps a modal textarea paste menu inside its dialog and restores focus', async () => {
    const contextMenuEdit = vi.fn().mockResolvedValue(undefined)

    installBridge({
      contextMenuEdit: contextMenuEdit as unknown as Window['hermesDesktop']['contextMenuEdit'],
      readClipboard: vi.fn().mockResolvedValue('clipboard payload')
    })
    render(
      <MemoryRouter>
        <AppContextMenu />
        <Dialog open>
          <DialogContent>
            <DialogTitle>Modal editor</DialogTitle>
            <textarea aria-label="modal textarea" />
          </DialogContent>
        </Dialog>
      </MemoryRouter>
    )
    const textarea = screen.getByLabelText('modal textarea')

    fireEvent.contextMenu(textarea)

    const paste = (await screen.findByText('Paste')).closest('[data-slot="dropdown-menu-item"]') as HTMLElement

    await waitFor(() => expect(paste.getAttribute('data-disabled')).toBeNull())

    const menu = paste.closest('[data-slot="dropdown-menu-content"]')
    const dialog = screen.getByRole('dialog')

    expect(dialog.contains(menu)).toBe(true)

    fireEvent.click(paste)

    await waitFor(() => expect(contextMenuEdit).toHaveBeenCalledWith('paste'))
    expect(document.activeElement).toBe(textarea)
  })

  it('grays out cut and copy when the field has text but no selection', async () => {
    installBridge()
    mountMenu()
    const host = attach('<textarea>plenty of text, none selected</textarea>')
    const textarea = host.querySelector('textarea')!

    textarea.setSelectionRange(0, 0)
    fireEvent.contextMenu(textarea)

    const item = (label: string) => screen.getByText(label).closest('[data-slot="dropdown-menu-item"]') as HTMLElement

    await screen.findByText('Select all')

    expect(item('Cut').getAttribute('data-disabled')).not.toBeNull()
    expect(item('Copy').getAttribute('data-disabled')).not.toBeNull()
    // Content is there, so select all stays live.
    expect(item('Select all').getAttribute('data-disabled')).toBeNull()
  })

  it('enables cut and copy when the field has a selection', async () => {
    installBridge()
    mountMenu()
    const host = attach('<textarea>pick some of this</textarea>')
    const textarea = host.querySelector('textarea')!

    textarea.setSelectionRange(0, 4)
    fireEvent.contextMenu(textarea)

    const item = (label: string) => screen.getByText(label).closest('[data-slot="dropdown-menu-item"]') as HTMLElement

    await screen.findByText('Select all')

    expect(item('Cut').getAttribute('data-disabled')).toBeNull()
    expect(item('Copy').getAttribute('data-disabled')).toBeNull()
  })

  it('select all stays inside the field and never reaches main', async () => {
    const contextMenuEdit = vi.fn().mockResolvedValue(undefined)

    installBridge({ contextMenuEdit: contextMenuEdit as unknown as Window['hermesDesktop']['contextMenuEdit'] })
    mountMenu()
    const host = attach('<textarea>alpha beta gamma</textarea>')
    const textarea = host.querySelector('textarea')!

    fireEvent.contextMenu(textarea)
    fireEvent.click(await screen.findByText('Select all'))

    // Renderer-side selection scoped to the field: main's selectAll acts on
    // the focused FRAME and selected the whole transcript when focus
    // slipped (the edit composer re-parents focus on blur).
    await waitFor(() => {
      expect(textarea.selectionStart).toBe(0)
      expect(textarea.selectionEnd).toBe('alpha beta gamma'.length)
    })
    expect(contextMenuEdit).not.toHaveBeenCalled()
    expect(document.activeElement).toBe(textarea)
  })

  it('grays out cut, copy, and select all in an empty field', async () => {
    installBridge()
    mountMenu()
    const host = attach('<textarea></textarea>')

    fireEvent.contextMenu(host.querySelector('textarea')!)

    const item = (label: string) => screen.getByText(label).closest('[data-slot="dropdown-menu-item"]') as HTMLElement

    await screen.findByText('Select all')

    expect(item('Cut').getAttribute('data-disabled')).not.toBeNull()
    expect(item('Copy').getAttribute('data-disabled')).not.toBeNull()
    expect(item('Select all').getAttribute('data-disabled')).not.toBeNull()
  })

  it('keeps paste clickable even when the clipboard probe reports empty', async () => {
    // #91553: the dom paste action runs webContents.paste() in main — the
    // same path Ctrl+V takes, which resolves the system clipboard itself.
    // A renderer-side probe that comes back empty (as the Win32
    // clipboard.readText() bridge can while that path succeeds) must not
    // gray the item out; pasting on a truly empty clipboard is a no-op.
    const readClipboard = vi.fn().mockResolvedValue('')

    installBridge({ readClipboard: readClipboard as unknown as Window['hermesDesktop']['readClipboard'] })
    mountMenu()
    const host = attach('<textarea>text</textarea>')

    fireEvent.contextMenu(host.querySelector('textarea')!)

    const pasteItem = (await screen.findByText('Paste')).closest('[data-slot="dropdown-menu-item"]')!

    expect(pasteItem.getAttribute('data-disabled')).toBeNull()
  })

  it('keeps paste clickable when the bridge has no clipboard read at all', async () => {
    installBridge()
    mountMenu()
    const host = attach('<textarea>text</textarea>')

    fireEvent.contextMenu(host.querySelector('textarea')!)

    const pasteItem = (await screen.findByText('Paste')).closest('[data-slot="dropdown-menu-item"]')!

    expect(pasteItem.getAttribute('data-disabled')).toBeNull()
  })

  it('offers the window verbs on bare chrome', async () => {
    installBridge()
    mountMenu()
    const host = attach('<div><p>plain chrome</p></div>')

    fireEvent.contextMenu(host.querySelector('p')!)

    expect(await screen.findByText('Settings')).toBeTruthy()
  })

  it('skips plain right-clicks inside a skip-marked surface, but not links in it', async () => {
    installBridge()
    mountMenu()

    const host = attach(
      '<div data-context-menu-skip=""><p>bubble text</p><a href="https://example.com/in">In-bubble</a></div>'
    )

    fireEvent.contextMenu(host.querySelector('p')!)
    expect($contextMenu.get()).toBeNull()

    fireEvent.contextMenu(host.querySelector('a')!)
    expect(await screen.findByText('Copy URL')).toBeTruthy()
  })

  it('lets editable targets inside a radix surface use the edit menu', async () => {
    installBridge()
    mountMenu()

    const host = attach(
      `<div data-zone-body="test" data-slot="context-menu-trigger"><textarea>draft text</textarea></div>`
    )

    const textarea = host.querySelector('textarea')!

    fireEvent.contextMenu(textarea)

    expect(await screen.findByText('Select all')).toBeTruthy()
    expect(screen.getByText('Paste')).toBeTruthy()
  })

  it('leaves surfaces with their own radix menu alone', () => {
    installBridge()
    mountMenu()
    const host = attach('<div data-slot="context-menu-trigger"><span>session row</span></div>')

    fireEvent.contextMenu(host.querySelector('span')!)

    expect($contextMenu.get()).toBeNull()
  })

  it('shows the terminal menu through a registered handle', async () => {
    installBridge()
    mountMenu()
    const host = attach('<div data-terminal=""><canvas></canvas></div>')
    const paste = vi.fn()

    const unregister = registerTerminalContextMenu(host.querySelector('[data-terminal]')!, {
      getSelection: () => 'picked text',
      paste,
      reload: vi.fn(),
      selectAll: vi.fn(),
      wordErase: null
    })

    fireEvent.contextMenu(host.querySelector('canvas')!)

    expect(await screen.findByText('Copy')).toBeTruthy()
    expect(screen.getByText('Paste')).toBeTruthy()
    expect(screen.getByText('Select all')).toBeTruthy()
    unregister()
  })

  it('hides paste on the read-only agent terminal', async () => {
    installBridge()
    mountMenu()
    const host = attach('<div data-terminal=""><canvas></canvas></div>')

    const unregister = registerTerminalContextMenu(host.querySelector('[data-terminal]')!, {
      getSelection: () => '',
      paste: null,
      reload: vi.fn(),
      selectAll: vi.fn(),
      wordErase: null
    })

    fireEvent.contextMenu(host.querySelector('canvas')!)

    expect(await screen.findByText('Select all')).toBeTruthy()
    expect(screen.queryByText('Paste')).toBeNull()
    unregister()
  })
})

describe('AppContextMenu guest (in-app browser)', () => {
  const guestHandle = (overrides: Partial<GuestMenuHandle> = {}): GuestMenuHandle => ({
    addToDictionary: vi.fn(),
    copyImage: vi.fn(),
    editCommand: vi.fn(),
    inspectElement: vi.fn(),
    replaceMisspelling: vi.fn(),
    ...overrides
  })

  const guestParams = (overrides: Partial<GuestMenuParams> = {}): GuestMenuParams => ({
    dictionarySuggestions: [],
    editFlags: { canCopy: true, canCut: true, canPaste: true, canSelectAll: true },
    hasImageContents: false,
    isEditable: false,
    linkURL: '',
    misspelledWord: '',
    selectionText: '',
    srcURL: '',
    ...overrides
  })

  it('shows text tools and inspect on a bare page right-click', async () => {
    installBridge()
    mountMenu()
    const guest = guestHandle()

    openGuestContextMenu(10, 10, guestParams(), guest)

    expect(await screen.findByText('Select all')).toBeTruthy()
    expect(screen.getByText('Inspect element')).toBeTruthy()
  })

  it('runs inspect element against the handle', async () => {
    installBridge()
    mountMenu()
    const guest = guestHandle()

    openGuestContextMenu(10, 10, guestParams(), guest)
    fireEvent.click(await screen.findByText('Inspect element'))

    expect(guest.inspectElement).toHaveBeenCalled()
  })

  it('adds a link section above the tools for guest links', async () => {
    installBridge()
    mountMenu()

    openGuestContextMenu(10, 10, guestParams({ linkURL: 'https://example.com/deep' }), guestHandle())

    expect(await screen.findByText('Open in in-app browser')).toBeTruthy()
    expect(screen.getByText('Copy URL')).toBeTruthy()
    expect(screen.getByText('Inspect element')).toBeTruthy()
  })

  it('grays out guest edit verbs from Chromium editFlags', async () => {
    installBridge()
    mountMenu()

    // An empty input: Chromium reports nothing to cut/copy/select, paste ok.
    openGuestContextMenu(
      10,
      10,
      guestParams({
        editFlags: { canCopy: false, canCut: false, canPaste: true, canSelectAll: false },
        isEditable: true
      }),
      guestHandle()
    )

    const item = (label: string) => screen.getByText(label).closest('[data-slot="dropdown-menu-item"]') as HTMLElement

    await screen.findByText('Select all')

    expect(item('Cut').getAttribute('data-disabled')).not.toBeNull()
    expect(item('Copy').getAttribute('data-disabled')).not.toBeNull()
    expect(item('Select all').getAttribute('data-disabled')).not.toBeNull()
    expect(item('Paste').getAttribute('data-disabled')).toBeNull()
  })

  it('dispatches guest edit verbs a frame after the menu closes', async () => {
    installBridge()
    mountMenu()
    const guest = guestHandle()

    openGuestContextMenu(10, 10, guestParams(), guest)
    fireEvent.click(await screen.findByText('Select all'))

    // Deferred past the radix unmount so the webview focus() is not stolen
    // back — dispatching with host focus selected the address bar + chat.
    expect(guest.editCommand).not.toHaveBeenCalled()

    await waitFor(() => expect(guest.editCommand).toHaveBeenCalledWith('selectAll'))
    expect($contextMenu.get()).toBeNull()
  })

  it('adds an image section with copy and save for guest images', async () => {
    installBridge()
    mountMenu()

    openGuestContextMenu(
      10,
      10,
      guestParams({ hasImageContents: true, srcURL: 'https://example.com/pic.png' }),
      guestHandle()
    )

    expect(await screen.findByText('Copy image')).toBeTruthy()
    expect(screen.getByText('Save image as…')).toBeTruthy()
  })

  it('shows spell suggestions immediately for guest editables', async () => {
    installBridge()
    mountMenu()
    const guest = guestHandle()

    openGuestContextMenu(
      10,
      10,
      guestParams({ dictionarySuggestions: ['the'], isEditable: true, misspelledWord: 'teh' }),
      guest
    )

    fireEvent.click(await screen.findByText('the'))

    expect(guest.replaceMisspelling).toHaveBeenCalledWith('the')
    expect(screen.queryByText('Add to dictionary')).toBeNull()
  })
})

describe('ContextMenuTrigger asChild', () => {
  it('keeps the coordinator marker when the child overwrites data-slot', () => {
    render(
      <ContextMenu>
        <ContextMenuTrigger asChild>
          <footer data-slot="statusbar">bar</footer>
        </ContextMenuTrigger>
      </ContextMenu>
    )

    const footer = screen.getByText('bar')

    expect(footer.getAttribute('data-slot')).toBe('statusbar')
    expect(footer.hasAttribute(HERMES_CONTEXT_MENU_TRIGGER_ATTR)).toBe(true)
  })
})