Files
opencode-manager/frontend/src/components/session/SessionMessagePickerDialog.test.tsx
T
Chris Scott 7102715068 fix(commands): run multi-line and Enter commands, restore focus, refresh forks
- Match command arguments across newlines so a multi-line /btw or custom
  command runs instead of being sent as plain text.
- Run a typed command on plain Enter once its name is complete, while
  Enter stays a new line for regular text and Shift+Enter for commands.
- Focus the prompt when a session opens on a pointer device, and return
  focus to the previously focused element when a dialog closes, except a
  text field on touch so the keyboard stays closed.
- Invalidate the session list on session.forked so a forked session shows
  in the sidebar without waiting for the next event.
- Correct the Quick Start keyboard shortcuts.
2026-10-02 10:45:20 -04:00

142 lines
4.3 KiB
TypeScript

import { describe, it, expect, vi, afterEach } from 'vitest'
import { stubMatchMedia } from '@/test/test-utils'
import { useState } from 'react'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import type { SessionMessageInfo } from '@opencode-manager/shared/opencode'
import { SessionMessagePickerDialog } from './SessionMessagePickerDialog'
function userMessage(id: string, text: string, created = 1000): SessionMessageInfo {
return { id, type: 'user', text, time: { created } } as SessionMessageInfo
}
function assistantMessage(id: string, text: string): SessionMessageInfo {
return { id, type: 'assistant', text, time: { created: 1000 } } as unknown as SessionMessageInfo
}
const renderPicker = (
props: Partial<React.ComponentProps<typeof SessionMessagePickerDialog>> = {},
) =>
render(
<SessionMessagePickerDialog
open
onOpenChange={vi.fn()}
title="Fork session"
messages={[]}
onSelect={vi.fn()}
{...props}
/>,
)
function FocusHost() {
const [open, setOpen] = useState(false)
return (
<>
<textarea aria-label="prompt" />
<button type="button" onClick={() => setOpen(true)}>open</button>
{open && (
<SessionMessagePickerDialog
open
onOpenChange={(next) => {
if (!next) setOpen(false)
}}
title="Fork session"
messages={[]}
onSelect={vi.fn()}
/>
)}
</>
)
}
describe('SessionMessagePickerDialog', () => {
afterEach(() => {
Reflect.deleteProperty(window, 'matchMedia')
})
it('focuses its search on open and returns focus to the previous element on Escape', async () => {
stubMatchMedia(true)
render(<FocusHost />)
const prompt = screen.getByLabelText('prompt')
prompt.focus()
fireEvent.click(screen.getByText('open'))
const search = await screen.findByPlaceholderText('Search messages')
await waitFor(() => expect(search).toHaveFocus())
fireEvent.keyDown(search, { key: 'Escape' })
await waitFor(() => expect(prompt).toHaveFocus())
})
it('lists only user messages, newest first', () => {
renderPicker({
messages: [
userMessage('old', 'older prompt', 1000),
assistantMessage('assistant', 'reply'),
userMessage('new', 'newer prompt', 2000),
],
})
const older = screen.getByText('older prompt')
const newer = screen.getByText('newer prompt')
expect(screen.queryByText('reply')).not.toBeInTheDocument()
expect(
newer.compareDocumentPosition(older) & Node.DOCUMENT_POSITION_FOLLOWING,
).toBeTruthy()
})
it('narrows the rows with the search query', () => {
renderPicker({
messages: [userMessage('a', 'alpha prompt'), userMessage('b', 'beta prompt')],
})
expect(screen.getByText('alpha prompt')).toBeInTheDocument()
expect(screen.getByText('beta prompt')).toBeInTheDocument()
fireEvent.change(screen.getByPlaceholderText('Search messages'), {
target: { value: 'beta' },
})
expect(screen.queryByText('alpha prompt')).not.toBeInTheDocument()
expect(screen.getByText('beta prompt')).toBeInTheDocument()
})
it('calls onSelect with the message id when a row is clicked', () => {
const onSelect = vi.fn()
renderPicker({ messages: [userMessage('msg-1', 'hello')], onSelect })
fireEvent.click(screen.getByText('hello'))
expect(onSelect).toHaveBeenCalledWith('msg-1')
})
it('calls onSelect with undefined from the leading row', () => {
const onSelect = vi.fn()
renderPicker({
messages: [userMessage('msg-1', 'hello')],
leadingOptionLabel: 'Entire conversation',
onSelect,
})
fireEvent.click(screen.getByText('Entire conversation'))
expect(onSelect).toHaveBeenCalledWith(undefined)
})
it('shows an empty state when no messages match', () => {
renderPicker({ messages: [assistantMessage('a', 'reply')] })
expect(screen.getByText('No messages')).toBeInTheDocument()
})
it('shows a loading state instead of the rows while loading', () => {
renderPicker({ messages: [userMessage('msg-1', 'hello')], loading: true })
expect(screen.getByText('Loading messages…')).toBeInTheDocument()
expect(screen.queryByText('hello')).not.toBeInTheDocument()
expect(screen.queryByText('No messages')).not.toBeInTheDocument()
})
})