Skip to content

The agent that grows with you

test(desktop): cover shared composer focus and draft ownership

brooklyn!784d5c3f9c2c
diff --git a/apps/desktop/src/app/chat/composer/floating-surface.test.tsx b/apps/desktop/src/app/chat/composer/floating-surface.test.tsx
new file mode 100644
index 00000000000..cb4d2137d4d
--- /dev/null
+++ b/apps/desktop/src/app/chat/composer/floating-surface.test.tsx
@@ -0,0 +1,126 @@
+import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
+import { useState } from 'react'
+import { afterEach, expect, it } from 'vitest'
+
+import { PaneGroupContext, PaneVisibleContext } from '@/components/pane-shell/pane-visibility'
+import { $composerPopout, setComposerPoppedOut } from '@/store/composer-popout'
+
+import { FloatingComposerSurface } from './floating-surface'
+import { claimFloatingComposer, pinFloatingComposerCapture } from './floating-target'
+import { getActiveComposer, markActiveComposer } from './focus'
+import { ComposerScopeProvider, ComposerSurfaceProvider, MAIN_COMPOSER_SCOPE } from './scope'
+
+function Surface({ id, visible = true, groupId = id }: { id: string; visible?: boolean; groupId?: string }) {
+ return (
+ <PaneGroupContext value={groupId}>
+ <PaneVisibleContext value={visible}>
+ <ComposerScopeProvider value={{ ...MAIN_COMPOSER_SCOPE, target: id }}>
+ <ComposerSurfaceProvider value={id}>
+ <div
+ data-chat-surface=""
+ data-composer-surface-id={id}
+ data-testid={`pane-${id}`}
+ data-tree-group={groupId}
+ >
+ <button>Focus {id}</button>
+ <FloatingComposerSurface>
+ <input aria-label={`Draft ${id}`} defaultValue={id} />
+ </FloatingComposerSurface>
+ </div>
+ </ComposerSurfaceProvider>
+ </ComposerScopeProvider>
+ </PaneVisibleContext>
+ </PaneGroupContext>
+ )
+}
+
+afterEach(async () => {
+ cleanup()
+ await act(async () => {})
+ $composerPopout.set({ poppedOut: false, position: { bottom: 24, right: 24 } })
+})
+
+it('shares one visible composer while preserving each editor and draft across hover, focus and docking', () => {
+ render(
+ <>
+ <Surface id="a" />
+ <Surface id="b" />
+ </>
+ )
+ const a = screen.getByLabelText('Draft a')
+ const b = screen.getByLabelText('Draft b')
+ fireEvent.change(a, { target: { value: 'draft for a' } })
+ fireEvent.change(b, { target: { value: 'draft for b' } })
+ const hostA = a.closest<HTMLElement>('[data-composer-owner]')!
+ const hostB = b.closest<HTMLElement>('[data-composer-owner]')!
+ markActiveComposer('b')
+ act(() => claimFloatingComposer('a'))
+ expect(getActiveComposer()).toBe('a')
+ act(() => setComposerPoppedOut(true))
+ expect(hostA.parentElement).toBe(globalThis.document.body)
+ expect(hostA.style.display).toBe('contents')
+ expect(hostB.style.display).toBe('none')
+ fireEvent.pointerMove(screen.getByTestId('pane-b'), { clientX: 500, clientY: 100 })
+ expect(hostA.style.display).toBe('none')
+ expect(hostB.style.display).toBe('contents')
+ fireEvent.pointerOver(b)
+ expect(hostB.style.display).toBe('contents')
+ let releaseCapture!: () => void
+ act(() => {
+ releaseCapture = pinFloatingComposerCapture('b')
+ })
+ fireEvent.pointerMove(screen.getByTestId('pane-a'), { clientX: 100, clientY: 100 })
+ expect(hostB.style.display).toBe('contents')
+ expect(getActiveComposer()).toBe('b')
+ act(() => releaseCapture())
+ fireEvent.keyDown(globalThis.document.body, { key: 'Tab' })
+ fireEvent.focusIn(screen.getByRole('button', { name: 'Focus a' }))
+ fireEvent.keyUp(globalThis.document.body, { key: 'Tab' })
+ expect(hostA.style.display).toBe('contents')
+ expect(hostB.style.display).toBe('none')
+ act(() => setComposerPoppedOut(false))
+ expect(screen.getByLabelText('Draft a')).toBe(a)
+ expect(screen.getByLabelText('Draft b')).toBe(b)
+ expect((a as HTMLInputElement).value).toBe('draft for a')
+ expect((b as HTMLInputElement).value).toBe('draft for b')
+ expect(hostA.closest('[data-chat-surface]')).toBe(screen.getByTestId('pane-a'))
+ expect(hostB.closest('[data-chat-surface]')).toBe(screen.getByTestId('pane-b'))
+})
+
+it.each([false, true])('keeps pointer-selected ownership through delayed events (floating=%s)', floating => {
+ render(
+ <>
+ <Surface id="a" />
+ <Surface id="b" />
+ </>
+ )
+ act(() => setComposerPoppedOut(floating))
+ const paneB = screen.getByTestId('pane-b')
+ fireEvent.pointerOver(paneB, { clientX: 500, clientY: 100 })
+ fireEvent.pointerMove(paneB, { clientX: 500, clientY: 100 })
+ expect(getActiveComposer()).toBe('b')
+
+ fireEvent.pointerOver(screen.getByTestId('pane-a'), { clientX: 500, clientY: 100 })
+ fireEvent.focusIn(screen.getByRole('button', { name: 'Focus a' }))
+ expect(getActiveComposer()).toBe('b')
+})
+
+it('hands the floating recipient to a visible tab when its old tab hides or closes', async () => {
+ function Tabs() {
+ const [first, setFirst] = useState(true)
+
+ return (
+ <>
+ <button onClick={() => setFirst(false)}>Switch</button>
+ <Surface groupId="tabs" id="a" visible={first} />
+ <Surface groupId="tabs" id="b" visible={!first} />
+ </>
+ )
+ }
+
+ render(<Tabs />)
+ act(() => setComposerPoppedOut(true))
+ await act(async () => fireEvent.click(screen.getByText('Switch')))
+ expect(screen.getByLabelText('Draft b').closest<HTMLElement>('[data-composer-owner]')!.style.display).toBe('contents')
+ expect(screen.queryByLabelText('Draft a')).not.toBeNull()
+})
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx
index 90a93a6e596..853faa0a512 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx
@@ -441,4 +441,46 @@ describe('useComposerDraft — a hidden keep-alive tab never auto-focuses its co
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()
+ })
})
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-popout.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-popout.test.tsx
index 91721c8e0bd..37167ccd390 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-popout.test.tsx
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-popout.test.tsx
@@ -2,7 +2,7 @@ import { cleanup, render, screen } from '@testing-library/react'
import { useRef } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
-import { $composerPopoutGesturesEnabled, $composerPopoutZones } from '@/store/composer-popout'
+import { $composerPopout, $composerPopoutGesturesEnabled } from '@/store/composer-popout'
import { useComposerPopout } from './use-composer-popout'
@@ -12,7 +12,10 @@ vi.mock('@/components/pane-shell/pane-visibility', () => ({
}))
vi.mock('@/hooks/use-resize-observer', () => ({ useResizeObserver: () => undefined }))
-vi.mock('@/store/windows', () => ({ isSecondaryWindow: () => false }))
+vi.mock('@/store/windows', async importOriginal => ({
+ ...(await importOriginal<Record<string, unknown>>()),
+ isSecondaryWindow: () => false
+}))
function PopoutAffordanceHarness() {
const composerRef = useRef<HTMLFormElement>(null)
@@ -25,13 +28,13 @@ function PopoutAffordanceHarness() {
describe('useComposerPopout', () => {
beforeEach(() => {
- $composerPopoutZones.set({})
+ $composerPopout.set({ poppedOut: false, position: { bottom: 24, right: 24 } })
$composerPopoutGesturesEnabled.set(true)
})
afterEach(() => {
cleanup()
- $composerPopoutZones.set({})
+ $composerPopout.set({ poppedOut: false, position: { bottom: 24, right: 24 } })
$composerPopoutGesturesEnabled.set(true)
})
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-popout-drag.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-popout-drag.test.tsx
index fbc4eaefce6..5c0465d6ade 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-popout-drag.test.tsx
+++ b/apps/desktop/src/app/chat/composer/hooks/use-popout-drag.test.tsx
@@ -9,7 +9,6 @@ function GestureHarness({ onPopOut }: { onPopOut: () => void }) {
const { onPointerDown } = useComposerPopoutGestures({
composerRef,
- groupId: 'test-zone',
onDock: vi.fn(),
onPopOut,
poppedOut: false,
diff --git a/apps/desktop/src/store/composer-popout-preference.test.ts b/apps/desktop/src/store/composer-popout-preference.test.ts
index ab5865bd844..fe6cdd6d300 100644
--- a/apps/desktop/src/store/composer-popout-preference.test.ts
+++ b/apps/desktop/src/store/composer-popout-preference.test.ts
@@ -1,7 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
const GESTURES_KEY = 'hermes.desktop.composerPopout.gesturesEnabled'
-const LEGACY_ENABLED_KEY = 'hermes.desktop.composerPopout.enabled'
const ZONES_KEY = 'hermes.desktop.composerPopout.zones.v1'
const loadStore = () => import('./composer-popout')
@@ -12,34 +11,26 @@ describe('composer pop-out preference', () => {
vi.resetModules()
})
- it('docks every floating zone, preserves positions, and persists the lock', async () => {
+ it('docks the shared composer, preserves its placement, and persists the lock', async () => {
const first = await loadStore()
- first.setComposerPopoutPosition('left', { bottom: 100, right: 100 })
- first.setComposerPoppedOut('left', true)
- first.setComposerPopoutPosition('right', { bottom: 200, right: 200 })
- first.setComposerPoppedOut('right', true)
+ first.setComposerPopoutPosition({ bottom: 200, right: 200 })
+ first.setComposerPoppedOut(true)
first.setComposerPopoutGesturesEnabled(false)
expect(first.$composerPopoutGesturesEnabled.get()).toBe(false)
- expect(first.getComposerPopoutZone('left')).toEqual({
- poppedOut: false,
- position: { bottom: 100, right: 100 }
- })
- expect(first.getComposerPopoutZone('right')).toEqual({
+ expect(first.$composerPopout.get()).toEqual({
poppedOut: false,
position: { bottom: 200, right: 200 }
})
expect(window.localStorage.getItem(GESTURES_KEY)).toBe('false')
- expect(window.localStorage.getItem(LEGACY_ENABLED_KEY)).toBe('false')
vi.resetModules()
const reloaded = await loadStore()
expect(reloaded.$composerPopoutGesturesEnabled.get()).toBe(false)
- expect(reloaded.getComposerPopoutZone('left').poppedOut).toBe(false)
- expect(reloaded.getComposerPopoutZone('right').poppedOut).toBe(false)
+ expect(reloaded.$composerPopout.get()).toEqual(first.$composerPopout.get())
})
it('normalizes stale floating zones when the persisted preference is disabled', async () => {
@@ -51,7 +42,7 @@ describe('composer pop-out preference', () => {
const store = await loadStore()
- expect(store.getComposerPopoutZone('stale')).toEqual({
+ expect(store.$composerPopout.get()).toEqual({
poppedOut: false,
position: { bottom: 48, right: 64 }
})
diff --git a/apps/desktop/src/store/composer-popout.test.ts b/apps/desktop/src/store/composer-popout.test.ts
index dd551fa4235..ec73b16d801 100644
--- a/apps/desktop/src/store/composer-popout.test.ts
+++ b/apps/desktop/src/store/composer-popout.test.ts
@@ -1,12 +1,10 @@
import { beforeEach, describe, expect, it } from 'vitest'
import {
- $composerPopoutZones,
+ $composerPopout,
clampPopoutPosition,
- getComposerPopoutZone,
POPOUT_WIDTH_REM,
type PopoutBounds,
- pruneComposerPopoutZones,
setComposerPopoutPosition,
setComposerPoppedOut
} from './composer-popout'
@@ -22,9 +20,6 @@ const RIGHT_HALF: PopoutBounds = { bottom: VH, left: VW / 2, right: VW, top: 40
// The same drag intent, viewed from the left half.
const LEFT_HALF: PopoutBounds = { bottom: VH, left: 0, right: VW / 2, top: 40 }
-const LEFT_ZONE = 'g-left'
-const RIGHT_ZONE = 'g-right'
-
describe('clampPopoutPosition', () => {
it('keeps the whole box inside its surface, not just its corner', () => {
const { bottom, right } = clampPopoutPosition({ bottom: 24, right: 24 }, BOX, RIGHT_HALF)
@@ -78,53 +73,18 @@ describe('clampPopoutPosition', () => {
})
})
-describe('pop-out state is scoped to a layout zone', () => {
+describe('window-wide pop-out placement', () => {
beforeEach(() => {
- $composerPopoutZones.set({})
- })
-
- it('floats one zone without touching its neighbor', () => {
- setComposerPoppedOut(LEFT_ZONE, true)
-
- // Tabs in the left zone float; the right split stays docked. This is the
- // whole point of keying by zone: popping out on the left must not fling a
- // composer out of every other pane.
- expect(getComposerPopoutZone(LEFT_ZONE).poppedOut).toBe(true)
- expect(getComposerPopoutZone(RIGHT_ZONE).poppedOut).toBe(false)
- })
-
- it('keeps each zone position independent', () => {
- setComposerPopoutPosition(LEFT_ZONE, { bottom: 100, right: 100 })
- setComposerPopoutPosition(RIGHT_ZONE, { bottom: 24, right: 24 })
-
- expect(getComposerPopoutZone(LEFT_ZONE).position).toEqual({ bottom: 100, right: 100 })
- expect(getComposerPopoutZone(RIGHT_ZONE).position).toEqual({ bottom: 24, right: 24 })
+ $composerPopout.set({ poppedOut: false, position: { bottom: 24, right: 24 } })
})
- it('reports the default for a zone nobody has touched', () => {
- // Every tab in a fresh zone reads the same default, so a new split starts
- // docked at the default corner rather than inheriting a neighbor's drag.
- expect(getComposerPopoutZone('g-unseen')).toEqual({ poppedOut: false, position: { bottom: 24, right: 24 } })
- })
-
- it('does not let one zone float seed the zones split after it', () => {
- setComposerPoppedOut(LEFT_ZONE, true)
- setComposerPopoutPosition(LEFT_ZONE, { bottom: 200, right: 200 })
-
- // A zone created later is its own thing — it must not inherit the float
- // state of whichever zone happened to be touched first.
- expect(getComposerPopoutZone('g-split-later')).toEqual({
- poppedOut: false,
- position: { bottom: 24, right: 24 }
- })
- })
-
- it('drops state for zones that no longer exist', () => {
- setComposerPoppedOut(LEFT_ZONE, true)
- setComposerPoppedOut(RIGHT_ZONE, true)
-
- pruneComposerPopoutZones([LEFT_ZONE])
-
- expect(Object.keys($composerPopoutZones.get())).toEqual([LEFT_ZONE])
+ it('keeps the resting placement through docking and reopening', () => {
+ setComposerPopoutPosition({ bottom: 200, right: 200 })
+ setComposerPoppedOut(true)
+ const floating = $composerPopout.get()
+ setComposerPoppedOut(false)
+ expect($composerPopout.get().position).toEqual(floating.position)
+ setComposerPoppedOut(true)
+ expect($composerPopout.get()).toEqual(floating)
})
})