Skip to content

The agent that grows with you

30.3 KB
Raw Blame
import { act, cleanup, render, waitFor } from '@testing-library/react'
import { useLayoutEffect } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { PaneVisibleContext } from '@/components/pane-shell/pane-visibility'
import {
  $freshDraftKey,
  $restoredDraftNotice,
  announceGoneSessionDraft,
  announceNewSessionDraftKey,
  clearSessionDraft,
  type ComposerAttachment,
  dismissRestoredDraftNotice,
  mainComposerScope,
  NEW_SESSION_DRAFT_KEY,
  rotateFreshDraftKey,
  stashSessionDraft,
  takeSessionDraft
} from '@/store/composer'
import { $connection } from '@/store/session'

import { useComposerActions } from '../../hooks/use-composer-actions'
import type { QueueEditState } from '../composer-utils'
import { type ComposerTarget, getActiveComposer, markActiveComposer } from '../focus'
import { caretOffsetInEditor, composerPlainText } from '../rich-editor'
import { type ComposerScope, ComposerScopeProvider, MAIN_COMPOSER_SCOPE } from '../scope'

import { useComposerDraft } from './use-composer-draft'

const mockComposerApi = { setText: vi.fn() }

vi.mock('@assistant-ui/react', () => ({
  useAui: () => ({ composer: () => mockComposerApi }),
  useAuiState: (selector: (state: { composer: { text: string } }) => unknown) => selector({ composer: { text: '' } }),
  useComposerRuntime: () => ({
    getState: () => ({ text: '' }),
    subscribe: () => () => undefined
  })
}))

interface ProbeHarnessProps {
  activeQueueSessionKey: string | null
  onLayoutSnapshot: (attachments: ComposerAttachment[]) => void
  /** Optional pre-paint text probe: called with the composer's mirrored text in the layout phase. */
  onTextSnapshot?: (text: string) => void
  sessionId: string
}

function ProbeHarness({ activeQueueSessionKey, onLayoutSnapshot, onTextSnapshot, sessionId }: ProbeHarnessProps) {
  const { draftRef } = useComposerDraft({
    activeQueueSessionKey,
    focusKey: null,
    inputDisabled: false,
    queueEditRef: { current: null as QueueEditState | null },
    sessionId
  })

  // useLayoutEffect fires synchronously right after the DOM commit, BEFORE
  // the hook's per-thread scope-swap useEffect (a passive effect) has a
  // chance to swap attachmentScope.$attachments over to the new session. A
  // synchronous read here — the same read ChatBar's `attachments` prop
  // performs at render time — observes the OUTGOING session's attachments.
  useLayoutEffect(() => {
    onLayoutSnapshot(mainComposerScope.$attachments.get())
    onTextSnapshot?.(draftRef.current)
  })

  return null
}

describe('useComposerDraft — attachment scope stays coherent with the committed session on switch (#59305)', () => {
  afterEach(() => {
    cleanup()
    mainComposerScope.clear()
    clearSessionDraft('session-A')
    clearSessionDraft('session-B')

    // Fresh-draft lifecycles rotate per test; the afterEach must sweep the
    // whole map or one test's abandoned bucket leaks into the next.
    for (const scope of ['session-created', NEW_SESSION_DRAFT_KEY, $freshDraftKey.get()]) {
      clearSessionDraft(scope)
    }

    rotateFreshDraftKey()
    delete (window as unknown as { hermesDesktop?: unknown }).hermesDesktop
    vi.unstubAllGlobals()
    $connection.set(null)
  })

  it('clears the outgoing session attachments by the layout phase right after switching sessions', () => {
    const attachmentA: ComposerAttachment = { id: 'url-A', kind: 'url', label: 'A' }
    stashSessionDraft('session-A', 'hi from A', [attachmentA])

    const snapshots: ComposerAttachment[][] = []

    const { rerender } = render(
      <ProbeHarness activeQueueSessionKey="session-A" onLayoutSnapshot={s => snapshots.push(s)} sessionId="session-A" />
    )

    // Mount loads session A's stashed attachment into the (module-level) main
    // scope — confirms the fixture actually seeded the leak precondition.
    expect(mainComposerScope.$attachments.get()).toEqual([attachmentA])

    snapshots.length = 0 // drop the initial-mount snapshot; only the switch matters

    act(() => {
      rerender(
        <ProbeHarness
          activeQueueSessionKey="session-B"
          onLayoutSnapshot={s => snapshots.push(s)}
          sessionId="session-B"
        />
      )
    })

    // By the layout phase the scope must already be B's (empty) — a submit
    // fired the instant B renders must never ship session A's attachment.
    expect(snapshots[0]).toEqual([])
  })

  it("swaps the draft TEXT before paint: the layout phase of session B never observes A's draft (#66662 review)", () => {
    // The attachment-scope test above pins the layout-phase guarantee for
    // chips; this pins it for the TEXT. The review on #62586 (carried through
    // #128476) observed that an assertion made after flushSync returns proves
    // the eventual swap but not that the previous draft could not be painted
    // first: a passive useEffect restore would let the browser paint session
    // B's view with session A's text still loaded. The deterministic pre-paint
    // probe is a useLayoutEffect that runs synchronously after the DOM commit —
    // exactly the last moment before the browser may paint. If the restore
    // lived in a passive effect, the probe would observe A's text here.
    stashSessionDraft('session-A', 'draft typed in session A', [])
    stashSessionDraft('session-B', 'draft typed in session B', [])

    const textSnapshots: string[] = []

    const { rerender } = render(
      <ProbeHarness
        activeQueueSessionKey="session-A"
        onLayoutSnapshot={() => undefined}
        onTextSnapshot={t => textSnapshots.push(t)}
        sessionId="session-A"
      />
    )

    // Mount restored A's draft — the seeded precondition, same as the
    // attachment test.
    expect(mockComposerApi.setText).toHaveBeenCalledWith('draft typed in session A')

    mockComposerApi.setText.mockClear()
    textSnapshots.length = 0

    act(() => {
      rerender(
        <ProbeHarness
          activeQueueSessionKey="session-B"
          onLayoutSnapshot={() => undefined}
          onTextSnapshot={t => textSnapshots.push(t)}
          sessionId="session-B"
        />
      )
    })

    // Layout phase of B's switch render: the draft must already be B's. A
    // passive (useEffect) restore leaves A's text in place at this instant —
    // the browser would paint it first.
    expect(textSnapshots[0]).toBe('draft typed in session B')

    // The restore also reached the composer core in the same commit.
    expect(mockComposerApi.setText).toHaveBeenCalledWith('draft typed in session B')

    clearSessionDraft('session-A')
    clearSessionDraft('session-B')
  })

  it('carries a pre-session draft onto the session the fresh chat is re-homed to, before its runtime id is known', () => {
    const preSessionAttachment: ComposerAttachment = {
      id: 'file:new',
      kind: 'file',
      label: 'new.txt',
      path: '/srv/hermes/profiles/owner/uploads/new.txt',
      stagedUpload: {
        install_id: '11111111111111111111111111111111',
        path: '/srv/hermes/profiles/owner/uploads/new.txt',
        profile_home: '/srv/hermes/profiles/owner'
      },
      titlePreview: 'Pasted text used to title the new session'
    }

    stashSessionDraft(null, 'do not lose this draft', [preSessionAttachment])

    const { rerender } = render(
      <ProbeHarness activeQueueSessionKey={null} onLayoutSnapshot={() => undefined} sessionId="" />
    )

    // Cold-start resume-last-session / first-send create: the route flips the
    // composer scope while `session.resume` has not published a runtime id yet.
    announceNewSessionDraftKey('session-created')
    act(() => {
      rerender(<ProbeHarness activeQueueSessionKey="session-created" onLayoutSnapshot={() => undefined} sessionId="" />)
    })

    expect(mainComposerScope.$attachments.get()).toEqual([preSessionAttachment])
    expect(takeSessionDraft('session-created')).toEqual({
      attachments: [preSessionAttachment],
      text: 'do not lose this draft'
    })
    expect(takeSessionDraft(null)).toEqual({ attachments: [], text: '' })
    clearSessionDraft('session-created')
  })

  it('carries the unsent draft of a GONE session into the fresh chat once, with an undoable notice (#111868)', () => {
    stashSessionDraft('session-gone', 'typed into a session that no longer exists', [])

    const { rerender } = render(
      <ProbeHarness activeQueueSessionKey="session-gone" onLayoutSnapshot={() => undefined} sessionId="session-gone" />
    )

    // The resume's gone verdict announces the dead key, then drops the window
    // to a fresh draft (route → /new, scope → the pre-session bucket).
    announceGoneSessionDraft('session-gone')
    act(() => {
      rerender(<ProbeHarness activeQueueSessionKey={null} onLayoutSnapshot={() => undefined} sessionId="" />)
    })

    expect(takeSessionDraft(null).text).toBe('typed into a session that no longer exists')
    expect(takeSessionDraft('session-gone').text).toBe('')
    expect($restoredDraftNotice.get()).toEqual({
      fromKey: 'session-gone',
      text: 'typed into a session that no longer exists'
    })

    // Fires once: a later trip through the fresh draft finds nothing to move
    // and does not re-publish the notice the user already dismissed.
    dismissRestoredDraftNotice()
    act(() => {
      rerender(
        <ProbeHarness activeQueueSessionKey="session-A" onLayoutSnapshot={() => undefined} sessionId="session-A" />
      )
    })
    act(() => {
      rerender(<ProbeHarness activeQueueSessionKey={null} onLayoutSnapshot={() => undefined} sessionId="" />)
    })

    expect($restoredDraftNotice.get()).toBeNull()
    expect(takeSessionDraft(null).text).toBe('typed into a session that no longer exists')
    clearSessionDraft(null)
  })

  it('leaves the pre-session draft in its bucket when the user opens another session from a fresh chat', () => {
    stashSessionDraft(null, 'still composing a new chat', [])

    const { rerender } = render(
      <ProbeHarness activeQueueSessionKey={null} onLayoutSnapshot={() => undefined} sessionId="" />
    )

    act(() => {
      rerender(
        <ProbeHarness activeQueueSessionKey="session-A" onLayoutSnapshot={() => undefined} sessionId="session-A" />
      )
    })

    expect(takeSessionDraft('session-A')).toEqual({ attachments: [], text: '' })
    expect(takeSessionDraft(null).text).toBe('still composing a new chat')
    clearSessionDraft(null)
  })

  it("isolates two concurrent new-chat lifecycles: the second fresh draft never shows the first one's text (#66662)", () => {
    const firstKey = rotateFreshDraftKey()
    const secondKey = rotateFreshDraftKey()

    expect(firstKey).not.toBe(secondKey)

    // First new chat: type unsent text under its own lifecycle key.
    stashSessionDraft(firstKey, 'first unsent chat', [])
    expect(takeSessionDraft(firstKey).text).toBe('first unsent chat')

    // A second New Chat rotated the key; its composer must restore empty —
    // the first chat's text is invisible until the user goes back.
    render(<ProbeHarness activeQueueSessionKey={secondKey} onLayoutSnapshot={() => undefined} sessionId="" />)

    expect(takeSessionDraft(secondKey)).toEqual({ attachments: [], text: '' })

    // The abandoned lifecycle keeps its text — no consumer of the second
    // lifecycle's scope can see or clobber it.
    expect(takeSessionDraft(firstKey).text).toBe('first unsent chat')

    clearSessionDraft(firstKey)
    clearSessionDraft(secondKey)
  })

  it("re-homes the ACTIVE lifecycle's draft onto the session its first send creates (#66662)", () => {
    const key = rotateFreshDraftKey()

    // The user typed in the current new chat; the swap cleanup stashed it
    // under the lifecycle key (null scope resolves to it).
    stashSessionDraft(null, 'typed before first send', [])

    const { rerender } = render(
      <ProbeHarness activeQueueSessionKey={key} onLayoutSnapshot={() => undefined} sessionId="" />
    )

    expect(takeSessionDraft(key).text).toBe('typed before first send')

    // First send: session.create assigns the stored id; the composer's scope
    // swap follows the announcement and moves THIS lifecycle's bucket.
    announceNewSessionDraftKey('session-created')
    act(() => {
      rerender(
        <ProbeHarness
          activeQueueSessionKey="session-created"
          onLayoutSnapshot={() => undefined}
          sessionId="session-created"
        />
      )
    })

    expect(takeSessionDraft('session-created').text).toBe('typed before first send')
    expect(takeSessionDraft(key).text).toBe('')

    clearSessionDraft('session-created')
  })

  it("keys a fresh chat's live stash under its lifecycle key, not the shared bucket (#66662)", () => {
    const key = rotateFreshDraftKey()

    const { unmount } = render(
      <ProbeHarness activeQueueSessionKey={key} onLayoutSnapshot={() => undefined} sessionId="" />
    )

    // Stash through the null scope the way the swap cleanup does when the
    // user types and navigates away mid-debounce.
    stashSessionDraft(null, 'typed in this lifecycle', [])

    expect(takeSessionDraft(key).text).toBe('typed in this lifecycle')
    expect(takeSessionDraft(NEW_SESSION_DRAFT_KEY).text).toBe('')

    unmount()
    clearSessionDraft(key)
  })

  it('applies a delayed image preview when it resolves while its attachment draft is inactive', async () => {
    const fullResolution =
      'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+GkZcAAAAASUVORK5CYII='

    const readFileDataUrl = vi.fn(async () => fullResolution)

    ;(
      window as unknown as {
        hermesDesktop: { readFileDataUrl: typeof readFileDataUrl }
      }
    ).hermesDesktop = { readFileDataUrl }

    let resolveBitmap!: (bitmap: { close: () => void; height: number; width: number }) => void

    vi.stubGlobal(
      'fetch',
      vi.fn(async () => ({ blob: async () => new Blob([new Uint8Array([0])], { type: 'image/png' }) }))
    )
    vi.stubGlobal(
      'createImageBitmap',
      vi.fn(
        () =>
          new Promise<{ close: () => void; height: number; width: number }>(resolve => {
            resolveBitmap = resolve
          })
      )
    )

    class MockOffscreenCanvas {
      getContext = () => ({ drawImage: vi.fn() })
      convertToBlob = vi.fn(async () => new Blob(['thumbnail'], { type: 'image/png' }))
      constructor(_width: number, _height: number) {}
    }

    vi.stubGlobal('OffscreenCanvas', MockOffscreenCanvas)

    let actions!: ReturnType<typeof useComposerActions>

    function PreviewHarness({ activeQueueSessionKey }: { activeQueueSessionKey: string }) {
      useComposerDraft({
        activeQueueSessionKey,
        focusKey: null,
        inputDisabled: false,
        queueEditRef: { current: null as QueueEditState | null },
        sessionId: activeQueueSessionKey
      })
      actions = useComposerActions({ activeSessionId: null, currentCwd: '', requestGateway: vi.fn() })

      return null
    }

    const { rerender } = render(<PreviewHarness activeQueueSessionKey="session-A" />)
    let pending!: Promise<boolean>

    act(() => {
      pending = actions.attachImagePath('/tmp/round-trip.png')
    })

    await waitFor(() => expect(createImageBitmap).toHaveBeenCalledOnce())

    act(() => rerender(<PreviewHarness activeQueueSessionKey="session-B" />))
    expect(mainComposerScope.$attachments.get()).toEqual([])

    resolveBitmap({ close: vi.fn(), height: 3000, width: 4000 })

    await act(async () => {
      await pending
    })

    // The late completion belongs to A and must not leak into active session B.
    expect(mainComposerScope.$attachments.get()).toEqual([])

    act(() => rerender(<PreviewHarness activeQueueSessionKey="session-A" />))

    expect(mainComposerScope.$attachments.get()[0]?.thumbnailUrl).toMatch(/^data:image\/png;base64,/)
  })
})

describe('useComposerDraft — rehydrate diagnostic log stays redacted', () => {
  afterEach(() => {
    cleanup()
    mainComposerScope.clear()
    vi.restoreAllMocks()
  })

  it('logs counts/kinds/scope on restore but never the raw url, refText, or label', () => {
    const secretUrl = 'https://secret.example.com/private-workspace-path'

    const attachment: ComposerAttachment = {
      id: 'url-secret',
      kind: 'url',
      label: 'do-not-leak-label',
      refText: `@url:${secretUrl}`
    }

    stashSessionDraft('session-secret', '', [attachment])

    const debugSpy = vi.spyOn(console, 'debug').mockImplementation(() => undefined)

    render(
      <ProbeHarness
        activeQueueSessionKey="session-secret"
        onLayoutSnapshot={() => undefined}
        sessionId="session-secret"
      />
    )

    const rehydrateCalls = debugSpy.mock.calls.filter(call => call[0] === '[composer-rehydrate]')
    expect(rehydrateCalls.length).toBeGreaterThan(0)

    const serialized = JSON.stringify(rehydrateCalls)
    expect(serialized).not.toContain(secretUrl)
    expect(serialized).not.toContain(attachment.label)
    expect(serialized).not.toContain(attachment.refText)

    expect(rehydrateCalls[0]?.[1]).toMatchObject({
      attachmentCount: 1,
      attachmentKinds: ['url'],
      scope: 'session-secret'
    })
  })
})

describe('useComposerDraft — draft survives full unmount (Settings navigation, #41079)', () => {
  afterEach(() => {
    cleanup()
    mainComposerScope.clear()
    clearSessionDraft('session-nav')
  })

  it('stashes the unsent draft on unmount and restores it on remount', () => {
    // The user typed but has not sent; the draft was stashed by the normal
    // typing debounce path at some earlier point.
    stashSessionDraft('session-nav', 'unsent thought', [])

    const { unmount } = render(
      <ProbeHarness activeQueueSessionKey="session-nav" onLayoutSnapshot={() => undefined} sessionId="session-nav" />
    )

    // Navigating to Settings unmounts the chat composer entirely. The swap
    // effect's cleanup must stash the loaded draft back under its scope —
    // NOT drop it with the React state.
    unmount()

    mockComposerApi.setText.mockClear()

    const remount = render(
      <ProbeHarness activeQueueSessionKey="session-nav" onLayoutSnapshot={() => undefined} sessionId="session-nav" />
    )

    // Remount restored the text into the composer core (setText mirrors
    // paintDraft's write path — the editor DOM isn't mounted in this harness).
    expect(mockComposerApi.setText).toHaveBeenCalledWith('unsent thought')

    remount.unmount()
  })
})

describe('useComposerDraft — same-draft scope rekey preserves only the focused caret (#120942)', () => {
  afterEach(() => {
    cleanup()
    mainComposerScope.clear()
    clearSessionDraft('lineage-root')
    clearSessionDraft('runtime-tip')
    markActiveComposer('main')
  })

  function renderDraft(scope: string) {
    let draft!: ReturnType<typeof useComposerDraft>

    function DraftHarness({ activeScope }: { activeScope: string }) {
      draft = useComposerDraft({
        activeQueueSessionKey: activeScope,
        focusKey: null,
        inputDisabled: false,
        queueEditRef: { current: null },
        sessionId: activeScope
      })

      return <div contentEditable data-slot="composer-rich-input" ref={draft.editorRef} tabIndex={0} />
    }

    const rendered = render(<DraftHarness activeScope={scope} />)

    return {
      get draft() {
        return draft
      },
      rerender: (activeScope: string) => rendered.rerender(<DraftHarness activeScope={activeScope} />)
    }
  }

  it('keeps the caret when reconciliation rekeys an unchanged focused draft', () => {
    stashSessionDraft('lineage-root', 'alpha beta', [])
    stashSessionDraft('runtime-tip', 'alpha beta', [])
    const view = renderDraft('lineage-root')
    const editor = view.draft.editorRef.current!

    editor.focus()
    const range = document.createRange()
    range.setStart(editor.firstChild!, 6)
    range.collapse(true)
    window.getSelection()?.removeAllRanges()
    window.getSelection()?.addRange(range)

    // Focus routing may transiently belong elsewhere during reconnect even
    // though this contentEditable is still the browser's active element.
    markActiveComposer('tile:reconnecting')
    act(() => view.rerender('runtime-tip'))

    expect(view.draft.editorRef.current).toBe(editor)
    expect(document.activeElement).toBe(editor)
    expect(composerPlainText(editor)).toBe('alpha beta')
    expect(caretOffsetInEditor(editor)).toBe(6)
  })

  it('still sends programmatic mutations to the end instead of preserving a stale caret', () => {
    stashSessionDraft('lineage-root', 'alpha beta', [])
    const view = renderDraft('lineage-root')
    const editor = view.draft.editorRef.current!

    editor.focus()
    const range = document.createRange()
    range.setStart(editor.firstChild!, 2)
    range.collapse(true)
    window.getSelection()?.removeAllRanges()
    window.getSelection()?.addRange(range)
    markActiveComposer('main')

    act(() => view.draft.insertText('gamma'))

    expect(composerPlainText(editor)).toBe('alpha beta\ngamma')
    expect(caretOffsetInEditor(editor)).toBe(composerPlainText(editor).length)
  })
})

describe('useComposerDraft — a closing composer hands the focus-bus key back', () => {
  afterEach(() => {
    cleanup()
    mainComposerScope.clear()
    markActiveComposer('main')
  })

  function renderScoped(target: ComposerTarget) {
    const scope: ComposerScope = { ...MAIN_COMPOSER_SCOPE, target }

    return render(
      <ComposerScopeProvider value={scope}>
        <ProbeHarness
          activeQueueSessionKey="session-tile"
          onLayoutSnapshot={() => undefined}
          sessionId="session-tile"
        />
      </ComposerScopeProvider>
    )
  }

  it('stops `active` resolving to a session tile once the tile unmounts', () => {
    const { unmount } = renderScoped('tile:abc')

    // Mounting claims the bus for this tile — the leak precondition.
    expect(getActiveComposer()).toBe('tile:abc')

    unmount()

    expect(getActiveComposer()).toBe('main')
  })

  it('leaves the key alone when another composer claimed it before this one unmounted', () => {
    const { unmount } = renderScoped('tile:abc')
    expect(getActiveComposer()).toBe('tile:abc')

    // The user clicks into a second tile, which claims the bus.
    markActiveComposer('tile:other')

    unmount()

    expect(getActiveComposer()).toBe('tile:other')
  })
})

describe('useComposerDraft — a hidden keep-alive tab never auto-focuses its composer', () => {
  afterEach(() => {
    cleanup()
    mainComposerScope.clear()
    markActiveComposer('main')
  })

  function renderScopedHidden(target: ComposerTarget, hidden: boolean) {
    const scope: ComposerScope = { ...MAIN_COMPOSER_SCOPE, target }

    return render(
      <PaneVisibleContext.Provider value={!hidden}>
        <ComposerScopeProvider value={scope}>
          <ProbeHarness
            activeQueueSessionKey="session-tile"
            onLayoutSnapshot={() => undefined}
            sessionId="session-tile"
          />
        </ComposerScopeProvider>
      </PaneVisibleContext.Provider>
    )
  }

  it('does not claim the focus bus when the composer mounts inside a hidden pane', () => {
    renderScopedHidden('tile:bg', true)

    expect(getActiveComposer()).toBe('main')
  })

  it('still claims the bus when the same composer becomes visible', () => {
    const { rerender } = renderScopedHidden('tile:fg', true)

    expect(getActiveComposer()).toBe('main')

    rerender(
      <PaneVisibleContext.Provider value={true}>
        <ComposerScopeProvider value={{ ...MAIN_COMPOSER_SCOPE, target: 'tile:fg' }}>
          <ProbeHarness
            activeQueueSessionKey="session-tile"
            onLayoutSnapshot={() => undefined}
            sessionId="session-tile"
          />
        </ComposerScopeProvider>
      </PaneVisibleContext.Provider>
    )

    expect(getActiveComposer()).toBe('tile:fg')
  })

  it('claims the bus on mount when visible', () => {
    renderScopedHidden('tile:vis', false)

    expect(getActiveComposer()).toBe('tile:vis')
  })

  function renderHiddenDraftHarness() {
    let hiddenDraft!: ReturnType<typeof useComposerDraft>

    function HiddenDraftHarness() {
      hiddenDraft = useComposerDraft({
        activeQueueSessionKey: 'session-hidden',
        focusKey: null,
        inputDisabled: false,
        queueEditRef: { current: null as QueueEditState | null },
        sessionId: 'session-hidden'
      })

      return <div contentEditable data-slot="composer-rich-input" ref={hiddenDraft.editorRef} />
    }

    render(
      <PaneVisibleContext.Provider value={false}>
        <ComposerScopeProvider value={{ ...MAIN_COMPOSER_SCOPE, target: 'tile:hidden' }}>
          <HiddenDraftHarness />
        </ComposerScopeProvider>
      </PaneVisibleContext.Provider>
    )

    return () => hiddenDraft
  }

  function createForegroundSelection() {
    const visibleEditor = globalThis.document.createElement('div')
    const visibleText = globalThis.document.createTextNode('foreground draft')
    visibleEditor.contentEditable = 'true'
    visibleEditor.tabIndex = 0
    visibleEditor.appendChild(visibleText)
    globalThis.document.body.appendChild(visibleEditor)
    visibleEditor.focus()
    expect(globalThis.document.activeElement).toBe(visibleEditor)

    const range = globalThis.document.createRange()
    range.setStart(visibleText, visibleText.textContent?.length ?? 0)
    range.collapse(true)
    window.getSelection()?.removeAllRanges()
    window.getSelection()?.addRange(range)

    return {
      editor: visibleEditor,
      startContainer: range.startContainer,
      startOffset: range.startOffset,
      endContainer: range.endContainer,
      endOffset: range.endOffset
    }
  }

  function expectForegroundSelectionPreserved(foreground: ReturnType<typeof createForegroundSelection>) {
    const selection = window.getSelection()
    expect(globalThis.document.activeElement).toBe(foreground.editor)
    expect(selection?.rangeCount).toBe(1)

    const range = selection!.getRangeAt(0)
    expect(range.startContainer).toBe(foreground.startContainer)
    expect(range.startOffset).toBe(foreground.startOffset)
    expect(range.endContainer).toBe(foreground.endContainer)
    expect(range.endOffset).toBe(foreground.endOffset)
  }

  it('does not move the document selection when a hidden composer reloads or clears its draft', () => {
    const getHiddenDraft = renderHiddenDraftHarness()
    const foreground = createForegroundSelection()

    act(() => getHiddenDraft().loadIntoComposer('background update', []))

    expect(composerPlainText(getHiddenDraft().editorRef.current!)).toBe('background update')
    expectForegroundSelectionPreserved(foreground)

    act(() => getHiddenDraft().clearDraft())

    expect(getHiddenDraft().editorRef.current?.textContent).toBe('')
    expectForegroundSelectionPreserved(foreground)
    foreground.editor.remove()
  })

  it('does not move the document selection when hidden composer refs are requested', () => {
    const getHiddenDraft = renderHiddenDraftHarness()
    const foreground = createForegroundSelection()

    act(() => getHiddenDraft().insertInlineRefs(['@file:`src/background.ts`']))

    expect(composerPlainText(getHiddenDraft().editorRef.current!)).toContain('@file:`src/background.ts`')
    expectForegroundSelectionPreserved(foreground)
    foreground.editor.remove()
  })

  it.each([false, true])('a late callback preserves another editor’s selection (hidden=%s)', hidden => {
    let draft!: ReturnType<typeof useComposerDraft>

    function Draft() {
      draft = useComposerDraft({
        activeQueueSessionKey: 'late-reject',
        focusKey: null,
        inputDisabled: false,
        queueEditRef: { current: null },
        sessionId: 'late-reject'
      })

      return <div contentEditable data-slot="composer-rich-input" ref={draft.editorRef} tabIndex={0} />
    }

    const { rerender } = render(
      <PaneVisibleContext value={true}>
        <Draft />
      </PaneVisibleContext>
    )

    const lateRestore = draft.loadIntoComposer
    const lateFocus = draft.focusInput
    rerender(
      <PaneVisibleContext value={!hidden}>
        <Draft />
      </PaneVisibleContext>
    )
    const foreground = createForegroundSelection()
    markActiveComposer('tile:foreground')

    act(() => {
      lateRestore('rejected draft', [])

      if (hidden) {
        lateFocus()
      }
    })

    expect(composerPlainText(draft.editorRef.current!)).toBe('rejected draft')
    expectForegroundSelectionPreserved(foreground)
    foreground.editor.remove()
  })
})

describe('useComposerDraft — touch never re-enters the editor on its own', () => {
  let draft!: ReturnType<typeof useComposerDraft>

  function TouchDraft({ disabled = false, sessionId }: { disabled?: boolean; sessionId: string }) {
    draft = useComposerDraft({
      activeQueueSessionKey: sessionId,
      focusKey: null,
      inputDisabled: disabled,
      queueEditRef: { current: null },
      sessionId
    })

    return <div contentEditable data-slot="composer-rich-input" ref={draft.editorRef} tabIndex={0} />
  }

  beforeEach(() => {
    // Touch handling is the browser-hosted Desktop's.
    window.document.documentElement.dataset.hermesDesktopHost = 'browser'
    vi.stubGlobal('matchMedia', vi.fn().mockReturnValue({ matches: true }))
  })

  afterEach(() => {
    cleanup()
    clearSessionDraft('touch-focus')
    clearSessionDraft('touch-clear')
    mainComposerScope.clear()
    markActiveComposer('main')
    delete window.document.documentElement.dataset.hermesDesktopHost
    vi.unstubAllGlobals()
  })

  it('keeps touch mount and turn completion from focusing the editor while accepting deliberate focus', () => {
    const { rerender } = render(<TouchDraft sessionId="touch-focus" />)
    expect(globalThis.document.activeElement).not.toBe(draft.editorRef.current)
    rerender(<TouchDraft disabled={true} sessionId="touch-focus" />)
    rerender(<TouchDraft sessionId="touch-focus" />)
    expect(globalThis.document.activeElement).not.toBe(draft.editorRef.current)
    act(() => draft.focusInput())
    expect(globalThis.document.activeElement).toBe(draft.editorRef.current)
  })

  // iOS treats a selection placed in a contenteditable during a tap as focus,
  // so clearing after Send must not put the caret back into a blurred editor.
  it('on touch, clears a sent draft without re-entering an editor whose keyboard was put away', () => {
    render(<TouchDraft sessionId="touch-clear" />)
    expect(getActiveComposer()).toBe('main')
    window.getSelection()?.removeAllRanges()

    act(() => draft.clearDraft())
    expect(window.getSelection()?.rangeCount).toBe(0)

    act(() => draft.focusInput())
    act(() => draft.clearDraft())
    expect(draft.editorRef.current?.contains(window.getSelection()?.anchorNode ?? null)).toBe(true)
  })
})