Files
shumai/packages/webui/components/chat/message-input.test.tsx
T
Yiling-J e06478df88 feat(chat): support markups, attachments, and timestamps in agent 1-to-1 chat (#368)
* feat(chat): support markups, attachments, and timestamps in agent 1-to-1 chat

* feat(chat): improve message card layout with inline badges and integrated assets

* fix(chat): enable image attachment thumbnails in 1-to-1 chat cards

* fix(chat): address code review feedback on input state, mentions, i18n, and session deletion

* fix(chat): replace ring with border color on selected message card
2026-08-31 20:11:27 +08:00

144 lines
5.0 KiB
TypeScript

// @vitest-environment happy-dom
import React from 'react'
import { cleanup, render, fireEvent, screen } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ChatInput } from './message-input'
import { useMemberStore } from '@/ui/stores/members'
describe('ChatInput mention navigation', () => {
afterEach(() => {
cleanup()
vi.clearAllMocks()
})
it('selects the correct member item when the Agents section is collapsed', async () => {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
})
useMemberStore.setState({
members: [
{ id: 'agent-1', name: 'Bot Alice', type: 'agent', role: 'bot' },
{ id: 'agent-2', name: 'Bot Bob', type: 'agent', role: 'bot' },
{ id: 'user-1', name: 'Carol Human', type: 'human', role: 'editor' },
{ id: 'user-2', name: 'David Human', type: 'human', role: 'editor' },
],
loading: false,
fetchMembers: vi.fn(),
fetchProjectMembers: vi.fn(),
})
const mockSendMessage = vi.fn()
const { container, getByText } = render(
<QueryClientProvider client={queryClient}>
<ChatInput projectId="proj-1" allowMentions={true} onSendMessage={mockSendMessage} />
</QueryClientProvider>,
)
const editor = container.querySelector('[contenteditable="true"]') as HTMLDivElement
expect(editor).toBeTruthy()
// Type @ and set cursor selection in DOM
const textNode = document.createTextNode('@')
editor.appendChild(textNode)
const range = document.createRange()
range.setStart(textNode, 1)
range.setEnd(textNode, 1)
const sel = window.getSelection()!
sel.removeAllRanges()
sel.addRange(range)
fireEvent.input(editor)
// Mention list should appear with Agents and Members headers
expect(getByText('Agents')).toBeTruthy()
expect(getByText('Members')).toBeTruthy()
// Collapse the Agents section
const agentsHeaderBtn = getByText('Agents').closest('button')!
fireEvent.click(agentsHeaderBtn)
// Agents should no longer be rendered, but Members should be visible
expect(screen.queryByText('Bot Alice')).toBeNull()
const carolBtn = getByText('Carol Human').closest('button')!
expect(carolBtn).toBeTruthy()
// Carol Human should have data-index="0" because bots are collapsed
expect(carolBtn.getAttribute('data-index')).toBe('0')
// Hover over Carol Human
fireEvent.mouseEnter(carolBtn)
// Press Enter to select Carol Human
fireEvent.keyDown(editor, { key: 'Enter' })
// Editor should now contain the mention badge for Carol Human (user-1)
const mentionNode = editor.querySelector('[data-type="mention"]') as HTMLElement
expect(mentionNode).toBeTruthy()
expect(mentionNode.dataset.id).toBe('user-1')
expect(mentionNode.textContent).toContain('Carol Human')
})
it('navigates with ArrowDown and ArrowUp after clicking to collapse the Agents section', async () => {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
})
useMemberStore.setState({
members: [
{ id: 'agent-1', name: 'Bot Alice', type: 'agent', role: 'bot' },
{ id: 'agent-2', name: 'Bot Bob', type: 'agent', role: 'bot' },
{ id: 'user-1', name: 'Carol Human', type: 'human', role: 'editor' },
{ id: 'user-2', name: 'David Human', type: 'human', role: 'editor' },
],
loading: false,
fetchMembers: vi.fn(),
fetchProjectMembers: vi.fn(),
})
const mockSendMessage = vi.fn()
const { container, getByText } = render(
<QueryClientProvider client={queryClient}>
<ChatInput projectId="proj-1" allowMentions={true} onSendMessage={mockSendMessage} />
</QueryClientProvider>,
)
const editor = container.querySelector('[contenteditable="true"]') as HTMLDivElement
expect(editor).toBeTruthy()
// Type @ and set cursor selection
const textNode = document.createTextNode('@')
editor.appendChild(textNode)
const range = document.createRange()
range.setStart(textNode, 1)
range.setEnd(textNode, 1)
const sel = window.getSelection()!
sel.removeAllRanges()
sel.addRange(range)
fireEvent.input(editor)
// Click collapse on Agents
const agentsHeaderBtn = getByText('Agents').closest('button')!
fireEvent.mouseDown(agentsHeaderBtn)
fireEvent.click(agentsHeaderBtn)
// Now press ArrowDown on editor to move from item 0 (Carol) to item 1 (David)
fireEvent.keyDown(editor, { key: 'ArrowDown' })
const davidBtn = getByText('David Human').closest('button')!
expect(davidBtn.className).toContain('bg-accent')
// Press Enter to select David
fireEvent.keyDown(editor, { key: 'Enter' })
const mentionNode = editor.querySelector('[data-type="mention"]') as HTMLElement
expect(mentionNode).toBeTruthy()
expect(mentionNode.dataset.id).toBe('user-2')
expect(mentionNode.textContent).toContain('David Human')
})
})