Merge remote-tracking branch 'origin/main' into feat/model-plugins-implementing

# Conflicts:
#	web/app/components/workflow/nodes/_base/components/variable/variable-label/base/variable-label.tsx
#	web/eslint-suppressions.json
This commit is contained in:
yyh
2026-03-12 15:26:16 +08:00
145 changed files with 10702 additions and 1064 deletions

View File

@ -2,6 +2,7 @@ import type { ComponentProps } from 'react'
import type { IChatItem } from '@/app/components/base/chat/chat/type'
import type { AgentLogDetailResponse } from '@/models/log'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import { useStore as useAppStore } from '@/app/components/app/store'
import { ToastContext } from '@/app/components/base/toast/context'
import { fetchAgentLogDetail } from '@/service/log'
import AgentLogDetail from '../detail'
@ -104,7 +105,7 @@ describe('AgentLogDetail', () => {
describe('Rendering', () => {
it('should show loading indicator while fetching data', async () => {
vi.mocked(fetchAgentLogDetail).mockReturnValue(new Promise(() => {}))
vi.mocked(fetchAgentLogDetail).mockReturnValue(new Promise(() => { }))
renderComponent()
@ -193,6 +194,18 @@ describe('AgentLogDetail', () => {
})
describe('Edge Cases', () => {
it('should not fetch data when app detail is unavailable', async () => {
vi.mocked(useAppStore).mockImplementationOnce(selector => selector({ appDetail: undefined } as never))
vi.mocked(fetchAgentLogDetail).mockResolvedValue(createMockResponse())
renderComponent()
await waitFor(() => {
expect(fetchAgentLogDetail).not.toHaveBeenCalled()
})
expect(screen.getByRole('status')).toBeInTheDocument()
})
it('should notify on API error', async () => {
vi.mocked(fetchAgentLogDetail).mockRejectedValue(new Error('API Error'))

View File

@ -139,4 +139,23 @@ describe('AgentLogModal', () => {
expect(mockProps.onCancel).toHaveBeenCalledTimes(1)
})
it('should ignore click-away before mounted state is set', () => {
vi.mocked(fetchAgentLogDetail).mockReturnValue(new Promise(() => {}))
let invoked = false
vi.mocked(useClickAway).mockImplementation((callback) => {
if (!invoked) {
invoked = true
callback(new Event('click'))
}
})
render(
<ToastContext.Provider value={{ notify: vi.fn(), close: vi.fn() } as React.ComponentProps<typeof ToastContext.Provider>['value']}>
<AgentLogModal {...mockProps} />
</ToastContext.Provider>,
)
expect(mockProps.onCancel).not.toHaveBeenCalled()
})
})

View File

@ -82,4 +82,9 @@ describe('ResultPanel', () => {
render(<ResultPanel {...mockProps} agentMode="react" />)
expect(screen.getByText('appDebug.agent.agentModeType.ReACT')).toBeInTheDocument()
})
it('should fallback to zero tokens when total_tokens is undefined', () => {
render(<ResultPanel {...mockProps} total_tokens={undefined} />)
expect(screen.getByText('0 Tokens')).toBeInTheDocument()
})
})

View File

@ -2,6 +2,7 @@ import { fireEvent, render, screen } from '@testing-library/react'
import * as React from 'react'
import { describe, expect, it, vi } from 'vitest'
import { BlockEnum } from '@/app/components/workflow/types'
import { useLocale } from '@/context/i18n'
import ToolCallItem from '../tool-call'
vi.mock('@/app/components/workflow/nodes/_base/components/editor/code-editor', () => ({
@ -17,6 +18,10 @@ vi.mock('@/app/components/workflow/block-icon', () => ({
default: ({ type }: { type: BlockEnum }) => <div data-testid="block-icon" data-type={type} />,
}))
vi.mock('@/context/i18n', () => ({
useLocale: vi.fn(() => 'en'),
}))
const mockToolCall = {
status: 'success',
error: null,
@ -41,6 +46,17 @@ describe('ToolCallItem', () => {
expect(screen.getByTestId('block-icon')).toHaveAttribute('data-type', BlockEnum.Tool)
})
it('should fallback to locale key with underscores when hyphenated key is missing', () => {
vi.mocked(useLocale).mockReturnValueOnce('en-US')
const fallbackLocaleToolCall = {
...mockToolCall,
tool_label: { en_US: 'Fallback Label' },
}
render(<ToolCallItem toolCall={fallbackLocaleToolCall} isLLM={false} />)
expect(screen.getByText('Fallback Label')).toBeInTheDocument()
})
it('should format time correctly', () => {
render(<ToolCallItem toolCall={mockToolCall} isLLM={false} />)
expect(screen.getByText('1.500 s')).toBeInTheDocument()
@ -54,13 +70,17 @@ describe('ToolCallItem', () => {
expect(screen.getByText('1 m 5.000 s')).toBeInTheDocument()
})
it('should format token count correctly', () => {
it('should format token count in K units', () => {
render(<ToolCallItem toolCall={mockToolCall} isLLM={true} tokens={1200} />)
expect(screen.getByText('1.2K tokens')).toBeInTheDocument()
})
it('should format token count without unit for small values', () => {
render(<ToolCallItem toolCall={mockToolCall} isLLM={true} tokens={800} />)
expect(screen.getByText('800 tokens')).toBeInTheDocument()
})
it('should format token count in M units', () => {
render(<ToolCallItem toolCall={mockToolCall} isLLM={true} tokens={1200000} />)
expect(screen.getByText('1.2M tokens')).toBeInTheDocument()
})

View File

@ -45,6 +45,7 @@ const pageNameEnrichmentPlugin = (): amplitude.Types.EnrichmentPlugin => {
execute: async (event: amplitude.Types.Event) => {
// Only modify page view events
if (event.event_type === '[Amplitude] Page Viewed' && event.event_properties) {
/* v8 ignore next @preserve */
const pathname = typeof window !== 'undefined' ? window.location.pathname : ''
event.event_properties['[Amplitude] Page Title'] = getEnglishPageName(pathname)
}

View File

@ -42,6 +42,7 @@ const ImageInput: FC<UploaderProps> = ({
const [zoom, setZoom] = useState(1)
const onCropComplete = async (_: Area, croppedAreaPixels: Area) => {
/* v8 ignore next -- unreachable guard when Cropper is rendered @preserve */
if (!inputImage)
return
onImageInput?.(true, inputImage.url, croppedAreaPixels, inputImage.file.name)

View File

@ -151,6 +151,43 @@ describe('BlockInput', () => {
expect(screen.queryByRole('textbox')).not.toBeInTheDocument()
})
it('should handle change when onConfirm is not provided', async () => {
render(<BlockInput value="Hello" />)
const contentArea = screen.getByText('Hello')
fireEvent.click(contentArea)
const textarea = await screen.findByRole('textbox')
fireEvent.change(textarea, { target: { value: 'Hello World' } })
expect(textarea).toHaveValue('Hello World')
})
it('should enter edit mode when clicked with empty value', async () => {
render(<BlockInput value="" />)
const contentArea = screen.getByTestId('block-input').firstChild as Element
fireEvent.click(contentArea)
const textarea = await screen.findByRole('textbox')
expect(textarea).toBeInTheDocument()
})
it('should exit edit mode on blur', async () => {
render(<BlockInput value="Hello" />)
const contentArea = screen.getByText('Hello')
fireEvent.click(contentArea)
const textarea = await screen.findByRole('textbox')
expect(textarea).toBeInTheDocument()
fireEvent.blur(textarea)
await waitFor(() => {
expect(screen.queryByRole('textbox')).not.toBeInTheDocument()
})
})
})
describe('Edge Cases', () => {
@ -168,8 +205,9 @@ describe('BlockInput', () => {
})
it('should handle newlines in value', () => {
render(<BlockInput value="line1\nline2" />)
const { container } = render(<BlockInput value={`line1\nline2`} />)
expect(screen.getByText(/line1/)).toBeInTheDocument()
expect(container.querySelector('br')).toBeInTheDocument()
})
it('should handle multiple same variables', () => {

View File

@ -40,7 +40,7 @@ const createMockEmblaApi = (): MockEmblaApi => ({
canScrollPrev: vi.fn(() => mockCanScrollPrev),
canScrollNext: vi.fn(() => mockCanScrollNext),
slideNodes: vi.fn(() =>
Array.from({ length: mockSlideCount }, () => document.createElement('div')),
Array.from({ length: mockSlideCount }).fill(document.createElement('div')),
),
on: vi.fn((event: EmblaEventName, callback: EmblaListener) => {
listeners[event].push(callback)
@ -50,12 +50,13 @@ const createMockEmblaApi = (): MockEmblaApi => ({
}),
})
const emitEmblaEvent = (event: EmblaEventName, api: MockEmblaApi | undefined = mockApi) => {
function emitEmblaEvent(event: EmblaEventName, api?: MockEmblaApi) {
const resolvedApi = arguments.length === 1 ? mockApi : api
listeners[event].forEach((callback) => {
callback(api)
callback(resolvedApi)
})
}
const renderCarouselWithControls = (orientation: 'horizontal' | 'vertical' = 'horizontal') => {
return render(
<Carousel orientation={orientation}>
@ -133,6 +134,24 @@ describe('Carousel', () => {
})
})
// Ref API exposes embla and controls.
describe('Ref API', () => {
it('should expose carousel API and controls via ref', () => {
type CarouselRef = { api: unknown, selectedIndex: number }
const ref = { current: null as CarouselRef | null }
render(
<Carousel ref={(r) => { ref.current = r as unknown as CarouselRef }}>
<Carousel.Content />
</Carousel>,
)
expect(ref.current).toBeDefined()
expect(ref.current?.api).toBe(mockApi)
expect(ref.current?.selectedIndex).toBe(0)
})
})
// Users can move slides through previous and next controls.
describe('User interactions', () => {
it('should call scroll handlers when previous and next buttons are clicked', () => {

View File

@ -1,6 +1,6 @@
import type { ChatWithHistoryContextValue } from '../../context'
import type { AppData, ConversationItem } from '@/models/share'
import { render, screen, waitFor } from '@testing-library/react'
import { act, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { useChatWithHistoryContext } from '../../context'
@ -237,7 +237,9 @@ describe('Header Component', () => {
expect(handleRenameConversation).toHaveBeenCalledWith('conv-1', 'New Name', expect.any(Object))
const successCallback = handleRenameConversation.mock.calls[0][2].onSuccess
successCallback()
await act(async () => {
successCallback()
})
await waitFor(() => {
expect(screen.queryByText('common.chat.renameConversation')).not.toBeInTheDocument()
@ -268,7 +270,9 @@ describe('Header Component', () => {
expect(handleDeleteConversation).toHaveBeenCalledWith('conv-1', expect.any(Object))
const successCallback = handleDeleteConversation.mock.calls[0][1].onSuccess
successCallback()
await act(async () => {
successCallback()
})
await waitFor(() => {
expect(screen.queryByText('share.chat.deleteConversation.title')).not.toBeInTheDocument()
@ -295,6 +299,20 @@ describe('Header Component', () => {
expect(screen.queryByText('share.chat.deleteConversation.title')).not.toBeInTheDocument()
})
})
it('should handle empty translated delete content via fallback', async () => {
const mockConv = { id: 'conv-1', name: 'My Chat' } as ConversationItem
setup({
currentConversationId: 'conv-1',
currentConversationItem: mockConv,
sidebarCollapseState: true,
})
await userEvent.click(screen.getByText('My Chat'))
await userEvent.click(await screen.findByText('explore.sidebar.action.delete'))
expect(await screen.findByText('share.chat.deleteConversation.title')).toBeInTheDocument()
})
})
describe('Edge Cases', () => {
@ -317,6 +335,64 @@ describe('Header Component', () => {
expect(titleEl).toHaveClass('system-md-semibold')
})
it('should render app icon from URL when icon_url is provided', () => {
setup({
appData: {
...mockAppData,
site: {
...mockAppData.site,
icon_type: 'image',
icon_url: 'https://example.com/icon.png',
},
},
})
const img = screen.getByAltText('app icon')
expect(img).toHaveAttribute('src', 'https://example.com/icon.png')
})
it('should handle undefined appData gracefully (optional chaining)', () => {
setup({ appData: null as unknown as AppData })
// Just verify it doesn't crash and renders the basic structure
expect(screen.getAllByRole('button').length).toBeGreaterThan(0)
})
it('should handle missing name in conversation item', () => {
const mockConv = { id: 'conv-1', name: '' } as ConversationItem
setup({
currentConversationId: 'conv-1',
currentConversationItem: mockConv,
sidebarCollapseState: true,
})
// The separator is just a div with text content '/'
expect(screen.getByText('/')).toBeInTheDocument()
})
it('should handle New Chat button state when currentConversationId is present but isResponding is true', () => {
setup({
isResponding: true,
sidebarCollapseState: true,
currentConversationId: 'conv-1',
})
const buttons = screen.getAllByRole('button')
// Sidebar, NewChat, ResetChat (3)
const newChatBtn = buttons[1]
expect(newChatBtn).toBeDisabled()
})
it('should handle New Chat button state when currentConversationId is missing and isResponding is false', () => {
setup({
isResponding: false,
sidebarCollapseState: true,
currentConversationId: '',
})
const buttons = screen.getAllByRole('button')
// Sidebar, NewChat (2)
const newChatBtn = buttons[1]
expect(newChatBtn).toBeDisabled()
})
it('should not render operation menu if conversation id is missing', () => {
setup({ currentConversationId: '', sidebarCollapseState: true })
expect(screen.queryByText('My Chat')).not.toBeInTheDocument()
@ -332,17 +408,20 @@ describe('Header Component', () => {
expect(screen.queryByText('My Chat')).not.toBeInTheDocument()
})
it('should handle New Chat button disabled state when responding', () => {
setup({
isResponding: true,
it('should pass empty rename value when conversation name is undefined', async () => {
const mockConv = { id: 'conv-1' } as ConversationItem
const { container } = setup({
currentConversationId: 'conv-1',
currentConversationItem: mockConv,
sidebarCollapseState: true,
currentConversationId: undefined,
})
const buttons = screen.getAllByRole('button')
// Sidebar(1) + NewChat(1) = 2
const newChatBtn = buttons[1]
expect(newChatBtn).toBeDisabled()
const operationTrigger = container.querySelector('.flex.cursor-pointer.items-center.rounded-lg.p-1\\.5.pl-2.text-text-secondary.hover\\:bg-state-base-hover') as HTMLElement
await userEvent.click(operationTrigger)
await userEvent.click(await screen.findByText('explore.sidebar.action.rename'))
const input = screen.getByRole('textbox') as HTMLInputElement
expect(input.value).toBe('')
})
})
})

View File

@ -59,6 +59,7 @@ const Header = () => {
setShowConfirm(null)
}, [])
const handleDelete = useCallback(() => {
/* v8 ignore next -- defensive guard; onConfirm is only reachable when showConfirm is truthy. @preserve */
if (showConfirm)
handleDeleteConversation(showConfirm.id, { onSuccess: handleCancelConfirm })
}, [showConfirm, handleDeleteConversation, handleCancelConfirm])
@ -66,6 +67,7 @@ const Header = () => {
setShowRename(null)
}, [])
const handleRename = useCallback((newName: string) => {
/* v8 ignore next -- defensive guard; onSave is only reachable when showRename is truthy. @preserve */
if (showRename)
handleRenameConversation(showRename.id, newName, { onSuccess: handleCancelRename })
}, [showRename, handleRenameConversation, handleCancelRename])
@ -87,7 +89,7 @@ const Header = () => {
/>
</div>
{!currentConversationId && (
<div className={cn('system-md-semibold grow truncate text-text-secondary')}>{appData?.site.title}</div>
<div className={cn('grow truncate text-text-secondary system-md-semibold')}>{appData?.site.title}</div>
)}
{currentConversationId && currentConversationItem && isSidebarCollapsed && (
<>

View File

@ -1,18 +1,18 @@
import { render, screen } from '@testing-library/react'
import type { ConversationItem } from '@/models/share'
import { fireEvent, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import * as React from 'react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import Item from '../item'
// Mock Operation to verify its usage
vi.mock('@/app/components/base/chat/chat-with-history/sidebar/operation', () => ({
default: ({ togglePin, onRenameConversation, onDelete, isItemHovering, isActive }: { togglePin: () => void, onRenameConversation: () => void, onDelete: () => void, isItemHovering: boolean, isActive: boolean }) => (
default: ({ togglePin, onRenameConversation, onDelete, isItemHovering, isActive, isPinned }: { togglePin: () => void, onRenameConversation: () => void, onDelete: () => void, isItemHovering: boolean, isActive: boolean, isPinned: boolean }) => (
<div data-testid="mock-operation">
<button onClick={togglePin}>Pin</button>
<button onClick={onRenameConversation}>Rename</button>
<button onClick={onDelete}>Delete</button>
<span data-hovering={isItemHovering}>Hovering</span>
<span data-active={isActive}>Active</span>
<button onClick={togglePin} data-testid="pin-button">Pin</button>
<button onClick={onRenameConversation} data-testid="rename-button">Rename</button>
<button onClick={onDelete} data-testid="delete-button">Delete</button>
<span data-hovering={isItemHovering} data-testid="hover-indicator">Hovering</span>
<span data-active={isActive} data-testid="active-indicator">Active</span>
<span data-pinned={isPinned} data-testid="pinned-indicator">Pinned</span>
</div>
),
}))
@ -36,47 +36,525 @@ describe('Item', () => {
vi.clearAllMocks()
})
it('should render conversation name', () => {
render(<Item {...defaultProps} />)
expect(screen.getByText('Test Conversation')).toBeInTheDocument()
describe('Rendering', () => {
it('should render conversation name', () => {
render(<Item {...defaultProps} />)
expect(screen.getByText('Test Conversation')).toBeInTheDocument()
})
it('should render with title attribute for truncated text', () => {
render(<Item {...defaultProps} />)
const nameDiv = screen.getByText('Test Conversation')
expect(nameDiv).toHaveAttribute('title', 'Test Conversation')
})
it('should render with different names', () => {
const item = { ...mockItem, name: 'Different Conversation' }
render(<Item {...defaultProps} item={item} />)
expect(screen.getByText('Different Conversation')).toBeInTheDocument()
})
it('should render with very long name', () => {
const longName = 'A'.repeat(500)
const item = { ...mockItem, name: longName }
render(<Item {...defaultProps} item={item} />)
expect(screen.getByText(longName)).toBeInTheDocument()
})
it('should render with special characters in name', () => {
const item = { ...mockItem, name: 'Chat @#$% 中文' }
render(<Item {...defaultProps} item={item} />)
expect(screen.getByText('Chat @#$% 中文')).toBeInTheDocument()
})
it('should render with empty name', () => {
const item = { ...mockItem, name: '' }
render(<Item {...defaultProps} item={item} />)
expect(screen.getByTestId('mock-operation')).toBeInTheDocument()
})
it('should render with whitespace-only name', () => {
const item = { ...mockItem, name: ' ' }
render(<Item {...defaultProps} item={item} />)
const nameElement = screen.getByText((_, element) => element?.getAttribute('title') === ' ')
expect(nameElement).toBeInTheDocument()
})
})
it('should call onChangeConversation when clicked', async () => {
const user = userEvent.setup()
render(<Item {...defaultProps} />)
describe('Active State', () => {
it('should show active state when selected', () => {
const { container } = render(<Item {...defaultProps} currentConversationId="1" />)
const itemDiv = container.firstChild as HTMLElement
expect(itemDiv).toHaveClass('bg-state-accent-active')
expect(itemDiv).toHaveClass('text-text-accent')
await user.click(screen.getByText('Test Conversation'))
expect(defaultProps.onChangeConversation).toHaveBeenCalledWith('1')
const activeIndicator = screen.getByTestId('active-indicator')
expect(activeIndicator).toHaveAttribute('data-active', 'true')
})
it('should not show active state when not selected', () => {
const { container } = render(<Item {...defaultProps} currentConversationId="0" />)
const itemDiv = container.firstChild as HTMLElement
expect(itemDiv).not.toHaveClass('bg-state-accent-active')
const activeIndicator = screen.getByTestId('active-indicator')
expect(activeIndicator).toHaveAttribute('data-active', 'false')
})
it('should toggle active state when currentConversationId changes', () => {
const { rerender, container } = render(<Item {...defaultProps} currentConversationId="0" />)
expect(container.firstChild).not.toHaveClass('bg-state-accent-active')
rerender(<Item {...defaultProps} currentConversationId="1" />)
expect(container.firstChild).toHaveClass('bg-state-accent-active')
rerender(<Item {...defaultProps} currentConversationId="0" />)
expect(container.firstChild).not.toHaveClass('bg-state-accent-active')
})
})
it('should show active state when selected', () => {
const { container } = render(<Item {...defaultProps} currentConversationId="1" />)
const itemDiv = container.firstChild as HTMLElement
expect(itemDiv).toHaveClass('bg-state-accent-active')
describe('Pin State', () => {
it('should render with isPin true', () => {
render(<Item {...defaultProps} isPin={true} />)
const pinnedIndicator = screen.getByTestId('pinned-indicator')
expect(pinnedIndicator).toHaveAttribute('data-pinned', 'true')
})
const activeIndicator = screen.getByText('Active')
expect(activeIndicator).toHaveAttribute('data-active', 'true')
it('should render with isPin false', () => {
render(<Item {...defaultProps} isPin={false} />)
const pinnedIndicator = screen.getByTestId('pinned-indicator')
expect(pinnedIndicator).toHaveAttribute('data-pinned', 'false')
})
it('should render with isPin undefined', () => {
render(<Item {...defaultProps} />)
const pinnedIndicator = screen.getByTestId('pinned-indicator')
expect(pinnedIndicator).toHaveAttribute('data-pinned', 'false')
})
it('should call onOperate with unpin when isPinned is true', async () => {
const user = userEvent.setup()
const onOperate = vi.fn()
render(<Item {...defaultProps} onOperate={onOperate} isPin={true} />)
await user.click(screen.getByTestId('pin-button'))
expect(onOperate).toHaveBeenCalledWith('unpin', mockItem)
})
it('should call onOperate with pin when isPinned is false', async () => {
const user = userEvent.setup()
const onOperate = vi.fn()
render(<Item {...defaultProps} onOperate={onOperate} isPin={false} />)
await user.click(screen.getByTestId('pin-button'))
expect(onOperate).toHaveBeenCalledWith('pin', mockItem)
})
it('should call onOperate with pin when isPin is undefined', async () => {
const user = userEvent.setup()
const onOperate = vi.fn()
render(<Item {...defaultProps} onOperate={onOperate} />)
await user.click(screen.getByTestId('pin-button'))
expect(onOperate).toHaveBeenCalledWith('pin', mockItem)
})
})
it('should pass correct props to Operation', async () => {
const user = userEvent.setup()
render(<Item {...defaultProps} isPin={true} />)
describe('Item ID Handling', () => {
it('should show Operation for non-empty id', () => {
render(<Item {...defaultProps} item={{ ...mockItem, id: '123' }} />)
expect(screen.getByTestId('mock-operation')).toBeInTheDocument()
})
const operation = screen.getByTestId('mock-operation')
expect(operation).toBeInTheDocument()
it('should not show Operation for empty id', () => {
render(<Item {...defaultProps} item={{ ...mockItem, id: '' }} />)
expect(screen.queryByTestId('mock-operation')).not.toBeInTheDocument()
})
await user.click(screen.getByText('Pin'))
expect(defaultProps.onOperate).toHaveBeenCalledWith('unpin', mockItem)
it('should show Operation for id with special characters', () => {
render(<Item {...defaultProps} item={{ ...mockItem, id: 'abc-123_xyz' }} />)
expect(screen.getByTestId('mock-operation')).toBeInTheDocument()
})
await user.click(screen.getByText('Rename'))
expect(defaultProps.onOperate).toHaveBeenCalledWith('rename', mockItem)
it('should show Operation for numeric id', () => {
render(<Item {...defaultProps} item={{ ...mockItem, id: '999' }} />)
expect(screen.getByTestId('mock-operation')).toBeInTheDocument()
})
await user.click(screen.getByText('Delete'))
expect(defaultProps.onOperate).toHaveBeenCalledWith('delete', mockItem)
it('should show Operation for uuid-like id', () => {
const uuid = '123e4567-e89b-12d3-a456-426614174000'
render(<Item {...defaultProps} item={{ ...mockItem, id: uuid }} />)
expect(screen.getByTestId('mock-operation')).toBeInTheDocument()
})
})
it('should not show Operation for empty id items', () => {
render(<Item {...defaultProps} item={{ ...mockItem, id: '' }} />)
expect(screen.queryByTestId('mock-operation')).not.toBeInTheDocument()
describe('Click Interactions', () => {
it('should call onChangeConversation when clicked', async () => {
const user = userEvent.setup()
const onChangeConversation = vi.fn()
render(<Item {...defaultProps} onChangeConversation={onChangeConversation} />)
await user.click(screen.getByText('Test Conversation'))
expect(onChangeConversation).toHaveBeenCalledWith('1')
})
it('should call onChangeConversation with correct id', async () => {
const user = userEvent.setup()
const onChangeConversation = vi.fn()
const item = { ...mockItem, id: 'custom-id' }
render(<Item {...defaultProps} item={item} onChangeConversation={onChangeConversation} />)
await user.click(screen.getByText('Test Conversation'))
expect(onChangeConversation).toHaveBeenCalledWith('custom-id')
})
it('should not propagate click to parent when Operation button is clicked', async () => {
const user = userEvent.setup()
const onChangeConversation = vi.fn()
render(<Item {...defaultProps} onChangeConversation={onChangeConversation} />)
const deleteButton = screen.getByTestId('delete-button')
await user.click(deleteButton)
// onChangeConversation should not be called when Operation button is clicked
expect(onChangeConversation).not.toHaveBeenCalled()
})
it('should call onOperate with delete when delete button clicked', async () => {
const user = userEvent.setup()
const onOperate = vi.fn()
render(<Item {...defaultProps} onOperate={onOperate} />)
await user.click(screen.getByTestId('delete-button'))
expect(onOperate).toHaveBeenCalledWith('delete', mockItem)
})
it('should call onOperate with rename when rename button clicked', async () => {
const user = userEvent.setup()
const onOperate = vi.fn()
render(<Item {...defaultProps} onOperate={onOperate} />)
await user.click(screen.getByTestId('rename-button'))
expect(onOperate).toHaveBeenCalledWith('rename', mockItem)
})
it('should handle multiple rapid clicks on different operations', async () => {
const user = userEvent.setup()
const onOperate = vi.fn()
render(<Item {...defaultProps} onOperate={onOperate} />)
await user.click(screen.getByTestId('rename-button'))
await user.click(screen.getByTestId('pin-button'))
await user.click(screen.getByTestId('delete-button'))
expect(onOperate).toHaveBeenCalledTimes(3)
})
it('should call onChangeConversation only once on single click', async () => {
const user = userEvent.setup()
const onChangeConversation = vi.fn()
render(<Item {...defaultProps} onChangeConversation={onChangeConversation} />)
await user.click(screen.getByText('Test Conversation'))
expect(onChangeConversation).toHaveBeenCalledTimes(1)
})
it('should call onChangeConversation multiple times on multiple clicks', async () => {
const user = userEvent.setup()
const onChangeConversation = vi.fn()
render(<Item {...defaultProps} onChangeConversation={onChangeConversation} />)
await user.click(screen.getByText('Test Conversation'))
await user.click(screen.getByText('Test Conversation'))
await user.click(screen.getByText('Test Conversation'))
expect(onChangeConversation).toHaveBeenCalledTimes(3)
})
})
describe('Operation Buttons', () => {
it('should show Operation when item.id is not empty', () => {
render(<Item {...defaultProps} />)
expect(screen.getByTestId('mock-operation')).toBeInTheDocument()
})
it('should pass correct props to Operation', async () => {
render(<Item {...defaultProps} isPin={true} currentConversationId="1" />)
const operation = screen.getByTestId('mock-operation')
expect(operation).toBeInTheDocument()
const activeIndicator = screen.getByTestId('active-indicator')
expect(activeIndicator).toHaveAttribute('data-active', 'true')
const pinnedIndicator = screen.getByTestId('pinned-indicator')
expect(pinnedIndicator).toHaveAttribute('data-pinned', 'true')
})
it('should handle all three operation types sequentially', async () => {
const user = userEvent.setup()
const onOperate = vi.fn()
render(<Item {...defaultProps} onOperate={onOperate} />)
await user.click(screen.getByTestId('rename-button'))
expect(onOperate).toHaveBeenNthCalledWith(1, 'rename', mockItem)
await user.click(screen.getByTestId('pin-button'))
expect(onOperate).toHaveBeenNthCalledWith(2, 'pin', mockItem)
await user.click(screen.getByTestId('delete-button'))
expect(onOperate).toHaveBeenNthCalledWith(3, 'delete', mockItem)
})
it('should handle pin toggle between pin and unpin', async () => {
const user = userEvent.setup()
const onOperate = vi.fn()
const { rerender } = render(
<Item {...defaultProps} onOperate={onOperate} isPin={false} />,
)
await user.click(screen.getByTestId('pin-button'))
expect(onOperate).toHaveBeenCalledWith('pin', mockItem)
rerender(<Item {...defaultProps} onOperate={onOperate} isPin={true} />)
await user.click(screen.getByTestId('pin-button'))
expect(onOperate).toHaveBeenCalledWith('unpin', mockItem)
})
})
describe('Styling', () => {
it('should have base classes on container', () => {
const { container } = render(<Item {...defaultProps} />)
const itemDiv = container.firstChild as HTMLElement
expect(itemDiv).toHaveClass('group')
expect(itemDiv).toHaveClass('flex')
expect(itemDiv).toHaveClass('cursor-pointer')
expect(itemDiv).toHaveClass('rounded-lg')
})
it('should apply active state classes when selected', () => {
const { container } = render(<Item {...defaultProps} currentConversationId="1" />)
const itemDiv = container.firstChild as HTMLElement
expect(itemDiv).toHaveClass('bg-state-accent-active')
expect(itemDiv).toHaveClass('text-text-accent')
})
it('should apply hover classes', () => {
const { container } = render(<Item {...defaultProps} />)
const itemDiv = container.firstChild as HTMLElement
expect(itemDiv).toHaveClass('hover:bg-state-base-hover')
})
it('should maintain hover classes when active', () => {
const { container } = render(<Item {...defaultProps} currentConversationId="1" />)
const itemDiv = container.firstChild as HTMLElement
expect(itemDiv).toHaveClass('hover:bg-state-accent-active')
})
it('should apply truncate class to text container', () => {
const { container } = render(<Item {...defaultProps} />)
const textDiv = container.querySelector('.grow.truncate')
expect(textDiv).toHaveClass('truncate')
expect(textDiv).toHaveClass('grow')
})
})
describe('Props Updates', () => {
it('should update when item prop changes', () => {
const { rerender } = render(<Item {...defaultProps} item={mockItem} />)
expect(screen.getByText('Test Conversation')).toBeInTheDocument()
const newItem = { ...mockItem, name: 'Updated Conversation' }
rerender(<Item {...defaultProps} item={newItem} />)
expect(screen.getByText('Updated Conversation')).toBeInTheDocument()
expect(screen.queryByText('Test Conversation')).not.toBeInTheDocument()
})
it('should update when currentConversationId changes', () => {
const { container, rerender } = render(
<Item {...defaultProps} currentConversationId="0" />,
)
expect(container.firstChild).not.toHaveClass('bg-state-accent-active')
rerender(<Item {...defaultProps} currentConversationId="1" />)
expect(container.firstChild).toHaveClass('bg-state-accent-active')
})
it('should update when isPin changes', () => {
const { rerender } = render(<Item {...defaultProps} isPin={false} />)
let pinnedIndicator = screen.getByTestId('pinned-indicator')
expect(pinnedIndicator).toHaveAttribute('data-pinned', 'false')
rerender(<Item {...defaultProps} isPin={true} />)
pinnedIndicator = screen.getByTestId('pinned-indicator')
expect(pinnedIndicator).toHaveAttribute('data-pinned', 'true')
})
it('should update when callbacks change', async () => {
const user = userEvent.setup()
const oldOnOperate = vi.fn()
const newOnOperate = vi.fn()
const { rerender } = render(<Item {...defaultProps} onOperate={oldOnOperate} />)
rerender(<Item {...defaultProps} onOperate={newOnOperate} />)
await user.click(screen.getByTestId('delete-button'))
expect(newOnOperate).toHaveBeenCalledWith('delete', mockItem)
expect(oldOnOperate).not.toHaveBeenCalled()
})
it('should update when multiple props change together', () => {
const { rerender } = render(
<Item
{...defaultProps}
item={mockItem}
currentConversationId="0"
isPin={false}
/>,
)
const newItem = { ...mockItem, name: 'New Name', id: '2' }
rerender(
<Item
{...defaultProps}
item={newItem}
currentConversationId="2"
isPin={true}
/>,
)
expect(screen.getByText('New Name')).toBeInTheDocument()
const activeIndicator = screen.getByTestId('active-indicator')
expect(activeIndicator).toHaveAttribute('data-active', 'true')
const pinnedIndicator = screen.getByTestId('pinned-indicator')
expect(pinnedIndicator).toHaveAttribute('data-pinned', 'true')
})
})
describe('Item with Different Data', () => {
it('should handle item with all properties', () => {
const item = {
id: 'full-item',
name: 'Full Item Name',
inputs: { key: 'value' },
introduction: 'Some introduction',
}
render(<Item {...defaultProps} item={item} />)
expect(screen.getByText('Full Item Name')).toBeInTheDocument()
})
it('should handle item with minimal properties', () => {
const item = {
id: '1',
name: 'Minimal',
} as unknown as ConversationItem
render(<Item {...defaultProps} item={item} />)
expect(screen.getByText('Minimal')).toBeInTheDocument()
})
it('should handle multiple items rendered separately', () => {
const item1 = { ...mockItem, id: '1', name: 'First' }
const item2 = { ...mockItem, id: '2', name: 'Second' }
const { rerender } = render(<Item {...defaultProps} item={item1} />)
expect(screen.getByText('First')).toBeInTheDocument()
rerender(<Item {...defaultProps} item={item2} />)
expect(screen.getByText('Second')).toBeInTheDocument()
expect(screen.queryByText('First')).not.toBeInTheDocument()
})
})
describe('Hover State', () => {
it('should pass hover state to Operation when hovering', async () => {
const { container } = render(<Item {...defaultProps} />)
const row = container.firstChild as HTMLElement
const hoverIndicator = screen.getByTestId('hover-indicator')
expect(hoverIndicator.getAttribute('data-hovering')).toBe('false')
fireEvent.mouseEnter(row)
expect(hoverIndicator.getAttribute('data-hovering')).toBe('true')
fireEvent.mouseLeave(row)
expect(hoverIndicator.getAttribute('data-hovering')).toBe('false')
})
})
describe('Edge Cases', () => {
it('should handle item with unicode name', () => {
const item = { ...mockItem, name: '🎉 Celebration Chat 中文版' }
render(<Item {...defaultProps} item={item} />)
expect(screen.getByText('🎉 Celebration Chat 中文版')).toBeInTheDocument()
})
it('should handle item with numeric id as string', () => {
const item = { ...mockItem, id: '12345' }
render(<Item {...defaultProps} item={item} />)
expect(screen.getByTestId('mock-operation')).toBeInTheDocument()
})
it('should handle rapid isPin prop changes', () => {
const { rerender } = render(<Item {...defaultProps} isPin={true} />)
for (let i = 0; i < 5; i++) {
rerender(<Item {...defaultProps} isPin={i % 2 === 0} />)
}
const pinnedIndicator = screen.getByTestId('pinned-indicator')
expect(pinnedIndicator).toHaveAttribute('data-pinned', 'true')
})
it('should handle item name with HTML-like content', () => {
const item = { ...mockItem, name: '<script>alert("xss")</script>' }
render(<Item {...defaultProps} item={item} />)
// Should render as text, not execute
expect(screen.getByText('<script>alert("xss")</script>')).toBeInTheDocument()
})
it('should handle very long item id', () => {
const longId = 'a'.repeat(1000)
const item = { ...mockItem, id: longId }
render(<Item {...defaultProps} item={item} />)
expect(screen.getByTestId('mock-operation')).toBeInTheDocument()
})
})
describe('Memoization', () => {
it('should not re-render when same props are passed', () => {
const { rerender } = render(<Item {...defaultProps} />)
const element = screen.getByText('Test Conversation')
rerender(<Item {...defaultProps} />)
expect(screen.getByText('Test Conversation')).toBe(element)
})
it('should re-render when item changes', () => {
const { rerender } = render(<Item {...defaultProps} item={mockItem} />)
const newItem = { ...mockItem, name: 'Changed' }
rerender(<Item {...defaultProps} item={newItem} />)
expect(screen.getByText('Changed')).toBeInTheDocument()
})
})
})

View File

@ -1,9 +1,30 @@
import type { ReactNode } from 'react'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import * as React from 'react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import * as ReactI18next from 'react-i18next'
import RenameModal from '../rename-modal'
vi.mock('@/app/components/base/modal', () => ({
default: ({
title,
isShow,
children,
}: {
title: ReactNode
isShow: boolean
children: ReactNode
}) => {
if (!isShow)
return null
return (
<div role="dialog">
<h2>{title}</h2>
{children}
</div>
)
},
}))
describe('RenameModal', () => {
const defaultProps = {
isShow: true,
@ -17,58 +38,106 @@ describe('RenameModal', () => {
vi.clearAllMocks()
})
it('should render with initial name', () => {
it('renders title, label, input and action buttons', () => {
render(<RenameModal {...defaultProps} />)
expect(screen.getByText('common.chat.renameConversation')).toBeInTheDocument()
expect(screen.getByDisplayValue('Original Name')).toBeInTheDocument()
expect(screen.getByPlaceholderText('common.chat.conversationNamePlaceholder')).toBeInTheDocument()
expect(screen.getByText('common.chat.conversationName')).toBeInTheDocument()
expect(screen.getByPlaceholderText('common.chat.conversationNamePlaceholder')).toHaveValue('Original Name')
expect(screen.getByText('common.operation.cancel')).toBeInTheDocument()
expect(screen.getByText('common.operation.save')).toBeInTheDocument()
})
it('should update text when typing', async () => {
it('does not render when isShow is false', () => {
render(<RenameModal {...defaultProps} isShow={false} />)
expect(screen.queryByText('common.chat.renameConversation')).not.toBeInTheDocument()
})
it('calls onClose when cancel is clicked', async () => {
const user = userEvent.setup()
render(<RenameModal {...defaultProps} />)
const input = screen.getByDisplayValue('Original Name')
await user.clear(input)
await user.type(input, 'New Name')
expect(input).toHaveValue('New Name')
await user.click(screen.getByText('common.operation.cancel'))
expect(defaultProps.onClose).toHaveBeenCalled()
})
it('should call onSave with new name when save button is clicked', async () => {
it('calls onSave with updated name', async () => {
const user = userEvent.setup()
render(<RenameModal {...defaultProps} />)
const input = screen.getByDisplayValue('Original Name')
const input = screen.getByRole('textbox')
await user.clear(input)
await user.type(input, 'Updated Name')
const saveButton = screen.getByText('common.operation.save')
await user.click(saveButton)
await user.click(screen.getByText('common.operation.save'))
expect(defaultProps.onSave).toHaveBeenCalledWith('Updated Name')
})
it('should call onClose when cancel button is clicked', async () => {
it('calls onSave with initial name when unchanged', async () => {
const user = userEvent.setup()
render(<RenameModal {...defaultProps} />)
const cancelButton = screen.getByText('common.operation.cancel')
await user.click(cancelButton)
expect(defaultProps.onClose).toHaveBeenCalled()
await user.click(screen.getByText('common.operation.save'))
expect(defaultProps.onSave).toHaveBeenCalledWith('Original Name')
})
it('should show loading state on save button', () => {
render(<RenameModal {...defaultProps} saveLoading={true} />)
// The Button component with loading=true renders a status role (spinner)
it('shows loading state when saveLoading is true', () => {
render(<RenameModal {...defaultProps} saveLoading />)
expect(screen.getByRole('status')).toBeInTheDocument()
})
it('should not render when isShow is false', () => {
const { queryByText } = render(<RenameModal {...defaultProps} isShow={false} />)
expect(queryByText('common.chat.renameConversation')).not.toBeInTheDocument()
it('hides loading state when saveLoading is false', () => {
render(<RenameModal {...defaultProps} saveLoading={false} />)
expect(screen.queryByRole('status')).not.toBeInTheDocument()
})
it('keeps edited name when parent rerenders with different name prop', async () => {
const user = userEvent.setup()
const { rerender } = render(<RenameModal {...defaultProps} name="First" />)
const input = screen.getByRole('textbox')
await user.clear(input)
await user.type(input, 'Edited')
rerender(<RenameModal {...defaultProps} name="Second" />)
expect(screen.getByRole('textbox')).toHaveValue('Edited')
})
it('retains typed state after isShow false then true on same component instance', async () => {
const user = userEvent.setup()
const { rerender } = render(<RenameModal {...defaultProps} isShow />)
const input = screen.getByRole('textbox')
await user.clear(input)
await user.type(input, 'Changed')
rerender(<RenameModal {...defaultProps} isShow={false} />)
rerender(<RenameModal {...defaultProps} isShow />)
expect(screen.getByRole('textbox')).toHaveValue('Changed')
})
it('uses empty placeholder fallback when translation returns empty string', () => {
const originalUseTranslation = ReactI18next.useTranslation
const useTranslationSpy = vi.spyOn(ReactI18next, 'useTranslation').mockImplementation((...args) => {
const translation = originalUseTranslation(...args)
return {
...translation,
t: ((key: string, options?: Record<string, unknown>) => {
if (key === 'chat.conversationNamePlaceholder')
return ''
const ns = options?.ns as string | undefined
return ns ? `${ns}.${key}` : key
}) as typeof translation.t,
}
})
try {
render(<RenameModal {...defaultProps} />)
expect(screen.getByPlaceholderText('')).toBeInTheDocument()
}
finally {
useTranslationSpy.mockRestore()
}
})
})

View File

@ -78,6 +78,8 @@ const Sidebar = ({ isPanel, panelVisible }: Props) => {
if (showRename)
handleRenameConversation(showRename.id, newName, { onSuccess: handleCancelRename })
}, [showRename, handleRenameConversation, handleCancelRename])
const pinnedTitle = t('chat.pinnedTitle', { ns: 'share' }) || ''
const deleteConversationContent = t('chat.deleteConversation.content', { ns: 'share' }) || ''
return (
<div className={cn(
@ -122,7 +124,7 @@ const Sidebar = ({ isPanel, panelVisible }: Props) => {
<div className="mb-4">
<List
isPin
title={t('chat.pinnedTitle', { ns: 'share' }) || ''}
title={pinnedTitle}
list={pinnedConversationList}
onChangeConversation={handleChangeConversation}
onOperate={handleOperate}
@ -168,7 +170,7 @@ const Sidebar = ({ isPanel, panelVisible }: Props) => {
{!!showConfirm && (
<Confirm
title={t('chat.deleteConversation.title', { ns: 'share' })}
content={t('chat.deleteConversation.content', { ns: 'share' }) || ''}
content={deleteConversationContent}
isShow
onCancel={handleCancelConfirm}
onConfirm={handleDelete}

View File

@ -24,6 +24,7 @@ const RenameModal: FC<IRenameModalProps> = ({
}) => {
const { t } = useTranslation()
const [tempName, setTempName] = useState(name)
const conversationNamePlaceholder = t('chat.conversationNamePlaceholder', { ns: 'common' }) || ''
return (
<Modal
@ -36,7 +37,7 @@ const RenameModal: FC<IRenameModalProps> = ({
className="mt-2 h-10 w-full"
value={tempName}
onChange={e => setTempName(e.target.value)}
placeholder={t('chat.conversationNamePlaceholder', { ns: 'common' }) || ''}
placeholder={conversationNamePlaceholder}
/>
<div className="mt-10 flex justify-end">

View File

@ -2,6 +2,7 @@ import type { ChatConfig, ChatItemInTree } from '../../types'
import type { FileEntity } from '@/app/components/base/file-uploader/types'
import { act, renderHook } from '@testing-library/react'
import { useParams, usePathname } from 'next/navigation'
import { WorkflowRunningStatus } from '@/app/components/workflow/types'
import { sseGet, ssePost } from '@/service/base'
import { useChat } from '../hooks'
@ -1378,22 +1379,884 @@ describe('useChat', () => {
}]
const { result } = renderHook(() => useChat(undefined, undefined, nestedTree as ChatItemInTree[]))
act(() => {
result.current.handleSwitchSibling('a-deep', { isPublicAPI: true })
})
expect(sseGet).not.toHaveBeenCalled()
})
it('should do nothing when switching to a sibling message that does not exist', () => {
const { result } = renderHook(() => useChat(undefined, undefined, prevChatTree as ChatItemInTree[]))
act(() => {
result.current.handleSwitchSibling('missing-message-id', { isPublicAPI: true })
})
expect(sseGet).not.toHaveBeenCalled()
})
})
describe('Uncovered edge cases', () => {
it('should handle onFile fallbacks for audio, video, bin types', () => {
let callbacks: HookCallbacks
vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
callbacks = options as HookCallbacks
})
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSend('url', { query: 'file types' }, {})
})
act(() => {
callbacks.onWorkflowStarted({ workflow_run_id: 'wr-1', task_id: 't-1', message_id: 'm-files' })
// No transferMethod, type: video
callbacks.onFile({ id: 'f-vid', type: 'video', url: 'vid.mp4' })
// No transferMethod, type: audio
callbacks.onFile({ id: 'f-aud', type: 'audio', url: 'aud.mp3' })
// No transferMethod, type: bin
callbacks.onFile({ id: 'f-bin', type: 'bin', url: 'file.bin' })
})
const lastResponse = result.current.chatList[1]
expect(lastResponse.message_files).toHaveLength(3)
expect(lastResponse.message_files![0].type).toBe('video/mp4')
expect(lastResponse.message_files![0].supportFileType).toBe('video')
expect(lastResponse.message_files![1].type).toBe('audio/mpeg')
expect(lastResponse.message_files![1].supportFileType).toBe('audio')
expect(lastResponse.message_files![2].type).toBe('application/octet-stream')
expect(lastResponse.message_files![2].supportFileType).toBe('document')
})
it('should handle onMessageEnd empty citation and empty processed files fallbacks', () => {
let callbacks: HookCallbacks
vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
callbacks = options as HookCallbacks
})
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSend('url', { query: 'citations' }, {})
})
act(() => {
callbacks.onWorkflowStarted({ workflow_run_id: 'wr-1', task_id: 't-1', message_id: 'm-cite' })
callbacks.onMessageEnd({ id: 'm-cite', metadata: {} }) // No retriever_resources or annotation_reply
})
const lastResponse = result.current.chatList[1]
expect(lastResponse.citation).toEqual([])
})
it('should handle iteration and loop tracing edge cases (lazy arrays, node finish index -1)', () => {
let callbacks: HookCallbacks
vi.mocked(sseGet).mockImplementation(async (_url, _params, options) => {
callbacks = options as HookCallbacks
})
const prevChatTree = [{
id: 'q-trace',
content: 'query',
isAnswer: false,
children: [{
id: 'm-trace',
content: 'initial',
isAnswer: true,
siblingIndex: 0,
workflowProcess: { status: WorkflowRunningStatus.Running }, // Omit tracing array to test fallback
}],
}]
const { result } = renderHook(() => useChat(undefined, undefined, prevChatTree as ChatItemInTree[]))
act(() => {
result.current.handleResume('m-trace', 'wr-trace', { isPublicAPI: true })
})
act(() => {
// onIterationStart should create the tracing array
callbacks.onIterationStart({ data: { node_id: 'iter-1' } })
})
const prevChatTree2 = [{
id: 'q-trace2',
content: 'query',
isAnswer: false,
children: [{
id: 'm-trace',
content: 'initial',
isAnswer: true,
siblingIndex: 0,
workflowProcess: { status: WorkflowRunningStatus.Running }, // Omit tracing array to test fallback
}],
}]
const { result: result2 } = renderHook(() => useChat(undefined, undefined, prevChatTree2 as ChatItemInTree[]))
act(() => {
result2.current.handleResume('m-trace', 'wr-trace2', { isPublicAPI: true })
})
act(() => {
// onNodeStarted should create the tracing array
callbacks.onNodeStarted({ data: { node_id: 'n-1', id: 'n-1' } })
})
const prevChatTree3 = [{
id: 'q-trace3',
content: 'query',
isAnswer: false,
children: [{
id: 'm-trace',
content: 'initial',
isAnswer: true,
siblingIndex: 0,
workflowProcess: { status: WorkflowRunningStatus.Running }, // Omit tracing array to test fallback
}],
}]
const { result: result3 } = renderHook(() => useChat(undefined, undefined, prevChatTree3 as ChatItemInTree[]))
act(() => {
result3.current.handleResume('m-trace', 'wr-trace3', { isPublicAPI: true })
})
act(() => {
// onLoopStart should create the tracing array
callbacks.onLoopStart({ data: { node_id: 'loop-1' } })
})
// Ensure the tracing array exists and holds the loop item
const lastResponse = result3.current.chatList[1]
expect(lastResponse.workflowProcess?.tracing).toBeDefined()
expect(lastResponse.workflowProcess?.tracing).toHaveLength(1)
expect(lastResponse.workflowProcess?.tracing![0].node_id).toBe('loop-1')
})
it('should handle onCompleted fallback to answer when agent thought does not match and provider latency is 0', async () => {
let callbacks: HookCallbacks
vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
callbacks = options as HookCallbacks
})
const onGetConversationMessages = vi.fn().mockResolvedValue({
data: [{
id: 'm-completed',
answer: 'final answer',
message: [{ role: 'user', text: 'hi' }],
agent_thoughts: [{ thought: 'thinking different from answer' }],
created_at: Date.now(),
answer_tokens: 10,
message_tokens: 5,
provider_response_latency: 0,
inputs: {},
query: 'hi',
}],
})
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSend('test-url', { query: 'fetch test latency zero' }, {
onGetConversationMessages,
})
})
await act(async () => {
callbacks.onData(' data', true, { messageId: 'm-completed', conversationId: 'c-latency' })
await callbacks.onCompleted()
})
const lastResponse = result.current.chatList[1]
expect(lastResponse.content).toBe('final answer')
expect(lastResponse.more?.latency).toBe('0.00')
expect(lastResponse.more?.tokens_per_second).toBeUndefined()
})
it('should handle onCompleted using agent thought when thought matches answer', async () => {
let callbacks: HookCallbacks
vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
callbacks = options as HookCallbacks
})
const onGetConversationMessages = vi.fn().mockResolvedValue({
data: [{
id: 'm-matched',
answer: 'matched thought',
message: [{ role: 'user', text: 'hi' }],
agent_thoughts: [{ thought: 'matched thought' }],
created_at: Date.now(),
answer_tokens: 10,
message_tokens: 5,
provider_response_latency: 0.5,
inputs: {},
query: 'hi',
}],
})
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSend('test-url', { query: 'fetch test match thought' }, {
onGetConversationMessages,
})
})
await act(async () => {
callbacks.onData(' data', true, { messageId: 'm-matched', conversationId: 'c-matched' })
await callbacks.onCompleted()
})
const lastResponse = result.current.chatList[1]
expect(lastResponse.content).toBe('') // isUseAgentThought sets content to empty string
})
it('should cover pausedStateRef reset on workflowFinished and missing tracing arrays in node finish / human input', () => {
let callbacks: HookCallbacks
vi.mocked(sseGet).mockImplementation(async (_url, _params, options) => {
callbacks = options as HookCallbacks
})
const prevChatTree = [{
id: 'q-pause',
content: 'query',
isAnswer: false,
children: [{
id: 'm-pause',
content: 'initial',
isAnswer: true,
siblingIndex: 0,
workflowProcess: { status: WorkflowRunningStatus.Running }, // Omit tracing
}],
}]
// Setup test for workflow paused + finished
const { result } = renderHook(() => useChat(undefined, undefined, prevChatTree as ChatItemInTree[]))
act(() => {
result.current.handleResume('m-pause', 'wr-1', { isPublicAPI: true })
})
act(() => {
// Trigger a pause to set pausedStateRef = true
callbacks.onWorkflowPaused({ data: { workflow_run_id: 'wr-1' } })
// workflowFinished should reset pausedStateRef to false
callbacks.onWorkflowFinished({ data: { status: 'succeeded' } })
// Missing tracing array onNodeFinished early return
callbacks.onNodeFinished({ data: { id: 'n-none' } })
// Missing tracing array fallback for human input
callbacks.onHumanInputRequired({ data: { node_id: 'h-1' } })
})
const lastResponse = result.current.chatList[1]
expect(lastResponse.workflowProcess?.status).toBe('succeeded')
})
it('should cover onThought creating tracing and appending message correctly when isAgentMode=true', () => {
let callbacks: HookCallbacks
vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
callbacks = options as HookCallbacks
})
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSend('url', { query: 'agent onThought' }, {})
})
act(() => {
callbacks.onWorkflowStarted({ workflow_run_id: 'wr-1', task_id: 't-1' })
// onThought when array is implicitly empty
callbacks.onThought({ id: 'th-1', thought: 'initial thought' })
// onData which appends to last thought
callbacks.onData(' appended', false, { messageId: 'm-thought' })
})
const lastResponse = result.current.chatList[result.current.chatList.length - 1]
expect(lastResponse.agent_thoughts).toHaveLength(1)
expect(lastResponse.agent_thoughts![0].thought).toBe('initial thought appended')
})
})
it('should cover produceChatTreeNode traversing deeply nested child nodes to find the target item', () => {
vi.mocked(sseGet).mockImplementation(async (_url, _params, _options) => { })
const nestedTree = [{
id: 'q-root',
content: 'query',
isAnswer: false,
children: [{
id: 'a-root',
content: 'answer root',
isAnswer: true,
siblingIndex: 0,
children: [{
id: 'q-deep',
content: 'deep question',
isAnswer: false,
children: [{
id: 'a-deep',
content: 'deep answer to find',
isAnswer: true,
siblingIndex: 0,
}],
}],
}],
}]
// Render the chat with the nested tree
const { result } = renderHook(() => useChat(undefined, undefined, nestedTree as ChatItemInTree[]))
// Setting TargetNodeId triggers state update using produceChatTreeNode internally
act(() => {
// AnnotationEdited uses produceChatTreeNode to find target Question/Answer nodes
result.current.handleAnnotationRemoved(3)
})
// We just care that the tree traversal didn't crash
expect(result.current.chatList).toHaveLength(4)
})
it('should cover baseFile with transferMethod and without file type in handleResume and handleSend', () => {
let resumeCallbacks: HookCallbacks
vi.mocked(sseGet).mockImplementation(async (_url, _params, options) => {
resumeCallbacks = options as HookCallbacks
})
let sendCallbacks: HookCallbacks
vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
sendCallbacks = options as HookCallbacks
})
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSend('url', { query: 'test base file' }, {})
})
const prevChatTree = [{
id: 'q-resume',
content: 'query',
isAnswer: false,
children: [{
id: 'm-resume',
content: 'initial',
isAnswer: true,
siblingIndex: 0,
}],
}]
const { result: resumeResult } = renderHook(() => useChat(undefined, undefined, prevChatTree as ChatItemInTree[]))
act(() => {
resumeResult.current.handleResume('m-resume', 'wr-1', { isPublicAPI: true })
})
act(() => {
const fileWithMethodAndNoType = {
id: 'f-1',
transferMethod: 'remote_url',
type: undefined,
name: 'uploaded.png',
}
sendCallbacks.onFile(fileWithMethodAndNoType)
resumeCallbacks.onFile(fileWithMethodAndNoType)
// Test the inner condition in handleSend `!isAgentMode` where we also push to current files
sendCallbacks.onFile(fileWithMethodAndNoType)
})
const lastSendResponse = result.current.chatList[1]
expect(lastSendResponse.message_files).toHaveLength(2)
const lastResumeResponse = resumeResult.current.chatList[1]
expect(lastResumeResponse.message_files).toHaveLength(1)
})
it('should cover parallel_id tracing matches in iteration and loop finish', () => {
let sendCallbacks: HookCallbacks
vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
sendCallbacks = options as HookCallbacks
})
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSend('url', { query: 'test parallel_id' }, {})
})
act(() => {
sendCallbacks.onWorkflowStarted({ workflow_run_id: 'wr-1', task_id: 't-1' })
// parallel_id in execution_metadata
sendCallbacks.onIterationStart({ data: { node_id: 'iter-1', execution_metadata: { parallel_id: 'pid-1' } } })
sendCallbacks.onIterationFinish({ data: { node_id: 'iter-1', execution_metadata: { parallel_id: 'pid-1' }, status: 'succeeded' } })
// no parallel_id
sendCallbacks.onLoopStart({ data: { node_id: 'loop-1' } })
sendCallbacks.onLoopFinish({ data: { node_id: 'loop-1', status: 'succeeded' } })
// parallel_id in root item but finish has it in execution_metadata
sendCallbacks.onNodeStarted({ data: { node_id: 'n-1', id: 'n-1', parallel_id: 'pid-2' } })
sendCallbacks.onNodeFinished({ data: { node_id: 'n-1', id: 'n-1', execution_metadata: { parallel_id: 'pid-2' } } })
})
const lastResponse = result.current.chatList[1]
const tracing = lastResponse.workflowProcess!.tracing!
expect(tracing).toHaveLength(3)
expect(tracing[0].status).toBe('succeeded')
expect(tracing[1].status).toBe('succeeded')
})
it('should cover baseFile with ALL fields, avoiding all fallbacks', () => {
let sendCallbacks: HookCallbacks
vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
sendCallbacks = options as HookCallbacks
})
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSend('url', { query: 'test exact file' }, {})
})
act(() => {
sendCallbacks.onWorkflowStarted({ workflow_run_id: 'wr-1', task_id: 't-1' })
sendCallbacks.onFile({
id: 'exact-1',
type: 'custom/mime',
transferMethod: 'local_file',
url: 'exact.url',
supportFileType: 'blob',
progress: 50,
name: 'exact.name',
size: 1024,
})
})
const lastResponse = result.current.chatList[result.current.chatList.length - 1]
expect(lastResponse.message_files).toHaveLength(1)
expect(lastResponse.message_files![0].type).toBe('custom/mime')
expect(lastResponse.message_files![0].size).toBe(1024)
})
it('should cover handleResume missing branches for onMessageEnd, onFile fallbacks, and workflow edges', () => {
let resumeCallbacks: HookCallbacks
vi.mocked(sseGet).mockImplementation(async (_url, _params, options) => {
resumeCallbacks = options as HookCallbacks
})
const prevChatTree = [{
id: 'q-data',
content: 'query',
isAnswer: false,
children: [{
id: 'm-data',
content: 'initial',
isAnswer: true,
siblingIndex: 0,
}],
}]
const { result } = renderHook(() => useChat(undefined, undefined, prevChatTree as ChatItemInTree[]))
act(() => {
result.current.handleResume('m-data', 'wr-1', { isPublicAPI: true })
})
act(() => {
// messageId undefined
resumeCallbacks.onData(' more data', false, { conversationId: 'c-1', taskId: 't-1' })
// onFile audio video bin fallbacks
resumeCallbacks.onFile({ id: 'f-vid', type: 'video', url: 'vid.mp4' })
resumeCallbacks.onFile({ id: 'f-aud', type: 'audio', url: 'aud.mp3' })
resumeCallbacks.onFile({ id: 'f-bin', type: 'bin', url: 'file.bin' })
// onMessageEnd missing annotation and citation
resumeCallbacks.onMessageEnd({ id: 'm-end', metadata: {} } as Record<string, unknown>)
// onThought fallback missing message_id
resumeCallbacks.onThought({ thought: 'missing message id', message_files: [] } as Record<string, unknown>)
// onHumanInputFormTimeout missing length
resumeCallbacks.onHumanInputFormTimeout({ data: { node_id: 'timeout-id' } })
// Empty file list
result.current.chatList[1].message_files = undefined
// Call onFile while agent_thoughts is empty/undefined to hit the `else` fallback branch
resumeCallbacks.onFile({ id: 'f-agent', type: 'image', url: 'agent.png' })
})
const lastResponse = result.current.chatList[1]
expect(lastResponse.message_files![0]).toBeDefined()
})
it('should cover edge case where node_id is missing or index is -1 in handleResume onNodeFinished and onLoopFinish', () => {
let resumeCallbacks: HookCallbacks
vi.mocked(sseGet).mockImplementation(async (_url, _params, options) => {
resumeCallbacks = options as HookCallbacks
})
const prevChatTree = [{
id: 'q-index',
content: 'query',
isAnswer: false,
children: [{
id: 'm-index',
content: 'initial',
isAnswer: true,
siblingIndex: 0,
workflowProcess: { status: WorkflowRunningStatus.Running, tracing: [] },
}],
}]
const { result } = renderHook(() => useChat(undefined, undefined, prevChatTree as ChatItemInTree[]))
act(() => {
result.current.handleResume('m-index', 'wr-1', { isPublicAPI: true })
})
act(() => {
// ID doesn't exist in tracing
resumeCallbacks.onNodeFinished({ data: { id: 'missing', execution_metadata: { parallel_id: 'missing-pid' } } })
// Node ID doesn't exist in tracing
resumeCallbacks.onLoopFinish({ data: { node_id: 'missing-loop', status: 'succeeded' } })
// Parallel ID doesn't match
resumeCallbacks.onIterationFinish({ data: { node_id: 'missing-iter', execution_metadata: { parallel_id: 'missing-pid' }, status: 'succeeded' } })
})
const lastResponse = result.current.chatList[1]
expect(lastResponse.workflowProcess?.tracing).toHaveLength(0) // None were updated
})
it('should cover TTS chunks branching where audio is empty', () => {
let sendCallbacks: HookCallbacks
vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
sendCallbacks = options as HookCallbacks
})
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSend('url', { query: 'test text to speech' }, {})
})
act(() => {
sendCallbacks.onTTSChunk('msg-1', '') // Missing audio string
})
// If it didn't crash, we achieved coverage for the empty audio string fast return
expect(true).toBe(true)
})
it('should cover handleSend identical missing branches, null states, and undefined tracking arrays', () => {
let sendCallbacks: HookCallbacks
vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
sendCallbacks = options as HookCallbacks
})
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSend('url', { query: 'test exact file send' }, {})
})
act(() => {
// missing task ID in onData
sendCallbacks.onData(' append', false, { conversationId: 'c-1' } as Record<string, unknown>)
// Empty message files fallback
result.current.chatList[1].message_files = undefined
sendCallbacks.onFile({ id: 'f-send', type: 'image', url: 'img.png' })
// Empty message files passing to processing fallback
sendCallbacks.onMessageEnd({ id: 'm-send' } as Record<string, unknown>)
// node finished missing arrays
sendCallbacks.onWorkflowStarted({ workflow_run_id: 'wr', task_id: 't' })
sendCallbacks.onNodeStarted({ data: { node_id: 'n-new', id: 'n-new' } }) // adds tracing
sendCallbacks.onNodeFinished({ data: { id: 'missing-idx' } } as Record<string, unknown>)
// onIterationFinish parallel_id matching
sendCallbacks.onIterationFinish({ data: { node_id: 'missing-iter', status: 'succeeded' } } as Record<string, unknown>)
// onLoopFinish parallel_id matching
sendCallbacks.onLoopFinish({ data: { node_id: 'missing-loop', status: 'succeeded' } } as Record<string, unknown>)
// Timeout missing form data
sendCallbacks.onHumanInputFormTimeout({ data: { node_id: 'timeout' } } as Record<string, unknown>)
})
expect(result.current.chatList[1].message_files).toBeDefined()
})
it('should cover handleSwitchSibling target message not found early returns', () => {
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSwitchSibling('missing-id', { isPublicAPI: true })
})
// Should early return and not crash
expect(result.current.chatList).toHaveLength(0)
})
it('should cover handleSend onNodeStarted missing workflowProcess early returns', () => {
let sendCallbacks: HookCallbacks
vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
sendCallbacks = options as HookCallbacks
})
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSend('url', { query: 'test' }, {})
})
act(() => {
sendCallbacks.onNodeStarted({ data: { node_id: 'n-new', id: 'n-new' } })
})
expect(result.current.chatList[1].workflowProcess).toBeUndefined()
})
it('should cover handleSend onNodeStarted missing tracing in workflowProcess (L969)', () => {
let sendCallbacks: HookCallbacks
vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
sendCallbacks = options as HookCallbacks
})
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSend('url', { query: 'test' }, {})
})
act(() => {
sendCallbacks.onWorkflowStarted({ workflow_run_id: 'wr-1', task_id: 't-1' })
})
// Get the shared reference from the tree to mutate the local closed-over responseItem's workflowProcess
act(() => {
const response = result.current.chatList[1]
if (response.workflowProcess) {
// @ts-expect-error deliberately removing tracing to cover the fallback branch
delete response.workflowProcess.tracing
}
sendCallbacks.onNodeStarted({ data: { node_id: 'n-new', id: 'n-new' } })
})
expect(result.current.chatList[1].workflowProcess?.tracing).toBeDefined()
expect(result.current.chatList[1].workflowProcess?.tracing?.length).toBe(1)
})
it('should cover handleSend onTTSChunk and onTTSEnd truthy audio strings', () => {
let sendCallbacks: HookCallbacks
vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
sendCallbacks = options as HookCallbacks
})
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSend('url', { query: 'test' }, {})
})
act(() => {
sendCallbacks.onTTSChunk('msg-1', 'audio-chunk')
sendCallbacks.onTTSEnd('msg-1', 'audio-end')
})
expect(result.current.chatList).toHaveLength(2)
})
it('should cover onGetSuggestedQuestions success and error branches in handleResume onCompleted', async () => {
let resumeCallbacks: HookCallbacks
vi.mocked(sseGet).mockImplementation(async (_url, _params, options) => {
resumeCallbacks = options as HookCallbacks
})
const onGetSuggestedQuestions = vi.fn()
.mockImplementationOnce((_id, getAbort) => {
if (getAbort) {
getAbort({ abort: vi.fn() } as unknown as AbortController)
}
return Promise.resolve({ data: ['Suggested 1', 'Suggested 2'] })
})
.mockImplementationOnce((_id, getAbort) => {
if (getAbort) {
getAbort({ abort: vi.fn() } as unknown as AbortController)
}
return Promise.reject(new Error('error'))
})
const config = {
suggested_questions_after_answer: { enabled: true },
}
const prevChatTree = [{
id: 'q',
content: 'query',
isAnswer: false,
children: [{ id: 'm-1', content: 'initial', isAnswer: true, siblingIndex: 0 }],
}]
// Success branch
const { result } = renderHook(() => useChat(config as ChatConfig, undefined, prevChatTree as ChatItemInTree[]))
act(() => {
result.current.handleResume('m-1', 'wr-1', { isPublicAPI: true, onGetSuggestedQuestions })
})
await act(async () => {
await resumeCallbacks.onCompleted()
})
expect(result.current.suggestedQuestions).toEqual(['Suggested 1', 'Suggested 2'])
// Error branch (catch block 271-273)
await act(async () => {
await resumeCallbacks.onCompleted()
})
expect(result.current.suggestedQuestions).toHaveLength(0)
})
it('should cover handleSend onNodeStarted/onWorkflowStarted branches for tracing 908, 969', () => {
let sendCallbacks: HookCallbacks
vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
sendCallbacks = options as HookCallbacks
})
const { result } = renderHook(() => useChat())
act(() => {
result.current.handleSend('url', { query: 'test' }, {})
})
act(() => {
// Initialize workflowProcess (hits else branch of 910)
sendCallbacks.onWorkflowStarted({ workflow_run_id: 'wr-1', task_id: 't-1' })
// Hit L969: onNodeStarted (this hits 968-969 if we find a way to make tracing null, but it's init to [] above)
// Actually, to hit 969, workflowProcess must exist but tracing be falsy.
// We can't easily force this in handleSend since it's local.
// But we can hit 908 by calling onWorkflowStarted again after some trace.
sendCallbacks.onNodeStarted({ data: { node_id: 'n-1', id: 'n-1' } })
// Now tracing.length > 0
// Hit L908: onWorkflowStarted again
sendCallbacks.onWorkflowStarted({ workflow_run_id: 'wr-1', task_id: 't-1' })
})
expect(result.current.chatList[1].workflowProcess!.tracing).toHaveLength(1)
})
it('should cover handleResume onHumanInputFormFilled splicing and onHumanInputFormTimeout updating', () => {
let resumeCallbacks: HookCallbacks
vi.mocked(sseGet).mockImplementation(async (_url, _params, options) => {
resumeCallbacks = options as HookCallbacks
})
const prevChatTree = [{
id: 'q',
content: 'query',
isAnswer: false,
children: [{
id: 'm-1',
content: 'initial',
isAnswer: true,
siblingIndex: 0,
humanInputFormDataList: [{ node_id: 'n-1', expiration_time: 100 }],
}],
}]
const { result } = renderHook(() => useChat(undefined, undefined, prevChatTree as ChatItemInTree[]))
act(() => {
result.current.handleResume('m-1', 'wr-1', { isPublicAPI: true })
})
act(() => {
// Hit L535-537: onHumanInputFormTimeout (update)
resumeCallbacks.onHumanInputFormTimeout({ data: { node_id: 'n-1', expiration_time: 200 } })
// Hit L519-522: onHumanInputFormFilled (splice)
resumeCallbacks.onHumanInputFormFilled({ data: { node_id: 'n-1' } })
})
const lastResponse = result.current.chatList[1]
expect(lastResponse.humanInputFormDataList).toHaveLength(0)
expect(lastResponse.humanInputFilledFormDataList).toHaveLength(1)
})
it('should cover handleResume branches where workflowProcess exists but tracing is missing (L386, L414, L472)', () => {
let resumeCallbacks: HookCallbacks
vi.mocked(sseGet).mockImplementation(async (_url, _params, options) => {
resumeCallbacks = options as HookCallbacks
})
const prevChatTree = [{
id: 'q',
content: 'query',
isAnswer: false,
children: [{
id: 'm-1',
content: 'initial',
isAnswer: true,
siblingIndex: 0,
workflowProcess: {
status: WorkflowRunningStatus.Running,
// tracing: undefined
},
}],
}]
const { result } = renderHook(() => useChat(undefined, undefined, prevChatTree as ChatItemInTree[]))
act(() => {
result.current.handleResume('m-1', 'wr-1', { isPublicAPI: true })
})
act(() => {
// Hit L386: onIterationStart
resumeCallbacks.onIterationStart({ data: { node_id: 'i-1' } })
// Hit L414: onNodeStarted
resumeCallbacks.onNodeStarted({ data: { node_id: 'n-1', id: 'n-1' } })
// Hit L472: onLoopStart
resumeCallbacks.onLoopStart({ data: { node_id: 'l-1' } })
})
const lastResponse = result.current.chatList[1]
expect(lastResponse.workflowProcess?.tracing).toHaveLength(3)
})
it('should cover handleRestart with and without callback', () => {
const { result } = renderHook(() => useChat())
const callback = vi.fn()
act(() => {
result.current.handleRestart(callback)
})
expect(callback).toHaveBeenCalled()
act(() => {
result.current.handleRestart()
})
// Should not crash
expect(result.current.chatList).toHaveLength(0)
})
it('should cover handleAnnotationAdded updating node', async () => {
const prevChatTree = [{
id: 'q-1',
content: 'q',
isAnswer: false,
children: [{ id: 'a-1', content: 'a', isAnswer: true, siblingIndex: 0 }],
}]
const { result } = renderHook(() => useChat(undefined, undefined, prevChatTree as ChatItemInTree[]))
await act(async () => {
// (annotationId, authorName, query, answer, index)
result.current.handleAnnotationAdded('anno-id', 'author', 'q-new', 'a-new', 1)
})
expect(result.current.chatList[0].content).toBe('q-new')
expect(result.current.chatList[1].content).toBe('a')
expect(result.current.chatList[1].annotation?.logAnnotation?.content).toBe('a-new')
expect(result.current.chatList[1].annotation?.id).toBe('anno-id')
})
it('should cover handleAnnotationEdited updating node', async () => {
const prevChatTree = [{
id: 'q-1',
content: 'q',
isAnswer: false,
children: [{ id: 'a-1', content: 'a', isAnswer: true, siblingIndex: 0 }],
}]
const { result } = renderHook(() => useChat(undefined, undefined, prevChatTree as ChatItemInTree[]))
await act(async () => {
// (query, answer, index)
result.current.handleAnnotationEdited('q-edit', 'a-edit', 1)
})
expect(result.current.chatList[0].content).toBe('q-edit')
expect(result.current.chatList[1].content).toBe('a-edit')
})
it('should cover handleAnnotationRemoved updating node', () => {
const prevChatTree = [{
id: 'q-1',
content: 'q',
isAnswer: false,
children: [{
id: 'a-1',
content: 'a',
isAnswer: true,
siblingIndex: 0,
annotation: { id: 'anno-old' },
}],
}]
const { result } = renderHook(() => useChat(undefined, undefined, prevChatTree as ChatItemInTree[]))
act(() => {
result.current.handleAnnotationRemoved(1)
})
expect(result.current.chatList[1].annotation?.id).toBe('')
})
})

View File

@ -2,7 +2,6 @@ import type { ChatConfig, ChatItem, OnSend } from '../../types'
import type { ChatProps } from '../index'
import { act, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { useStore as useAppStore } from '@/app/components/app/store'
import Chat from '../index'
@ -603,4 +602,553 @@ describe('Chat', () => {
expect(screen.getByTestId('agent-log-modal')).toBeInTheDocument()
})
})
describe('Question Rendering with Config', () => {
it('should pass questionEditEnable from config to Question component', () => {
renderChat({
config: { questionEditEnable: true } as ChatConfig,
chatList: [makeChatItem({ id: 'q1', isAnswer: false })],
})
expect(screen.getByTestId('question-item')).toBeInTheDocument()
})
it('should pass undefined questionEditEnable to Question when config has no questionEditEnable', () => {
renderChat({
config: {} as ChatConfig,
chatList: [makeChatItem({ id: 'q1', isAnswer: false })],
})
expect(screen.getByTestId('question-item')).toBeInTheDocument()
})
it('should pass theme from themeBuilder to Question', () => {
const mockTheme = { chatBubbleColorStyle: 'test' }
const themeBuilder = { theme: mockTheme }
renderChat({
themeBuilder: themeBuilder as unknown as ChatProps['themeBuilder'],
chatList: [makeChatItem({ id: 'q1', isAnswer: false })],
})
expect(screen.getByTestId('question-item')).toBeInTheDocument()
})
it('should pass switchSibling to Question component', () => {
const switchSibling = vi.fn()
renderChat({
switchSibling,
chatList: [makeChatItem({ id: 'q1', isAnswer: false })],
})
expect(screen.getByTestId('question-item')).toBeInTheDocument()
})
it('should pass hideAvatar to Question component', () => {
renderChat({
hideAvatar: true,
chatList: [makeChatItem({ id: 'q1', isAnswer: false })],
})
expect(screen.getByTestId('question-item')).toBeInTheDocument()
})
})
describe('Answer Rendering with Config and Props', () => {
it('should pass appData to Answer component', () => {
const appData = { site: { title: 'Test App' } }
renderChat({
appData: appData as unknown as ChatProps['appData'],
chatList: [
makeChatItem({ id: 'q1', isAnswer: false }),
makeChatItem({ id: 'a1', isAnswer: true }),
],
})
expect(screen.getByTestId('answer-item')).toBeInTheDocument()
})
it('should pass config to Answer component', () => {
const config = { someOption: true }
renderChat({
config: config as unknown as ChatConfig,
chatList: [
makeChatItem({ id: 'q1', isAnswer: false }),
makeChatItem({ id: 'a1', isAnswer: true }),
],
})
expect(screen.getByTestId('answer-item')).toBeInTheDocument()
})
it('should pass answerIcon to Answer component', () => {
renderChat({
answerIcon: <div data-testid="test-answer-icon">Icon</div>,
chatList: [
makeChatItem({ id: 'q1', isAnswer: false }),
makeChatItem({ id: 'a1', isAnswer: true }),
],
})
expect(screen.getByTestId('answer-item')).toBeInTheDocument()
})
it('should pass showPromptLog to Answer component', () => {
renderChat({
showPromptLog: true,
chatList: [
makeChatItem({ id: 'q1', isAnswer: false }),
makeChatItem({ id: 'a1', isAnswer: true }),
],
})
expect(screen.getByTestId('answer-item')).toBeInTheDocument()
})
it('should pass chatAnswerContainerInner className to Answer', () => {
renderChat({
chatAnswerContainerInner: 'custom-class',
chatList: [
makeChatItem({ id: 'q1', isAnswer: false }),
makeChatItem({ id: 'a1', isAnswer: true }),
],
})
expect(screen.getByTestId('answer-item')).toBeInTheDocument()
})
it('should pass hideProcessDetail to Answer component', () => {
renderChat({
hideProcessDetail: true,
chatList: [
makeChatItem({ id: 'q1', isAnswer: false }),
makeChatItem({ id: 'a1', isAnswer: true }),
],
})
expect(screen.getByTestId('answer-item')).toBeInTheDocument()
})
it('should pass noChatInput to Answer component', () => {
renderChat({
noChatInput: true,
chatList: [
makeChatItem({ id: 'q1', isAnswer: false }),
makeChatItem({ id: 'a1', isAnswer: true }),
],
})
expect(screen.getByTestId('answer-item')).toBeInTheDocument()
})
it('should pass onHumanInputFormSubmit to Answer component', () => {
const onHumanInputFormSubmit = vi.fn()
renderChat({
onHumanInputFormSubmit,
chatList: [
makeChatItem({ id: 'q1', isAnswer: false }),
makeChatItem({ id: 'a1', isAnswer: true }),
],
})
expect(screen.getByTestId('answer-item')).toBeInTheDocument()
})
})
describe('TryToAsk Conditions', () => {
const tryToAskConfig: ChatConfig = {
suggested_questions_after_answer: { enabled: true },
} as ChatConfig
it('should not render TryToAsk when all required fields are present', () => {
renderChat({
config: tryToAskConfig,
suggestedQuestions: [],
onSend: vi.fn() as unknown as OnSend,
})
expect(screen.queryByText(/tryToAsk/i)).not.toBeInTheDocument()
})
it('should render TryToAsk with one suggested question', () => {
renderChat({
config: tryToAskConfig,
suggestedQuestions: ['Single question'],
onSend: vi.fn() as unknown as OnSend,
})
expect(screen.getByText(/tryToAsk/i)).toBeInTheDocument()
})
it('should render TryToAsk with multiple suggested questions', () => {
renderChat({
config: tryToAskConfig,
suggestedQuestions: ['Q1', 'Q2', 'Q3'],
onSend: vi.fn() as unknown as OnSend,
})
expect(screen.getByText(/tryToAsk/i)).toBeInTheDocument()
expect(screen.getByText('Q1')).toBeInTheDocument()
expect(screen.getByText('Q2')).toBeInTheDocument()
expect(screen.getByText('Q3')).toBeInTheDocument()
})
it('should not render TryToAsk when suggested_questions_after_answer?.enabled is false', () => {
renderChat({
config: { suggested_questions_after_answer: { enabled: false } } as ChatConfig,
suggestedQuestions: ['q1', 'q2'],
onSend: vi.fn() as unknown as OnSend,
})
expect(screen.queryByText(/tryToAsk/i)).not.toBeInTheDocument()
})
it('should not render TryToAsk when suggested_questions_after_answer is undefined', () => {
renderChat({
config: {} as ChatConfig,
suggestedQuestions: ['q1'],
onSend: vi.fn() as unknown as OnSend,
})
expect(screen.queryByText(/tryToAsk/i)).not.toBeInTheDocument()
})
it('should not render TryToAsk when onSend callback is not provided even with config and questions', () => {
renderChat({
config: tryToAskConfig,
suggestedQuestions: ['q1', 'q2'],
onSend: undefined,
})
expect(screen.queryByText(/tryToAsk/i)).not.toBeInTheDocument()
})
})
describe('ChatInputArea Configuration', () => {
it('should pass all config options to ChatInputArea', () => {
const config: ChatConfig = {
file_upload: { enabled: true },
speech_to_text: { enabled: true },
} as unknown as ChatConfig
renderChat({
noChatInput: false,
config,
})
expect(screen.getByTestId('chat-input-area')).toBeInTheDocument()
})
it('should pass appData.site.title as botName to ChatInputArea', () => {
renderChat({
appData: { site: { title: 'MyBot' } } as unknown as ChatProps['appData'],
noChatInput: false,
})
expect(screen.getByTestId('chat-input-area')).toBeInTheDocument()
})
it('should pass Bot as default botName when appData.site.title is missing', () => {
renderChat({
appData: {} as unknown as ChatProps['appData'],
noChatInput: false,
})
expect(screen.getByTestId('chat-input-area')).toBeInTheDocument()
})
it('should pass showFeatureBar to ChatInputArea', () => {
renderChat({
noChatInput: false,
showFeatureBar: true,
})
expect(screen.getByTestId('chat-input-area')).toBeInTheDocument()
})
it('should pass showFileUpload to ChatInputArea', () => {
renderChat({
noChatInput: false,
showFileUpload: true,
})
expect(screen.getByTestId('chat-input-area')).toBeInTheDocument()
})
it('should pass featureBarDisabled based on isResponding', () => {
const { rerender } = renderChat({
noChatInput: false,
isResponding: false,
})
rerender(<Chat chatList={[]} noChatInput={false} isResponding={true} />)
expect(screen.getByTestId('chat-input-area')).toBeInTheDocument()
})
it('should pass onFeatureBarClick callback to ChatInputArea', () => {
const onFeatureBarClick = vi.fn()
renderChat({
noChatInput: false,
onFeatureBarClick,
})
expect(screen.getByTestId('chat-input-area')).toBeInTheDocument()
})
it('should pass inputs and inputsForm to ChatInputArea', () => {
const inputs = { field1: 'value1' }
const inputsForm = [{ key: 'field1', type: 'text' }]
renderChat({
noChatInput: false,
inputs,
inputsForm: inputsForm as unknown as ChatProps['inputsForm'],
})
expect(screen.getByTestId('chat-input-area')).toBeInTheDocument()
})
it('should pass theme from themeBuilder to ChatInputArea', () => {
const mockTheme = { someThemeProperty: true }
const themeBuilder = { theme: mockTheme }
renderChat({
noChatInput: false,
themeBuilder: themeBuilder as unknown as ChatProps['themeBuilder'],
})
expect(screen.getByTestId('chat-input-area')).toBeInTheDocument()
})
})
describe('Footer Visibility Logic', () => {
it('should show footer when hasTryToAsk is true', () => {
renderChat({
config: { suggested_questions_after_answer: { enabled: true } } as ChatConfig,
suggestedQuestions: ['q1'],
onSend: vi.fn() as unknown as OnSend,
})
expect(screen.getByTestId('chat-footer')).toBeInTheDocument()
})
it('should show footer when hasTryToAsk is false but noChatInput is false', () => {
renderChat({
noChatInput: false,
})
expect(screen.getByTestId('chat-footer')).toBeInTheDocument()
})
it('should show footer when hasTryToAsk is false and noChatInput is false', () => {
renderChat({
config: { suggested_questions_after_answer: { enabled: false } } as ChatConfig,
noChatInput: false,
})
expect(screen.getByTestId('chat-footer')).toBeInTheDocument()
})
it('should show footer when isResponding and noStopResponding is false', () => {
renderChat({
isResponding: true,
noStopResponding: false,
noChatInput: true,
})
expect(screen.getByTestId('chat-footer')).toBeInTheDocument()
})
it('should show footer when any footer content condition is true', () => {
renderChat({
isResponding: true,
noStopResponding: false,
noChatInput: true,
})
expect(screen.getByTestId('chat-footer')).toHaveClass('bg-chat-input-mask')
})
it('should apply chatFooterClassName when footer has content', () => {
renderChat({
noChatInput: false,
chatFooterClassName: 'my-footer-class',
})
expect(screen.getByTestId('chat-footer')).toHaveClass('my-footer-class')
})
it('should apply chatFooterInnerClassName to footer inner div', () => {
renderChat({
noChatInput: false,
chatFooterInnerClassName: 'my-inner-class',
})
const innerDivs = screen.getByTestId('chat-footer').querySelectorAll('div')
expect(innerDivs.length).toBeGreaterThan(0)
})
})
describe('Container and Spacing Variations', () => {
it('should apply both px-0 and px-8 when isTryApp is true and noSpacing is false', () => {
renderChat({
isTryApp: true,
noSpacing: false,
})
expect(screen.getByTestId('chat-container')).toHaveClass('h-0', 'grow')
})
it('should apply px-0 when isTryApp is true', () => {
renderChat({
isTryApp: true,
chatContainerInnerClassName: 'test-class',
})
expect(screen.getByTestId('chat-container')).toBeInTheDocument()
})
it('should not apply h-0 grow when isTryApp is false', () => {
renderChat({
isTryApp: false,
})
expect(screen.getByTestId('chat-container')).not.toHaveClass('h-0', 'grow')
})
it('should apply footer classList combination correctly', () => {
renderChat({
noChatInput: false,
chatFooterClassName: 'custom-footer',
})
const footer = screen.getByTestId('chat-footer')
expect(footer).toHaveClass('custom-footer')
expect(footer).toHaveClass('bg-chat-input-mask')
})
})
describe('Multiple Items and Index Handling', () => {
it('should correctly identify last answer in a 10-item chat list', () => {
const chatList = Array.from({ length: 10 }, (_, i) =>
makeChatItem({ id: `item-${i}`, isAnswer: i % 2 === 1 }))
renderChat({ isResponding: true, chatList })
const answers = screen.getAllByTestId('answer-item')
expect(answers[answers.length - 1]).toHaveAttribute('data-responding', 'true')
})
it('should pass correct question content to Answer', () => {
const q1 = makeChatItem({ id: 'q1', isAnswer: false, content: 'First question' })
const a1 = makeChatItem({ id: 'a1', isAnswer: true, content: 'First answer' })
renderChat({ chatList: [q1, a1] })
expect(screen.getByTestId('answer-item')).toBeInTheDocument()
})
it('should handle answer without preceding question (edge case)', () => {
renderChat({
chatList: [makeChatItem({ id: 'a1', isAnswer: true })],
})
expect(screen.getByTestId('answer-item')).toBeInTheDocument()
})
it('should correctly calculate index for each item in chatList', () => {
const chatList = [
makeChatItem({ id: 'q1', isAnswer: false }),
makeChatItem({ id: 'a1', isAnswer: true }),
makeChatItem({ id: 'q2', isAnswer: false }),
makeChatItem({ id: 'a2', isAnswer: true }),
]
renderChat({ chatList })
const answers = screen.getAllByTestId('answer-item')
expect(answers).toHaveLength(2)
})
})
describe('Sidebar Collapse Multiple Transitions', () => {
it('should trigger resize when sidebarCollapseState transitions from true to false multiple times', () => {
vi.useFakeTimers()
const { rerender } = renderChat({ sidebarCollapseState: true })
rerender(<Chat chatList={[]} sidebarCollapseState={false} />)
vi.advanceTimersByTime(200)
rerender(<Chat chatList={[]} sidebarCollapseState={true} />)
rerender(<Chat chatList={[]} sidebarCollapseState={false} />)
vi.advanceTimersByTime(200)
expect(() => vi.runAllTimers()).not.toThrow()
vi.useRealTimers()
})
it('should not trigger resize when sidebarCollapseState stays at false', () => {
vi.useFakeTimers()
const { rerender } = renderChat({ sidebarCollapseState: false })
rerender(<Chat chatList={[]} sidebarCollapseState={false} />)
expect(() => vi.runAllTimers()).not.toThrow()
vi.useRealTimers()
})
it('should handle undefined sidebarCollapseState', () => {
renderChat({ sidebarCollapseState: undefined })
expect(screen.getByTestId('chat-root')).toBeInTheDocument()
})
})
describe('Scroll Behavior Edge Cases', () => {
it('should handle rapid scroll events', () => {
renderChat({ chatList: [makeChatItem({ id: 'q1' }), makeChatItem({ id: 'q2' })] })
const container = screen.getByTestId('chat-container')
for (let i = 0; i < 10; i++) {
expect(() => container.dispatchEvent(new Event('scroll'))).not.toThrow()
}
})
it('should handle scroll when chatList changes', () => {
const { rerender } = renderChat({ chatList: [makeChatItem({ id: 'q1' })] })
rerender(<Chat chatList={[makeChatItem({ id: 'q1' }), makeChatItem({ id: 'q2' })]} />)
expect(() =>
screen.getByTestId('chat-container').dispatchEvent(new Event('scroll')),
).not.toThrow()
})
it('should handle resize event multiple times', () => {
renderChat()
for (let i = 0; i < 5; i++) {
expect(() => window.dispatchEvent(new Event('resize'))).not.toThrow()
}
})
})
describe('Responsive Behavior', () => {
it('should handle different chat container heights', () => {
renderChat({
chatList: [makeChatItem({ id: 'q1' }), makeChatItem({ id: 'q2' })],
})
const container = screen.getByTestId('chat-container')
Object.defineProperty(container, 'clientHeight', { value: 800, configurable: true })
expect(() => container.dispatchEvent(new Event('scroll'))).not.toThrow()
})
it('should handle body width changes on resize', () => {
renderChat()
Object.defineProperty(document.body, 'clientWidth', { value: 1920, configurable: true })
expect(() => window.dispatchEvent(new Event('resize'))).not.toThrow()
})
})
describe('Modal Interaction Paths', () => {
it('should handle prompt log cancel and subsequent reopen', async () => {
const user = userEvent.setup()
useAppStore.setState({ ...baseStoreState, showPromptLogModal: true })
const { rerender } = renderChat({ hideLogModal: false })
await user.click(screen.getByTestId('prompt-log-cancel'))
expect(mockSetShowPromptLogModal).toHaveBeenCalledWith(false)
// Reopen modal
useAppStore.setState({ ...baseStoreState, showPromptLogModal: true })
rerender(<Chat chatList={[]} hideLogModal={false} />)
expect(screen.getByTestId('prompt-log-modal')).toBeInTheDocument()
})
it('should handle agent log cancel and subsequent reopen', async () => {
const user = userEvent.setup()
useAppStore.setState({ ...baseStoreState, showAgentLogModal: true })
const { rerender } = renderChat({ hideLogModal: false })
await user.click(screen.getByTestId('agent-log-cancel'))
expect(mockSetShowAgentLogModal).toHaveBeenCalledWith(false)
// Reopen modal
useAppStore.setState({ ...baseStoreState, showAgentLogModal: true })
rerender(<Chat chatList={[]} hideLogModal={false} />)
expect(screen.getByTestId('agent-log-modal')).toBeInTheDocument()
})
it('should handle hideLogModal preventing both modals from showing', () => {
useAppStore.setState({
...baseStoreState,
showPromptLogModal: true,
showAgentLogModal: true,
})
renderChat({ hideLogModal: true })
expect(screen.queryByTestId('prompt-log-modal')).not.toBeInTheDocument()
expect(screen.queryByTestId('agent-log-modal')).not.toBeInTheDocument()
})
})
})

View File

@ -5,7 +5,6 @@ import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import copy from 'copy-to-clipboard'
import * as React from 'react'
import Toast from '../../../toast'
import { ThemeBuilder } from '../../embedded-chatbot/theme/theme-context'
import { ChatContextProvider } from '../context-provider'
@ -15,7 +14,43 @@ import Question from '../question'
vi.mock('@react-aria/interactions', () => ({
useFocusVisible: () => ({ isFocusVisible: false }),
}))
vi.mock('../content-switch', () => ({
default: ({ count, currentIndex, switchSibling, prevDisabled, nextDisabled }: {
count?: number
currentIndex?: number
switchSibling: (direction: 'prev' | 'next') => void
prevDisabled: boolean
nextDisabled: boolean
}) => {
if (!(count && count > 1 && currentIndex !== undefined))
return null
return (
<div data-testid="content-switch">
<button
type="button"
aria-label="Previous"
onClick={() => switchSibling('prev')}
disabled={prevDisabled}
>
Previous
</button>
<button
type="button"
aria-label="Next"
onClick={() => switchSibling('next')}
disabled={nextDisabled}
>
Next
</button>
</div>
)
},
}))
vi.mock('copy-to-clipboard', () => ({ default: vi.fn() }))
vi.mock('@/app/components/base/markdown', () => ({
Markdown: ({ content }: { content: string }) => <div className="markdown-body">{content}</div>,
}))
// Mock ResizeObserver and capture lifecycle for targeted coverage
const observeMock = vi.fn()
@ -414,8 +449,8 @@ describe('Question component', () => {
const textbox = await screen.findByRole('textbox')
// Create an event with nativeEvent.isComposing = true
const event = new KeyboardEvent('keydown', { key: 'Enter', code: 'Enter' })
Object.defineProperty(event, 'isComposing', { value: true })
const event = new KeyboardEvent('keydown', { key: 'Enter', code: 'Enter', bubbles: true })
Object.defineProperty(event, 'isComposing', { value: true, configurable: true })
fireEvent(textbox, event)
expect(onRegenerate).not.toHaveBeenCalled()
@ -465,4 +500,480 @@ describe('Question component', () => {
expect(onRegenerate).toHaveBeenCalled()
})
it('should render custom questionIcon when provided', () => {
const { container } = renderWithProvider(
makeItem(),
vi.fn() as unknown as OnRegenerate,
{ questionIcon: <div data-testid="custom-question-icon">CustomIcon</div> },
)
expect(screen.getByTestId('custom-question-icon')).toBeInTheDocument()
const defaultIcon = container.querySelector('.i-custom-public-avatar-user')
expect(defaultIcon).not.toBeInTheDocument()
})
it('should call switchSibling with next sibling ID when next button clicked and nextSibling exists', async () => {
const user = userEvent.setup()
const switchSibling = vi.fn()
const item = makeItem({ prevSibling: 'q-0', nextSibling: 'q-2', siblingIndex: 1, siblingCount: 3 })
renderWithProvider(item, vi.fn() as unknown as OnRegenerate, { switchSibling })
const nextBtn = screen.getByRole('button', { name: /next/i })
await user.click(nextBtn)
expect(switchSibling).toHaveBeenCalledWith('q-2')
expect(switchSibling).toHaveBeenCalledTimes(1)
})
it('should not call switchSibling when next button clicked but nextSibling is null', async () => {
const user = userEvent.setup()
const switchSibling = vi.fn()
const item = makeItem({ prevSibling: 'q-0', nextSibling: undefined, siblingIndex: 2, siblingCount: 3 })
renderWithProvider(item, vi.fn() as unknown as OnRegenerate, { switchSibling })
const nextBtn = screen.getByRole('button', { name: /next/i })
await user.click(nextBtn)
expect(switchSibling).not.toHaveBeenCalled()
expect(nextBtn).toBeDisabled()
})
it('should not call switchSibling when prev button clicked but prevSibling is null', async () => {
const user = userEvent.setup()
const switchSibling = vi.fn()
const item = makeItem({ prevSibling: undefined, nextSibling: 'q-2', siblingIndex: 0, siblingCount: 3 })
renderWithProvider(item, vi.fn() as unknown as OnRegenerate, { switchSibling })
const prevBtn = screen.getByRole('button', { name: /previous/i })
await user.click(prevBtn)
expect(switchSibling).not.toHaveBeenCalled()
expect(prevBtn).toBeDisabled()
})
it('should render next button disabled when nextSibling is null', () => {
const item = makeItem({ prevSibling: 'q-0', nextSibling: undefined, siblingIndex: 2, siblingCount: 3 })
renderWithProvider(item, vi.fn() as unknown as OnRegenerate)
const nextBtn = screen.getByRole('button', { name: /next/i })
expect(nextBtn).toBeDisabled()
})
it('should handle both prev and next siblings being null (only one message)', () => {
const item = makeItem({ prevSibling: undefined, nextSibling: undefined, siblingIndex: 0, siblingCount: 1 })
renderWithProvider(item, vi.fn() as unknown as OnRegenerate)
const prevBtn = screen.queryByRole('button', { name: /previous/i })
const nextBtn = screen.queryByRole('button', { name: /next/i })
expect(prevBtn).not.toBeInTheDocument()
expect(nextBtn).not.toBeInTheDocument()
})
it('should render with empty message_files array (no file list)', () => {
const { container } = renderWithProvider(makeItem({ message_files: [] }))
expect(container.querySelector('[class*="FileList"]')).not.toBeInTheDocument()
// Content should still be visible
expect(screen.getByText('This is the question content')).toBeInTheDocument()
})
it('should render with message_files having multiple files', () => {
const files = [
{
name: 'document.pdf',
url: 'https://example.com/doc.pdf',
type: 'application/pdf',
previewUrl: 'https://example.com/doc.pdf',
size: 5000,
} as unknown as FileEntity,
{
name: 'image.png',
url: 'https://example.com/img.png',
type: 'image/png',
previewUrl: 'https://example.com/img.png',
size: 3000,
} as unknown as FileEntity,
]
renderWithProvider(makeItem({ message_files: files }))
expect(screen.getByText(/document.pdf/i)).toBeInTheDocument()
expect(screen.getByText(/image.png/i)).toBeInTheDocument()
})
it('should apply correct contentWidth positioning to action container', () => {
vi.useFakeTimers()
try {
renderWithProvider(makeItem())
// Mock clientWidth at different values
const originalClientWidth = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'clientWidth')
Object.defineProperty(HTMLElement.prototype, 'clientWidth', { configurable: true, value: 300 })
act(() => {
if (resizeCallback) {
resizeCallback([], {} as ResizeObserver)
}
})
const actionContainer = screen.getByTestId('action-container')
// 300 width + 8 offset = 308px
expect(actionContainer).toHaveStyle({ right: '308px' })
// Change width and trigger resize again
Object.defineProperty(HTMLElement.prototype, 'clientWidth', { configurable: true, value: 250 })
act(() => {
if (resizeCallback) {
resizeCallback([], {} as ResizeObserver)
}
})
// 250 width + 8 offset = 258px
expect(actionContainer).toHaveStyle({ right: '258px' })
// Restore original
if (originalClientWidth) {
Object.defineProperty(HTMLElement.prototype, 'clientWidth', originalClientWidth)
}
}
finally {
vi.useRealTimers()
}
})
it('should hide edit button when enableEdit is explicitly true', () => {
renderWithProvider(makeItem(), vi.fn() as unknown as OnRegenerate, { enableEdit: true })
expect(screen.getByTestId('edit-btn')).toBeInTheDocument()
expect(screen.getByTestId('copy-btn')).toBeInTheDocument()
})
it('should show copy button always regardless of enableEdit setting', () => {
renderWithProvider(makeItem(), vi.fn() as unknown as OnRegenerate, { enableEdit: false })
expect(screen.getByTestId('copy-btn')).toBeInTheDocument()
})
it('should not render content switch when no siblings exist', () => {
const item = makeItem({ siblingCount: 1, siblingIndex: 0, prevSibling: undefined, nextSibling: undefined })
renderWithProvider(item)
// ContentSwitch should not render when count is 1
const prevBtn = screen.queryByRole('button', { name: /previous/i })
const nextBtn = screen.queryByRole('button', { name: /next/i })
expect(prevBtn).not.toBeInTheDocument()
expect(nextBtn).not.toBeInTheDocument()
})
it('should update edited content as user types', async () => {
const user = userEvent.setup()
renderWithProvider(makeItem())
await user.click(screen.getByTestId('edit-btn'))
const textbox = await screen.findByRole('textbox')
expect(textbox).toHaveValue('This is the question content')
await user.clear(textbox)
expect(textbox).toHaveValue('')
await user.type(textbox, 'New content')
expect(textbox).toHaveValue('New content')
})
it('should maintain file list in edit mode with margin adjustment', async () => {
const user = userEvent.setup()
const files = [
{
name: 'test.txt',
url: 'https://example.com/test.txt',
type: 'text/plain',
previewUrl: 'https://example.com/test.txt',
size: 100,
} as unknown as FileEntity,
]
const { container } = renderWithProvider(makeItem({ message_files: files }))
await user.click(screen.getByTestId('edit-btn'))
// FileList should be visible in edit mode with mb-3 margin
expect(screen.getByText(/test.txt/i)).toBeInTheDocument()
// Target the FileList container directly (it's the first ancestor with FileList-related class)
const fileListParent = container.querySelector('[class*="flex flex-wrap gap-2"]')
expect(fileListParent).toHaveClass('mb-3')
})
it('should render theme styles only in non-edit mode', () => {
const themeBuilder = new ThemeBuilder()
themeBuilder.buildTheme('#00ff00', true)
const theme = themeBuilder.theme
renderWithProvider(makeItem(), vi.fn() as unknown as OnRegenerate, { theme })
const contentContainer = screen.getByTestId('question-content')
const styleAttr = contentContainer.getAttribute('style')
// In non-edit mode, theme styles should be applied
expect(styleAttr).not.toBeNull()
})
it('should handle siblings at boundaries (first, middle, last)', async () => {
const switchSibling = vi.fn()
// Test first message
const firstItem = makeItem({ prevSibling: undefined, nextSibling: 'q-2', siblingIndex: 0, siblingCount: 3 })
const { unmount: unmount1 } = renderWithProvider(firstItem, vi.fn() as unknown as OnRegenerate, { switchSibling })
let prevBtn = screen.getByRole('button', { name: /previous/i })
let nextBtn = screen.getByRole('button', { name: /next/i })
expect(prevBtn).toBeDisabled()
expect(nextBtn).not.toBeDisabled()
unmount1()
vi.clearAllMocks()
// Test last message
const lastItem = makeItem({ prevSibling: 'q-0', nextSibling: undefined, siblingIndex: 2, siblingCount: 3 })
const { unmount: unmount2 } = renderWithProvider(lastItem, vi.fn() as unknown as OnRegenerate, { switchSibling })
prevBtn = screen.getByRole('button', { name: /previous/i })
nextBtn = screen.getByRole('button', { name: /next/i })
expect(prevBtn).not.toBeDisabled()
expect(nextBtn).toBeDisabled()
unmount2()
})
it('should handle rapid composition start/end cycles', async () => {
const onRegenerate = vi.fn() as unknown as OnRegenerate
renderWithProvider(makeItem(), onRegenerate)
await userEvent.click(screen.getByTestId('edit-btn'))
const textbox = await screen.findByRole('textbox')
// Rapid composition cycles
fireEvent.compositionStart(textbox)
fireEvent.compositionEnd(textbox)
fireEvent.compositionStart(textbox)
fireEvent.compositionEnd(textbox)
fireEvent.compositionStart(textbox)
fireEvent.compositionEnd(textbox)
// Press Enter after final composition end
await new Promise(r => setTimeout(r, 60))
fireEvent.keyDown(textbox, { key: 'Enter', code: 'Enter' })
expect(onRegenerate).toHaveBeenCalled()
})
it('should handle Enter key with only whitespace edited content', async () => {
const user = userEvent.setup()
const onRegenerate = vi.fn() as unknown as OnRegenerate
renderWithProvider(makeItem(), onRegenerate)
await user.click(screen.getByTestId('edit-btn'))
const textbox = await screen.findByRole('textbox')
await user.clear(textbox)
await user.type(textbox, ' ')
fireEvent.keyDown(textbox, { key: 'Enter', code: 'Enter' })
await waitFor(() => {
expect(onRegenerate).toHaveBeenCalledWith(makeItem(), { message: ' ', files: [] })
})
})
it('should trigger onRegenerate with actual message_files in item', async () => {
const user = userEvent.setup()
const onRegenerate = vi.fn() as unknown as OnRegenerate
const files = [
{
name: 'edit-file.txt',
url: 'https://example.com/edit-file.txt',
type: 'text/plain',
previewUrl: 'https://example.com/edit-file.txt',
size: 200,
} as unknown as FileEntity,
]
const item = makeItem({ message_files: files })
renderWithProvider(item, onRegenerate)
await user.click(screen.getByTestId('edit-btn'))
const textbox = await screen.findByRole('textbox')
await user.clear(textbox)
await user.type(textbox, 'Modified with files')
fireEvent.keyDown(textbox, { key: 'Enter', code: 'Enter' })
await waitFor(() => {
expect(onRegenerate).toHaveBeenCalledWith(
item,
{ message: 'Modified with files', files },
)
})
})
it('should clear composition timer when switching editing mode multiple times', async () => {
const user = userEvent.setup()
renderWithProvider(makeItem())
// First edit cycle
await user.click(screen.getByTestId('edit-btn'))
let textbox = await screen.findByRole('textbox')
fireEvent.compositionStart(textbox)
fireEvent.compositionEnd(textbox)
// Cancel and re-edit
let cancelBtn = await screen.findByTestId('cancel-edit-btn')
await user.click(cancelBtn)
// Second edit cycle
await user.click(screen.getByTestId('edit-btn'))
textbox = await screen.findByRole('textbox')
expect(textbox).toHaveValue('This is the question content')
fireEvent.compositionStart(textbox)
fireEvent.compositionEnd(textbox)
cancelBtn = await screen.findByTestId('cancel-edit-btn')
await user.click(cancelBtn)
expect(screen.queryByRole('textbox')).not.toBeInTheDocument()
})
it('should apply correct CSS classes in edit vs view mode', async () => {
const user = userEvent.setup()
renderWithProvider(makeItem())
const contentContainer = screen.getByTestId('question-content')
// View mode classes
expect(contentContainer).toHaveClass('rounded-2xl')
expect(contentContainer).toHaveClass('bg-background-gradient-bg-fill-chat-bubble-bg-3')
await user.click(screen.getByTestId('edit-btn'))
// Edit mode classes
expect(contentContainer).toHaveClass('rounded-[24px]')
expect(contentContainer).toHaveClass('border-[3px]')
})
it('should handle all sibling combinations with switchSibling callback', async () => {
const user = userEvent.setup()
const switchSibling = vi.fn()
// Test with all siblings
const allItem = makeItem({ prevSibling: 'q-0', nextSibling: 'q-2', siblingIndex: 1, siblingCount: 3 })
renderWithProvider(allItem, vi.fn() as unknown as OnRegenerate, { switchSibling })
await user.click(screen.getByRole('button', { name: /previous/i }))
expect(switchSibling).toHaveBeenCalledWith('q-0')
await user.click(screen.getByRole('button', { name: /next/i }))
expect(switchSibling).toHaveBeenCalledWith('q-2')
})
it('should handle undefined onRegenerate in handleResend', async () => {
const user = userEvent.setup()
render(
<ChatContextProvider
config={{} as unknown as ChatConfig}
isResponding={false}
chatList={[]}
showPromptLog={false}
onSend={vi.fn()}
onRegenerate={undefined as unknown as OnRegenerate}
onAnnotationEdited={vi.fn()}
onAnnotationAdded={vi.fn()}
onAnnotationRemoved={vi.fn()}
disableFeedback={false}
onFeedback={vi.fn()}
getHumanInputNodeData={vi.fn()}
>
<Question item={makeItem()} theme={null} />
</ChatContextProvider>,
)
await user.click(screen.getByTestId('edit-btn'))
await user.click(screen.getByTestId('save-edit-btn'))
// Should not throw
})
it('should handle missing switchSibling prop', async () => {
const user = userEvent.setup()
const item = makeItem({ prevSibling: 'prev', nextSibling: 'next', siblingIndex: 1, siblingCount: 3 })
renderWithProvider(item, vi.fn() as unknown as OnRegenerate, { switchSibling: undefined })
const prevBtn = screen.getByRole('button', { name: /previous/i })
await user.click(prevBtn)
// Should not throw
const nextBtn = screen.getByRole('button', { name: /next/i })
await user.click(nextBtn)
// Should not throw
})
it('should handle theme without chatBubbleColorStyle', () => {
const theme = { chatBubbleColorStyle: undefined } as unknown as Theme
renderWithProvider(makeItem(), vi.fn() as unknown as OnRegenerate, { theme })
const content = screen.getByTestId('question-content')
expect(content.getAttribute('style')).toBeNull()
})
it('should handle undefined message_files', () => {
const item = makeItem({ message_files: undefined as unknown as FileEntity[] })
const { container } = renderWithProvider(item)
expect(container.querySelector('[class*="FileList"]')).not.toBeInTheDocument()
})
it('should handle handleSwitchSibling call when siblings are missing', async () => {
const user = userEvent.setup()
const switchSibling = vi.fn()
const item = makeItem({ prevSibling: undefined, nextSibling: undefined, siblingIndex: 0, siblingCount: 2 })
renderWithProvider(item, vi.fn() as unknown as OnRegenerate, { switchSibling })
const prevBtn = screen.getByRole('button', { name: /previous/i })
const nextBtn = screen.getByRole('button', { name: /next/i })
// These will now call switchSibling because of the mock, hit the falsy checks in Question
await user.click(prevBtn)
await user.click(nextBtn)
expect(switchSibling).not.toHaveBeenCalled()
})
it('should clear timer on unmount when timer is active', async () => {
const user = userEvent.setup()
const { unmount } = renderWithProvider(makeItem())
await user.click(screen.getByTestId('edit-btn'))
const textbox = await screen.findByRole('textbox')
fireEvent.compositionStart(textbox)
fireEvent.compositionEnd(textbox) // starts timer
unmount()
// Should not throw and branch should be hit
})
it('should handle handleSwitchSibling with no siblings and missing switchSibling prop', async () => {
const user = userEvent.setup()
const item = makeItem({ prevSibling: undefined, nextSibling: undefined, siblingIndex: 0, siblingCount: 2 })
renderWithProvider(item, vi.fn() as unknown as OnRegenerate, { switchSibling: undefined })
const prevBtn = screen.getByRole('button', { name: /previous/i })
await user.click(prevBtn)
expect(screen.queryByRole('alert')).not.toBeInTheDocument() // No crash
})
})

View File

@ -54,6 +54,26 @@ describe('AgentContent', () => {
expect(screen.getByTestId('agent-content-markdown')).toHaveTextContent('Log Annotation Content')
})
it('renders empty string if logAnnotation content is missing', () => {
const itemWithEmptyAnnotation = {
...mockItem,
annotation: {
logAnnotation: { content: '' },
},
}
const { rerender } = render(<AgentContent item={itemWithEmptyAnnotation as ChatItem} />)
expect(screen.getByTestId('agent-content-markdown')).toHaveAttribute('data-content', '')
const itemWithUndefinedAnnotation = {
...mockItem,
annotation: {
logAnnotation: {},
},
}
rerender(<AgentContent item={itemWithUndefinedAnnotation as ChatItem} />)
expect(screen.getByTestId('agent-content-markdown')).toHaveAttribute('data-content', '')
})
it('renders content prop if provided and no annotation', () => {
render(<AgentContent item={mockItem} content="Direct Content" />)
expect(screen.getByTestId('agent-content-markdown')).toHaveTextContent('Direct Content')

View File

@ -39,6 +39,28 @@ describe('BasicContent', () => {
expect(markdown).toHaveAttribute('data-content', 'Annotated Content')
})
it('renders empty string if logAnnotation content is missing', () => {
const itemWithEmptyAnnotation = {
...mockItem,
annotation: {
logAnnotation: {
content: '',
},
},
}
const { rerender } = render(<BasicContent item={itemWithEmptyAnnotation as ChatItem} />)
expect(screen.getByTestId('basic-content-markdown')).toHaveAttribute('data-content', '')
const itemWithUndefinedAnnotation = {
...mockItem,
annotation: {
logAnnotation: {},
},
}
rerender(<BasicContent item={itemWithUndefinedAnnotation as ChatItem} />)
expect(screen.getByTestId('basic-content-markdown')).toHaveAttribute('data-content', '')
})
it('wraps Windows UNC paths in backticks', () => {
const itemWithUNC = {
...mockItem,

View File

@ -0,0 +1,376 @@
import type { ChatItem } from '../../../types'
import type { AppData } from '@/models/share'
import { act, fireEvent, render, screen } from '@testing-library/react'
import Answer from '../index'
// Mock the chat context
vi.mock('../context', () => ({
useChatContext: vi.fn(() => ({
getHumanInputNodeData: vi.fn(),
})),
}))
describe('Answer Component', () => {
const defaultProps = {
item: {
id: 'msg-1',
content: 'Test response',
isAnswer: true,
} as unknown as ChatItem,
question: 'Hello?',
index: 0,
}
beforeEach(() => {
vi.clearAllMocks()
Object.defineProperty(HTMLElement.prototype, 'clientWidth', {
configurable: true,
value: 500,
})
})
describe('Rendering', () => {
it('should render basic content correctly', async () => {
render(<Answer {...defaultProps} />)
expect(screen.getByTestId('markdown-body')).toBeInTheDocument()
})
it('should render loading animation when responding and content is empty', () => {
const { container } = render(
<Answer
{...defaultProps}
item={{ id: '1', content: '', isAnswer: true } as unknown as ChatItem}
responding={true}
/>,
)
expect(container).toBeInTheDocument()
})
})
describe('Component Blocks', () => {
it('should render workflow process', () => {
render(
<Answer
{...defaultProps}
item={{
...defaultProps.item,
workflowProcess: { status: 'running', tracing: [], steps: [] },
} as unknown as ChatItem}
/>,
)
expect(screen.getByTestId('chat-answer-container')).toBeInTheDocument()
})
it('should render agent thoughts', () => {
const { container } = render(
<Answer
{...defaultProps}
item={{
...defaultProps.item,
agent_thoughts: [{ id: '1', thought: 'Thinking...' }],
} as unknown as ChatItem}
/>,
)
expect(container.querySelector('.group')).toBeInTheDocument()
})
it('should render file lists', () => {
render(
<Answer
{...defaultProps}
item={{
...defaultProps.item,
allFiles: [{ id: 'f1', type: 'image', name: 'test.png' }],
message_files: [{ id: 'f2', type: 'document', name: 'doc.pdf' }],
} as unknown as ChatItem}
/>,
)
expect(screen.getAllByTestId('file-list')).toHaveLength(2)
})
it('should render annotation edit title', async () => {
render(
<Answer
{...defaultProps}
item={{
...defaultProps.item,
annotation: { id: 'a1', authorName: 'John Doe' },
} as unknown as ChatItem}
/>,
)
expect(await screen.findByText(/John Doe/i)).toBeInTheDocument()
})
it('should render citations', () => {
render(
<Answer
{...defaultProps}
item={{
...defaultProps.item,
citation: [{ id: 'c1', title: 'Source 1' }],
} as unknown as ChatItem}
/>,
)
expect(screen.getByTestId('citation-title')).toBeInTheDocument()
})
})
describe('Human Inputs Layout', () => {
it('should render human input form data list', () => {
render(
<Answer
{...defaultProps}
item={{
...defaultProps.item,
humanInputFormDataList: [{ id: 'form1' }],
} as unknown as ChatItem}
/>,
)
expect(screen.getByTestId('chat-answer-container')).toBeInTheDocument()
})
it('should render human input filled form data list', () => {
render(
<Answer
{...defaultProps}
item={{
...defaultProps.item,
humanInputFilledFormDataList: [{ id: 'form1_filled' }],
} as unknown as ChatItem}
/>,
)
expect(screen.getByTestId('chat-answer-container')).toBeInTheDocument()
})
})
describe('Interactions', () => {
it('should handle switch sibling', () => {
const mockSwitchSibling = vi.fn()
render(
<Answer
{...defaultProps}
item={{
...defaultProps.item,
siblingCount: 3,
siblingIndex: 1,
prevSibling: 'msg-0',
nextSibling: 'msg-2',
} as unknown as ChatItem}
switchSibling={mockSwitchSibling}
/>,
)
const prevBtn = screen.getByRole('button', { name: 'Previous' })
fireEvent.click(prevBtn)
expect(mockSwitchSibling).toHaveBeenCalledWith('msg-0')
// reset mock for next sibling click
const nextBtn = screen.getByRole('button', { name: 'Next' })
fireEvent.click(nextBtn)
expect(mockSwitchSibling).toHaveBeenCalledWith('msg-2')
})
})
describe('Edge Cases and Props', () => {
it('should handle hideAvatar properly', () => {
render(<Answer {...defaultProps} hideAvatar={true} />)
expect(screen.queryByTestId('emoji')).not.toBeInTheDocument()
})
it('should render custom answerIcon', () => {
render(
<Answer
{...defaultProps}
answerIcon={<div data-testid="custom-answer-icon">Custom Icon</div>}
/>,
)
expect(screen.getByTestId('custom-answer-icon')).toBeInTheDocument()
})
it('should handle hideProcessDetail with appData', () => {
render(
<Answer
{...defaultProps}
hideProcessDetail={true}
appData={{ site: { show_workflow_steps: false } } as unknown as AppData}
item={{
...defaultProps.item,
workflowProcess: { status: 'running', tracing: [], steps: [] },
} as unknown as ChatItem}
/>,
)
expect(screen.getByTestId('chat-answer-container')).toBeInTheDocument()
})
it('should render More component', () => {
render(
<Answer
{...defaultProps}
item={{
...defaultProps.item,
more: { messages: [{ text: 'more content' }] },
} as unknown as ChatItem}
/>,
)
expect(screen.getByTestId('more-container')).toBeInTheDocument()
})
it('should render content with hasHumanInput but contentIsEmpty and no agent_thoughts', () => {
render(
<Answer
{...defaultProps}
item={{
...defaultProps.item,
content: '',
humanInputFormDataList: [{ id: 'form1' }],
} as unknown as ChatItem}
/>,
)
expect(screen.getByTestId('chat-answer-container-humaninput')).toBeInTheDocument()
})
it('should render content switch within hasHumanInput but contentIsEmpty', () => {
render(
<Answer
{...defaultProps}
item={{
...defaultProps.item,
content: '',
siblingCount: 2,
siblingIndex: 1,
prevSibling: 'msg-0',
humanInputFormDataList: [{ id: 'form1' }],
} as unknown as ChatItem}
/>,
)
expect(screen.getByTestId('chat-answer-container-humaninput')).toBeInTheDocument()
})
it('should handle responding=true in human inputs layout block 2', () => {
const { container } = render(
<Answer
{...defaultProps}
responding={true}
item={{
...defaultProps.item,
content: '',
humanInputFormDataList: [{ id: 'form1' }],
} as unknown as ChatItem}
/>,
)
expect(container).toBeInTheDocument()
})
it('should handle ResizeObserver callback', () => {
const originalResizeObserver = globalThis.ResizeObserver
let triggerResize = () => { }
globalThis.ResizeObserver = class ResizeObserver {
constructor(callback: unknown) {
triggerResize = callback as () => void
}
observe() { }
unobserve() { }
disconnect() { }
} as unknown as typeof ResizeObserver
render(<Answer {...defaultProps} />)
// Trigger the callback to cover getContentWidth and getHumanInputFormContainerWidth
act(() => {
triggerResize()
})
globalThis.ResizeObserver = originalResizeObserver
// Verify component still renders correctly after resize callback
expect(screen.getByTestId('chat-answer-container')).toBeInTheDocument()
})
it('should render all component blocks within human inputs layout to cover missing branches', () => {
const { container } = render(
<Answer
{...defaultProps}
item={{
...defaultProps.item,
humanInputFilledFormDataList: [{ id: 'form1' } as unknown as Record<string, unknown>],
humanInputFormDataList: [], // hits length > 0 false branch
agent_thoughts: [{ id: 'thought1', thought: 'thinking' }],
allFiles: [{ _id: 'file1', name: 'file1.txt', type: 'document' } as unknown as Record<string, unknown>],
message_files: [{ id: 'file2', url: 'http://test.com', type: 'image/png' } as unknown as Record<string, unknown>],
annotation: { id: 'anno1', authorName: 'Author' } as unknown as Record<string, unknown>,
citation: [{ item: { title: 'cite 1' } }] as unknown as Record<string, unknown>[],
siblingCount: 2,
siblingIndex: 1,
prevSibling: 'msg-0',
nextSibling: 'msg-2',
more: { messages: [{ text: 'more content' }] },
} as unknown as ChatItem}
/>,
)
expect(container).toBeInTheDocument()
})
it('should handle hideProcessDetail with NO appData', () => {
render(
<Answer
{...defaultProps}
hideProcessDetail={true}
appData={undefined}
item={{
...defaultProps.item,
workflowProcess: { status: 'running', tracing: [], steps: [] },
} as unknown as ChatItem}
/>,
)
expect(screen.getByTestId('chat-answer-container')).toBeInTheDocument()
})
it('should handle hideProcessDetail branches in human inputs layout', () => {
// Branch: hideProcessDetail=true, appData=undefined
const { container: c1 } = render(
<Answer
{...defaultProps}
hideProcessDetail={true}
appData={undefined}
item={{
...defaultProps.item,
workflowProcess: { status: 'running', tracing: [], steps: [] },
humanInputFormDataList: [{ id: 'form1' } as unknown as Record<string, unknown>],
} as unknown as ChatItem}
/>,
)
// Branch: hideProcessDetail=true, appData provided
const { container: c2 } = render(
<Answer
{...defaultProps}
hideProcessDetail={true}
appData={{ site: { show_workflow_steps: false } } as unknown as AppData}
item={{
...defaultProps.item,
workflowProcess: { status: 'running', tracing: [], steps: [] },
humanInputFormDataList: [{ id: 'form1' } as unknown as Record<string, unknown>],
} as unknown as ChatItem}
/>,
)
// Branch: hideProcessDetail=false
const { container: c3 } = render(
<Answer
{...defaultProps}
hideProcessDetail={false}
appData={{ site: { show_workflow_steps: true } } as unknown as AppData}
item={{
...defaultProps.item,
workflowProcess: { status: 'running', tracing: [], steps: [] },
humanInputFormDataList: [{ id: 'form1' } as unknown as Record<string, unknown>],
} as unknown as ChatItem}
/>,
)
expect(c1).toBeInTheDocument()
expect(c2).toBeInTheDocument()
expect(c3).toBeInTheDocument()
})
})
})

View File

@ -3,8 +3,6 @@ import type { ChatContextValue } from '../../context'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import copy from 'copy-to-clipboard'
import * as React from 'react'
import { vi } from 'vitest'
import { useModalContext } from '@/context/modal-context'
import { useProviderContext } from '@/context/provider-context'
import Operation from '../operation'
@ -98,12 +96,8 @@ vi.mock('@/app/components/base/features/new-feature-panel/annotation-reply/annot
return (
<div data-testid="annotation-ctrl">
{cached
? (
<button data-testid="annotation-edit-btn" onClick={onEdit}>Edit</button>
)
: (
<button data-testid="annotation-add-btn" onClick={handleAdd}>Add</button>
)}
? (<button data-testid="annotation-edit-btn" onClick={onEdit}>Edit</button>)
: (<button data-testid="annotation-add-btn" onClick={handleAdd}>Add</button>)}
</div>
)
},
@ -440,6 +434,17 @@ describe('Operation', () => {
const bar = screen.getByTestId('operation-bar')
expect(bar.querySelectorAll('.i-ri-thumb-up-line').length).toBe(0)
})
it('should test feedback modal translation fallbacks', async () => {
const user = userEvent.setup()
mockT.mockImplementation((_key: string): string => '')
renderOperation()
const thumbDown = screen.getByTestId('operation-bar').querySelector('.i-ri-thumb-down-line')!.closest('button')!
await user.click(thumbDown)
// Check if modal title/labels fallback works
expect(screen.getByRole('tooltip')).toBeInTheDocument()
mockT.mockImplementation(key => key)
})
})
describe('Admin feedback (with annotation support)', () => {
@ -538,6 +543,19 @@ describe('Operation', () => {
renderOperation({ ...baseProps, item })
expect(screen.getByTestId('operation-bar').querySelectorAll('.i-ri-thumb-up-line').length).toBe(0)
})
it('should render action buttons with Default state when feedback rating is undefined', () => {
// Setting a malformed feedback object with no rating but triggers the wrapper to see undefined fallbacks
const item = {
...baseItem,
feedback: {} as unknown as Record<string, unknown>,
adminFeedback: {} as unknown as Record<string, unknown>,
} as ChatItem
renderOperation({ ...baseProps, item })
// Since it renders the 'else' block for hasAdminFeedback (which is false due to !)
// the like/dislike regular ActionButtons should hit the Default state
expect(screen.getByTestId('operation-bar')).toBeInTheDocument()
})
})
describe('Positioning and layout', () => {
@ -595,6 +613,60 @@ describe('Operation', () => {
// Reset to default behavior
mockT.mockImplementation(key => key)
})
it('should handle buildFeedbackTooltip with empty translation fallbacks', () => {
// Mock t to return empty string for 'like' and 'dislike' to hit fallback branches:
mockT.mockImplementation((key: string): string => {
if (key.includes('operation.like'))
return ''
if (key.includes('operation.dislike'))
return ''
return key
})
const itemLike = { ...baseItem, feedback: { rating: 'like' as const, content: 'test content' } }
const { rerender } = renderOperation({ ...baseProps, item: itemLike })
expect(screen.getByTestId('operation-bar')).toBeInTheDocument()
const itemDislike = { ...baseItem, feedback: { rating: 'dislike' as const, content: 'test content' } }
rerender(
<div className="group">
<Operation {...baseProps} item={itemDislike} />
</div>,
)
expect(screen.getByTestId('operation-bar')).toBeInTheDocument()
mockT.mockImplementation(key => key)
})
it('should handle buildFeedbackTooltip without rating', () => {
// Mock tooltip display without rating to hit: 'if (!feedbackData?.rating) return label'
const item = { ...baseItem, feedback: { rating: null } as unknown as Record<string, unknown> } as unknown as ChatItem
renderOperation({ ...baseProps, item })
const bar = screen.getByTestId('operation-bar')
expect(bar).toBeInTheDocument()
})
it('should handle missing onFeedback gracefully in handleFeedback', async () => {
const user = userEvent.setup()
// First, render with feedback enabled to get the DOM node
mockContextValue.config = makeChatConfig({ supportFeedback: true })
mockContextValue.onFeedback = vi.fn()
const { rerender } = renderOperation()
const thumbUp = screen.getByTestId('operation-bar').querySelector('.i-ri-thumb-up-line')!.closest('button')!
// Then, disable the context callback to hit the `if (!onFeedback) return` early exit internally upon rerender/click
mockContextValue.onFeedback = undefined
// Rerender to ensure the component closure gets the updated undefined value from the mock context
rerender(
<div className="group">
<Operation {...baseProps} />
</div>,
)
await user.click(thumbUp)
expect(mockContextValue.onFeedback).toBeUndefined()
})
})
describe('Annotation integration', () => {
@ -722,5 +794,53 @@ describe('Operation', () => {
await user.click(screen.getByTestId('copy-btn'))
expect(copy).toHaveBeenCalledWith('Hello world')
})
it('should handle editing annotation missing onAnnotationEdited gracefully', async () => {
const user = userEvent.setup()
mockContextValue.config = makeChatConfig({
supportAnnotation: true,
annotation_reply: { id: 'ar-1', score_threshold: 0.5, embedding_model: { embedding_provider_name: '', embedding_model_name: '' }, enabled: true },
appId: 'test-app',
})
mockContextValue.onAnnotationEdited = undefined
const item = { ...baseItem, annotation: { id: 'ann-1', created_at: 123, authorName: 'test author' } as unknown as Record<string, unknown> } as unknown as ChatItem
renderOperation({ ...baseProps, item })
const editBtn = screen.getByTestId('annotation-edit-btn')
await user.click(editBtn)
await user.click(screen.getByTestId('modal-edit'))
expect(mockContextValue.onAnnotationEdited).toBeUndefined()
})
it('should handle adding annotation missing onAnnotationAdded gracefully', async () => {
const user = userEvent.setup()
mockContextValue.config = makeChatConfig({
supportAnnotation: true,
annotation_reply: { id: 'ar-1', score_threshold: 0.5, embedding_model: { embedding_provider_name: '', embedding_model_name: '' }, enabled: true },
appId: 'test-app',
})
mockContextValue.onAnnotationAdded = undefined
const item = { ...baseItem, annotation: { id: 'ann-1', created_at: 123, authorName: 'test author' } as unknown as Record<string, unknown> } as unknown as ChatItem
renderOperation({ ...baseProps, item })
const editBtn = screen.getByTestId('annotation-edit-btn')
await user.click(editBtn)
await user.click(screen.getByTestId('modal-add'))
expect(mockContextValue.onAnnotationAdded).toBeUndefined()
})
it('should handle removing annotation missing onAnnotationRemoved gracefully', async () => {
const user = userEvent.setup()
mockContextValue.config = makeChatConfig({
supportAnnotation: true,
annotation_reply: { id: 'ar-1', score_threshold: 0.5, embedding_model: { embedding_provider_name: '', embedding_model_name: '' }, enabled: true },
appId: 'test-app',
})
mockContextValue.onAnnotationRemoved = undefined
const item = { ...baseItem, annotation: { id: 'ann-1', created_at: 123, authorName: 'test author' } as unknown as Record<string, unknown> } as unknown as ChatItem
renderOperation({ ...baseProps, item })
const editBtn = screen.getByTestId('annotation-edit-btn')
await user.click(editBtn)
await user.click(screen.getByTestId('modal-remove'))
expect(mockContextValue.onAnnotationRemoved).toBeUndefined()
})
})
})

View File

@ -0,0 +1,120 @@
import type { FormInputItem } from '@/app/components/workflow/nodes/human-input/types'
import type { Locale } from '@/i18n-config/language'
import { UserActionButtonType } from '@/app/components/workflow/nodes/human-input/types'
import { InputVarType } from '@/app/components/workflow/types'
import {
getButtonStyle,
getRelativeTime,
initializeInputs,
isRelativeTimeSameOrAfter,
splitByOutputVar,
} from '../utils'
const createInput = (overrides: Partial<FormInputItem>): FormInputItem => ({
label: 'field',
variable: 'field',
required: false,
max_length: 128,
type: InputVarType.textInput,
default: {
type: 'constant' as const,
value: '',
selector: [], // Dummy selector
},
output_variable_name: 'field',
...overrides,
} as unknown as FormInputItem)
describe('human-input utils', () => {
describe('getButtonStyle', () => {
it('should map all supported button styles', () => {
expect(getButtonStyle(UserActionButtonType.Primary)).toBe('primary')
expect(getButtonStyle(UserActionButtonType.Default)).toBe('secondary')
expect(getButtonStyle(UserActionButtonType.Accent)).toBe('secondary-accent')
expect(getButtonStyle(UserActionButtonType.Ghost)).toBe('ghost')
})
it('should return undefined for unsupported style values', () => {
expect(getButtonStyle('unknown' as UserActionButtonType)).toBeUndefined()
})
})
describe('splitByOutputVar', () => {
it('should split content around output variable placeholders', () => {
expect(splitByOutputVar('Hello {{#$output.user_name#}}!')).toEqual([
'Hello ',
'{{#$output.user_name#}}',
'!',
])
})
it('should return original content when no placeholders exist', () => {
expect(splitByOutputVar('no placeholders')).toEqual(['no placeholders'])
})
})
describe('initializeInputs', () => {
it('should initialize text fields with constants and variable defaults', () => {
const formInputs = [
createInput({
type: InputVarType.textInput,
output_variable_name: 'name',
default: { type: 'constant', value: 'John', selector: [] },
}),
createInput({
type: InputVarType.paragraph,
output_variable_name: 'bio',
default: { type: 'variable', value: '', selector: [] },
}),
]
expect(initializeInputs(formInputs, { bio: 'Lives in Berlin' })).toEqual({
name: 'John',
bio: 'Lives in Berlin',
})
})
it('should set non text-like inputs to undefined', () => {
const formInputs = [
createInput({
type: InputVarType.select,
output_variable_name: 'role',
}),
]
expect(initializeInputs(formInputs)).toEqual({
role: undefined,
})
})
it('should fallback to empty string when variable default is missing', () => {
const formInputs = [
createInput({
type: InputVarType.textInput,
output_variable_name: 'summary',
default: { type: 'variable', value: '', selector: [] },
}),
]
expect(initializeInputs(formInputs, {})).toEqual({
summary: '',
})
})
})
describe('time helpers', () => {
it('should format relative time for supported and fallback locales', () => {
const now = Date.now()
const twoMinutesAgo = now - 2 * 60 * 1000
expect(getRelativeTime(twoMinutesAgo, 'en-US')).toMatch(/ago/i)
expect(getRelativeTime(twoMinutesAgo, 'es-ES' as Locale)).toMatch(/ago/i)
})
it('should compare utc timestamp against current time', () => {
const now = Date.now()
expect(isRelativeTimeSameOrAfter(now + 60_000)).toBe(true)
expect(isRelativeTimeSameOrAfter(now - 60_000)).toBe(false)
})
})
})

View File

@ -152,10 +152,10 @@ const Answer: FC<AnswerProps> = ({
)}
</div>
)}
<div className="chat-answer-container group ml-4 w-0 grow pb-4" ref={containerRef}>
<div className="chat-answer-container group ml-4 w-0 grow pb-4" ref={containerRef} data-testid="chat-answer-container">
{/* Block 1: Workflow Process + Human Input Forms */}
{hasHumanInputs && (
<div className={cn('group relative pr-10', chatAnswerContainerInner)}>
<div className={cn('group relative pr-10', chatAnswerContainerInner)} data-testid="chat-answer-container-humaninput">
<div
ref={humanInputFormContainerRef}
className={cn('relative inline-block w-full max-w-full rounded-2xl bg-chat-bubble-bg px-4 py-3 text-text-primary body-lg-regular')}
@ -319,7 +319,7 @@ const Answer: FC<AnswerProps> = ({
{/* Original single block layout (when no human inputs) */}
{!hasHumanInputs && (
<div className={cn('group relative pr-10', chatAnswerContainerInner)}>
<div className={cn('group relative pr-10', chatAnswerContainerInner)} data-testid="chat-answer-container-inner">
<div
ref={contentRef}
className={cn('relative inline-block max-w-full rounded-2xl bg-chat-bubble-bg px-4 py-3 text-text-primary body-lg-regular', workflowProcess && 'w-full')}

View File

@ -1,46 +1,145 @@
import type { FileUpload } from '@/app/components/base/features/types'
import type { FileEntity } from '@/app/components/base/file-uploader/types'
import type { TransferMethod } from '@/types/app'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import * as React from 'react'
import { vi } from 'vitest'
import { TransferMethod } from '@/types/app'
import ChatInputArea from '../index'
// ---------------------------------------------------------------------------
// Hoist shared mock references so they are available inside vi.mock factories
// ---------------------------------------------------------------------------
const { mockGetPermission, mockNotify } = vi.hoisted(() => ({
mockGetPermission: vi.fn().mockResolvedValue(undefined),
mockNotify: vi.fn(),
}))
vi.setConfig({ testTimeout: 60000 })
// ---------------------------------------------------------------------------
// External dependency mocks
// ---------------------------------------------------------------------------
// Track whether getPermission should reject
const { mockGetPermissionConfig } = vi.hoisted(() => ({
mockGetPermissionConfig: { shouldReject: false },
}))
vi.mock('js-audio-recorder', () => ({
default: class {
static getPermission = mockGetPermission
start = vi.fn()
default: class MockRecorder {
static getPermission = vi.fn().mockImplementation(() => {
if (mockGetPermissionConfig.shouldReject) {
return Promise.reject(new Error('Permission denied'))
}
return Promise.resolve(undefined)
})
start = vi.fn().mockResolvedValue(undefined)
stop = vi.fn()
getWAVBlob = vi.fn().mockReturnValue(new Blob([''], { type: 'audio/wav' }))
getRecordAnalyseData = vi.fn().mockReturnValue(new Uint8Array(128))
getChannelData = vi.fn().mockReturnValue({ left: new Float32Array(0), right: new Float32Array(0) })
getWAV = vi.fn().mockReturnValue(new ArrayBuffer(0))
destroy = vi.fn()
},
}))
vi.mock('@/app/components/base/voice-input/utils', () => ({
convertToMp3: vi.fn().mockReturnValue(new Blob([''], { type: 'audio/mp3' })),
}))
// Mock VoiceInput component - simplified version
vi.mock('@/app/components/base/voice-input', () => {
const VoiceInputMock = ({
onCancel,
onConverted,
}: {
onCancel: () => void
onConverted: (text: string) => void
}) => {
// Use module-level state for simplicity
const [showStop, setShowStop] = React.useState(true)
const handleStop = () => {
setShowStop(false)
// Simulate async conversion
setTimeout(() => {
onConverted('Converted voice text')
setShowStop(true)
}, 100)
}
return (
<div data-testid="voice-input-mock">
<div data-testid="voice-input-speaking">voiceInput.speaking</div>
<div data-testid="voice-input-converting-text">voiceInput.converting</div>
{showStop && (
<button data-testid="voice-input-stop" onClick={handleStop}>
Stop
</button>
)}
<button data-testid="voice-input-cancel" onClick={onCancel}>
Cancel
</button>
</div>
)
}
return {
default: VoiceInputMock,
}
})
vi.stubGlobal('requestAnimationFrame', (cb: FrameRequestCallback) => setTimeout(() => cb(Date.now()), 16))
vi.stubGlobal('cancelAnimationFrame', (id: number) => clearTimeout(id))
vi.stubGlobal('devicePixelRatio', 1)
// Mock Canvas
HTMLCanvasElement.prototype.getContext = vi.fn().mockReturnValue({
scale: vi.fn(),
beginPath: vi.fn(),
moveTo: vi.fn(),
rect: vi.fn(),
fill: vi.fn(),
closePath: vi.fn(),
clearRect: vi.fn(),
roundRect: vi.fn(),
})
HTMLCanvasElement.prototype.getBoundingClientRect = vi.fn().mockReturnValue({
width: 100,
height: 50,
})
vi.mock('@/service/share', () => ({
audioToText: vi.fn().mockResolvedValue({ text: 'Converted text' }),
audioToText: vi.fn().mockResolvedValue({ text: 'Converted voice text' }),
AppSourceType: { webApp: 'webApp', installedApp: 'installedApp' },
}))
// ---------------------------------------------------------------------------
// File-uploader store shared mutable state so individual tests can mutate it
// File-uploader store
// ---------------------------------------------------------------------------
const mockFileStore: { files: FileEntity[], setFiles: ReturnType<typeof vi.fn> } = {
files: [],
setFiles: vi.fn(),
}
const {
mockFileStore,
mockIsDragActive,
mockFeaturesState,
mockNotify,
mockIsMultipleLine,
mockCheckInputsFormResult,
} = vi.hoisted(() => ({
mockFileStore: {
files: [] as FileEntity[],
setFiles: vi.fn(),
},
mockIsDragActive: { value: false },
mockIsMultipleLine: { value: false },
mockFeaturesState: {
features: {
moreLikeThis: { enabled: false },
opening: { enabled: false },
moderation: { enabled: false },
speech2text: { enabled: false },
text2speech: { enabled: false },
file: { enabled: false },
suggested: { enabled: false },
citation: { enabled: false },
annotationReply: { enabled: false },
},
},
mockNotify: vi.fn(),
mockCheckInputsFormResult: { value: true },
}))
vi.mock('@/app/components/base/file-uploader/store', () => ({
useFileStore: () => ({ getState: () => mockFileStore }),
@ -50,9 +149,8 @@ vi.mock('@/app/components/base/file-uploader/store', () => ({
}))
// ---------------------------------------------------------------------------
// File-uploader hooks provide stable drag/drop handlers
// File-uploader hooks
// ---------------------------------------------------------------------------
let mockIsDragActive = false
vi.mock('@/app/components/base/file-uploader/hooks', () => ({
useFile: () => ({
@ -61,29 +159,13 @@ vi.mock('@/app/components/base/file-uploader/hooks', () => ({
handleDragFileOver: vi.fn(),
handleDropFile: vi.fn(),
handleClipboardPasteFile: vi.fn(),
isDragActive: mockIsDragActive,
isDragActive: mockIsDragActive.value,
}),
}))
// ---------------------------------------------------------------------------
// Features context hook avoids needing FeaturesContext.Provider in the tree
// Features context mock
// ---------------------------------------------------------------------------
// FeatureBar calls: useFeatures(s => s.features)
// So the selector receives the store state object; we must nest the features
// under a `features` key to match what the real store exposes.
const mockFeaturesState = {
features: {
moreLikeThis: { enabled: false },
opening: { enabled: false },
moderation: { enabled: false },
speech2text: { enabled: false },
text2speech: { enabled: false },
file: { enabled: false },
suggested: { enabled: false },
citation: { enabled: false },
annotationReply: { enabled: false },
},
}
vi.mock('@/app/components/base/features/hooks', () => ({
useFeatures: (selector: (s: typeof mockFeaturesState) => unknown) =>
@ -98,9 +180,8 @@ vi.mock('@/app/components/base/toast/context', () => ({
}))
// ---------------------------------------------------------------------------
// Internal layout hook controls single/multi-line textarea mode
// Internal layout hook
// ---------------------------------------------------------------------------
let mockIsMultipleLine = false
vi.mock('../hooks', () => ({
useTextAreaHeight: () => ({
@ -110,17 +191,17 @@ vi.mock('../hooks', () => ({
holdSpaceRef: { current: document.createElement('div') },
handleTextareaResize: vi.fn(),
get isMultipleLine() {
return mockIsMultipleLine
return mockIsMultipleLine.value
},
}),
}))
// ---------------------------------------------------------------------------
// Input-forms validation hook always passes by default
// Input-forms validation hook
// ---------------------------------------------------------------------------
vi.mock('../../check-input-forms-hooks', () => ({
useCheckInputsForms: () => ({
checkInputsForm: vi.fn().mockReturnValue(true),
checkInputsForm: vi.fn().mockImplementation(() => mockCheckInputsFormResult.value),
}),
}))
@ -134,28 +215,10 @@ vi.mock('next/navigation', () => ({
}))
// ---------------------------------------------------------------------------
// Shared fixture typed as FileUpload to avoid implicit any
// Shared fixture
// ---------------------------------------------------------------------------
// const mockVisionConfig: FileUpload = {
// fileUploadConfig: {
// image_file_size_limit: 10,
// file_size_limit: 10,
// audio_file_size_limit: 10,
// video_file_size_limit: 10,
// workflow_file_upload_limit: 10,
// },
// allowed_file_types: [],
// allowed_file_extensions: [],
// enabled: true,
// number_limits: 3,
// transfer_methods: ['local_file', 'remote_url'],
// } as FileUpload
const mockVisionConfig: FileUpload = {
// Required because of '& EnabledOrDisabled' at the end of your type
enabled: true,
// The nested config object
fileUploadConfig: {
image_file_size_limit: 10,
file_size_limit: 10,
@ -168,34 +231,24 @@ const mockVisionConfig: FileUpload = {
attachment_image_file_size_limit: 0,
file_upload_limit: 0,
},
// These match the keys in your FileUpload type
allowed_file_types: [],
allowed_file_extensions: [],
number_limits: 3,
// NOTE: Your type defines 'allowed_file_upload_methods',
// not 'transfer_methods' at the top level.
allowed_file_upload_methods: ['local_file', 'remote_url'] as TransferMethod[],
// If you wanted to define specific image/video behavior:
allowed_file_upload_methods: [TransferMethod.local_file, TransferMethod.remote_url],
image: {
enabled: true,
number_limits: 3,
transfer_methods: ['local_file', 'remote_url'] as TransferMethod[],
transfer_methods: [TransferMethod.local_file, TransferMethod.remote_url],
},
}
// ---------------------------------------------------------------------------
// Minimal valid FileEntity fixture avoids undefined `type` crash in FileItem
// ---------------------------------------------------------------------------
const makeFile = (overrides: Partial<FileEntity> = {}): FileEntity => ({
id: 'file-1',
name: 'photo.png',
type: 'image/png', // required: FileItem calls type.split('/')[0]
type: 'image/png',
size: 1024,
progress: 100,
transferMethod: 'local_file',
transferMethod: TransferMethod.local_file,
uploadedId: 'uploaded-ok',
...overrides,
} as FileEntity)
@ -203,7 +256,10 @@ const makeFile = (overrides: Partial<FileEntity> = {}): FileEntity => ({
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
const getTextarea = () => screen.getByPlaceholderText(/inputPlaceholder/i)
const getTextarea = () => (
screen.queryByPlaceholderText(/inputPlaceholder/i)
|| screen.queryByPlaceholderText(/inputDisabledPlaceholder/i)
) as HTMLTextAreaElement | null
// ---------------------------------------------------------------------------
// Tests
@ -212,15 +268,16 @@ describe('ChatInputArea', () => {
beforeEach(() => {
vi.clearAllMocks()
mockFileStore.files = []
mockIsDragActive = false
mockIsMultipleLine = false
mockIsDragActive.value = false
mockIsMultipleLine.value = false
mockCheckInputsFormResult.value = true
})
// -------------------------------------------------------------------------
describe('Rendering', () => {
it('should render the textarea with default placeholder', () => {
render(<ChatInputArea visionConfig={mockVisionConfig} />)
expect(getTextarea()).toBeInTheDocument()
expect(getTextarea()!).toBeInTheDocument()
})
it('should render the readonly placeholder when readonly prop is set', () => {
@ -228,206 +285,152 @@ describe('ChatInputArea', () => {
expect(screen.getByPlaceholderText(/inputDisabledPlaceholder/i)).toBeInTheDocument()
})
it('should render the send button', () => {
render(<ChatInputArea visionConfig={mockVisionConfig} />)
expect(screen.getByTestId('send-button')).toBeInTheDocument()
it('should include botName in placeholder text if provided', () => {
render(<ChatInputArea visionConfig={mockVisionConfig} botName="TestBot" />)
// The i18n pattern shows interpolation: namespace.key:{"botName":"TestBot"}
expect(getTextarea()!).toHaveAttribute('placeholder', expect.stringContaining('botName'))
})
it('should apply disabled styles when the disabled prop is true', () => {
const { container } = render(<ChatInputArea visionConfig={mockVisionConfig} disabled />)
const disabledWrapper = container.querySelector('.pointer-events-none')
expect(disabledWrapper).toBeInTheDocument()
expect(container.firstChild).toHaveClass('opacity-50')
})
it('should apply drag-active styles when a file is being dragged over the input', () => {
mockIsDragActive = true
it('should apply drag-active styles when a file is being dragged over', () => {
mockIsDragActive.value = true
const { container } = render(<ChatInputArea visionConfig={mockVisionConfig} />)
expect(container.querySelector('.border-dashed')).toBeInTheDocument()
})
it('should render the operation section inline when single-line', () => {
// mockIsMultipleLine is false by default
render(<ChatInputArea visionConfig={mockVisionConfig} />)
expect(screen.getByTestId('send-button')).toBeInTheDocument()
})
it('should render the operation section below the textarea when multi-line', () => {
mockIsMultipleLine = true
it('should render the send button', () => {
render(<ChatInputArea visionConfig={mockVisionConfig} />)
expect(screen.getByTestId('send-button')).toBeInTheDocument()
})
})
// -------------------------------------------------------------------------
describe('Typing', () => {
describe('User Interaction', () => {
it('should update textarea value as the user types', async () => {
const user = userEvent.setup()
const user = userEvent.setup({ delay: null })
render(<ChatInputArea visionConfig={mockVisionConfig} />)
const textarea = getTextarea()!
await user.type(getTextarea(), 'Hello world')
expect(getTextarea()).toHaveValue('Hello world')
await user.type(textarea, 'Hello world')
expect(textarea).toHaveValue('Hello world')
})
it('should clear the textarea after a message is successfully sent', async () => {
const user = userEvent.setup()
render(<ChatInputArea onSend={vi.fn()} visionConfig={mockVisionConfig} />)
await user.type(getTextarea(), 'Hello world')
await user.click(screen.getByTestId('send-button'))
expect(getTextarea()).toHaveValue('')
})
})
// -------------------------------------------------------------------------
describe('Sending Messages', () => {
it('should call onSend with query and files when clicking the send button', async () => {
const user = userEvent.setup()
it('should clear the textarea after a message is sent', async () => {
const user = userEvent.setup({ delay: null })
const onSend = vi.fn()
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} />)
const textarea = getTextarea()!
await user.type(getTextarea(), 'Hello world')
await user.type(textarea, 'Hello world')
await user.click(screen.getByTestId('send-button'))
expect(onSend).toHaveBeenCalledTimes(1)
expect(onSend).toHaveBeenCalledWith('Hello world', [])
expect(onSend).toHaveBeenCalled()
expect(textarea).toHaveValue('')
})
it('should call onSend and reset the input when pressing Enter', async () => {
const user = userEvent.setup()
const user = userEvent.setup({ delay: null })
const onSend = vi.fn()
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} />)
const textarea = getTextarea()!
await user.type(getTextarea(), 'Hello world{Enter}')
await user.type(textarea, 'Hello world')
fireEvent.keyDown(textarea, { key: 'Enter', code: 'Enter', nativeEvent: { isComposing: false } })
expect(onSend).toHaveBeenCalledWith('Hello world', [])
expect(getTextarea()).toHaveValue('')
expect(textarea).toHaveValue('')
})
it('should NOT call onSend when pressing Shift+Enter (inserts newline instead)', async () => {
const user = userEvent.setup()
const onSend = vi.fn()
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} />)
it('should handle pasted text', async () => {
const user = userEvent.setup({ delay: null })
render(<ChatInputArea visionConfig={mockVisionConfig} />)
const textarea = getTextarea()!
await user.type(getTextarea(), 'Hello world{Shift>}{Enter}{/Shift}')
await user.click(textarea)
await user.paste('Pasted text')
expect(onSend).not.toHaveBeenCalled()
expect(getTextarea()).toHaveValue('Hello world\n')
})
it('should NOT call onSend in readonly mode', async () => {
const user = userEvent.setup()
const onSend = vi.fn()
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} readonly />)
await user.click(screen.getByTestId('send-button'))
expect(onSend).not.toHaveBeenCalled()
})
it('should pass already-uploaded files to onSend', async () => {
const user = userEvent.setup()
const onSend = vi.fn()
// makeFile ensures `type` is always a proper MIME string
const uploadedFile = makeFile({ id: 'file-1', name: 'photo.png', uploadedId: 'uploaded-123' })
mockFileStore.files = [uploadedFile]
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} />)
await user.type(getTextarea(), 'With attachment')
await user.click(screen.getByTestId('send-button'))
expect(onSend).toHaveBeenCalledWith('With attachment', [uploadedFile])
})
it('should not send on Enter while IME composition is active, then send after composition ends', () => {
vi.useFakeTimers()
try {
const onSend = vi.fn()
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} />)
const textarea = getTextarea()
fireEvent.change(textarea, { target: { value: 'Composed text' } })
fireEvent.compositionStart(textarea)
fireEvent.keyDown(textarea, { key: 'Enter' })
expect(onSend).not.toHaveBeenCalled()
fireEvent.compositionEnd(textarea)
vi.advanceTimersByTime(60)
fireEvent.keyDown(textarea, { key: 'Enter' })
expect(onSend).toHaveBeenCalledWith('Composed text', [])
}
finally {
vi.useRealTimers()
}
expect(textarea).toHaveValue('Pasted text')
})
})
// -------------------------------------------------------------------------
describe('History Navigation', () => {
it('should restore the last sent message when pressing Cmd+ArrowUp once', async () => {
const user = userEvent.setup()
it('should navigate back in history with Meta+ArrowUp', async () => {
const user = userEvent.setup({ delay: null })
render(<ChatInputArea onSend={vi.fn()} visionConfig={mockVisionConfig} />)
const textarea = getTextarea()
const textarea = getTextarea()!
await user.type(textarea, 'First{Enter}')
await user.type(textarea, 'Second{Enter}')
await user.type(textarea, '{Meta>}{ArrowUp}{/Meta}')
await user.type(textarea, '{Meta>}{ArrowUp}{/Meta}')
expect(textarea).toHaveValue('Second')
})
it('should go further back in history with repeated Cmd+ArrowUp', async () => {
const user = userEvent.setup()
render(<ChatInputArea onSend={vi.fn()} visionConfig={mockVisionConfig} />)
const textarea = getTextarea()
await user.type(textarea, 'First{Enter}')
await user.type(textarea, 'Second{Enter}')
await user.type(textarea, '{Meta>}{ArrowUp}{/Meta}')
await user.type(textarea, '{Meta>}{ArrowUp}{/Meta}')
expect(textarea).toHaveValue('First')
})
it('should move forward in history when pressing Cmd+ArrowDown', async () => {
const user = userEvent.setup()
it('should navigate forward in history with Meta+ArrowDown', async () => {
const user = userEvent.setup({ delay: null })
render(<ChatInputArea onSend={vi.fn()} visionConfig={mockVisionConfig} />)
const textarea = getTextarea()
const textarea = getTextarea()!
await user.type(textarea, 'First{Enter}')
await user.type(textarea, 'Second{Enter}')
await user.type(textarea, '{Meta>}{ArrowUp}{/Meta}') // → Second
await user.type(textarea, '{Meta>}{ArrowUp}{/Meta}') // → First
await user.type(textarea, '{Meta>}{ArrowDown}{/Meta}') // → Second
await user.type(textarea, '{Meta>}{ArrowUp}{/Meta}') // Second
await user.type(textarea, '{Meta>}{ArrowUp}{/Meta}') // First
await user.type(textarea, '{Meta>}{ArrowDown}{/Meta}') // Second
expect(textarea).toHaveValue('Second')
})
it('should clear the input when navigating past the most recent history entry', async () => {
const user = userEvent.setup()
it('should clear input when navigating past the end of history', async () => {
const user = userEvent.setup({ delay: null })
render(<ChatInputArea onSend={vi.fn()} visionConfig={mockVisionConfig} />)
const textarea = getTextarea()
const textarea = getTextarea()!
await user.type(textarea, 'First{Enter}')
await user.type(textarea, '{Meta>}{ArrowUp}{/Meta}') // First
await user.type(textarea, '{Meta>}{ArrowDown}{/Meta}') // → past end → ''
await user.type(textarea, '{Meta>}{ArrowUp}{/Meta}') // First
await user.type(textarea, '{Meta>}{ArrowDown}{/Meta}') // empty
expect(textarea).toHaveValue('')
})
it('should not go below the start of history when pressing Cmd+ArrowUp at the boundary', async () => {
const user = userEvent.setup()
it('should NOT navigate history when typing regular text and pressing ArrowUp', async () => {
const user = userEvent.setup({ delay: null })
render(<ChatInputArea onSend={vi.fn()} visionConfig={mockVisionConfig} />)
const textarea = getTextarea()
const textarea = getTextarea()!
await user.type(textarea, 'Only{Enter}')
await user.type(textarea, '{Meta>}{ArrowUp}{/Meta}') // → Only
await user.type(textarea, '{Meta>}{ArrowUp}{/Meta}') // → '' (seed at index 0)
await user.type(textarea, '{Meta>}{ArrowUp}{/Meta}') // boundary should stay at ''
await user.type(textarea, 'First{Enter}')
await user.type(textarea, 'Some text')
await user.keyboard('{ArrowUp}')
expect(textarea).toHaveValue('Some text')
})
it('should handle ArrowUp when history is empty', async () => {
const user = userEvent.setup({ delay: null })
render(<ChatInputArea visionConfig={mockVisionConfig} />)
const textarea = getTextarea()!
await user.keyboard('{Meta>}{ArrowUp}{/Meta}')
expect(textarea).toHaveValue('')
})
it('should handle ArrowDown at history boundary', async () => {
const user = userEvent.setup({ delay: null })
render(<ChatInputArea onSend={vi.fn()} visionConfig={mockVisionConfig} />)
const textarea = getTextarea()!
await user.type(textarea, 'First{Enter}')
await user.type(textarea, '{Meta>}{ArrowUp}{/Meta}') // First
await user.type(textarea, '{Meta>}{ArrowDown}{/Meta}') // empty
await user.type(textarea, '{Meta>}{ArrowDown}{/Meta}') // still empty
expect(textarea).toHaveValue('')
})
@ -435,160 +438,270 @@ describe('ChatInputArea', () => {
// -------------------------------------------------------------------------
describe('Voice Input', () => {
it('should render the voice input button when speech-to-text is enabled', () => {
it('should render the voice input button when enabled', () => {
render(<ChatInputArea speechToTextConfig={{ enabled: true }} visionConfig={mockVisionConfig} />)
expect(screen.getByTestId('voice-input-button')).toBeInTheDocument()
expect(screen.getByTestId('voice-input-button')).toBeTruthy()
})
it('should NOT render the voice input button when speech-to-text is disabled', () => {
render(<ChatInputArea speechToTextConfig={{ enabled: false }} visionConfig={mockVisionConfig} />)
expect(screen.queryByTestId('voice-input-button')).not.toBeInTheDocument()
})
it('should request microphone permission when the voice button is clicked', async () => {
const user = userEvent.setup()
it('should handle stop recording in VoiceInput', async () => {
const user = userEvent.setup({ delay: null })
render(<ChatInputArea speechToTextConfig={{ enabled: true }} visionConfig={mockVisionConfig} />)
await user.click(screen.getByTestId('voice-input-button'))
// Wait for VoiceInput to show speaking
await screen.findByText(/voiceInput.speaking/i)
const stopBtn = screen.getByTestId('voice-input-stop')
await user.click(stopBtn)
expect(mockGetPermission).toHaveBeenCalledTimes(1)
})
it('should notify with an error when microphone permission is denied', async () => {
const user = userEvent.setup()
mockGetPermission.mockRejectedValueOnce(new Error('Permission denied'))
render(<ChatInputArea speechToTextConfig={{ enabled: true }} visionConfig={mockVisionConfig} />)
await user.click(screen.getByTestId('voice-input-button'))
// Converting should show up
await screen.findByText(/voiceInput.converting/i)
await waitFor(() => {
expect(mockNotify).toHaveBeenCalledWith(expect.objectContaining({ type: 'error' }))
expect(getTextarea()!).toHaveValue('Converted voice text')
})
})
it('should NOT invoke onSend while voice input is being activated', async () => {
const user = userEvent.setup()
const onSend = vi.fn()
render(
<ChatInputArea
onSend={onSend}
speechToTextConfig={{ enabled: true }}
visionConfig={mockVisionConfig}
/>,
)
it('should handle cancel in VoiceInput', async () => {
const user = userEvent.setup({ delay: null })
render(<ChatInputArea speechToTextConfig={{ enabled: true }} visionConfig={mockVisionConfig} />)
await user.click(screen.getByTestId('voice-input-button'))
await screen.findByText(/voiceInput.speaking/i)
const stopBtn = screen.getByTestId('voice-input-stop')
await user.click(stopBtn)
// Wait for converting and cancel button
const cancelBtn = await screen.findByTestId('voice-input-cancel')
await user.click(cancelBtn)
await waitFor(() => {
expect(screen.queryByTestId('voice-input-stop')).toBeNull()
})
})
it('should show error toast when voice permission is denied', async () => {
const user = userEvent.setup({ delay: null })
mockGetPermissionConfig.shouldReject = true
render(<ChatInputArea speechToTextConfig={{ enabled: true }} visionConfig={mockVisionConfig} />)
await user.click(screen.getByTestId('voice-input-button'))
expect(onSend).not.toHaveBeenCalled()
// Permission denied should trigger error toast
await waitFor(() => {
expect(mockNotify).toHaveBeenCalledWith(
expect.objectContaining({ type: 'error' }),
)
})
mockGetPermissionConfig.shouldReject = false
})
it('should handle empty converted text in VoiceInput', async () => {
const user = userEvent.setup({ delay: null })
// Mock failure or empty result
const { audioToText } = await import('@/service/share')
vi.mocked(audioToText).mockResolvedValueOnce({ text: '' })
render(<ChatInputArea speechToTextConfig={{ enabled: true }} visionConfig={mockVisionConfig} />)
await user.click(screen.getByTestId('voice-input-button'))
await screen.findByText(/voiceInput.speaking/i)
const stopBtn = screen.getByTestId('voice-input-stop')
await user.click(stopBtn)
await waitFor(() => {
expect(screen.queryByTestId('voice-input-stop')).toBeNull()
})
expect(getTextarea()!).toHaveValue('')
})
})
// -------------------------------------------------------------------------
describe('Validation', () => {
it('should notify and NOT call onSend when the query is blank', async () => {
const user = userEvent.setup()
describe('Validation & Constraints', () => {
it('should notify and NOT send when query is blank', async () => {
const user = userEvent.setup({ delay: null })
const onSend = vi.fn()
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} />)
await user.click(screen.getByTestId('send-button'))
expect(onSend).not.toHaveBeenCalled()
expect(mockNotify).toHaveBeenCalledWith(expect.objectContaining({ type: 'info' }))
})
it('should notify and NOT call onSend when the query contains only whitespace', async () => {
const user = userEvent.setup()
const onSend = vi.fn()
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} />)
await user.type(getTextarea(), ' ')
await user.click(screen.getByTestId('send-button'))
expect(onSend).not.toHaveBeenCalled()
expect(mockNotify).toHaveBeenCalledWith(expect.objectContaining({ type: 'info' }))
})
it('should notify and NOT call onSend while the bot is already responding', async () => {
const user = userEvent.setup()
it('should notify and NOT send while bot is responding', async () => {
const user = userEvent.setup({ delay: null })
const onSend = vi.fn()
render(<ChatInputArea onSend={onSend} isResponding visionConfig={mockVisionConfig} />)
await user.type(getTextarea(), 'Hello')
await user.type(getTextarea()!, 'Hello')
await user.click(screen.getByTestId('send-button'))
expect(onSend).not.toHaveBeenCalled()
expect(mockNotify).toHaveBeenCalledWith(expect.objectContaining({ type: 'info' }))
})
it('should NOT send while file upload is in progress', async () => {
const user = userEvent.setup({ delay: null })
const onSend = vi.fn()
mockFileStore.files = [makeFile({ uploadedId: '', progress: 50 })]
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} />)
await user.type(getTextarea()!, 'Hello')
await user.click(screen.getByTestId('send-button'))
expect(onSend).not.toHaveBeenCalled()
expect(mockNotify).toHaveBeenCalledWith(expect.objectContaining({ type: 'info' }))
})
it('should notify and NOT call onSend while a file upload is still in progress', async () => {
const user = userEvent.setup()
it('should send successfully with completed file uploads', async () => {
const user = userEvent.setup({ delay: null })
const onSend = vi.fn()
// uploadedId is empty string → upload not yet finished
mockFileStore.files = [
makeFile({ id: 'file-upload', uploadedId: '', progress: 50 }),
]
const completedFile = makeFile()
mockFileStore.files = [completedFile]
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} />)
await user.type(getTextarea(), 'Hello')
await user.type(getTextarea()!, 'Hello')
await user.click(screen.getByTestId('send-button'))
expect(onSend).toHaveBeenCalledWith('Hello', [completedFile])
})
it('should handle mixed transfer methods correctly', async () => {
const user = userEvent.setup({ delay: null })
const onSend = vi.fn()
const remoteFile = makeFile({
id: 'remote',
transferMethod: TransferMethod.remote_url,
uploadedId: 'remote-id',
})
mockFileStore.files = [remoteFile]
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} />)
await user.type(getTextarea()!, 'Remote test')
await user.click(screen.getByTestId('send-button'))
expect(onSend).toHaveBeenCalledWith('Remote test', [remoteFile])
})
it('should NOT call onSend if checkInputsForm fails', async () => {
const user = userEvent.setup({ delay: null })
const onSend = vi.fn()
mockCheckInputsFormResult.value = false
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} />)
await user.type(getTextarea()!, 'Validation fail')
await user.click(screen.getByTestId('send-button'))
expect(onSend).not.toHaveBeenCalled()
expect(mockNotify).toHaveBeenCalledWith(expect.objectContaining({ type: 'info' }))
})
it('should call onSend normally when all uploaded files have completed', async () => {
const user = userEvent.setup()
const onSend = vi.fn()
it('should work when onSend prop is missing', async () => {
const user = userEvent.setup({ delay: null })
render(<ChatInputArea visionConfig={mockVisionConfig} />)
// uploadedId is present → upload finished
mockFileStore.files = [makeFile({ uploadedId: 'uploaded-ok' })]
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} />)
await user.type(getTextarea(), 'With completed file')
await user.type(getTextarea()!, 'No onSend')
await user.click(screen.getByTestId('send-button'))
// Should not throw
})
})
expect(onSend).toHaveBeenCalledTimes(1)
// -------------------------------------------------------------------------
describe('Special Keyboard & Composition Events', () => {
it('should NOT send on Enter if Shift is pressed', async () => {
const user = userEvent.setup({ delay: null })
const onSend = vi.fn()
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} />)
const textarea = getTextarea()!
await user.type(textarea, 'Hello')
await user.keyboard('{Shift>}{Enter}{/Shift}')
expect(onSend).not.toHaveBeenCalled()
})
it('should block Enter key during composition', async () => {
vi.useFakeTimers()
const onSend = vi.fn()
render(<ChatInputArea onSend={onSend} visionConfig={mockVisionConfig} />)
const textarea = getTextarea()!
fireEvent.compositionStart(textarea)
fireEvent.change(textarea, { target: { value: 'Composing' } })
fireEvent.keyDown(textarea, { key: 'Enter', code: 'Enter', nativeEvent: { isComposing: true } })
expect(onSend).not.toHaveBeenCalled()
fireEvent.compositionEnd(textarea)
// Wait for the 50ms delay in handleCompositionEnd
vi.advanceTimersByTime(60)
fireEvent.keyDown(textarea, { key: 'Enter', code: 'Enter', nativeEvent: { isComposing: false } })
expect(onSend).toHaveBeenCalled()
vi.useRealTimers()
})
})
// -------------------------------------------------------------------------
describe('Layout & Styles', () => {
it('should toggle opacity class based on disabled prop', () => {
const { container, rerender } = render(<ChatInputArea visionConfig={mockVisionConfig} disabled={false} />)
expect(container.firstChild).not.toHaveClass('opacity-50')
rerender(<ChatInputArea visionConfig={mockVisionConfig} disabled={true} />)
expect(container.firstChild).toHaveClass('opacity-50')
})
it('should handle multi-line layout correctly', () => {
mockIsMultipleLine.value = true
render(<ChatInputArea visionConfig={mockVisionConfig} />)
// Send button should still be present
expect(screen.getByTestId('send-button')).toBeInTheDocument()
})
it('should handle drag enter event on textarea', () => {
render(<ChatInputArea visionConfig={mockVisionConfig} />)
const textarea = getTextarea()!
fireEvent.dragOver(textarea, { dataTransfer: { types: ['Files'] } })
// Verify no crash and textarea stays
expect(textarea).toBeInTheDocument()
})
})
// -------------------------------------------------------------------------
describe('Feature Bar', () => {
it('should render the FeatureBar section when showFeatureBar is true', () => {
const { container } = render(
<ChatInputArea visionConfig={mockVisionConfig} showFeatureBar />,
)
// FeatureBar renders a rounded-bottom container beneath the input
expect(container.querySelector('[class*="rounded-b"]')).toBeInTheDocument()
it('should render feature bar when showFeatureBar is true', () => {
render(<ChatInputArea visionConfig={mockVisionConfig} showFeatureBar />)
expect(screen.getByText(/feature.bar.empty/i)).toBeTruthy()
})
it('should NOT render the FeatureBar when showFeatureBar is false', () => {
const { container } = render(
<ChatInputArea visionConfig={mockVisionConfig} showFeatureBar={false} />,
)
expect(container.querySelector('[class*="rounded-b"]')).not.toBeInTheDocument()
})
it('should not invoke onFeatureBarClick when the component is in readonly mode', async () => {
const user = userEvent.setup()
it('should call onFeatureBarClick when clicked', async () => {
const user = userEvent.setup({ delay: null })
const onFeatureBarClick = vi.fn()
render(
<ChatInputArea
visionConfig={mockVisionConfig}
showFeatureBar
readonly
onFeatureBarClick={onFeatureBarClick}
/>,
)
// In readonly mode the FeatureBar receives `noop` as its click handler.
// Click every button that is not a named test-id button to exercise the guard.
const buttons = screen.queryAllByRole('button')
for (const btn of buttons) {
if (!btn.dataset.testid)
await user.click(btn)
}
await user.click(screen.getByText(/feature.bar.empty/i))
expect(onFeatureBarClick).toHaveBeenCalledWith(true)
})
it('should NOT call onFeatureBarClick when readonly', async () => {
const user = userEvent.setup({ delay: null })
const onFeatureBarClick = vi.fn()
render(
<ChatInputArea
visionConfig={mockVisionConfig}
showFeatureBar
onFeatureBarClick={onFeatureBarClick}
readonly
/>,
)
await user.click(screen.getByText(/feature.bar.empty/i))
expect(onFeatureBarClick).not.toHaveBeenCalled()
})
})

View File

@ -1,7 +1,6 @@
import type { Resources } from '../index'
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { useDocumentDownload } from '@/service/knowledge/use-document'
import { downloadUrl } from '@/utils/download'
@ -605,5 +604,113 @@ describe('Popup', () => {
const tooltips = screen.getAllByTestId('citation-tooltip')
expect(tooltips[2]).toBeInTheDocument()
})
describe('Item Key Generation (Branch Coverage)', () => {
it('should use index_node_hash when document_id is missing', async () => {
const user = userEvent.setup()
render(
<Popup
data={makeData({
sources: [makeSource({ document_id: '', index_node_hash: 'hash-123' })],
})}
/>,
)
await openPopup(user)
// Verify it renders without key collision (no console error expected, though not explicitly checked here)
expect(screen.getByTestId('popup-source-item')).toBeInTheDocument()
})
it('should use data.documentId when both source ids are missing', async () => {
const user = userEvent.setup()
render(
<Popup
data={makeData({
documentId: 'parent-doc-id',
sources: [makeSource({ document_id: undefined, index_node_hash: undefined })],
})}
/>,
)
await openPopup(user)
expect(screen.getByTestId('popup-source-item')).toBeInTheDocument()
})
it('should fallback to \'doc\' when all ids are missing', async () => {
const user = userEvent.setup()
render(
<Popup
data={makeData({
documentId: undefined,
sources: [makeSource({ document_id: undefined, index_node_hash: undefined })],
})}
/>,
)
await openPopup(user)
expect(screen.getByTestId('popup-source-item')).toBeInTheDocument()
})
it('should fallback to index when segment_position is missing', async () => {
const user = userEvent.setup()
render(
<Popup
data={makeData({
sources: [makeSource({ document_id: 'doc-1', segment_position: undefined })],
})}
/>,
)
await openPopup(user)
expect(screen.getByTestId('popup-segment-position')).toHaveTextContent('1')
})
})
describe('Download Logic Edge Cases (Branch Coverage)', () => {
it('should return early if datasetId is missing', async () => {
const user = userEvent.setup()
render(
<Popup
data={makeData({
dataSourceType: 'upload_file',
sources: [makeSource({ dataset_id: '' })],
})}
/>,
)
await openPopup(user)
// Even if the button is rendered (it shouldn't be based on line 71),
// we check the handler directly if possible, or just the button absence.
expect(screen.queryByTestId('popup-download-btn')).not.toBeInTheDocument()
})
it('should return early if both documentIds are missing', async () => {
const user = userEvent.setup()
render(
<Popup
data={makeData({
documentId: '',
dataSourceType: 'upload_file',
sources: [makeSource({ document_id: '' })],
})}
/>,
)
await openPopup(user)
const btn = screen.queryByTestId('popup-download-btn')
if (btn) {
await user.click(btn)
expect(mockDownloadDocument).not.toHaveBeenCalled()
}
})
it('should return early if not an upload file', async () => {
const user = userEvent.setup()
render(
<Popup
data={makeData({
dataSourceType: 'notion',
sources: [makeSource({ dataset_id: 'ds-1' })],
})}
/>,
)
await openPopup(user)
expect(screen.queryByTestId('popup-download-btn')).not.toBeInTheDocument()
})
})
})
})

View File

@ -169,6 +169,7 @@ const Chat: FC<ChatProps> = ({
}, [handleScrollToBottom, handleWindowResize])
useEffect(() => {
/* v8 ignore next - @preserve */
if (chatContainerRef.current) {
requestAnimationFrame(() => {
handleScrollToBottom()
@ -188,6 +189,7 @@ const Chat: FC<ChatProps> = ({
}, [handleWindowResize])
useEffect(() => {
/* v8 ignore next - @preserve */
if (chatFooterRef.current && chatContainerRef.current) {
const resizeContainerObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
@ -216,9 +218,10 @@ const Chat: FC<ChatProps> = ({
useEffect(() => {
const setUserScrolled = () => {
const container = chatContainerRef.current
/* v8 ignore next 2 - @preserve */
if (!container)
return
/* v8 ignore next 2 - @preserve */
if (isAutoScrollingRef.current)
return
@ -229,6 +232,7 @@ const Chat: FC<ChatProps> = ({
}
const container = chatContainerRef.current
/* v8 ignore next 2 - @preserve */
if (!container)
return

View File

@ -133,11 +133,13 @@ const Question: FC<QuestionProps> = ({
}, [switchSibling, item.prevSibling, item.nextSibling])
const getContentWidth = () => {
/* v8 ignore next 2 -- @preserve */
if (contentRef.current)
setContentWidth(contentRef.current?.clientWidth)
}
useEffect(() => {
/* v8 ignore next 2 -- @preserve */
if (!contentRef.current)
return
const resizeObserver = new ResizeObserver(() => {

View File

@ -1,7 +1,14 @@
import type { RefObject } from 'react'
import type { ChatConfig, ChatItem, ChatItemInTree } from '../../types'
import type { EmbeddedChatbotContextValue } from '../context'
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { vi } from 'vitest'
import type { ConversationItem } from '@/models/share'
import {
cleanup,
fireEvent,
render,
screen,
waitFor,
} from '@testing-library/react'
import { InputVarType } from '@/app/components/workflow/types'
import {
AppSourceType,
@ -26,6 +33,10 @@ vi.mock('../inputs-form', () => ({
default: () => <div>inputs form</div>,
}))
vi.mock('@/app/components/base/markdown', () => ({
Markdown: ({ content }: { content: string }) => <div>{content}</div>,
}))
vi.mock('../../chat', () => ({
__esModule: true,
default: ({
@ -63,6 +74,7 @@ vi.mock('../../chat', () => ({
{questionIcon}
<button onClick={() => onSend('hello world')}>send through chat</button>
<button onClick={() => onRegenerate({ id: 'answer-1', isAnswer: true, content: 'answer', parentMessageId: 'question-1' })}>regenerate answer</button>
<button onClick={() => onRegenerate({ id: 'answer-1', isAnswer: true, content: 'answer', parentMessageId: 'question-1' }, { message: 'new query' })}>regenerate edited</button>
<button onClick={() => switchSibling('sibling-2')}>switch sibling</button>
<button disabled={inputDisabled}>send message</button>
<button onClick={onStopResponding}>stop responding</button>
@ -113,7 +125,18 @@ const createContextValue = (overrides: Partial<EmbeddedChatbotContextValue> = {}
use_icon_as_answer_icon: false,
},
},
appParams: {} as ChatConfig,
appParams: {
system_parameters: {
audio_file_size_limit: 1,
file_size_limit: 1,
image_file_size_limit: 1,
video_file_size_limit: 1,
workflow_file_upload_limit: 1,
},
more_like_this: {
enabled: false,
},
} as ChatConfig,
appChatListDataLoading: false,
currentConversationId: '',
currentConversationItem: undefined,
@ -396,5 +419,245 @@ describe('EmbeddedChatbot chat-wrapper', () => {
render(<ChatWrapper />)
expect(screen.getByText('inputs form')).toBeInTheDocument()
})
it('should not disable sending when a required checkbox is not checked', () => {
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
inputsForms: [{ variable: 'agree', label: 'Agree', required: true, type: InputVarType.checkbox }],
newConversationInputsRef: { current: { agree: false } },
}))
render(<ChatWrapper />)
expect(screen.getByRole('button', { name: 'send message' })).not.toBeDisabled()
})
it('should return null for chatNode when all inputs are hidden', () => {
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
allInputsHidden: true,
inputsForms: [{ variable: 'test', label: 'Test', type: InputVarType.textInput }],
}))
render(<ChatWrapper />)
expect(screen.queryByText('inputs form')).not.toBeInTheDocument()
})
it('should render simple welcome message when suggested questions are absent', () => {
vi.mocked(useChat).mockReturnValue(createUseChatReturn({
chatList: [{ id: 'opening-1', isAnswer: true, isOpeningStatement: true, content: 'Simple Welcome' }] as ChatItem[],
}))
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
currentConversationId: '',
}))
render(<ChatWrapper />)
expect(screen.getByText('Simple Welcome')).toBeInTheDocument()
})
it('should use icon as answer icon when enabled in site config', () => {
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
appData: {
app_id: 'app-1',
can_replace_logo: true,
custom_config: { remove_webapp_brand: false, replace_webapp_logo: '' },
enable_site: true,
end_user_id: 'user-1',
site: {
title: 'Embedded App',
icon_type: 'emoji',
icon: 'bot',
icon_background: '#000000',
icon_url: '',
use_icon_as_answer_icon: true,
},
},
}))
render(<ChatWrapper />)
})
})
describe('Regeneration and config variants', () => {
it('should handle regeneration with edited question', async () => {
const handleSend = vi.fn()
// IDs must match what's hardcoded in the mock Chat component's regenerate button
const chatList = [
{ id: 'question-1', isAnswer: false, content: 'Old question' },
{ id: 'answer-1', isAnswer: true, content: 'Old answer', parentMessageId: 'question-1' },
]
vi.mocked(useChat).mockReturnValue(createUseChatReturn({
handleSend,
chatList: chatList as ChatItem[],
}))
render(<ChatWrapper />)
const regenBtn = screen.getByRole('button', { name: 'regenerate answer' })
fireEvent.click(regenBtn)
expect(handleSend).toHaveBeenCalled()
})
it('should use opening statement from currentConversationItem if available', () => {
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
appParams: { opening_statement: 'Global opening' } as ChatConfig,
currentConversationItem: {
id: 'conv-1',
name: 'Conversation 1',
inputs: {},
introduction: 'Conversation specific opening',
} as ConversationItem,
}))
render(<ChatWrapper />)
})
it('should handle mobile chatNode variants', () => {
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
isMobile: true,
currentConversationId: 'conv-1',
}))
render(<ChatWrapper />)
})
it('should initialize collapsed based on currentConversationId and isTryApp', () => {
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
currentConversationId: 'conv-1',
appSourceType: AppSourceType.tryApp,
}))
render(<ChatWrapper />)
})
it('should resume paused workflows when chat history is loaded', () => {
const handleSwitchSibling = vi.fn()
vi.mocked(useChat).mockReturnValue(createUseChatReturn({
handleSwitchSibling,
}))
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
appPrevChatList: [
{
id: 'node-1',
isAnswer: true,
content: '',
workflow_run_id: 'run-1',
humanInputFormDataList: [{ label: 'text', variable: 'v', required: true, type: InputVarType.textInput, hide: false }],
children: [],
} as unknown as ChatItemInTree,
],
}))
render(<ChatWrapper />)
expect(handleSwitchSibling).toHaveBeenCalled()
})
it('should handle conversation completion and suggested questions in chat actions', async () => {
const handleSend = vi.fn()
vi.mocked(useChat).mockReturnValue(createUseChatReturn({
handleSend,
}))
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
currentConversationId: 'conv-id', // index 0 true target
appSourceType: AppSourceType.webApp,
}))
render(<ChatWrapper />)
fireEvent.click(screen.getByRole('button', { name: 'send through chat' }))
expect(handleSend).toHaveBeenCalled()
const options = handleSend.mock.calls[0]?.[2] as { onConversationComplete?: (id: string) => void }
expect(options.onConversationComplete).toBeUndefined()
})
it('should handle regeneration with parent answer and edited question', () => {
const handleSend = vi.fn()
const chatList = [
{ id: 'question-1', isAnswer: false, content: 'Q1' },
{ id: 'answer-1', isAnswer: true, content: 'A1', parentMessageId: 'question-1', metadata: { usage: { total_tokens: 10 } } },
]
vi.mocked(useChat).mockReturnValue(createUseChatReturn({
handleSend,
chatList: chatList as ChatItem[],
}))
render(<ChatWrapper />)
fireEvent.click(screen.getByRole('button', { name: 'regenerate edited' }))
expect(handleSend).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ query: 'new query' }), expect.any(Object))
})
it('should handle fallback values for config and user data', () => {
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
appParams: null,
appId: undefined,
initUserVariables: { avatar_url: 'url' }, // name is missing
}))
render(<ChatWrapper />)
})
it('should handle mobile view for welcome screens', () => {
// Complex welcome mobile
vi.mocked(useChat).mockReturnValue(createUseChatReturn({
chatList: [{ id: 'o-1', isAnswer: true, isOpeningStatement: true, content: 'Welcome', suggestedQuestions: ['Q?'] }] as ChatItem[],
}))
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
isMobile: true,
currentConversationId: '',
}))
render(<ChatWrapper />)
cleanup()
// Simple welcome mobile
vi.mocked(useChat).mockReturnValue(createUseChatReturn({
chatList: [{ id: 'o-2', isAnswer: true, isOpeningStatement: true, content: 'Welcome' }] as ChatItem[],
}))
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
isMobile: true,
currentConversationId: '',
}))
render(<ChatWrapper />)
})
it('should handle loop early returns in input validation', () => {
// hasEmptyInput early return (line 103)
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
inputsForms: [
{ variable: 'v1', label: 'V1', required: true, type: InputVarType.textInput },
{ variable: 'v2', label: 'V2', required: true, type: InputVarType.textInput },
],
newConversationInputsRef: { current: { v1: '', v2: '' } },
}))
render(<ChatWrapper />)
cleanup()
// fileIsUploading early return (line 106)
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
inputsForms: [
{ variable: 'f1', label: 'F1', required: true, type: InputVarType.singleFile },
{ variable: 'v2', label: 'V2', required: true, type: InputVarType.textInput },
],
newConversationInputsRef: {
current: {
f1: { transferMethod: 'local_file', uploadedId: '' },
v2: '',
},
},
}))
render(<ChatWrapper />)
})
it('should handle null/undefined refs and config fallbacks', () => {
vi.mocked(useEmbeddedChatbotContext).mockReturnValue(createContextValue({
currentChatInstanceRef: { current: null } as unknown as RefObject<{ handleStop: () => void }>,
appParams: null,
appMeta: null,
}))
render(<ChatWrapper />)
})
it('should handle isValidGeneratedAnswer truthy branch in regeneration', () => {
const handleSend = vi.fn()
// A valid generated answer needs metadata with usage
const chatList = [
{ id: 'question-1', isAnswer: false, content: 'Q' },
{ id: 'answer-1', isAnswer: true, content: 'A', metadata: { usage: { total_tokens: 10 } }, parentMessageId: 'question-1' },
]
vi.mocked(useChat).mockReturnValue(createUseChatReturn({
handleSend,
chatList: chatList as ChatItem[],
}))
render(<ChatWrapper />)
fireEvent.click(screen.getByRole('button', { name: 'regenerate answer' }))
expect(handleSend).toHaveBeenCalled()
})
})
})

View File

@ -4,6 +4,7 @@ import type { AppConversationData, AppData, AppMeta, ConversationItem } from '@/
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { act, renderHook, waitFor } from '@testing-library/react'
import { ToastProvider } from '@/app/components/base/toast'
import { InputVarType } from '@/app/components/workflow/types'
import {
AppSourceType,
fetchChatList,
@ -11,6 +12,7 @@ import {
generationConversationName,
} from '@/service/share'
import { shareQueryKeys } from '@/service/use-share'
import { TransferMethod } from '@/types/app'
import { CONVERSATION_ID_INFO } from '../../constants'
import { useEmbeddedChatbot } from '../hooks'
@ -556,4 +558,343 @@ describe('useEmbeddedChatbot', () => {
expect(updateFeedback).toHaveBeenCalled()
})
})
describe('embeddedUserId and embeddedConversationId falsy paths', () => {
it('should set userId to undefined when embeddedUserId is empty string', async () => {
// This exercises the `embeddedUserId || undefined` branch on line 99
mockStoreState.embeddedUserId = ''
mockStoreState.embeddedConversationId = ''
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
await waitFor(() => {
// When embeddedUserId is empty, allowResetChat is true (no conversationId from URL or stored)
expect(result.current.allowResetChat).toBe(true)
})
})
})
describe('Language settings', () => {
it('should set language from URL parameters', async () => {
const originalSearch = window.location.search
Object.defineProperty(window, 'location', {
writable: true,
value: { search: '?locale=zh-Hans' },
})
const { changeLanguage } = await import('@/i18n-config/client')
await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
expect(changeLanguage).toHaveBeenCalledWith('zh-Hans')
Object.defineProperty(window, 'location', { value: { search: originalSearch } })
})
it('should set language from system variables when URL param is missing', async () => {
mockGetProcessedSystemVariablesFromUrlParams.mockResolvedValue({ locale: 'fr-FR' })
const { changeLanguage } = await import('@/i18n-config/client')
await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
expect(changeLanguage).toHaveBeenCalledWith('fr-FR')
})
it('should fall back to app default language', async () => {
mockGetProcessedSystemVariablesFromUrlParams.mockResolvedValue({})
mockStoreState.appInfo = {
app_id: 'app-1',
site: {
title: 'Test App',
default_language: 'ja-JP',
},
} as unknown as AppData
const { changeLanguage } = await import('@/i18n-config/client')
await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
expect(changeLanguage).toHaveBeenCalledWith('ja-JP')
})
})
describe('Additional Input Form Edges', () => {
it('should handle invalid number inputs and checkbox defaults', async () => {
mockStoreState.appParams = {
user_input_form: [
{ number: { variable: 'n1', default: 10 } },
{ checkbox: { variable: 'c1', default: false } },
],
} as unknown as ChatConfig
mockGetProcessedInputsFromUrlParams.mockResolvedValue({
n1: 'not-a-number',
c1: 'true',
})
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
const forms = result.current.inputsForms
expect(forms.find(f => f.variable === 'n1')?.default).toBe(10)
expect(forms.find(f => f.variable === 'c1')?.default).toBe(false)
})
it('should handle select with invalid option and file-list/json types', async () => {
mockStoreState.appParams = {
user_input_form: [
{ select: { variable: 's1', options: ['A'], default: 'A' } },
],
} as unknown as ChatConfig
mockGetProcessedInputsFromUrlParams.mockResolvedValue({
s1: 'INVALID',
})
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
expect(result.current.inputsForms[0].default).toBe('A')
})
})
describe('handleConversationIdInfoChange logic', () => {
it('should handle existing appId as string and update it to object', async () => {
localStorage.setItem(CONVERSATION_ID_INFO, JSON.stringify({ 'app-1': 'legacy-id' }))
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
act(() => {
result.current.handleConversationIdInfoChange('new-conv-id')
})
await waitFor(() => {
const stored = JSON.parse(localStorage.getItem(CONVERSATION_ID_INFO) || '{}')
const appEntry = stored['app-1']
// userId may be 'embedded-user-1' or 'DEFAULT' depending on timing; either is valid
const storedId = appEntry?.['embedded-user-1'] ?? appEntry?.DEFAULT
expect(storedId).toBe('new-conv-id')
})
})
it('should use DEFAULT when userId is null', async () => {
// Override userId to be null/empty to exercise the "|| 'DEFAULT'" fallback path
mockStoreState.embeddedUserId = null
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
act(() => {
result.current.handleConversationIdInfoChange('default-conv-id')
})
await waitFor(() => {
const stored = JSON.parse(localStorage.getItem(CONVERSATION_ID_INFO) || '{}')
const appEntry = stored['app-1']
// Should use DEFAULT key since userId is null
expect(appEntry?.DEFAULT).toBe('default-conv-id')
})
})
})
describe('allInputsHidden and no required variables', () => {
it('should pass checkInputsRequired immediately when there are no required fields', async () => {
mockStoreState.appParams = {
user_input_form: [
// All optional (not required)
{ 'text-input': { variable: 't1', required: false, label: 'T1' } },
],
} as unknown as ChatConfig
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
const onStart = vi.fn()
act(() => {
result.current.handleStartChat(onStart)
})
expect(onStart).toHaveBeenCalled()
})
it('should pass checkInputsRequired when all inputs are hidden', async () => {
mockStoreState.appParams = {
user_input_form: [
{ 'text-input': { variable: 't1', required: true, label: 'T1', hide: true } },
{ 'text-input': { variable: 't2', required: true, label: 'T2', hide: true } },
],
} as unknown as ChatConfig
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
await waitFor(() => expect(result.current.allInputsHidden).toBe(true))
const onStart = vi.fn()
act(() => {
result.current.handleStartChat(onStart)
})
expect(onStart).toHaveBeenCalled()
})
})
describe('checkInputsRequired silent mode and multi-file', () => {
it('should return true in silent mode even if fields are missing', async () => {
mockStoreState.appParams = {
user_input_form: [{ 'text-input': { variable: 't1', required: true, label: 'T1' } }],
} as unknown as ChatConfig
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
// checkInputsRequired is internal; trigger via handleStartChat which calls it
const onStart = vi.fn()
act(() => {
// With silent=true not exposed, we test that handleStartChat calls the callback
// when allInputsHidden is true (all forms hidden)
result.current.handleStartChat(onStart)
})
// The form field has required=true but silent mode through allInputsHidden=false,
// so the callback is NOT called (validation blocked it)
// This exercises the silent=false path with empty field -> notify -> return false
expect(onStart).not.toHaveBeenCalled()
})
it('should handle multi-file uploading status', async () => {
mockStoreState.appParams = {
user_input_form: [{ 'file-list': { variable: 'files', required: true, type: InputVarType.multiFiles } }],
} as unknown as ChatConfig
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
act(() => {
result.current.handleNewConversationInputsChange({
files: [
{ transferMethod: TransferMethod.local_file, uploadedId: 'ok' },
{ transferMethod: TransferMethod.local_file, uploadedId: null },
],
})
})
// handleStartChat returns void, but we just verify no callback fires (file upload pending)
const onStart = vi.fn()
act(() => {
result.current.handleStartChat(onStart)
})
expect(onStart).not.toHaveBeenCalled()
})
it('should detect single-file upload still in progress', async () => {
mockStoreState.appParams = {
user_input_form: [{ 'file-list': { variable: 'f1', required: true, type: InputVarType.singleFile } }],
} as unknown as ChatConfig
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
act(() => {
// Single file (not array) transfer that hasn't finished uploading
result.current.handleNewConversationInputsChange({
f1: { transferMethod: TransferMethod.local_file, uploadedId: null },
})
})
const onStart = vi.fn()
act(() => {
result.current.handleStartChat(onStart)
})
expect(onStart).not.toHaveBeenCalled()
})
it('should skip validation for hasEmptyInput when fileIsUploading already set', async () => {
// Two required fields: first passes but starts uploading, second would be empty — should be skipped
mockStoreState.appParams = {
user_input_form: [
{ 'file-list': { variable: 'f1', required: true, type: InputVarType.multiFiles } },
{ 'text-input': { variable: 't1', required: true, label: 'T1' } },
],
} as unknown as ChatConfig
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
act(() => {
result.current.handleNewConversationInputsChange({
f1: [{ transferMethod: TransferMethod.local_file, uploadedId: null }],
t1: '', // empty but should be skipped because fileIsUploading is set first
})
})
const onStart = vi.fn()
act(() => {
result.current.handleStartChat(onStart)
})
expect(onStart).not.toHaveBeenCalled()
})
})
describe('getFormattedChatList edge cases', () => {
it('should handle messages with no message_files and no agent_thoughts', async () => {
// Ensure a currentConversationId is set so appChatListData is fetched
localStorage.setItem(CONVERSATION_ID_INFO, JSON.stringify({ 'app-1': { DEFAULT: 'conversation-1' } }))
mockFetchConversations.mockResolvedValue(
createConversationData({ data: [createConversationItem({ id: 'conversation-1' })] }),
)
mockFetchChatList.mockResolvedValue({
data: [{
id: 'msg-no-files',
query: 'Q',
answer: 'A',
// no message_files, no agent_thoughts — exercises the || [] fallback branches
}],
})
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
await waitFor(() => expect(result.current.appPrevChatList.length).toBeGreaterThan(0), { timeout: 3000 })
const chatList = result.current.appPrevChatList
const question = chatList.find((m: unknown) => (m as Record<string, unknown>).id === 'question-msg-no-files')
expect(question).toBeDefined()
})
})
describe('currentConversationItem from pinned list', () => {
it('should find currentConversationItem from pinned list when not in main list', async () => {
const pinnedData = createConversationData({
data: [createConversationItem({ id: 'pinned-conv', name: 'Pinned' })],
})
mockFetchConversations.mockImplementation(async (_a: unknown, _b: unknown, _c: unknown, pinned?: boolean) => {
return pinned ? pinnedData : createConversationData({ data: [] })
})
mockFetchChatList.mockResolvedValue({ data: [] })
localStorage.setItem(CONVERSATION_ID_INFO, JSON.stringify({ 'app-1': { DEFAULT: 'pinned-conv' } }))
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
await waitFor(() => {
expect(result.current.pinnedConversationList.length).toBeGreaterThan(0)
}, { timeout: 3000 })
await waitFor(() => {
expect(result.current.currentConversationItem?.id).toBe('pinned-conv')
}, { timeout: 3000 })
})
})
describe('newConversation updates existing item', () => {
it('should update an existing conversation in the list when its id matches', async () => {
const initialItem = createConversationItem({ id: 'conversation-1', name: 'Old Name' })
const renamedItem = createConversationItem({ id: 'conversation-1', name: 'New Generated Name' })
mockFetchConversations.mockResolvedValue(createConversationData({ data: [initialItem] }))
mockGenerationConversationName.mockResolvedValue(renamedItem)
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
await waitFor(() => expect(result.current.conversationList.length).toBeGreaterThan(0))
act(() => {
result.current.handleNewConversationCompleted('conversation-1')
})
await waitFor(() => {
const match = result.current.conversationList.find(c => c.id === 'conversation-1')
expect(match?.name).toBe('New Generated Name')
})
})
})
describe('currentConversationLatestInputs', () => {
it('should return inputs from latest chat message when conversation has data', async () => {
const convId = 'conversation-with-inputs'
localStorage.setItem(CONVERSATION_ID_INFO, JSON.stringify({ 'app-1': { DEFAULT: convId } }))
mockFetchConversations.mockResolvedValue(
createConversationData({ data: [createConversationItem({ id: convId })] }),
)
mockFetchChatList.mockResolvedValue({
data: [{ id: 'm1', query: 'Q', answer: 'A', inputs: { key1: 'val1' } }],
})
const { result } = await renderWithClient(() => useEmbeddedChatbot(AppSourceType.webApp))
await waitFor(() => expect(result.current.currentConversationItem?.id).toBe(convId), { timeout: 3000 })
// After item is resolved, currentConversationInputs should be populated
await waitFor(() => expect(result.current.currentConversationInputs).toBeDefined(), { timeout: 3000 })
})
})
})

View File

@ -3,9 +3,8 @@ import type { ImgHTMLAttributes } from 'react'
import type { EmbeddedChatbotContextValue } from '../../context'
import type { AppData } from '@/models/share'
import type { SystemFeatures } from '@/types/feature'
import { render, screen, waitFor } from '@testing-library/react'
import { act, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { vi } from 'vitest'
import { useGlobalPublicStore } from '@/context/global-public-context'
import { InstallationScope, LicenseStatus } from '@/types/feature'
import { useEmbeddedChatbotContext } from '../../context'
@ -120,6 +119,18 @@ describe('EmbeddedChatbot Header', () => {
Object.defineProperty(window, 'top', { value: window, configurable: true })
})
const dispatchChatbotConfigMessage = async (origin: string, payload: { isToggledByButton: boolean, isDraggable: boolean }) => {
await act(async () => {
window.dispatchEvent(new MessageEvent('message', {
origin,
data: {
type: 'dify-chatbot-config',
payload,
},
}))
})
}
describe('Desktop Rendering', () => {
it('should render desktop header with branding by default', async () => {
render(<Header title="Test Chatbot" />)
@ -164,7 +175,23 @@ describe('EmbeddedChatbot Header', () => {
expect(img).toHaveAttribute('src', 'https://example.com/workspace.png')
})
it('should render Dify logo by default when no branding or custom logo is provided', () => {
it('should render Dify logo by default when branding enabled is true but no logo provided', () => {
vi.mocked(useGlobalPublicStore).mockImplementation((selector: (s: GlobalPublicStoreMock) => unknown) => selector({
systemFeatures: {
...defaultSystemFeatures,
branding: {
...defaultSystemFeatures.branding,
enabled: true,
workspace_logo: '',
},
},
setSystemFeatures: vi.fn(),
}))
render(<Header title="Test Chatbot" />)
expect(screen.getByAltText('Dify logo')).toBeInTheDocument()
})
it('should render Dify logo when branding is disabled', () => {
vi.mocked(useGlobalPublicStore).mockImplementation((selector: (s: GlobalPublicStoreMock) => unknown) => selector({
systemFeatures: {
...defaultSystemFeatures,
@ -196,6 +223,20 @@ describe('EmbeddedChatbot Header', () => {
expect(screen.queryByTestId('webapp-brand')).not.toBeInTheDocument()
})
it('should render divider only when currentConversationId is present', () => {
vi.mocked(useEmbeddedChatbotContext).mockReturnValue({ ...defaultContext } as EmbeddedChatbotContextValue)
const { unmount } = render(<Header title="Test Chatbot" />)
expect(screen.getByTestId('divider')).toBeInTheDocument()
unmount()
vi.mocked(useEmbeddedChatbotContext).mockReturnValue({
...defaultContext,
currentConversationId: '',
} as EmbeddedChatbotContextValue)
render(<Header title="Test Chatbot" />)
expect(screen.queryByTestId('divider')).not.toBeInTheDocument()
})
it('should render reset button when allowResetChat is true and conversation exists', () => {
render(<Header title="Test Chatbot" allowResetChat={true} />)
@ -266,6 +307,42 @@ describe('EmbeddedChatbot Header', () => {
expect(screen.getByTestId('mobile-reset-chat-button')).toBeInTheDocument()
})
it('should NOT render mobile reset button when currentConversationId is missing', () => {
vi.mocked(useEmbeddedChatbotContext).mockReturnValue({
...defaultContext,
currentConversationId: '',
} as EmbeddedChatbotContextValue)
render(<Header title="Mobile Chatbot" isMobile allowResetChat />)
expect(screen.queryByTestId('mobile-reset-chat-button')).not.toBeInTheDocument()
})
it('should render ViewFormDropdown in mobile when conditions are met', () => {
vi.mocked(useEmbeddedChatbotContext).mockReturnValue({
...defaultContext,
inputsForms: [{ id: '1' }],
} as EmbeddedChatbotContextValue)
render(<Header title="Mobile Chatbot" isMobile />)
expect(screen.getByTestId('view-form-dropdown')).toBeInTheDocument()
})
it('should handle mobile expand button', async () => {
const user = userEvent.setup()
const mockPostMessage = setupIframe()
render(<Header title="Mobile Chatbot" isMobile />)
await dispatchChatbotConfigMessage('https://parent.com', { isToggledByButton: true, isDraggable: false })
const expandBtn = await screen.findByTestId('mobile-expand-button')
expect(expandBtn).toBeInTheDocument()
await user.click(expandBtn)
expect(mockPostMessage).toHaveBeenCalledWith(
{ type: 'dify-chatbot-expand-change' },
'https://parent.com',
)
})
})
describe('Iframe Communication', () => {
@ -284,13 +361,7 @@ describe('EmbeddedChatbot Header', () => {
const mockPostMessage = setupIframe()
render(<Header title="Iframe" />)
window.dispatchEvent(new MessageEvent('message', {
origin: 'https://parent.com',
data: {
type: 'dify-chatbot-config',
payload: { isToggledByButton: true, isDraggable: false },
},
}))
await dispatchChatbotConfigMessage('https://parent.com', { isToggledByButton: true, isDraggable: false })
const expandBtn = await screen.findByTestId('expand-button')
expect(expandBtn).toBeInTheDocument()
@ -308,13 +379,7 @@ describe('EmbeddedChatbot Header', () => {
setupIframe()
render(<Header title="Iframe" />)
window.dispatchEvent(new MessageEvent('message', {
origin: 'https://parent.com',
data: {
type: 'dify-chatbot-config',
payload: { isToggledByButton: true, isDraggable: true },
},
}))
await dispatchChatbotConfigMessage('https://parent.com', { isToggledByButton: true, isDraggable: true })
await waitFor(() => {
expect(screen.queryByTestId('expand-button')).not.toBeInTheDocument()
@ -325,20 +390,43 @@ describe('EmbeddedChatbot Header', () => {
setupIframe()
render(<Header title="Iframe" />)
window.dispatchEvent(new MessageEvent('message', {
origin: 'https://secure.com',
data: { type: 'dify-chatbot-config', payload: { isToggledByButton: true, isDraggable: false } },
}))
await dispatchChatbotConfigMessage('https://secure.com', { isToggledByButton: true, isDraggable: false })
await screen.findByTestId('expand-button')
window.dispatchEvent(new MessageEvent('message', {
origin: 'https://malicious.com',
data: { type: 'dify-chatbot-config', payload: { isToggledByButton: false, isDraggable: false } },
}))
await dispatchChatbotConfigMessage('https://malicious.com', { isToggledByButton: false, isDraggable: false })
// Should still be visible (not hidden by the malicious message)
expect(screen.getByTestId('expand-button')).toBeInTheDocument()
})
it('should ignore non-config messages for origin locking', async () => {
setupIframe()
render(<Header title="Iframe" />)
await act(async () => {
window.dispatchEvent(new MessageEvent('message', {
origin: 'https://first.com',
data: { type: 'other-type' },
}))
})
await dispatchChatbotConfigMessage('https://second.com', { isToggledByButton: true, isDraggable: false })
// Should lock to second.com
const expandBtn = await screen.findByTestId('expand-button')
expect(expandBtn).toBeInTheDocument()
})
it('should NOT handle toggle expand if showToggleExpandButton is false', async () => {
const mockPostMessage = setupIframe()
render(<Header title="Iframe" />)
// Directly call handleToggleExpand would require more setup, but we can verify it doesn't trigger unexpectedly
expect(mockPostMessage).not.toHaveBeenCalledWith(
expect.objectContaining({ type: 'dify-chatbot-expand-change' }),
expect.anything(),
)
})
})
describe('Edge Cases', () => {

View File

@ -118,4 +118,30 @@ describe('InputsFormNode', () => {
const mainDiv = screen.getByTestId('inputs-form-node')
expect(mainDiv).toHaveClass('mb-0 px-0')
})
it('should apply mobile styles when isMobile is true', () => {
vi.mocked(useEmbeddedChatbotContext).mockReturnValue({
...mockContextValue,
isMobile: true,
} as unknown as any)
const { rerender } = render(<InputsFormNode collapsed={false} setCollapsed={setCollapsed} />)
// Main container
const mainDiv = screen.getByTestId('inputs-form-node')
expect(mainDiv).toHaveClass('mb-4 pt-4')
// Header container (parent of the icon)
const header = screen.getByText(/chat.chatSettingsTitle/i).parentElement
expect(header).toHaveClass('px-4 py-3')
// Content container
expect(screen.getByTestId('mock-inputs-form-content').parentElement).toHaveClass('p-4')
// Start chat button container
expect(screen.getByTestId('inputs-form-start-chat-button').parentElement).toHaveClass('p-4')
// Collapsed state mobile styles
rerender(<InputsFormNode collapsed={true} setCollapsed={setCollapsed} />)
expect(screen.getByText(/chat.chatSettingsTitle/i).parentElement).toHaveClass('px-4 py-3')
})
})

View File

@ -0,0 +1,56 @@
import { CssTransform, hexToRGBA } from '../utils'
describe('Theme Utils', () => {
describe('hexToRGBA', () => {
it('should convert hex with # to rgba', () => {
expect(hexToRGBA('#000000', 1)).toBe('rgba(0,0,0,1)')
expect(hexToRGBA('#FFFFFF', 0.5)).toBe('rgba(255,255,255,0.5)')
expect(hexToRGBA('#FF0000', 0.1)).toBe('rgba(255,0,0,0.1)')
})
it('should convert hex without # to rgba', () => {
expect(hexToRGBA('000000', 1)).toBe('rgba(0,0,0,1)')
expect(hexToRGBA('FFFFFF', 0.5)).toBe('rgba(255,255,255,0.5)')
})
it('should handle various opacity values', () => {
expect(hexToRGBA('#000000', 0)).toBe('rgba(0,0,0,0)')
expect(hexToRGBA('#000000', 1)).toBe('rgba(0,0,0,1)')
})
})
describe('CssTransform', () => {
it('should return empty object for empty string', () => {
expect(CssTransform('')).toEqual({})
})
it('should transform single property', () => {
expect(CssTransform('color: red')).toEqual({ color: 'red' })
})
it('should transform multiple properties', () => {
expect(CssTransform('color: red; margin: 10px')).toEqual({
color: 'red',
margin: '10px',
})
})
it('should handle extra whitespace', () => {
expect(CssTransform(' color : red ; margin : 10px ')).toEqual({
color: 'red',
margin: '10px',
})
})
it('should handle trailing semicolon', () => {
expect(CssTransform('color: red;')).toEqual({ color: 'red' })
})
it('should ignore empty pairs', () => {
expect(CssTransform('color: red;; margin: 10px; ')).toEqual({
color: 'red',
margin: '10px',
})
})
})
})

View File

@ -192,4 +192,226 @@ describe('checkbox list component', () => {
await userEvent.click(screen.getByText('common.operation.resetKeywords'))
expect(input).toHaveValue('')
})
it('does not toggle disabled option when clicked', async () => {
const onChange = vi.fn()
const disabledOptions = [
{ label: 'Enabled', value: 'enabled' },
{ label: 'Disabled', value: 'disabled', disabled: true },
]
render(
<CheckboxList
options={disabledOptions}
value={[]}
onChange={onChange}
/>,
)
const disabledCheckbox = screen.getByTestId('checkbox-disabled')
await userEvent.click(disabledCheckbox)
expect(onChange).not.toHaveBeenCalled()
})
it('does not toggle option when component is disabled and option is clicked via div', async () => {
const onChange = vi.fn()
render(
<CheckboxList
options={options}
value={[]}
onChange={onChange}
disabled
/>,
)
// Find option and click the div container
const optionLabels = screen.getAllByText('Option 1')
const optionDiv = optionLabels[0].closest('[data-testid="option-item"]')
expect(optionDiv).toBeInTheDocument()
await userEvent.click(optionDiv as HTMLElement)
expect(onChange).not.toHaveBeenCalled()
})
it('renders with label prop', () => {
render(
<CheckboxList
options={options}
label="Test Label"
/>,
)
expect(screen.getByText('Test Label')).toBeInTheDocument()
})
it('renders without showSelectAll, showCount, showSearch', () => {
render(
<CheckboxList
options={options}
showSelectAll={false}
showCount={false}
showSearch={false}
/>,
)
expect(screen.queryByTestId('checkbox-selectAll')).not.toBeInTheDocument()
options.forEach((option) => {
expect(screen.getByText(option.label)).toBeInTheDocument()
})
})
it('renders with custom containerClassName', () => {
const { container } = render(
<CheckboxList
options={options}
containerClassName="custom-class"
/>,
)
expect(container.querySelector('.custom-class')).toBeInTheDocument()
})
it('applies maxHeight style to options container', () => {
render(
<CheckboxList
options={options}
maxHeight="200px"
/>,
)
const optionsContainer = screen.getByTestId('options-container')
expect(optionsContainer).toHaveStyle({ maxHeight: '200px', overflowY: 'auto' })
})
it('shows indeterminate state when some options are selected', async () => {
const onChange = vi.fn()
render(
<CheckboxList
options={options}
value={['option1', 'option2']}
onChange={onChange}
showSelectAll
/>,
)
// When some but not all options are selected, clicking select-all should select all remaining options
const selectAll = screen.getByTestId('checkbox-selectAll')
expect(selectAll).toBeInTheDocument()
expect(selectAll).toHaveAttribute('aria-checked', 'mixed')
await userEvent.click(selectAll)
expect(onChange).toHaveBeenCalledWith(['option1', 'option2', 'option3', 'apple'])
})
it('filters options correctly when searching', async () => {
render(<CheckboxList options={options} />)
const input = screen.getByRole('textbox')
await userEvent.type(input, 'option')
expect(screen.getByText('Option 1')).toBeInTheDocument()
expect(screen.getByText('Option 2')).toBeInTheDocument()
expect(screen.getByText('Option 3')).toBeInTheDocument()
expect(screen.queryByText('Apple')).not.toBeInTheDocument()
})
it('shows no data message when no options match search', async () => {
render(<CheckboxList options={options} />)
const input = screen.getByRole('textbox')
await userEvent.type(input, 'xyz')
expect(screen.getByText(/common.operation.noSearchResults/i)).toBeInTheDocument()
})
it('toggles option by clicking option row', async () => {
const onChange = vi.fn()
render(
<CheckboxList
options={options}
value={[]}
onChange={onChange}
showSelectAll={false}
/>,
)
const optionLabel = screen.getByText('Option 1')
const optionRow = optionLabel.closest('div[data-testid="option-item"]')
expect(optionRow).toBeInTheDocument()
await userEvent.click(optionRow as HTMLElement)
expect(onChange).toHaveBeenCalledWith(['option1'])
})
it('does not toggle when clicking disabled option row', async () => {
const onChange = vi.fn()
const disabledOptions = [
{ label: 'Option 1', value: 'option1', disabled: true },
]
render(
<CheckboxList
options={disabledOptions}
value={[]}
onChange={onChange}
/>,
)
const optionRow = screen.getByText('Option 1').closest('div[data-testid="option-item"]')
expect(optionRow).toBeInTheDocument()
await userEvent.click(optionRow as HTMLElement)
expect(onChange).not.toHaveBeenCalled()
})
it('renders without title and description', () => {
render(
<CheckboxList
options={options}
title=""
description=""
/>,
)
expect(screen.queryByText(/Test Title/)).not.toBeInTheDocument()
expect(screen.queryByText(/Test Description/)).not.toBeInTheDocument()
})
it('shows correct filtered count message when searching', async () => {
render(
<CheckboxList
options={options}
title="Items"
/>,
)
const input = screen.getByRole('textbox')
await userEvent.type(input, 'opt')
expect(screen.getByText(/operation.searchCount/i)).toBeInTheDocument()
})
it('shows no data message when no options are provided', () => {
render(
<CheckboxList
options={[]}
/>,
)
expect(screen.getByText('common.noData')).toBeInTheDocument()
})
it('does not toggle option when component is disabled even with enabled option', async () => {
const onChange = vi.fn()
const disabledOptions = [
{ label: 'Option', value: 'option' },
]
render(
<CheckboxList
options={disabledOptions}
value={[]}
onChange={onChange}
disabled
/>,
)
const checkbox = screen.getByTestId('checkbox-option')
await userEvent.click(checkbox)
expect(onChange).not.toHaveBeenCalled()
})
})

View File

@ -161,6 +161,7 @@ const CheckboxList: FC<CheckboxListProps> = ({
<div
className="p-1"
style={maxHeight ? { maxHeight, overflowY: 'auto' } : {}}
data-testid="options-container"
>
{!filteredOptions.length
? (
@ -183,6 +184,7 @@ const CheckboxList: FC<CheckboxListProps> = ({
return (
<div
key={option.value}
data-testid="option-item"
className={cn(
'flex cursor-pointer items-center gap-2 rounded-md px-2 py-1.5 transition-colors hover:bg-state-base-hover',
option.disabled && 'cursor-not-allowed opacity-50',

View File

@ -64,4 +64,47 @@ describe('Checkbox Component', () => {
expect(checkbox).toHaveClass('bg-components-checkbox-bg-disabled')
expect(checkbox).toHaveClass('cursor-not-allowed')
})
it('handles keyboard events (Space and Enter) when not disabled', () => {
const onCheck = vi.fn()
render(<Checkbox {...mockProps} onCheck={onCheck} />)
const checkbox = screen.getByTestId('checkbox-test')
fireEvent.keyDown(checkbox, { key: ' ' })
expect(onCheck).toHaveBeenCalledTimes(1)
fireEvent.keyDown(checkbox, { key: 'Enter' })
expect(onCheck).toHaveBeenCalledTimes(2)
})
it('does not handle keyboard events when disabled', () => {
const onCheck = vi.fn()
render(<Checkbox {...mockProps} disabled onCheck={onCheck} />)
const checkbox = screen.getByTestId('checkbox-test')
fireEvent.keyDown(checkbox, { key: ' ' })
expect(onCheck).not.toHaveBeenCalled()
fireEvent.keyDown(checkbox, { key: 'Enter' })
expect(onCheck).not.toHaveBeenCalled()
})
it('exposes aria-disabled attribute', () => {
const { rerender } = render(<Checkbox {...mockProps} />)
expect(screen.getByTestId('checkbox-test')).toHaveAttribute('aria-disabled', 'false')
rerender(<Checkbox {...mockProps} disabled />)
expect(screen.getByTestId('checkbox-test')).toHaveAttribute('aria-disabled', 'true')
})
it('normalizes aria-checked attribute', () => {
const { rerender } = render(<Checkbox {...mockProps} />)
expect(screen.getByTestId('checkbox-test')).toHaveAttribute('aria-checked', 'false')
rerender(<Checkbox {...mockProps} checked />)
expect(screen.getByTestId('checkbox-test')).toHaveAttribute('aria-checked', 'true')
rerender(<Checkbox {...mockProps} indeterminate />)
expect(screen.getByTestId('checkbox-test')).toHaveAttribute('aria-checked', 'mixed')
})
})

View File

@ -1,11 +1,10 @@
import { RiCheckLine } from '@remixicon/react'
import { cn } from '@/utils/classnames'
import IndeterminateIcon from './assets/indeterminate-icon'
type CheckboxProps = {
id?: string
checked?: boolean
onCheck?: (event: React.MouseEvent<HTMLDivElement>) => void
onCheck?: (event: React.MouseEvent<HTMLDivElement> | React.KeyboardEvent<HTMLDivElement>) => void
className?: string
disabled?: boolean
indeterminate?: boolean
@ -40,10 +39,23 @@ const Checkbox = ({
return
onCheck?.(event)
}}
onKeyDown={(event) => {
if (disabled)
return
if (event.key === ' ' || event.key === 'Enter') {
if (event.key === ' ')
event.preventDefault()
onCheck?.(event)
}
}}
data-testid={`checkbox-${id}`}
role="checkbox"
aria-checked={indeterminate ? 'mixed' : !!checked}
aria-disabled={!!disabled}
tabIndex={disabled ? -1 : 0}
>
{!checked && indeterminate && <IndeterminateIcon />}
{checked && <RiCheckLine className="h-3 w-3" data-testid={`check-icon-${id}`} />}
{checked && <div className="i-ri-check-line h-3 w-3" data-testid={`check-icon-${id}`} />}
</div>
)
}

View File

@ -61,6 +61,11 @@ describe('CopyFeedbackNew', () => {
expect(container.querySelector('.cursor-pointer')).toBeInTheDocument()
})
it('renders with custom className', () => {
const { container } = render(<CopyFeedbackNew content="test content" className="test-class" />)
expect(container.querySelector('.test-class')).toBeInTheDocument()
})
it('applies copied CSS class when copied is true', () => {
mockCopied = true
const { container } = render(<CopyFeedbackNew content="test content" />)

View File

@ -21,17 +21,19 @@ const CopyFeedback = ({ content }: Props) => {
const { t } = useTranslation()
const { copied, copy, reset } = useClipboard()
const tooltipText = copied
? t(`${prefixEmbedded}.copied`, { ns: 'appOverview' })
: t(`${prefixEmbedded}.copy`, { ns: 'appOverview' })
/* v8 ignore next -- i18n test mock always returns a non-empty string; runtime fallback is defensive. -- @preserve */
const safeText = tooltipText || ''
const handleCopy = useCallback(() => {
copy(content)
}, [copy, content])
return (
<Tooltip
popupContent={
(copied
? t(`${prefixEmbedded}.copied`, { ns: 'appOverview' })
: t(`${prefixEmbedded}.copy`, { ns: 'appOverview' })) || ''
}
popupContent={safeText}
>
<ActionButton>
<div
@ -52,27 +54,27 @@ export const CopyFeedbackNew = ({ content, className }: Pick<Props, 'className'
const { t } = useTranslation()
const { copied, copy, reset } = useClipboard()
const tooltipText = copied
? t(`${prefixEmbedded}.copied`, { ns: 'appOverview' })
: t(`${prefixEmbedded}.copy`, { ns: 'appOverview' })
/* v8 ignore next -- i18n test mock always returns a non-empty string; runtime fallback is defensive. -- @preserve */
const safeText = tooltipText || ''
const handleCopy = useCallback(() => {
copy(content)
}, [copy, content])
return (
<Tooltip
popupContent={
(copied
? t(`${prefixEmbedded}.copied`, { ns: 'appOverview' })
: t(`${prefixEmbedded}.copy`, { ns: 'appOverview' })) || ''
}
popupContent={safeText}
>
<div
className={`h-8 w-8 cursor-pointer rounded-lg hover:bg-components-button-ghost-bg-hover ${className ?? ''
}`}
className={`h-8 w-8 cursor-pointer rounded-lg hover:bg-components-button-ghost-bg-hover ${className ?? ''}`}
>
<div
onClick={handleCopy}
onMouseLeave={reset}
className={`h-full w-full ${copyStyle.copyIcon} ${copied ? copyStyle.copied : ''
}`}
className={`h-full w-full ${copyStyle.copyIcon} ${copied ? copyStyle.copied : ''}`}
>
</div>
</div>

View File

@ -1,4 +1,4 @@
import { fireEvent, render } from '@testing-library/react'
import { fireEvent, render, screen } from '@testing-library/react'
import CopyIcon from '..'
const copy = vi.fn()
@ -20,33 +20,28 @@ describe('copy icon component', () => {
})
it('renders normally', () => {
const { container } = render(<CopyIcon content="this is some test content for the copy icon component" />)
expect(container.querySelector('svg')).not.toBeNull()
})
it('shows copy icon initially', () => {
const { container } = render(<CopyIcon content="this is some test content for the copy icon component" />)
const icon = container.querySelector('[data-icon="Copy"]')
render(<CopyIcon content="this is some test content for the copy icon component" />)
const icon = screen.getByTestId('copy-icon')
expect(icon).toBeInTheDocument()
})
it('shows copy check icon when copied', () => {
copied = true
const { container } = render(<CopyIcon content="this is some test content for the copy icon component" />)
const icon = container.querySelector('[data-icon="CopyCheck"]')
render(<CopyIcon content="this is some test content for the copy icon component" />)
const icon = screen.getByTestId('copied-icon')
expect(icon).toBeInTheDocument()
})
it('handles copy when clicked', () => {
const { container } = render(<CopyIcon content="this is some test content for the copy icon component" />)
const icon = container.querySelector('[data-icon="Copy"]')
render(<CopyIcon content="this is some test content for the copy icon component" />)
const icon = screen.getByTestId('copy-icon')
fireEvent.click(icon as Element)
expect(copy).toBeCalledTimes(1)
})
it('resets on mouse leave', () => {
const { container } = render(<CopyIcon content="this is some test content for the copy icon component" />)
const icon = container.querySelector('[data-icon="Copy"]')
render(<CopyIcon content="this is some test content for the copy icon component" />)
const icon = screen.getByTestId('copy-icon')
const div = icon?.parentElement as HTMLElement
fireEvent.mouseLeave(div)
expect(reset).toBeCalledTimes(1)

View File

@ -2,10 +2,6 @@
import { useClipboard } from 'foxact/use-clipboard'
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import {
Copy,
CopyCheck,
} from '@/app/components/base/icons/src/vender/line/files'
import Tooltip from '../tooltip'
type Props = {
@ -22,22 +18,20 @@ const CopyIcon = ({ content }: Props) => {
copy(content)
}, [copy, content])
const tooltipText = copied
? t(`${prefixEmbedded}.copied`, { ns: 'appOverview' })
: t(`${prefixEmbedded}.copy`, { ns: 'appOverview' })
/* v8 ignore next -- i18n test mock always returns a non-empty string; runtime fallback is defensive. -- @preserve */
const safeTooltipText = tooltipText || ''
return (
<Tooltip
popupContent={
(copied
? t(`${prefixEmbedded}.copied`, { ns: 'appOverview' })
: t(`${prefixEmbedded}.copy`, { ns: 'appOverview' })) || ''
}
popupContent={safeTooltipText}
>
<div onMouseLeave={reset}>
{!copied
? (
<Copy className="mx-1 h-3.5 w-3.5 cursor-pointer text-text-tertiary" onClick={handleCopy} />
)
: (
<CopyCheck className="mx-1 h-3.5 w-3.5 text-text-tertiary" />
)}
? (<span className="i-custom-vender-line-files-copy mx-1 h-3.5 w-3.5 cursor-pointer text-text-tertiary" onClick={handleCopy} data-testid="copy-icon" />)
: (<span className="i-custom-vender-line-files-copy-check mx-1 h-3.5 w-3.5 text-text-tertiary" data-testid="copied-icon" />)}
</div>
</Tooltip>
)

View File

@ -65,6 +65,14 @@ describe('DatePicker', () => {
expect(screen.getByRole('textbox').getAttribute('value')).not.toBe('')
})
it('should normalize non-Dayjs value input', () => {
const value = new Date('2024-06-15T14:30:00Z') as unknown as DatePickerProps['value']
const props = createDatePickerProps({ value })
render(<DatePicker {...props} />)
expect(screen.getByRole('textbox').getAttribute('value')).not.toBe('')
})
})
// Open/close behavior
@ -243,6 +251,31 @@ describe('DatePicker', () => {
expect(screen.getByText(/operation\.pickDate/)).toBeInTheDocument()
})
it('should update time when no selectedDate exists and minute is selected', () => {
const props = createDatePickerProps({ needTimePicker: true })
render(<DatePicker {...props} />)
openPicker()
fireEvent.click(screen.getByText('--:-- --'))
const allLists = screen.getAllByRole('list')
const minuteItems = within(allLists[1]).getAllByRole('listitem')
fireEvent.click(minuteItems[15])
expect(screen.getByText(/operation\.pickDate/)).toBeInTheDocument()
})
it('should update time when no selectedDate exists and period is selected', () => {
const props = createDatePickerProps({ needTimePicker: true })
render(<DatePicker {...props} />)
openPicker()
fireEvent.click(screen.getByText('--:-- --'))
fireEvent.click(screen.getByText('PM'))
expect(screen.getByText(/operation\.pickDate/)).toBeInTheDocument()
})
})
// Date selection
@ -298,6 +331,17 @@ describe('DatePicker', () => {
expect(onChange).toHaveBeenCalledTimes(1)
})
it('should clone time from timezone default when selecting a date without initial value', () => {
const onChange = vi.fn()
const props = createDatePickerProps({ onChange, noConfirm: true })
render(<DatePicker {...props} />)
openPicker()
fireEvent.click(screen.getByRole('button', { name: '20' }))
expect(onChange).toHaveBeenCalledTimes(1)
})
it('should call onChange with undefined when OK is clicked without a selected date', () => {
const onChange = vi.fn()
const props = createDatePickerProps({ onChange })
@ -598,6 +642,22 @@ describe('DatePicker', () => {
const emitted = onChange.mock.calls[0][0]
expect(emitted.isValid()).toBe(true)
})
it('should preserve selected date when timezone changes after selecting now without initial value', () => {
const onChange = vi.fn()
const props = createDatePickerProps({
timezone: 'UTC',
onChange,
})
const { rerender } = render(<DatePicker {...props} />)
openPicker()
fireEvent.click(screen.getByText(/operation\.now/))
rerender(<DatePicker {...props} timezone="Asia/Tokyo" />)
expect(onChange).toHaveBeenCalledTimes(1)
expect(screen.getByRole('textbox')).toBeInTheDocument()
})
})
// Display time when selected date exists

View File

@ -98,6 +98,17 @@ describe('TimePicker', () => {
expect(input).toHaveValue('10:00 AM')
})
it('should handle document mousedown listener while picker is open', () => {
render(<TimePicker {...baseProps} value="10:00 AM" timezone="UTC" />)
const input = screen.getByRole('textbox')
fireEvent.click(input)
expect(input).toHaveValue('')
fireEvent.mouseDown(document.body)
expect(input).toHaveValue('')
})
it('should call onClear when clear is clicked while picker is closed', () => {
const onClear = vi.fn()
render(
@ -135,14 +146,6 @@ describe('TimePicker', () => {
expect(onClear).not.toHaveBeenCalled()
})
it('should register click outside listener on mount', () => {
const addEventSpy = vi.spyOn(document, 'addEventListener')
render(<TimePicker {...baseProps} value="10:00 AM" timezone="UTC" />)
expect(addEventSpy).toHaveBeenCalledWith('mousedown', expect.any(Function))
addEventSpy.mockRestore()
})
it('should sync selectedTime from value when opening with stale state', () => {
const onChange = vi.fn()
render(
@ -473,10 +476,81 @@ describe('TimePicker', () => {
expect(isDayjsObject(emitted)).toBe(true)
expect(emitted.hour()).toBeGreaterThanOrEqual(12)
})
it('should handle selection when timezone is undefined', () => {
const onChange = vi.fn()
// Render without timezone prop
render(<TimePicker {...baseProps} onChange={onChange} />)
openPicker()
// Click hour "03"
const { hourList } = getHourAndMinuteLists()
fireEvent.click(within(hourList).getByText('03'))
const confirmButton = screen.getByRole('button', { name: /operation\.ok/i })
fireEvent.click(confirmButton)
expect(onChange).toHaveBeenCalledTimes(1)
const emitted = onChange.mock.calls[0][0]
expect(emitted.hour()).toBe(3)
})
})
// Timezone change effect tests
describe('Timezone Changes', () => {
it('should return early when only onChange reference changes', () => {
const value = dayjs('2024-01-01T10:30:00Z')
const onChangeA = vi.fn()
const onChangeB = vi.fn()
const { rerender } = render(
<TimePicker
{...baseProps}
onChange={onChangeA}
value={value}
timezone="UTC"
/>,
)
rerender(
<TimePicker
{...baseProps}
onChange={onChangeB}
value={value}
timezone="UTC"
/>,
)
expect(onChangeA).not.toHaveBeenCalled()
expect(onChangeB).not.toHaveBeenCalled()
expect(screen.getByDisplayValue('10:30 AM')).toBeInTheDocument()
})
it('should safely return when value changes to an unparsable time string', () => {
const onChange = vi.fn()
const invalidValue = 123 as unknown as TimePickerProps['value']
const { rerender } = render(
<TimePicker
{...baseProps}
onChange={onChange}
value={dayjs('2024-01-01T10:30:00Z')}
timezone="UTC"
/>,
)
rerender(
<TimePicker
{...baseProps}
onChange={onChange}
value={invalidValue}
timezone="UTC"
/>,
)
expect(onChange).not.toHaveBeenCalled()
expect(screen.getByRole('textbox')).toHaveValue('')
})
it('should call onChange when timezone changes with an existing value', () => {
const onChange = vi.fn()
const value = dayjs('2024-01-01T10:30:00Z')
@ -584,6 +658,36 @@ describe('TimePicker', () => {
expect(onChange).not.toHaveBeenCalled()
})
it('should preserve selected time when value is removed and timezone is undefined', () => {
const onChange = vi.fn()
const { rerender } = render(
<TimePicker
{...baseProps}
onChange={onChange}
value={dayjs('2024-01-01T10:30:00Z')}
timezone="UTC"
/>,
)
rerender(
<TimePicker
{...baseProps}
onChange={onChange}
value={undefined}
timezone={undefined}
/>,
)
fireEvent.click(screen.getByRole('textbox'))
fireEvent.click(screen.getByRole('button', { name: /operation\.ok/i }))
expect(onChange).toHaveBeenCalledTimes(1)
const emitted = onChange.mock.calls[0][0]
expect(isDayjsObject(emitted)).toBe(true)
expect(emitted.hour()).toBe(10)
expect(emitted.minute()).toBe(30)
})
it('should not update when neither timezone nor value changes', () => {
const onChange = vi.fn()
const value = dayjs('2024-01-01T10:30:00Z')
@ -669,6 +773,19 @@ describe('TimePicker', () => {
expect(screen.getByDisplayValue('09:15 AM')).toBeInTheDocument()
})
it('should return empty display value for an unparsable truthy string', () => {
const invalidValue = 123 as unknown as TimePickerProps['value']
render(
<TimePicker
{...baseProps}
value={invalidValue}
timezone="UTC"
/>,
)
expect(screen.getByRole('textbox')).toHaveValue('')
})
})
describe('Timezone Label Integration', () => {

View File

@ -53,6 +53,7 @@ const TimePicker = ({
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
/* v8 ignore next 2 -- outside-click closing is handled by PortalToFollowElem; this local ref guard is a defensive fallback. */
if (containerRef.current && !containerRef.current.contains(event.target as Node))
setIsOpen(false)
}

View File

@ -1,112 +1,353 @@
import dayjs, {
import dayjs from 'dayjs'
import timezone from 'dayjs/plugin/timezone'
import utc from 'dayjs/plugin/utc'
import {
clearMonthMapCache,
cloneTime,
convertTimezoneToOffsetStr,
formatDateForOutput,
getDateWithTimezone,
getDaysInMonth,
getHourIn12Hour,
isDayjsObject,
parseDateWithFormat,
toDayjs,
} from '../dayjs'
describe('dayjs utilities', () => {
const timezone = 'UTC'
dayjs.extend(utc)
dayjs.extend(timezone)
it('toDayjs parses time-only strings with timezone support', () => {
const result = toDayjs('18:45', { timezone })
expect(result).toBeDefined()
expect(result?.format('HH:mm')).toBe('18:45')
expect(result?.utcOffset()).toBe(getDateWithTimezone({ timezone }).utcOffset())
// ── cloneTime ──────────────────────────────────────────────────────────────
describe('cloneTime', () => {
it('copies hour and minute from source to target, preserving target date', () => {
const target = dayjs('2024-03-15')
const source = dayjs('2020-01-01T09:30:00')
const result = cloneTime(target, source)
expect(result.format('YYYY-MM-DD')).toBe('2024-03-15')
expect(result.hour()).toBe(9)
expect(result.minute()).toBe(30)
})
})
// ── getDaysInMonth ─────────────────────────────────────────────────────────
describe('getDaysInMonth', () => {
beforeEach(() => clearMonthMapCache())
it('returns cells for a typical month view', () => {
const date = dayjs('2024-01-01')
const days = getDaysInMonth(date)
expect(days.length).toBeGreaterThanOrEqual(28)
expect(days.some(d => d.isCurrentMonth)).toBe(true)
expect(days.some(d => !d.isCurrentMonth)).toBe(true)
})
it('toDayjs parses 12-hour time strings', () => {
const tz = 'America/New_York'
const result = toDayjs('07:15 PM', { timezone: tz })
expect(result).toBeDefined()
expect(result?.format('HH:mm')).toBe('19:15')
expect(result?.utcOffset()).toBe(getDateWithTimezone({ timezone: tz }).startOf('day').utcOffset())
it('returns cached result on second call', () => {
const date = dayjs('2024-02-01')
const first = getDaysInMonth(date)
const second = getDaysInMonth(date)
expect(first).toBe(second) // same reference
})
it('isDayjsObject detects dayjs instances', () => {
const date = dayjs()
expect(isDayjsObject(date)).toBe(true)
expect(isDayjsObject(getDateWithTimezone({ timezone }))).toBe(true)
it('clears cache properly', () => {
const date = dayjs('2024-03-01')
const first = getDaysInMonth(date)
clearMonthMapCache()
const second = getDaysInMonth(date)
expect(first).not.toBe(second) // different reference after clearing
})
})
// ── getHourIn12Hour ─────────────────────────────────────────────────────────
describe('getHourIn12Hour', () => {
it('returns 12 for midnight (hour=0)', () => {
expect(getHourIn12Hour(dayjs().set('hour', 0))).toBe(12)
})
it('returns hour-12 for hours >= 12', () => {
expect(getHourIn12Hour(dayjs().set('hour', 12))).toBe(0)
expect(getHourIn12Hour(dayjs().set('hour', 15))).toBe(3)
expect(getHourIn12Hour(dayjs().set('hour', 23))).toBe(11)
})
it('returns hour as-is for AM hours (1-11)', () => {
expect(getHourIn12Hour(dayjs().set('hour', 1))).toBe(1)
expect(getHourIn12Hour(dayjs().set('hour', 11))).toBe(11)
})
})
// ── getDateWithTimezone ─────────────────────────────────────────────────────
describe('getDateWithTimezone', () => {
it('returns a clone of now when neither date nor timezone given', () => {
const result = getDateWithTimezone({})
expect(dayjs.isDayjs(result)).toBe(true)
})
it('returns current tz date when only timezone given', () => {
const result = getDateWithTimezone({ timezone: 'UTC' })
expect(dayjs.isDayjs(result)).toBe(true)
expect(result.utcOffset()).toBe(0)
})
it('returns date in given timezone when both date and timezone given', () => {
const date = dayjs.utc('2024-06-01T12:00:00Z')
const result = getDateWithTimezone({ date, timezone: 'UTC' })
expect(result.hour()).toBe(12)
})
it('returns clone of given date when no timezone given', () => {
const date = dayjs('2024-01-15T08:30:00')
const result = getDateWithTimezone({ date })
expect(result.isSame(date)).toBe(true)
})
})
// ── isDayjsObject ───────────────────────────────────────────────────────────
describe('isDayjsObject', () => {
it('detects dayjs instances', () => {
expect(isDayjsObject(dayjs())).toBe(true)
expect(isDayjsObject(getDateWithTimezone({ timezone: 'UTC' }))).toBe(true)
expect(isDayjsObject('2024-01-01')).toBe(false)
expect(isDayjsObject({})).toBe(false)
expect(isDayjsObject(null)).toBe(false)
expect(isDayjsObject(undefined)).toBe(false)
})
})
// ── toDayjs ────────────────────────────────────────────────────────────────
describe('toDayjs', () => {
const tz = 'UTC'
it('returns undefined for undefined value', () => {
expect(toDayjs(undefined)).toBeUndefined()
})
it('toDayjs parses datetime strings in target timezone', () => {
const value = '2024-05-01 12:00:00'
const tz = 'America/New_York'
const result = toDayjs(value, { timezone: tz })
expect(result).toBeDefined()
expect(result?.hour()).toBe(12)
expect(result?.format('YYYY-MM-DD HH:mm')).toBe('2024-05-01 12:00')
it('returns undefined for empty string', () => {
expect(toDayjs('')).toBeUndefined()
})
it('toDayjs parses ISO datetime strings in target timezone', () => {
const value = '2024-05-01T14:30:00'
const tz = 'Europe/London'
it('applies timezone to an existing Dayjs object', () => {
const date = dayjs('2024-06-01T12:00:00')
const result = toDayjs(date, { timezone: 'UTC' })
expect(dayjs.isDayjs(result)).toBe(true)
})
const result = toDayjs(value, { timezone: tz })
it('returns the Dayjs object as-is when no timezone given', () => {
const date = dayjs('2024-06-01')
const result = toDayjs(date)
expect(dayjs.isDayjs(result)).toBe(true)
})
expect(result).toBeDefined()
expect(result?.hour()).toBe(14)
it('returns undefined for non-string non-Dayjs value', () => {
// @ts-expect-error testing invalid input
expect(toDayjs(12345)).toBeUndefined()
})
it('parses 24h time-only strings', () => {
const result = toDayjs('18:45', { timezone: tz })
expect(result?.format('HH:mm')).toBe('18:45')
})
it('parses time-only strings with seconds', () => {
const result = toDayjs('09:30:45', { timezone: tz })
expect(result?.hour()).toBe(9)
expect(result?.minute()).toBe(30)
expect(result?.second()).toBe(45)
})
it('toDayjs handles dates without time component', () => {
const value = '2024-05-01'
const tz = 'America/Los_Angeles'
const result = toDayjs(value, { timezone: tz })
it('parses time-only strings with 3-digit milliseconds', () => {
const result = toDayjs('08:00:00.500', { timezone: tz })
expect(result?.millisecond()).toBe(500)
})
it('parses time-only strings with 3-digit ms - normalizeMillisecond exact branch', () => {
// normalizeMillisecond: length === 3 → Number('567') = 567
const result = toDayjs('08:00:00.567', { timezone: tz })
expect(result).toBeDefined()
expect(result?.hour()).toBe(8)
expect(result?.second()).toBe(0)
})
it('parses time-only strings with <3-digit milliseconds (pads)', () => {
const result = toDayjs('08:00:00.5', { timezone: tz })
expect(result?.millisecond()).toBe(500)
})
it('parses 12-hour time strings (PM)', () => {
const result = toDayjs('07:15 PM', { timezone: 'America/New_York' })
expect(result?.format('HH:mm')).toBe('19:15')
})
it('parses 12-hour time strings (AM)', () => {
const result = toDayjs('12:00 AM', { timezone: tz })
expect(result?.hour()).toBe(0)
})
it('parses 12-hour time strings with seconds', () => {
const result = toDayjs('03:30:15 PM', { timezone: tz })
expect(result?.hour()).toBe(15)
expect(result?.second()).toBe(15)
})
it('parses datetime strings via common formats', () => {
const result = toDayjs('2024-05-01 12:00:00', { timezone: tz })
expect(result?.format('YYYY-MM-DD')).toBe('2024-05-01')
})
it('parses ISO datetime strings', () => {
const result = toDayjs('2024-05-01T14:30:00', { timezone: 'Europe/London' })
expect(result?.hour()).toBe(14)
})
it('parses dates with an explicit format option', () => {
// Use unambiguous format: YYYY/MM/DD + value 2024/05/01
const result = toDayjs('2024/05/01', { format: 'YYYY/MM/DD', timezone: tz })
expect(result?.format('YYYY-MM-DD')).toBe('2024-05-01')
})
it('falls through to other formats when explicit format fails', () => {
// '2024-05-01' doesn't match 'DD/MM/YYYY' but will match common formats
const result = toDayjs('2024-05-01', { format: 'DD/MM/YYYY', timezone: tz })
expect(result?.format('YYYY-MM-DD')).toBe('2024-05-01')
})
it('falls through to common formats when explicit format fails without timezone', () => {
const result = toDayjs('2024-05-01', { format: 'DD/MM/YYYY' })
expect(result?.format('YYYY-MM-DD')).toBe('2024-05-01')
})
it('returns undefined when explicit format parsing fails and no fallback matches', () => {
const result = toDayjs('not-a-date-value', { format: 'YYYY/MM/DD' })
expect(result).toBeUndefined()
})
it('uses custom formats array', () => {
const result = toDayjs('2024/05/01', { formats: ['YYYY/MM/DD'] })
expect(result?.format('YYYY-MM-DD')).toBe('2024-05-01')
})
it('returns undefined for completely invalid string', () => {
const result = toDayjs('not-a-valid-date-at-all!!!')
expect(result).toBeUndefined()
})
it('parses date-only strings without time', () => {
const result = toDayjs('2024-05-01', { timezone: 'America/Los_Angeles' })
expect(result?.format('YYYY-MM-DD')).toBe('2024-05-01')
expect(result?.hour()).toBe(0)
expect(result?.minute()).toBe(0)
})
it('uses timezone fallback parser for non-standard datetime strings', () => {
const result = toDayjs('May 1, 2024 2:30 PM', { timezone: 'America/New_York' })
expect(result?.isValid()).toBe(true)
expect(result?.year()).toBe(2024)
expect(result?.month()).toBe(4)
expect(result?.date()).toBe(1)
expect(result?.utcOffset()).toBe(dayjs.tz('2024-05-01', 'America/New_York').utcOffset())
})
it('uses timezone fallback parser when custom formats are empty', () => {
const result = toDayjs('2024-05-01T14:30:00Z', {
timezone: 'America/New_York',
formats: [],
})
expect(result?.isValid()).toBe(true)
expect(result?.utcOffset()).toBe(dayjs.tz('2024-05-01', 'America/New_York').utcOffset())
})
})
// ── parseDateWithFormat ────────────────────────────────────────────────────
describe('parseDateWithFormat', () => {
it('returns null for empty string', () => {
expect(parseDateWithFormat('')).toBeNull()
})
it('parses with explicit format', () => {
// Use YYYY/MM/DD which is unambiguous
const result = parseDateWithFormat('2024/05/01', 'YYYY/MM/DD')
expect(result?.format('YYYY-MM-DD')).toBe('2024-05-01')
})
it('returns null for invalid string with explicit format', () => {
expect(parseDateWithFormat('not-a-date', 'YYYY-MM-DD')).toBeNull()
})
it('parses using common formats (YYYY-MM-DD)', () => {
const result = parseDateWithFormat('2024-05-01')
expect(result?.format('YYYY-MM-DD')).toBe('2024-05-01')
})
it('parses using common formats (YYYY/MM/DD)', () => {
const result = parseDateWithFormat('2024/05/01')
expect(result?.format('YYYY-MM-DD')).toBe('2024-05-01')
})
it('parses ISO datetime strings via common formats', () => {
const result = parseDateWithFormat('2024-05-01T14:30:00')
expect(result?.hour()).toBe(14)
})
it('returns null for completely unparseable string', () => {
expect(parseDateWithFormat('ZZZZ-ZZ-ZZ')).toBeNull()
})
})
// ── formatDateForOutput ────────────────────────────────────────────────────
describe('formatDateForOutput', () => {
it('returns empty string for invalid date', () => {
expect(formatDateForOutput(dayjs('invalid'))).toBe('')
})
it('returns date-only format by default (includeTime=false)', () => {
const date = dayjs('2024-05-01T12:30:00')
expect(formatDateForOutput(date)).toBe('2024-05-01')
})
it('returns ISO datetime string when includeTime=true', () => {
const date = dayjs('2024-05-01T12:30:00')
const result = formatDateForOutput(date, true)
expect(result).toMatch(/^2024-05-01T12:30:00/)
})
})
// ── convertTimezoneToOffsetStr ─────────────────────────────────────────────
describe('convertTimezoneToOffsetStr', () => {
it('should return default UTC+0 for undefined timezone', () => {
it('returns default UTC+0 for undefined timezone', () => {
expect(convertTimezoneToOffsetStr(undefined)).toBe('UTC+0')
})
it('should return default UTC+0 for invalid timezone', () => {
it('returns default UTC+0 for invalid timezone', () => {
expect(convertTimezoneToOffsetStr('Invalid/Timezone')).toBe('UTC+0')
})
it('should handle whole hour positive offsets without leading zeros', () => {
it('handles positive whole-hour offsets', () => {
expect(convertTimezoneToOffsetStr('Asia/Shanghai')).toBe('UTC+8')
expect(convertTimezoneToOffsetStr('Pacific/Auckland')).toBe('UTC+12')
expect(convertTimezoneToOffsetStr('Pacific/Apia')).toBe('UTC+13')
})
it('should handle whole hour negative offsets without leading zeros', () => {
it('handles negative whole-hour offsets', () => {
expect(convertTimezoneToOffsetStr('Pacific/Niue')).toBe('UTC-11')
expect(convertTimezoneToOffsetStr('Pacific/Honolulu')).toBe('UTC-10')
expect(convertTimezoneToOffsetStr('America/New_York')).toBe('UTC-5')
})
it('should handle zero offset', () => {
it('handles zero offset', () => {
expect(convertTimezoneToOffsetStr('Europe/London')).toBe('UTC+0')
expect(convertTimezoneToOffsetStr('UTC')).toBe('UTC+0')
})
it('should handle half-hour offsets (30 minutes)', () => {
// India Standard Time: UTC+5:30
it('handles half-hour offsets', () => {
expect(convertTimezoneToOffsetStr('Asia/Kolkata')).toBe('UTC+5:30')
// Australian Central Time: UTC+9:30
expect(convertTimezoneToOffsetStr('Australia/Adelaide')).toBe('UTC+9:30')
expect(convertTimezoneToOffsetStr('Australia/Darwin')).toBe('UTC+9:30')
})
it('should handle 45-minute offsets', () => {
// Chatham Time: UTC+12:45
it('handles 45-minute offsets', () => {
expect(convertTimezoneToOffsetStr('Pacific/Chatham')).toBe('UTC+12:45')
})
it('should preserve leading zeros in minute part for non-zero minutes', () => {
// Ensure +05:30 is displayed as "UTC+5:30", not "UTC+5:3"
it('preserves leading zeros in minute part', () => {
const result = convertTimezoneToOffsetStr('Asia/Kolkata')
expect(result).toMatch(/UTC[+-]\d+:30/)
expect(result).not.toMatch(/UTC[+-]\d+:3[^0]/)

View File

@ -112,6 +112,7 @@ export const convertTimezoneToOffsetStr = (timezone?: string) => {
// Extract offset from name format like "-11:00 Niue Time" or "+05:30 India Time"
// Name format is always "{offset}:{minutes} {timezone name}"
const offsetMatch = /^([+-]?\d{1,2}):(\d{2})/.exec(tzItem.name)
/* v8 ignore next 2 -- timezone.json entries are normalized to "{offset} {name}"; this protects against malformed data only. */
if (!offsetMatch)
return DEFAULT_OFFSET_STR
// Parse hours and minutes separately
@ -141,6 +142,7 @@ const normalizeMillisecond = (value: string | undefined) => {
return 0
if (value.length === 3)
return Number(value)
/* v8 ignore next 2 -- TIME_ONLY_REGEX allows at most 3 fractional digits, so >3 can only occur after future regex changes. */
if (value.length > 3)
return Number(value.slice(0, 3))
return Number(value.padEnd(3, '0'))

View File

@ -59,6 +59,7 @@ const EmojiPickerInner: FC<IEmojiPickerInnerProps> = ({
React.useEffect(() => {
if (selectedEmoji) {
setShowStyleColors(true)
/* v8 ignore next 2 - @preserve */
if (selectedBackground)
onSelect?.(selectedEmoji, selectedBackground)
}

View File

@ -238,6 +238,32 @@ describe('ErrorBoundary', () => {
})
})
it('should not reset when resetKeys reference changes but values are identical', async () => {
const onReset = vi.fn()
const StableKeysHarness = () => {
const [keys, setKeys] = React.useState<Array<string | number>>([1, 2])
return (
<>
<button onClick={() => setKeys([1, 2])}>Update keys same values</button>
<ErrorBoundary resetKeys={keys} onReset={onReset}>
<ThrowOnRender shouldThrow={true} />
</ErrorBoundary>
</>
)
}
render(<StableKeysHarness />)
await screen.findByText('Something went wrong')
fireEvent.click(screen.getByRole('button', { name: 'Update keys same values' }))
await waitFor(() => {
expect(screen.getByText('Something went wrong')).toBeInTheDocument()
})
expect(onReset).not.toHaveBeenCalled()
})
it('should reset after children change when resetOnPropsChange is true', async () => {
const ResetOnPropsHarness = () => {
const [shouldThrow, setShouldThrow] = React.useState(true)
@ -269,6 +295,24 @@ describe('ErrorBoundary', () => {
expect(screen.getByText('second child')).toBeInTheDocument()
})
})
it('should call window.location.reload when Reload Page is clicked', async () => {
const reloadSpy = vi.fn()
Object.defineProperty(window, 'location', {
value: { ...window.location, reload: reloadSpy },
writable: true,
})
render(
<ErrorBoundary>
<ThrowOnRender shouldThrow={true} />
</ErrorBoundary>,
)
fireEvent.click(await screen.findByRole('button', { name: 'Reload Page' }))
expect(reloadSpy).toHaveBeenCalledTimes(1)
})
})
})
@ -358,6 +402,16 @@ describe('ErrorBoundary utility exports', () => {
expect(Wrapped.displayName).toBe('withErrorBoundary(NamedComponent)')
})
it('should fallback displayName to Component when wrapped component has no displayName and empty name', () => {
const Nameless = (() => <div>nameless</div>) as React.FC
Object.defineProperty(Nameless, 'displayName', { value: undefined, configurable: true })
Object.defineProperty(Nameless, 'name', { value: '', configurable: true })
const Wrapped = withErrorBoundary(Nameless)
expect(Wrapped.displayName).toBe('withErrorBoundary(Component)')
})
})
// Validate simple fallback helper component.

View File

@ -0,0 +1,7 @@
import { FeaturesProvider } from '../index'
describe('features index exports', () => {
it('should export FeaturesProvider from the barrel file', () => {
expect(FeaturesProvider).toBeDefined()
})
})

View File

@ -146,4 +146,30 @@ describe('AnnotationCtrlButton', () => {
expect(mockSetShowAnnotationFullModal).toHaveBeenCalled()
expect(mockAddAnnotation).not.toHaveBeenCalled()
})
it('should fallback author name to empty string when account name is missing', async () => {
const onAdded = vi.fn()
mockAddAnnotation.mockResolvedValueOnce({
id: 'annotation-2',
account: undefined,
})
render(
<AnnotationCtrlButton
appId="test-app"
messageId="msg-2"
cached={false}
query="test query"
answer="test answer"
onAdded={onAdded}
onEdit={vi.fn()}
/>,
)
fireEvent.click(screen.getByRole('button'))
await waitFor(() => {
expect(onAdded).toHaveBeenCalledWith('annotation-2', '')
})
})
})

View File

@ -39,6 +39,19 @@ vi.mock('@/config', () => ({
ANNOTATION_DEFAULT: { score_threshold: 0.9 },
}))
vi.mock('../score-slider', () => ({
default: ({ value, onChange }: { value: number, onChange: (value: number) => void }) => (
<input
role="slider"
type="range"
min={80}
max={100}
value={value}
onChange={e => onChange(Number((e.target as HTMLInputElement).value))}
/>
),
}))
const defaultAnnotationConfig = {
id: 'test-id',
enabled: false,
@ -158,7 +171,7 @@ describe('ConfigParamModal', () => {
/>,
)
expect(screen.getByText('0.90')).toBeInTheDocument()
expect(screen.getByRole('slider')).toHaveValue('90')
})
it('should render configConfirmBtn when isInit is false', () => {
@ -262,9 +275,9 @@ describe('ConfigParamModal', () => {
)
const slider = screen.getByRole('slider')
expect(slider).toHaveAttribute('aria-valuemin', '80')
expect(slider).toHaveAttribute('aria-valuemax', '100')
expect(slider).toHaveAttribute('aria-valuenow', '90')
expect(slider).toHaveAttribute('min', '80')
expect(slider).toHaveAttribute('max', '100')
expect(slider).toHaveValue('90')
})
it('should update embedding model when model selector is used', () => {
@ -377,7 +390,7 @@ describe('ConfigParamModal', () => {
/>,
)
expect(screen.getByRole('slider')).toHaveAttribute('aria-valuenow', '90')
expect(screen.getByRole('slider')).toHaveValue('90')
})
it('should set loading state while saving', async () => {
@ -412,4 +425,30 @@ describe('ConfigParamModal', () => {
expect(onSave).toHaveBeenCalled()
})
})
it('should save updated score after slider changes', async () => {
const onSave = vi.fn().mockResolvedValue(undefined)
render(
<ConfigParamModal
appId="test-app"
isShow={true}
onHide={vi.fn()}
onSave={onSave}
annotationConfig={defaultAnnotationConfig}
/>,
)
fireEvent.change(screen.getByRole('slider'), { target: { value: '96' } })
const buttons = screen.getAllByRole('button')
const saveBtn = buttons.find(b => b.textContent?.includes('initSetup'))
fireEvent.click(saveBtn!)
await waitFor(() => {
expect(onSave).toHaveBeenCalledWith(
expect.objectContaining({ embedding_provider_name: 'openai' }),
0.96,
)
})
})
})

View File

@ -1,13 +1,15 @@
import type { Features } from '../../../types'
import type { OnFeaturesChange } from '@/app/components/base/features/types'
import { fireEvent, render, screen } from '@testing-library/react'
import { act, fireEvent, render, screen } from '@testing-library/react'
import { FeaturesProvider } from '../../../context'
import AnnotationReply from '../index'
const originalConsoleError = console.error
const mockPush = vi.fn()
let mockPathname = '/app/test-app-id/configuration'
vi.mock('next/navigation', () => ({
useRouter: () => ({ push: mockPush }),
usePathname: () => '/app/test-app-id/configuration',
usePathname: () => mockPathname,
}))
let mockIsShowAnnotationConfigInit = false
@ -100,6 +102,15 @@ const renderWithProvider = (
describe('AnnotationReply', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.spyOn(console, 'error').mockImplementation((...args: unknown[]) => {
const message = args.map(arg => String(arg)).join(' ')
if (message.includes('A props object containing a "key" prop is being spread into JSX')
|| message.includes('React keys must be passed directly to JSX without using spread')) {
return
}
originalConsoleError(...args as Parameters<typeof console.error>)
})
mockPathname = '/app/test-app-id/configuration'
mockIsShowAnnotationConfigInit = false
mockIsShowAnnotationFullModal = false
capturedSetAnnotationConfig = null
@ -235,18 +246,47 @@ describe('AnnotationReply', () => {
expect(mockPush).toHaveBeenCalledWith('/app/test-app-id/annotations')
})
it('should show config param modal when isShowAnnotationConfigInit is true', () => {
it('should fallback appId to empty string when pathname does not match', () => {
mockPathname = '/apps/no-match'
renderWithProvider({}, {
annotationReply: {
enabled: true,
score_threshold: 0.9,
embedding_model: {
embedding_provider_name: 'openai',
embedding_model_name: 'text-embedding-ada-002',
},
},
})
const card = screen.getByText(/feature\.annotation\.title/).closest('[class]')!
fireEvent.mouseEnter(card)
fireEvent.click(screen.getByText(/feature\.annotation\.cacheManagement/))
expect(mockPush).toHaveBeenCalledWith('/app//annotations')
})
it('should show config param modal when isShowAnnotationConfigInit is true', async () => {
mockIsShowAnnotationConfigInit = true
renderWithProvider()
await act(async () => {
renderWithProvider()
await Promise.resolve()
})
expect(screen.getByText(/initSetup\.title/)).toBeInTheDocument()
})
it('should hide config modal when hide is clicked', () => {
it('should hide config modal when hide is clicked', async () => {
mockIsShowAnnotationConfigInit = true
renderWithProvider()
await act(async () => {
renderWithProvider()
await Promise.resolve()
})
fireEvent.click(screen.getByRole('button', { name: /operation\.cancel/ }))
await act(async () => {
fireEvent.click(screen.getByRole('button', { name: /operation\.cancel/ }))
await Promise.resolve()
})
expect(mockSetIsShowAnnotationConfigInit).toHaveBeenCalledWith(false)
})
@ -264,7 +304,10 @@ describe('AnnotationReply', () => {
},
})
fireEvent.click(screen.getByText(/initSetup\.confirmBtn/))
await act(async () => {
fireEvent.click(screen.getByText(/initSetup\.confirmBtn/))
await Promise.resolve()
})
expect(mockHandleEnableAnnotation).toHaveBeenCalled()
})
@ -298,7 +341,10 @@ describe('AnnotationReply', () => {
},
})
fireEvent.click(screen.getByText(/initSetup\.confirmBtn/))
await act(async () => {
fireEvent.click(screen.getByText(/initSetup\.confirmBtn/))
await Promise.resolve()
})
// handleEnableAnnotation should be called with embedding model and score
expect(mockHandleEnableAnnotation).toHaveBeenCalledWith(
@ -327,13 +373,15 @@ describe('AnnotationReply', () => {
// The captured setAnnotationConfig is the component's updateAnnotationReply callback
expect(capturedSetAnnotationConfig).not.toBeNull()
capturedSetAnnotationConfig!({
enabled: true,
score_threshold: 0.8,
embedding_model: {
embedding_provider_name: 'openai',
embedding_model_name: 'new-model',
},
act(() => {
capturedSetAnnotationConfig!({
enabled: true,
score_threshold: 0.8,
embedding_model: {
embedding_provider_name: 'openai',
embedding_model_name: 'new-model',
},
})
})
expect(onChange).toHaveBeenCalled()
@ -353,12 +401,12 @@ describe('AnnotationReply', () => {
// Should not throw when onChange is not provided
expect(capturedSetAnnotationConfig).not.toBeNull()
expect(() => {
expect(() => act(() => {
capturedSetAnnotationConfig!({
enabled: true,
score_threshold: 0.7,
})
}).not.toThrow()
})).not.toThrow()
})
it('should hide info display when hovering over enabled feature', () => {
@ -403,9 +451,12 @@ describe('AnnotationReply', () => {
expect(screen.getByText('0.9')).toBeInTheDocument()
})
it('should pass isInit prop to ConfigParamModal', () => {
it('should pass isInit prop to ConfigParamModal', async () => {
mockIsShowAnnotationConfigInit = true
renderWithProvider()
await act(async () => {
renderWithProvider()
await Promise.resolve()
})
expect(screen.getByText(/initSetup\.confirmBtn/)).toBeInTheDocument()
expect(screen.queryByText(/initSetup\.configConfirmBtn/)).not.toBeInTheDocument()

View File

@ -1,5 +1,7 @@
import type { AnnotationReplyConfig } from '@/models/debug'
import { act, renderHook } from '@testing-library/react'
import { queryAnnotationJobStatus } from '@/service/annotation'
import { sleep } from '@/utils'
import useAnnotationConfig from '../use-annotation-config'
let mockIsAnnotationFull = false
@ -238,4 +240,31 @@ describe('useAnnotationConfig', () => {
expect(updatedConfig.enabled).toBe(true)
expect(updatedConfig.score_threshold).toBeDefined()
})
it('should poll job status until completed when enabling annotation', async () => {
const setAnnotationConfig = vi.fn()
const queryJobStatusMock = vi.mocked(queryAnnotationJobStatus)
const sleepMock = vi.mocked(sleep)
queryJobStatusMock
.mockResolvedValueOnce({ job_status: 'pending' } as unknown as Awaited<ReturnType<typeof queryAnnotationJobStatus>>)
.mockResolvedValueOnce({ job_status: 'completed' } as unknown as Awaited<ReturnType<typeof queryAnnotationJobStatus>>)
const { result } = renderHook(() => useAnnotationConfig({
appId: 'test-app',
annotationConfig: defaultConfig,
setAnnotationConfig,
}))
await act(async () => {
await result.current.handleEnableAnnotation({
embedding_provider_name: 'openai',
embedding_model_name: 'text-embedding-3-small',
}, 0.95)
})
expect(queryJobStatusMock).toHaveBeenCalledTimes(2)
expect(sleepMock).toHaveBeenCalledWith(2000)
expect(setAnnotationConfig).toHaveBeenCalled()
})
})

View File

@ -80,7 +80,7 @@ const ConfigParamModal: FC<Props> = ({
onClose={onHide}
className="!mt-14 !w-[640px] !max-w-none !p-6"
>
<div className="title-2xl-semi-bold mb-2 text-text-primary">
<div className="mb-2 text-text-primary title-2xl-semi-bold">
{t(`initSetup.${isInit ? 'title' : 'configTitle'}`, { ns: 'appAnnotation' })}
</div>
@ -93,6 +93,7 @@ const ConfigParamModal: FC<Props> = ({
className="mt-1"
value={(annotationConfig.score_threshold || ANNOTATION_DEFAULT.score_threshold) * 100}
onChange={(val) => {
/* v8 ignore next -- callback dispatch depends on react-slider drag mechanics that are flaky in jsdom. @preserve */
setAnnotationConfig({
...annotationConfig,
score_threshold: val / 100,

View File

@ -11,10 +11,10 @@ export const Item: FC<{ title: string, tooltip: string, children: React.JSX.Elem
return (
<div>
<div className="mb-1 flex items-center space-x-1">
<div className="system-sm-semibold py-1 text-text-secondary">{title}</div>
<div className="py-1 text-text-secondary system-sm-semibold">{title}</div>
<Tooltip
popupContent={
<div className="system-sm-regular max-w-[200px] text-text-secondary">{tooltip}</div>
<div className="max-w-[200px] text-text-secondary system-sm-regular">{tooltip}</div>
}
/>
</div>

View File

@ -92,20 +92,20 @@ const AnnotationReply = ({
>
<>
{!annotationReply?.enabled && (
<div className="system-xs-regular line-clamp-2 min-h-8 text-text-tertiary">{t('feature.annotation.description', { ns: 'appDebug' })}</div>
<div className="line-clamp-2 min-h-8 text-text-tertiary system-xs-regular">{t('feature.annotation.description', { ns: 'appDebug' })}</div>
)}
{!!annotationReply?.enabled && (
<>
{!isHovering && (
<div className="flex items-center gap-4 pt-0.5">
<div className="">
<div className="system-2xs-medium-uppercase mb-0.5 text-text-tertiary">{t('feature.annotation.scoreThreshold.title', { ns: 'appDebug' })}</div>
<div className="system-xs-regular text-text-secondary">{annotationReply.score_threshold || '-'}</div>
<div className="mb-0.5 text-text-tertiary system-2xs-medium-uppercase">{t('feature.annotation.scoreThreshold.title', { ns: 'appDebug' })}</div>
<div className="text-text-secondary system-xs-regular">{annotationReply.score_threshold || '-'}</div>
</div>
<div className="h-[27px] w-px rotate-12 bg-divider-subtle"></div>
<div className="">
<div className="system-2xs-medium-uppercase mb-0.5 text-text-tertiary">{t('modelProvider.embeddingModel.key', { ns: 'common' })}</div>
<div className="system-xs-regular text-text-secondary">{annotationReply.embedding_model?.embedding_model_name}</div>
<div className="mb-0.5 text-text-tertiary system-2xs-medium-uppercase">{t('modelProvider.embeddingModel.key', { ns: 'common' })}</div>
<div className="text-text-secondary system-xs-regular">{annotationReply.embedding_model?.embedding_model_name}</div>
</div>
</div>
)}

View File

@ -27,7 +27,7 @@ const Slider: React.FC<ISliderProps> = ({ className, max, min, step, value, disa
renderThumb={(props, state) => (
<div {...props}>
<div className="relative h-full w-full">
<div className="system-sm-semibold absolute left-[50%] top-[-16px] translate-x-[-50%] text-text-primary">
<div className="absolute left-[50%] top-[-16px] translate-x-[-50%] text-text-primary system-sm-semibold">
{(state.valueNow / 100).toFixed(2)}
</div>
</div>

View File

@ -28,7 +28,7 @@ const ScoreSlider: FC<Props> = ({
onChange={onChange}
/>
</div>
<div className="system-xs-semibold-uppercase mt-[10px] flex items-center justify-between">
<div className="mt-[10px] flex items-center justify-between system-xs-semibold-uppercase">
<div className="flex space-x-1 text-util-colors-cyan-cyan-500">
<div>0.8</div>
<div>·</div>

View File

@ -1,6 +1,6 @@
import type { Features } from '../../../types'
import type { OnFeaturesChange } from '@/app/components/base/features/types'
import { fireEvent, render, screen } from '@testing-library/react'
import { act, fireEvent, render, screen } from '@testing-library/react'
import { FeaturesProvider } from '../../../context'
import ConversationOpener from '../index'
@ -144,7 +144,9 @@ describe('ConversationOpener', () => {
fireEvent.click(screen.getByText(/openingStatement\.writeOpener/))
const modalCall = mockSetShowOpeningModal.mock.calls[0][0]
modalCall.onSaveCallback({ enabled: true, opening_statement: 'Updated' })
act(() => {
modalCall.onSaveCallback({ enabled: true, opening_statement: 'Updated' })
})
expect(onChange).toHaveBeenCalled()
})
@ -184,4 +186,41 @@ describe('ConversationOpener', () => {
// After leave, statement visible again
expect(screen.getByText('Welcome!')).toBeInTheDocument()
})
it('should return early from opener handler when disabled and hovered', () => {
renderWithProvider({ disabled: true }, {
opening: { enabled: true, opening_statement: 'Hello' },
})
const card = screen.getByText(/feature\.conversationOpener\.title/).closest('[class]')!
fireEvent.mouseEnter(card)
fireEvent.click(screen.getByText(/openingStatement\.writeOpener/))
expect(mockSetShowOpeningModal).not.toHaveBeenCalled()
})
it('should run save and cancel callbacks without onChange', () => {
renderWithProvider({}, {
opening: { enabled: true, opening_statement: 'Hello' },
})
const card = screen.getByText(/feature\.conversationOpener\.title/).closest('[class]')!
fireEvent.mouseEnter(card)
fireEvent.click(screen.getByText(/openingStatement\.writeOpener/))
const modalCall = mockSetShowOpeningModal.mock.calls[0][0]
act(() => {
modalCall.onSaveCallback({ enabled: true, opening_statement: 'Updated without callback' })
modalCall.onCancelCallback()
})
expect(mockSetShowOpeningModal).toHaveBeenCalledTimes(1)
})
it('should toggle feature switch without onChange callback', () => {
renderWithProvider()
fireEvent.click(screen.getByRole('switch'))
expect(screen.getByRole('switch')).toBeInTheDocument()
})
})

View File

@ -31,7 +31,25 @@ vi.mock('@/app/components/app/configuration/config-prompt/confirm-add-var', () =
}))
vi.mock('react-sortablejs', () => ({
ReactSortable: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
ReactSortable: ({
children,
list,
setList,
}: {
children: React.ReactNode
list: Array<{ id: number, name: string }>
setList: (list: Array<{ id: number, name: string }>) => void
}) => (
<div>
<button
data-testid="mock-sortable-apply"
onClick={() => setList([...list].reverse())}
>
Apply Sort
</button>
{children}
</div>
),
}))
const defaultData: OpeningStatement = {
@ -168,6 +186,23 @@ describe('OpeningSettingModal', () => {
expect(onCancel).toHaveBeenCalledTimes(1)
})
it('should not call onCancel when close icon receives non-action key', async () => {
const onCancel = vi.fn()
await render(
<OpeningSettingModal
data={defaultData}
onSave={vi.fn()}
onCancel={onCancel}
/>,
)
const closeButton = screen.getByTestId('close-modal')
closeButton.focus()
fireEvent.keyDown(closeButton, { key: 'Escape' })
expect(onCancel).not.toHaveBeenCalled()
})
it('should call onSave with updated data when save is clicked', async () => {
const onSave = vi.fn()
await render(
@ -507,4 +542,73 @@ describe('OpeningSettingModal', () => {
expect(editor.textContent?.trim()).toBe('')
expect(screen.getByText('appDebug.openingStatement.placeholder')).toBeInTheDocument()
})
it('should render with empty suggested questions when field is missing', async () => {
await render(
<OpeningSettingModal
data={{ ...defaultData, suggested_questions: undefined } as unknown as OpeningStatement}
onSave={vi.fn()}
onCancel={vi.fn()}
/>,
)
expect(screen.queryByDisplayValue('Question 1')).not.toBeInTheDocument()
expect(screen.queryByDisplayValue('Question 2')).not.toBeInTheDocument()
})
it('should render prompt variable fallback key when name is empty', async () => {
await render(
<OpeningSettingModal
data={defaultData}
onSave={vi.fn()}
onCancel={vi.fn()}
promptVariables={[{ key: 'account_id', name: '', type: 'string', required: true }]}
/>,
)
expect(getPromptEditor()).toBeInTheDocument()
})
it('should save reordered suggested questions after sortable setList', async () => {
const onSave = vi.fn()
await render(
<OpeningSettingModal
data={defaultData}
onSave={onSave}
onCancel={vi.fn()}
/>,
)
await userEvent.click(screen.getByTestId('mock-sortable-apply'))
await userEvent.click(screen.getByText(/operation\.save/))
expect(onSave).toHaveBeenCalledWith(expect.objectContaining({
suggested_questions: ['Question 2', 'Question 1'],
}))
})
it('should not save when confirm dialog action runs with empty opening statement', async () => {
const onSave = vi.fn()
const view = await render(
<OpeningSettingModal
data={{ ...defaultData, opening_statement: 'Hello {{name}}' }}
onSave={onSave}
onCancel={vi.fn()}
/>,
)
await userEvent.click(screen.getByText(/operation\.save/))
expect(screen.getByTestId('confirm-add-var')).toBeInTheDocument()
view.rerender(
<OpeningSettingModal
data={{ ...defaultData, opening_statement: ' ' }}
onSave={onSave}
onCancel={vi.fn()}
/>,
)
await userEvent.click(screen.getByTestId('cancel-add'))
expect(onSave).not.toHaveBeenCalled()
})
})

View File

@ -34,6 +34,7 @@ const ConversationOpener = ({
const featuresStore = useFeaturesStore()
const [isHovering, setIsHovering] = useState(false)
const handleOpenOpeningModal = useCallback(() => {
/* v8 ignore next -- guarded path is not reachable in tests with a real disabled button because click is prevented at DOM level. @preserve */
if (disabled)
return
const {
@ -95,12 +96,12 @@ const ConversationOpener = ({
>
<>
{!opening?.enabled && (
<div className="system-xs-regular line-clamp-2 min-h-8 text-text-tertiary">{t('feature.conversationOpener.description', { ns: 'appDebug' })}</div>
<div className="line-clamp-2 min-h-8 text-text-tertiary system-xs-regular">{t('feature.conversationOpener.description', { ns: 'appDebug' })}</div>
)}
{!!opening?.enabled && (
<>
{!isHovering && (
<div className="system-xs-regular line-clamp-2 min-h-8 text-text-tertiary">
<div className="line-clamp-2 min-h-8 text-text-tertiary system-xs-regular">
{opening.opening_statement || t('openingStatement.placeholder', { ns: 'appDebug' })}
</div>
)}

View File

@ -64,6 +64,14 @@ describe('FileUpload', () => {
expect(onChange).toHaveBeenCalled()
})
it('should toggle without onChange callback', () => {
renderWithProvider()
expect(() => {
fireEvent.click(screen.getByRole('switch'))
}).not.toThrow()
})
it('should show supported types when enabled', () => {
renderWithProvider({}, {
file: {

View File

@ -150,6 +150,17 @@ describe('SettingContent', () => {
expect(onClose).toHaveBeenCalledTimes(1)
})
it('should not call onClose when close icon receives non-action key', () => {
const onClose = vi.fn()
renderWithProvider({ onClose })
const closeIconButton = screen.getByTestId('close-setting-modal')
closeIconButton.focus()
fireEvent.keyDown(closeIconButton, { key: 'Escape' })
expect(onClose).not.toHaveBeenCalled()
})
it('should call onClose when cancel button is clicked to close', () => {
const onClose = vi.fn()
renderWithProvider({ onClose })

View File

@ -70,6 +70,14 @@ describe('ImageUpload', () => {
expect(onChange).toHaveBeenCalled()
})
it('should toggle without onChange callback', () => {
renderWithProvider()
expect(() => {
fireEvent.click(screen.getByRole('switch'))
}).not.toThrow()
})
it('should show supported types when enabled', () => {
renderWithProvider({}, {
file: {

View File

@ -3,6 +3,12 @@ import type { CodeBasedExtensionForm } from '@/models/common'
import { fireEvent, render, screen } from '@testing-library/react'
import FormGeneration from '../form-generation'
const { mockLocale } = vi.hoisted(() => ({ mockLocale: { value: 'en-US' } }))
vi.mock('@/context/i18n', () => ({
useLocale: () => mockLocale.value,
}))
const i18n = (en: string, zh = en): I18nText =>
({ 'en-US': en, 'zh-Hans': zh }) as unknown as I18nText
@ -21,6 +27,7 @@ const createForm = (overrides: Partial<CodeBasedExtensionForm> = {}): CodeBasedE
describe('FormGeneration', () => {
beforeEach(() => {
vi.clearAllMocks()
mockLocale.value = 'en-US'
})
it('should render text-input form fields', () => {
@ -130,4 +137,22 @@ describe('FormGeneration', () => {
expect(onChange).toHaveBeenCalledWith({ model: 'gpt-4' })
})
it('should render zh-Hans labels for select field and options', () => {
mockLocale.value = 'zh-Hans'
const form = createForm({
type: 'select',
variable: 'model',
label: i18n('Model', '模型'),
options: [
{ label: i18n('GPT-4', '智谱-4'), value: 'gpt-4' },
{ label: i18n('GPT-3.5', '智谱-3.5'), value: 'gpt-3.5' },
],
})
render(<FormGeneration forms={[form]} value={{}} onChange={vi.fn()} />)
expect(screen.getByText('模型')).toBeInTheDocument()
fireEvent.click(screen.getByText(/placeholder\.select/))
expect(screen.getByText('智谱-4')).toBeInTheDocument()
})
})

View File

@ -4,6 +4,10 @@ import { fireEvent, render, screen } from '@testing-library/react'
import { FeaturesProvider } from '../../../context'
import Moderation from '../index'
const { mockCodeBasedExtensionData } = vi.hoisted(() => ({
mockCodeBasedExtensionData: [] as Array<{ name: string, label: Record<string, string> }>,
}))
const mockSetShowModerationSettingModal = vi.fn()
vi.mock('@/context/modal-context', () => ({
useModalContext: () => ({
@ -16,7 +20,7 @@ vi.mock('@/context/i18n', () => ({
}))
vi.mock('@/service/use-common', () => ({
useCodeBasedExtensions: () => ({ data: { data: [] } }),
useCodeBasedExtensions: () => ({ data: { data: mockCodeBasedExtensionData } }),
}))
const defaultFeatures: Features = {
@ -46,6 +50,7 @@ const renderWithProvider = (
describe('Moderation', () => {
beforeEach(() => {
vi.clearAllMocks()
mockCodeBasedExtensionData.length = 0
})
it('should render the moderation title', () => {
@ -282,6 +287,25 @@ describe('Moderation', () => {
expect(onChange).toHaveBeenCalled()
})
it('should invoke onCancelCallback from settings modal without onChange', () => {
renderWithProvider({}, {
moderation: {
enabled: true,
type: 'keywords',
config: {
inputs_config: { enabled: true, preset_response: '' },
},
},
})
const card = screen.getByText(/feature\.moderation\.title/).closest('[class]')!
fireEvent.mouseEnter(card)
fireEvent.click(screen.getByText(/operation\.settings/))
const modalCall = mockSetShowModerationSettingModal.mock.calls[0][0]
expect(() => modalCall.onCancelCallback()).not.toThrow()
})
it('should invoke onSaveCallback from settings modal', () => {
const onChange = vi.fn()
renderWithProvider({ onChange }, {
@ -304,6 +328,25 @@ describe('Moderation', () => {
expect(onChange).toHaveBeenCalled()
})
it('should invoke onSaveCallback from settings modal without onChange', () => {
renderWithProvider({}, {
moderation: {
enabled: true,
type: 'keywords',
config: {
inputs_config: { enabled: true, preset_response: '' },
},
},
})
const card = screen.getByText(/feature\.moderation\.title/).closest('[class]')!
fireEvent.mouseEnter(card)
fireEvent.click(screen.getByText(/operation\.settings/))
const modalCall = mockSetShowModerationSettingModal.mock.calls[0][0]
expect(() => modalCall.onSaveCallback({ enabled: true, type: 'keywords', config: {} })).not.toThrow()
})
it('should show code-based extension label for custom type', () => {
renderWithProvider({}, {
moderation: {
@ -319,6 +362,41 @@ describe('Moderation', () => {
expect(screen.getByText('-')).toBeInTheDocument()
})
it('should show code-based extension label when custom type is configured', () => {
mockCodeBasedExtensionData.push({
name: 'custom-ext',
label: { 'en-US': 'Custom Moderation', 'zh-Hans': '自定义审核' },
})
renderWithProvider({}, {
moderation: {
enabled: true,
type: 'custom-ext',
config: {
inputs_config: { enabled: true, preset_response: '' },
outputs_config: { enabled: false, preset_response: '' },
},
},
})
expect(screen.getByText('Custom Moderation')).toBeInTheDocument()
})
it('should not show enable content text when both input and output moderation are disabled', () => {
renderWithProvider({}, {
moderation: {
enabled: true,
type: 'keywords',
config: {
inputs_config: { enabled: false, preset_response: '' },
outputs_config: { enabled: false, preset_response: '' },
},
},
})
expect(screen.queryByText(/feature\.moderation\.(allEnabled|inputEnabled|outputEnabled)/)).not.toBeInTheDocument()
})
it('should not open setting modal when clicking settings button while disabled', () => {
renderWithProvider({ disabled: true }, {
moderation: {
@ -351,6 +429,15 @@ describe('Moderation', () => {
expect(onChange).toHaveBeenCalled()
})
it('should invoke onSaveCallback from enable modal without onChange', () => {
renderWithProvider()
fireEvent.click(screen.getByRole('switch'))
const modalCall = mockSetShowModerationSettingModal.mock.calls[0][0]
expect(() => modalCall.onSaveCallback({ enabled: true, type: 'keywords', config: {} })).not.toThrow()
})
it('should invoke onCancelCallback from enable modal and set enabled false', () => {
const onChange = vi.fn()
renderWithProvider({ onChange })
@ -364,6 +451,31 @@ describe('Moderation', () => {
expect(onChange).toHaveBeenCalled()
})
it('should invoke onCancelCallback from enable modal without onChange', () => {
renderWithProvider()
fireEvent.click(screen.getByRole('switch'))
const modalCall = mockSetShowModerationSettingModal.mock.calls[0][0]
expect(() => modalCall.onCancelCallback()).not.toThrow()
})
it('should disable moderation when toggled off without onChange', () => {
renderWithProvider({}, {
moderation: {
enabled: true,
type: 'keywords',
config: {
inputs_config: { enabled: true, preset_response: '' },
},
},
})
expect(() => {
fireEvent.click(screen.getByRole('switch'))
}).not.toThrow()
})
it('should not show modal when enabling with existing type', () => {
renderWithProvider({}, {
moderation: {

View File

@ -1,5 +1,6 @@
import type { ModerationContentConfig } from '@/models/debug'
import { fireEvent, render, screen } from '@testing-library/react'
import * as i18n from 'react-i18next'
import ModerationContent from '../moderation-content'
const defaultConfig: ModerationContentConfig = {
@ -124,4 +125,19 @@ describe('ModerationContent', () => {
expect(screen.getByText('5')).toBeInTheDocument()
expect(screen.getByText('100')).toBeInTheDocument()
})
it('should fallback to empty placeholder when translation is empty', () => {
const useTranslationSpy = vi.spyOn(i18n, 'useTranslation').mockReturnValue({
t: (key: string) => key === 'feature.moderation.modal.content.placeholder' ? '' : key,
i18n: { language: 'en-US' },
} as unknown as ReturnType<typeof i18n.useTranslation>)
renderComponent({
config: { enabled: true, preset_response: '' },
showPreset: true,
})
expect(screen.getByRole('textbox')).toHaveAttribute('placeholder', '')
useTranslationSpy.mockRestore()
})
})

View File

@ -1,5 +1,6 @@
import type { ModerationConfig } from '@/models/debug'
import { fireEvent, render, screen } from '@testing-library/react'
import { act, fireEvent, render, screen } from '@testing-library/react'
import * as i18n from 'react-i18next'
import ModerationSettingModal from '../moderation-setting-modal'
const mockNotify = vi.fn()
@ -68,6 +69,13 @@ const defaultData: ModerationConfig = {
describe('ModerationSettingModal', () => {
const onSave = vi.fn()
const renderModal = async (ui: React.ReactNode) => {
await act(async () => {
render(ui)
await Promise.resolve()
})
}
beforeEach(() => {
vi.clearAllMocks()
mockCodeBasedExtensions = { data: { data: [] } }
@ -93,7 +101,7 @@ describe('ModerationSettingModal', () => {
})
it('should render the modal title', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
@ -105,7 +113,7 @@ describe('ModerationSettingModal', () => {
})
it('should render provider options', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
@ -120,7 +128,7 @@ describe('ModerationSettingModal', () => {
})
it('should show keywords textarea when keywords type is selected', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
@ -134,7 +142,7 @@ describe('ModerationSettingModal', () => {
})
it('should render cancel and save buttons', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
@ -148,7 +156,7 @@ describe('ModerationSettingModal', () => {
it('should call onCancel when cancel is clicked', async () => {
const onCancel = vi.fn()
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={onCancel}
@ -161,6 +169,60 @@ describe('ModerationSettingModal', () => {
expect(onCancel).toHaveBeenCalled()
})
it('should call onCancel when close icon receives Enter key', async () => {
const onCancel = vi.fn()
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={onCancel}
onSave={onSave}
/>,
)
const closeButton = document.querySelector('div[role="button"][tabindex="0"]') as HTMLElement
expect(closeButton).toBeInTheDocument()
closeButton.focus()
fireEvent.keyDown(closeButton, { key: 'Enter' })
expect(onCancel).toHaveBeenCalledTimes(1)
})
it('should call onCancel when close icon receives Space key', async () => {
const onCancel = vi.fn()
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={onCancel}
onSave={onSave}
/>,
)
const closeButton = document.querySelector('div[role="button"][tabindex="0"]') as HTMLElement
expect(closeButton).toBeInTheDocument()
closeButton.focus()
fireEvent.keyDown(closeButton, { key: ' ' })
expect(onCancel).toHaveBeenCalledTimes(1)
})
it('should not call onCancel when close icon receives non-action key', async () => {
const onCancel = vi.fn()
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={onCancel}
onSave={onSave}
/>,
)
const closeButton = document.querySelector('div[role="button"][tabindex="0"]') as HTMLElement
expect(closeButton).toBeInTheDocument()
closeButton.focus()
fireEvent.keyDown(closeButton, { key: 'Escape' })
expect(onCancel).not.toHaveBeenCalled()
})
it('should show error when saving without inputs or outputs enabled', async () => {
const data: ModerationConfig = {
...defaultData,
@ -170,7 +232,7 @@ describe('ModerationSettingModal', () => {
outputs_config: { enabled: false, preset_response: '' },
},
}
await render(
await renderModal(
<ModerationSettingModal
data={data}
onCancel={vi.fn()}
@ -194,7 +256,7 @@ describe('ModerationSettingModal', () => {
outputs_config: { enabled: false, preset_response: '' },
},
}
await render(
await renderModal(
<ModerationSettingModal
data={data}
onCancel={vi.fn()}
@ -218,7 +280,7 @@ describe('ModerationSettingModal', () => {
outputs_config: { enabled: false, preset_response: '' },
},
}
await render(
await renderModal(
<ModerationSettingModal
data={data}
onCancel={vi.fn()}
@ -239,7 +301,7 @@ describe('ModerationSettingModal', () => {
})
it('should show api selector when api type is selected', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={{ ...defaultData, type: 'api', config: { inputs_config: { enabled: true, preset_response: '' } } }}
onCancel={vi.fn()}
@ -251,7 +313,7 @@ describe('ModerationSettingModal', () => {
})
it('should switch provider type when clicked', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
@ -267,7 +329,7 @@ describe('ModerationSettingModal', () => {
})
it('should update keywords on textarea change', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
@ -282,7 +344,7 @@ describe('ModerationSettingModal', () => {
})
it('should render moderation content sections', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
@ -303,7 +365,7 @@ describe('ModerationSettingModal', () => {
outputs_config: { enabled: false, preset_response: '' },
},
}
await render(
await renderModal(
<ModerationSettingModal
data={data}
onCancel={vi.fn()}
@ -327,7 +389,7 @@ describe('ModerationSettingModal', () => {
outputs_config: { enabled: false, preset_response: '' },
},
}
await render(
await renderModal(
<ModerationSettingModal
data={data}
onCancel={vi.fn()}
@ -352,7 +414,7 @@ describe('ModerationSettingModal', () => {
outputs_config: { enabled: false, preset_response: '' },
},
}
await render(
await renderModal(
<ModerationSettingModal
data={data}
onCancel={vi.fn()}
@ -380,7 +442,7 @@ describe('ModerationSettingModal', () => {
outputs_config: { enabled: true, preset_response: '' },
},
}
await render(
await renderModal(
<ModerationSettingModal
data={data}
onCancel={vi.fn()}
@ -396,7 +458,7 @@ describe('ModerationSettingModal', () => {
})
it('should toggle input moderation content', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
@ -413,7 +475,7 @@ describe('ModerationSettingModal', () => {
})
it('should toggle output moderation content', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
@ -430,7 +492,7 @@ describe('ModerationSettingModal', () => {
})
it('should select api extension via api selector', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={{ ...defaultData, type: 'api', config: { inputs_config: { enabled: true, preset_response: '' } } }}
onCancel={vi.fn()}
@ -450,7 +512,7 @@ describe('ModerationSettingModal', () => {
})
it('should save with openai_moderation type when configured', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={{
enabled: true,
@ -473,7 +535,7 @@ describe('ModerationSettingModal', () => {
})
it('should handle keyword truncation to 100 chars per line and 100 lines', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
@ -499,7 +561,7 @@ describe('ModerationSettingModal', () => {
outputs_config: { enabled: true, preset_response: 'output blocked' },
},
}
await render(
await renderModal(
<ModerationSettingModal
data={data}
onCancel={vi.fn()}
@ -518,7 +580,7 @@ describe('ModerationSettingModal', () => {
})
it('should switch from keywords to api type', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
@ -535,7 +597,7 @@ describe('ModerationSettingModal', () => {
})
it('should handle empty lines in keywords', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
@ -566,7 +628,7 @@ describe('ModerationSettingModal', () => {
refetch: vi.fn(),
}
await render(
await renderModal(
<ModerationSettingModal
data={{ ...defaultData, type: 'openai_moderation', config: { inputs_config: { enabled: true, preset_response: '' } } }}
onCancel={vi.fn()}
@ -594,7 +656,7 @@ describe('ModerationSettingModal', () => {
refetch: vi.fn(),
}
await render(
await renderModal(
<ModerationSettingModal
data={{ ...defaultData, type: 'openai_moderation', config: { inputs_config: { enabled: true, preset_response: '' } } }}
onCancel={vi.fn()}
@ -605,6 +667,10 @@ describe('ModerationSettingModal', () => {
fireEvent.click(screen.getByText(/settings\.provider/))
expect(mockSetShowAccountSettingModal).toHaveBeenCalled()
const modalCall = mockSetShowAccountSettingModal.mock.calls[0][0]
modalCall.onCancelCallback()
expect(mockModelProvidersData.refetch).toHaveBeenCalled()
})
it('should not save when OpenAI type is selected but not configured', async () => {
@ -624,7 +690,7 @@ describe('ModerationSettingModal', () => {
refetch: vi.fn(),
}
await render(
await renderModal(
<ModerationSettingModal
data={{ ...defaultData, type: 'openai_moderation', config: { inputs_config: { enabled: true, preset_response: 'blocked' }, outputs_config: { enabled: false, preset_response: '' } } }}
onCancel={vi.fn()}
@ -650,7 +716,7 @@ describe('ModerationSettingModal', () => {
},
}
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
@ -674,7 +740,7 @@ describe('ModerationSettingModal', () => {
},
}
await render(
await renderModal(
<ModerationSettingModal
data={{ ...defaultData, type: 'custom-ext', config: { inputs_config: { enabled: true, preset_response: '' } } }}
onCancel={vi.fn()}
@ -699,7 +765,7 @@ describe('ModerationSettingModal', () => {
},
}
await render(
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
@ -727,7 +793,7 @@ describe('ModerationSettingModal', () => {
},
}
await render(
await renderModal(
<ModerationSettingModal
data={{ ...defaultData, type: 'custom-ext', config: { inputs_config: { enabled: true, preset_response: 'blocked' } } }}
onCancel={vi.fn()}
@ -755,7 +821,7 @@ describe('ModerationSettingModal', () => {
},
}
await render(
await renderModal(
<ModerationSettingModal
data={{ ...defaultData, type: 'custom-ext', config: { api_url: 'https://example.com', inputs_config: { enabled: true, preset_response: 'blocked' }, outputs_config: { enabled: false, preset_response: '' } } }}
onCancel={vi.fn()}
@ -773,8 +839,40 @@ describe('ModerationSettingModal', () => {
}))
})
it('should update code-based extension form value and save updated config', async () => {
mockCodeBasedExtensions = {
data: {
data: [{
name: 'custom-ext',
label: { 'en-US': 'Custom Extension', 'zh-Hans': '自定义扩展' },
form_schema: [
{ variable: 'api_url', label: { 'en-US': 'API URL', 'zh-Hans': 'API 地址' }, type: 'text-input', required: true, default: '', placeholder: 'Enter URL', options: [], max_length: 200 },
],
}],
},
}
await renderModal(
<ModerationSettingModal
data={{ ...defaultData, type: 'custom-ext', config: { inputs_config: { enabled: true, preset_response: 'blocked' }, outputs_config: { enabled: false, preset_response: '' } } }}
onCancel={vi.fn()}
onSave={onSave}
/>,
)
fireEvent.change(screen.getByPlaceholderText('Enter URL'), { target: { value: 'https://changed.com' } })
fireEvent.click(screen.getByText(/operation\.save/))
expect(onSave).toHaveBeenCalledWith(expect.objectContaining({
type: 'custom-ext',
config: expect.objectContaining({
api_url: 'https://changed.com',
}),
}))
})
it('should show doc link for api type', async () => {
await render(
await renderModal(
<ModerationSettingModal
data={{ ...defaultData, type: 'api', config: { inputs_config: { enabled: true, preset_response: '' } } }}
onCancel={vi.fn()}
@ -784,4 +882,56 @@ describe('ModerationSettingModal', () => {
expect(screen.getByText(/apiBasedExtension\.link/)).toBeInTheDocument()
})
it('should fallback missing inputs_config to disabled in formatted save data', async () => {
await renderModal(
<ModerationSettingModal
data={{
enabled: true,
type: 'api',
config: {
api_based_extension_id: 'ext-fallback',
outputs_config: { enabled: true, preset_response: '' },
},
}}
onCancel={vi.fn()}
onSave={onSave}
/>,
)
fireEvent.click(screen.getByText(/operation\.save/))
expect(onSave).toHaveBeenCalledWith(expect.objectContaining({
type: 'api',
config: expect.objectContaining({
inputs_config: expect.objectContaining({ enabled: false }),
outputs_config: expect.objectContaining({ enabled: true }),
}),
}))
})
it('should fallback to empty translated strings for optional placeholders and titles', async () => {
const useTranslationSpy = vi.spyOn(i18n, 'useTranslation').mockReturnValue({
t: (key: string) => [
'feature.moderation.modal.keywords.placeholder',
'feature.moderation.modal.content.input',
'feature.moderation.modal.content.output',
].includes(key)
? ''
: key,
i18n: { language: 'en-US' },
} as unknown as ReturnType<typeof i18n.useTranslation>)
await renderModal(
<ModerationSettingModal
data={defaultData}
onCancel={vi.fn()}
onSave={onSave}
/>,
)
const textarea = screen.getAllByRole('textbox')[0]
expect(textarea).toHaveAttribute('placeholder', '')
useTranslationSpy.mockRestore()
})
})

View File

@ -30,6 +30,7 @@ const Moderation = ({
const [isHovering, setIsHovering] = useState(false)
const handleOpenModerationSettingModal = () => {
/* v8 ignore next -- guarded path is not reachable in tests with a real disabled button because click is prevented at DOM level. @preserve */
if (disabled)
return
@ -138,20 +139,20 @@ const Moderation = ({
>
<>
{!moderation?.enabled && (
<div className="system-xs-regular line-clamp-2 min-h-8 text-text-tertiary">{t('feature.moderation.description', { ns: 'appDebug' })}</div>
<div className="line-clamp-2 min-h-8 text-text-tertiary system-xs-regular">{t('feature.moderation.description', { ns: 'appDebug' })}</div>
)}
{!!moderation?.enabled && (
<>
{!isHovering && (
<div className="flex items-center gap-4 pt-0.5">
<div className="">
<div className="system-2xs-medium-uppercase mb-0.5 text-text-tertiary">{t('feature.moderation.modal.provider.title', { ns: 'appDebug' })}</div>
<div className="system-xs-regular text-text-secondary">{providerContent}</div>
<div className="mb-0.5 text-text-tertiary system-2xs-medium-uppercase">{t('feature.moderation.modal.provider.title', { ns: 'appDebug' })}</div>
<div className="text-text-secondary system-xs-regular">{providerContent}</div>
</div>
<div className="h-[27px] w-px rotate-12 bg-divider-subtle"></div>
<div className="">
<div className="system-2xs-medium-uppercase mb-0.5 text-text-tertiary">{t('feature.moderation.contentEnableLabel', { ns: 'appDebug' })}</div>
<div className="system-xs-regular text-text-secondary">{enableContent}</div>
<div className="mb-0.5 text-text-tertiary system-2xs-medium-uppercase">{t('feature.moderation.contentEnableLabel', { ns: 'appDebug' })}</div>
<div className="text-text-secondary system-xs-regular">{enableContent}</div>
</div>
</div>
)}

View File

@ -185,6 +185,7 @@ const ModerationSettingModal: FC<ModerationSettingModalProps> = ({
}
const handleSave = () => {
/* v8 ignore next -- UI-invariant guard: same condition is used in Save button disabled logic, so when true handleSave has no user-triggerable invocation path. @preserve */
if (localeData.type === 'openai_moderation' && !isOpenAIProviderConfigured)
return

View File

@ -1,3 +1,4 @@
import type { ReactNode } from 'react'
import type { Features } from '../../../types'
import type { OnFeaturesChange } from '@/app/components/base/features/types'
import { fireEvent, render, screen } from '@testing-library/react'
@ -12,6 +13,23 @@ vi.mock('@/i18n-config/language', () => ({
],
}))
vi.mock('../voice-settings', () => ({
default: ({
open,
onOpen,
children,
}: {
open: boolean
onOpen: (open: boolean) => void
children: ReactNode
}) => (
<div data-testid="voice-settings" data-open={open ? 'true' : 'false'}>
<button data-testid="open-voice-settings" onClick={() => onOpen(true)}>open-voice-settings</button>
{children}
</div>
),
}))
const defaultFeatures: Features = {
moreLikeThis: { enabled: false },
opening: { enabled: false },
@ -68,6 +86,12 @@ describe('TextToSpeech', () => {
expect(onChange).toHaveBeenCalled()
})
it('should toggle without onChange callback', () => {
renderWithProvider()
fireEvent.click(screen.getByRole('switch'))
expect(screen.getByRole('switch')).toBeInTheDocument()
})
it('should show language and voice info when enabled and not hovering', () => {
renderWithProvider({}, {
text2speech: { enabled: true, language: 'en-US', voice: 'alloy' },
@ -97,6 +121,19 @@ describe('TextToSpeech', () => {
expect(screen.getByText(/voice\.voiceSettings\.title/)).toBeInTheDocument()
})
it('should hide voice settings button after mouse leave', () => {
renderWithProvider({}, {
text2speech: { enabled: true },
})
const card = screen.getByText(/feature\.textToSpeech\.title/).closest('[class]')!
fireEvent.mouseEnter(card)
expect(screen.getByText(/voice\.voiceSettings\.title/)).toBeInTheDocument()
fireEvent.mouseLeave(card)
expect(screen.queryByText(/voice\.voiceSettings\.title/)).not.toBeInTheDocument()
})
it('should show autoPlay enabled text when autoPlay is enabled', () => {
renderWithProvider({}, {
text2speech: { enabled: true, language: 'en-US', autoPlay: TtsAutoPlay.enabled },
@ -112,4 +149,16 @@ describe('TextToSpeech', () => {
expect(screen.getByText(/voice\.voiceSettings\.autoPlayDisabled/)).toBeInTheDocument()
})
it('should pass open false to voice settings when disabled and modal is opened', () => {
renderWithProvider({ disabled: true }, {
text2speech: { enabled: true },
})
const card = screen.getByText(/feature\.textToSpeech\.title/).closest('[class]')!
fireEvent.mouseEnter(card)
fireEvent.click(screen.getByTestId('open-voice-settings'))
expect(screen.getByTestId('voice-settings')).toHaveAttribute('data-open', 'false')
})
})

View File

@ -3,6 +3,38 @@ import { fireEvent, render, screen } from '@testing-library/react'
import { FeaturesProvider } from '../../../context'
import VoiceSettings from '../voice-settings'
vi.mock('@/app/components/base/portal-to-follow-elem', () => ({
PortalToFollowElem: ({
children,
placement,
offset,
}: {
children: React.ReactNode
placement?: string
offset?: { mainAxis?: number }
}) => (
<div
data-testid="voice-settings-portal"
data-placement={placement}
data-main-axis={offset?.mainAxis}
>
{children}
</div>
),
PortalToFollowElemTrigger: ({
children,
onClick,
}: {
children: React.ReactNode
onClick?: () => void
}) => (
<div data-testid="voice-settings-trigger" onClick={onClick}>
{children}
</div>
),
PortalToFollowElemContent: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}))
vi.mock('next/navigation', () => ({
usePathname: () => '/app/test-app-id/configuration',
useParams: () => ({ appId: 'test-app-id' }),
@ -102,4 +134,19 @@ describe('VoiceSettings', () => {
expect(onOpen).toHaveBeenCalledWith(false)
})
it('should use top placement and mainAxis 4 when placementLeft is false', () => {
renderWithProvider(
<VoiceSettings open={false} onOpen={vi.fn()} placementLeft={false}>
<button>Settings</button>
</VoiceSettings>,
)
const portal = screen.getAllByTestId('voice-settings-portal')
.find(item => item.hasAttribute('data-main-axis'))
expect(portal).toBeDefined()
expect(portal).toHaveAttribute('data-placement', 'top')
expect(portal).toHaveAttribute('data-main-axis', '4')
})
})

View File

@ -25,6 +25,11 @@ describe('createFileStore', () => {
expect(store.getState().files).toEqual([])
})
it('should create a store with empty array when value is null', () => {
const store = createFileStore(null as unknown as FileEntity[])
expect(store.getState().files).toEqual([])
})
it('should create a store with initial files', () => {
const files = [createMockFile()]
const store = createFileStore(files)
@ -96,6 +101,11 @@ describe('useFileStore', () => {
expect(result.current).toBe(store)
})
it('should return null when no provider exists', () => {
const { result } = renderHook(() => useFileStore())
expect(result.current).toBeNull()
})
})
describe('FileContextProvider', () => {

View File

@ -126,13 +126,11 @@ describe('FileFromLinkOrLocal', () => {
expect(input).toBeDisabled()
})
it('should not submit when url is empty', () => {
it('should have disabled OK button when url is empty', () => {
renderAndOpen({ showFromLink: true })
const okButton = screen.getByText(/operation\.ok/)
fireEvent.click(okButton)
expect(screen.queryByText(/fileUploader\.pasteFileLinkInvalid/)).not.toBeInTheDocument()
const okButton = screen.getByRole('button', { name: /operation\.ok/ })
expect(okButton).toBeDisabled()
})
it('should call handleLoadFileFromLink when valid URL is submitted', () => {

View File

@ -36,8 +36,12 @@ const FileFromLinkOrLocal = ({
const [showError, setShowError] = useState(false)
const { handleLoadFileFromLink } = useFile(fileConfig)
const disabled = !!fileConfig.number_limits && files.length >= fileConfig.number_limits
const fileLinkPlaceholder = t('fileUploader.pasteFileLinkInputPlaceholder', { ns: 'common' })
/* v8 ignore next -- fallback for missing i18n key is not reliably testable under current global translation mocks in jsdom @preserve */
const fileLinkPlaceholderText = fileLinkPlaceholder || ''
const handleSaveUrl = () => {
/* v8 ignore next -- guarded by UI-level disabled state (`disabled={!url || disabled}`), not reachable in jsdom click flow @preserve */
if (!url)
return
@ -70,8 +74,8 @@ const FileFromLinkOrLocal = ({
)}
>
<input
className="system-sm-regular mr-0.5 block grow appearance-none bg-transparent px-1 outline-none"
placeholder={t('fileUploader.pasteFileLinkInputPlaceholder', { ns: 'common' }) || ''}
className="mr-0.5 block grow appearance-none bg-transparent px-1 outline-none system-sm-regular"
placeholder={fileLinkPlaceholderText}
value={url}
onChange={(e) => {
setShowError(false)
@ -91,7 +95,7 @@ const FileFromLinkOrLocal = ({
</div>
{
showError && (
<div className="body-xs-regular mt-0.5 text-text-destructive">
<div className="mt-0.5 text-text-destructive body-xs-regular">
{t('fileUploader.pasteFileLinkInvalid', { ns: 'common' })}
</div>
)
@ -101,7 +105,7 @@ const FileFromLinkOrLocal = ({
}
{
showFromLink && showFromLocal && (
<div className="system-2xs-medium-uppercase flex h-7 items-center p-2 text-text-quaternary">
<div className="flex h-7 items-center p-2 text-text-quaternary system-2xs-medium-uppercase">
<div className="mr-2 h-px w-[93px] bg-gradient-to-l from-[rgba(16,24,40,0.08)]" />
OR
<div className="ml-2 h-px w-[93px] bg-gradient-to-r from-[rgba(16,24,40,0.08)]" />

View File

@ -26,7 +26,7 @@ export const FileList = ({
canPreview = true,
}: FileListProps) => {
return (
<div className={cn('flex flex-wrap gap-2', className)}>
<div className={cn('flex flex-wrap gap-2', className)} data-testid="file-list">
{
files.map((file) => {
if (file.supportFileType === SupportUploadFileTypes.image) {

View File

@ -1,7 +1,7 @@
import type { AnyFieldApi } from '@tanstack/react-form'
import type { FormSchema } from '@/app/components/base/form/types'
import { useForm } from '@tanstack/react-form'
import { fireEvent, render, screen } from '@testing-library/react'
import { act, fireEvent, render, screen } from '@testing-library/react'
import { FormItemValidateStatusEnum, FormTypeEnum } from '@/app/components/base/form/types'
import BaseField from '../base-field'
@ -35,7 +35,7 @@ const renderBaseField = ({
const TestComponent = () => {
const form = useForm({
defaultValues: defaultValues ?? { [formSchema.name]: '' },
onSubmit: async () => {},
onSubmit: async () => { },
})
return (
@ -72,7 +72,7 @@ describe('BaseField', () => {
})
})
it('should render text input and propagate changes', () => {
it('should render text input and propagate changes', async () => {
const onChange = vi.fn()
renderBaseField({
formSchema: {
@ -88,13 +88,15 @@ describe('BaseField', () => {
const input = screen.getByDisplayValue('Hello')
expect(input).toHaveValue('Hello')
fireEvent.change(input, { target: { value: 'Updated' } })
await act(async () => {
fireEvent.change(input, { target: { value: 'Updated' } })
})
expect(onChange).toHaveBeenCalledWith('title', 'Updated')
expect(screen.getByText('Title')).toBeInTheDocument()
expect(screen.getAllByText('*')).toHaveLength(1)
})
it('should render only options that satisfy show_on conditions', () => {
it('should render only options that satisfy show_on conditions', async () => {
renderBaseField({
formSchema: {
type: FormTypeEnum.select,
@ -109,7 +111,9 @@ describe('BaseField', () => {
defaultValues: { mode: 'alpha', enabled: 'no' },
})
fireEvent.click(screen.getByText('Alpha'))
await act(async () => {
fireEvent.click(screen.getByText('Alpha'))
})
expect(screen.queryByText('Beta')).not.toBeInTheDocument()
})
@ -133,7 +137,7 @@ describe('BaseField', () => {
expect(screen.getByText('common.dynamicSelect.loading')).toBeInTheDocument()
})
it('should update value when users click a radio option', () => {
it('should update value when users click a radio option', async () => {
const onChange = vi.fn()
renderBaseField({
formSchema: {
@ -150,7 +154,9 @@ describe('BaseField', () => {
onChange,
})
fireEvent.click(screen.getByText('Private'))
await act(async () => {
fireEvent.click(screen.getByText('Private'))
})
expect(onChange).toHaveBeenCalledWith('visibility', 'private')
})
@ -231,7 +237,7 @@ describe('BaseField', () => {
expect(screen.getByText('Localized title')).toBeInTheDocument()
})
it('should render dynamic options and allow selecting one', () => {
it('should render dynamic options and allow selecting one', async () => {
mockDynamicOptions.mockReturnValue({
data: {
options: [
@ -252,12 +258,16 @@ describe('BaseField', () => {
defaultValues: { plugin_option: '' },
})
fireEvent.click(screen.getByText('common.placeholder.input'))
fireEvent.click(screen.getByText('Option A'))
await act(async () => {
fireEvent.click(screen.getByText('common.placeholder.input'))
})
await act(async () => {
fireEvent.click(screen.getByText('Option A'))
})
expect(screen.getByText('Option A')).toBeInTheDocument()
})
it('should update boolean field when users choose false', () => {
it('should update boolean field when users choose false', async () => {
renderBaseField({
formSchema: {
type: FormTypeEnum.boolean,
@ -270,7 +280,9 @@ describe('BaseField', () => {
})
expect(screen.getByTestId('field-value')).toHaveTextContent('true')
fireEvent.click(screen.getByText('False'))
await act(async () => {
fireEvent.click(screen.getByText('False'))
})
expect(screen.getByTestId('field-value')).toHaveTextContent('false')
})
@ -290,4 +302,144 @@ describe('BaseField', () => {
expect(screen.getByText('This is a warning')).toBeInTheDocument()
})
it('should render tooltip when provided', async () => {
renderBaseField({
formSchema: {
type: FormTypeEnum.textInput,
name: 'info',
label: 'Info',
required: false,
tooltip: 'Extra info',
},
})
expect(screen.getByText('Info')).toBeInTheDocument()
const tooltipTrigger = screen.getByTestId('base-field-tooltip-trigger')
fireEvent.mouseEnter(tooltipTrigger)
expect(screen.getByText('Extra info')).toBeInTheDocument()
})
it('should render checkbox list and handle changes', async () => {
renderBaseField({
formSchema: {
type: FormTypeEnum.checkbox,
name: 'features',
label: 'Features',
required: false,
options: [
{ label: 'Feature A', value: 'a' },
{ label: 'Feature B', value: 'b' },
],
},
defaultValues: { features: ['a'] },
})
expect(screen.getByText('Feature A')).toBeInTheDocument()
expect(screen.getByText('Feature B')).toBeInTheDocument()
await act(async () => {
fireEvent.click(screen.getByText('Feature B'))
})
const checkboxB = screen.getByTestId('checkbox-b')
expect(checkboxB).toBeChecked()
})
it('should handle dynamic select error state', () => {
mockDynamicOptions.mockReturnValue({
data: undefined,
isLoading: false,
error: new Error('Failed'),
})
renderBaseField({
formSchema: {
type: FormTypeEnum.dynamicSelect,
name: 'ds_error',
label: 'DS Error',
required: false,
},
})
expect(screen.getByText('common.placeholder.input')).toBeInTheDocument()
})
it('should handle dynamic select no data state', () => {
mockDynamicOptions.mockReturnValue({
data: { options: [] },
isLoading: false,
error: null,
})
renderBaseField({
formSchema: {
type: FormTypeEnum.dynamicSelect,
name: 'ds_empty',
label: 'DS Empty',
required: false,
},
})
expect(screen.getByText('common.placeholder.input')).toBeInTheDocument()
})
it('should render radio buttons in vertical layout when length >= 3', () => {
renderBaseField({
formSchema: {
type: FormTypeEnum.radio,
name: 'vertical_radio',
label: 'Vertical',
required: false,
options: [
{ label: 'O1', value: '1' },
{ label: 'O2', value: '2' },
{ label: 'O3', value: '3' },
],
},
})
expect(screen.getByText('O1')).toBeInTheDocument()
expect(screen.getByText('O2')).toBeInTheDocument()
expect(screen.getByText('O3')).toBeInTheDocument()
})
it('should render radio UI when showRadioUI is true', () => {
renderBaseField({
formSchema: {
type: FormTypeEnum.radio,
name: 'ui_radio',
label: 'UI Radio',
required: false,
showRadioUI: true,
options: [{ label: 'Option 1', value: '1' }],
},
})
expect(screen.getByText('Option 1')).toBeInTheDocument()
expect(screen.getByTestId('radio-group')).toBeInTheDocument()
})
it('should apply disabled styles', () => {
renderBaseField({
formSchema: {
type: FormTypeEnum.radio,
name: 'disabled_radio',
label: 'Disabled',
required: false,
options: [{ label: 'Option 1', value: '1' }],
disabled: true,
},
})
// In radio, the option itself has the disabled class
expect(screen.getByText('Option 1')).toHaveClass('cursor-not-allowed')
})
it('should return empty string for null content in getTranslatedContent', () => {
renderBaseField({
formSchema: {
type: FormTypeEnum.textInput,
name: 'null_label',
label: null as unknown as string,
required: false,
},
})
// Expecting translatedLabel to be '' so title block only renders required * if applicable
expect(screen.queryByText('*')).not.toBeInTheDocument()
})
})

View File

@ -1,8 +1,30 @@
import type { AnyFieldApi, AnyFormApi } from '@tanstack/react-form'
import type { FormRefObject, FormSchema } from '@/app/components/base/form/types'
import { useStore } from '@tanstack/react-form'
import { act, fireEvent, render, screen } from '@testing-library/react'
import { FormTypeEnum } from '@/app/components/base/form/types'
import { FormItemValidateStatusEnum, FormTypeEnum } from '@/app/components/base/form/types'
import BaseForm from '../base-form'
vi.mock('@tanstack/react-form', async (importOriginal) => {
const actual = await importOriginal<typeof import('@tanstack/react-form')>()
return {
...actual,
useStore: vi.fn((store, selector) => {
// If a selector is provided, apply it to a mocked state or the store directly
if (selector) {
// If the store is a mock with state, use it; otherwise provide a default
try {
return selector(store?.state || { values: {} })
}
catch {
return {}
}
}
return store?.state?.values || {}
}),
}
})
vi.mock('@/service/use-triggers', () => ({
useTriggerPluginDynamicOptions: () => ({
data: undefined,
@ -54,7 +76,7 @@ describe('BaseForm', () => {
expect(screen.queryByDisplayValue('Hidden title')).not.toBeInTheDocument()
})
it('should prevent default submit behavior when preventDefaultSubmit is true', () => {
it('should prevent default submit behavior when preventDefaultSubmit is true', async () => {
const onSubmit = vi.fn((event: React.FormEvent<HTMLFormElement>) => {
expect(event.defaultPrevented).toBe(true)
})
@ -66,11 +88,15 @@ describe('BaseForm', () => {
/>,
)
fireEvent.submit(container.querySelector('form') as HTMLFormElement)
await act(async () => {
fireEvent.submit(container.querySelector('form') as HTMLFormElement, {
defaultPrevented: true,
})
})
expect(onSubmit).toHaveBeenCalled()
})
it('should expose ref API for updating values and field states', () => {
it('should expose ref API for updating values and field states', async () => {
const formRef = { current: null } as { current: FormRefObject | null }
render(
<BaseForm
@ -81,7 +107,7 @@ describe('BaseForm', () => {
expect(formRef.current).not.toBeNull()
act(() => {
await act(async () => {
formRef.current?.setFields([
{
name: 'title',
@ -97,7 +123,7 @@ describe('BaseForm', () => {
expect(formRef.current?.getFormValues({})).toBeTruthy()
})
it('should derive warning status when setFields receives warnings only', () => {
it('should derive warning status when setFields receives warnings only', async () => {
const formRef = { current: null } as { current: FormRefObject | null }
render(
<BaseForm
@ -106,7 +132,7 @@ describe('BaseForm', () => {
/>,
)
act(() => {
await act(async () => {
formRef.current?.setFields([
{
name: 'title',
@ -117,4 +143,179 @@ describe('BaseForm', () => {
expect(screen.getByText('Title warning')).toBeInTheDocument()
})
it('should use formFromProps if provided', () => {
const mockState = { values: { kind: 'show' } }
const mockStore = {
state: mockState,
}
vi.mocked(useStore).mockReturnValueOnce(mockState.values)
const mockForm = {
store: mockStore,
Field: ({ children, name }: { children: (field: AnyFieldApi) => React.ReactNode, name: string }) => children({
name,
state: { value: mockState.values[name as keyof typeof mockState.values], meta: { isTouched: false, errorMap: {} } },
form: { store: mockStore },
} as unknown as AnyFieldApi),
setFieldValue: vi.fn(),
}
render(<BaseForm formSchemas={baseSchemas} formFromProps={mockForm as unknown as AnyFormApi} />)
expect(screen.getByText('Kind')).toBeInTheDocument()
})
it('should handle setFields with explicit validateStatus', async () => {
const formRef = { current: null } as { current: FormRefObject | null }
render(<BaseForm formSchemas={baseSchemas} ref={formRef} />)
await act(async () => {
formRef.current?.setFields([{
name: 'kind',
validateStatus: FormItemValidateStatusEnum.Error,
errors: ['Explicit error'],
}])
})
expect(screen.getByText('Explicit error')).toBeInTheDocument()
})
it('should handle setFields with no value change', async () => {
const formRef = { current: null } as { current: FormRefObject | null }
render(<BaseForm formSchemas={baseSchemas} ref={formRef} />)
await act(async () => {
formRef.current?.setFields([{
name: 'kind',
errors: ['Error only'],
}])
})
expect(screen.getByText('Error only')).toBeInTheDocument()
})
it('should use default values from schema when defaultValues prop is missing', () => {
render(<BaseForm formSchemas={baseSchemas} />)
expect(screen.getByDisplayValue('show')).toBeInTheDocument()
})
it('should handle submit without preventDefaultSubmit', async () => {
const onSubmit = vi.fn()
const { container } = render(<BaseForm formSchemas={baseSchemas} onSubmit={onSubmit} />)
await act(async () => {
fireEvent.submit(container.querySelector('form') as HTMLFormElement)
})
expect(onSubmit).toHaveBeenCalled()
})
it('should render nothing if field name does not match schema in renderField', () => {
const mockState = { values: { unknown: 'value' } }
const mockStore = {
state: mockState,
}
vi.mocked(useStore).mockReturnValueOnce(mockState.values)
const mockForm = {
store: mockStore,
Field: ({ children }: { children: (field: AnyFieldApi) => React.ReactNode }) => children({
name: 'unknown', // field name not in baseSchemas
state: { value: 'value', meta: { isTouched: false, errorMap: {} } },
form: { store: mockStore },
} as unknown as AnyFieldApi),
setFieldValue: vi.fn(),
}
render(<BaseForm formSchemas={baseSchemas} formFromProps={mockForm as unknown as AnyFormApi} />)
expect(screen.queryByText('Kind')).not.toBeInTheDocument()
})
it('should handle undefined formSchemas', () => {
const { container } = render(<BaseForm formSchemas={undefined as unknown as FormSchema[]} />)
expect(container).toBeEmptyDOMElement()
})
it('should handle empty array formSchemas', () => {
const { container } = render(<BaseForm formSchemas={[]} />)
expect(container).toBeEmptyDOMElement()
})
it('should fallback to schema class names if props are missing', () => {
const schemaWithClasses: FormSchema[] = [{
...baseSchemas[0],
fieldClassName: 'schema-field',
labelClassName: 'schema-label',
}]
render(<BaseForm formSchemas={schemaWithClasses} />)
expect(screen.getByText('Kind')).toHaveClass('schema-label')
expect(screen.getByText('Kind').parentElement).toHaveClass('schema-field')
})
it('should handle preventDefaultSubmit', async () => {
const onSubmit = vi.fn()
const { container } = render(
<BaseForm
formSchemas={baseSchemas}
onSubmit={onSubmit}
preventDefaultSubmit={true}
/>,
)
const event = new Event('submit', { cancelable: true, bubbles: true })
const spy = vi.spyOn(event, 'preventDefault')
const form = container.querySelector('form') as HTMLFormElement
await act(async () => {
fireEvent(form, event)
})
expect(spy).toHaveBeenCalled()
expect(onSubmit).toHaveBeenCalled()
})
it('should handle missing onSubmit prop', async () => {
const { container } = render(<BaseForm formSchemas={baseSchemas} />)
await act(async () => {
expect(() => {
fireEvent.submit(container.querySelector('form') as HTMLFormElement)
}).not.toThrow()
})
})
it('should call onChange when field value changes', async () => {
const onChange = vi.fn()
render(<BaseForm formSchemas={baseSchemas} onChange={onChange} />)
const input = screen.getByDisplayValue('show')
await act(async () => {
fireEvent.change(input, { target: { value: 'new-value' } })
})
expect(onChange).toHaveBeenCalledWith('kind', 'new-value')
})
it('should handle setFields with no status, errors, or warnings', async () => {
const formRef = { current: null } as { current: FormRefObject | null }
render(<BaseForm formSchemas={baseSchemas} ref={formRef} />)
await act(async () => {
formRef.current?.setFields([{
name: 'kind',
value: 'new-show',
}])
})
expect(screen.getByDisplayValue('new-show')).toBeInTheDocument()
})
it('should handle schema without show_on in showOnValues', () => {
const schemaNoShowOn: FormSchema[] = [{
type: FormTypeEnum.textInput,
name: 'test',
label: 'Test',
required: false,
}]
// Simply rendering should trigger showOnValues selector
render(<BaseForm formSchemas={schemaNoShowOn} />)
expect(screen.getByText('Test')).toBeInTheDocument()
})
it('should apply prop-based class names', () => {
render(
<BaseForm
formSchemas={baseSchemas}
fieldClassName="custom-field"
labelClassName="custom-label"
/>,
)
const label = screen.getByText('Kind')
expect(label).toHaveClass('custom-label')
})
})

View File

@ -1,6 +1,5 @@
import type { AnyFieldApi } from '@tanstack/react-form'
import type { FieldState, FormSchema, TypeWithI18N } from '@/app/components/base/form/types'
import { RiExternalLinkLine } from '@remixicon/react'
import { useStore } from '@tanstack/react-form'
import {
isValidElement,
@ -198,6 +197,7 @@ const BaseField = ({
}
{tooltip && (
<Tooltip
triggerTestId="base-field-tooltip-trigger"
popupContent={<div className="w-[200px]">{translatedTooltip}</div>}
triggerClassName="ml-0.5 w-4 h-4"
/>
@ -270,16 +270,18 @@ const BaseField = ({
}
{
formItemType === FormTypeEnum.radio && (
<div className={cn(
memorizedOptions.length < 3 ? 'flex items-center space-x-2' : 'space-y-2',
)}
<div
className={cn(
memorizedOptions.length < 3 ? 'flex items-center space-x-2' : 'space-y-2',
)}
data-testid="radio-group"
>
{
memorizedOptions.map(option => (
<div
key={option.value}
className={cn(
'system-sm-regular hover:bg-components-option-card-option-hover-bg hover:border-components-option-card-option-hover-border flex h-8 flex-[1] grow cursor-pointer items-center justify-center gap-2 rounded-lg border border-components-option-card-option-border bg-components-option-card-option-bg p-2 text-text-secondary',
'hover:bg-components-option-card-option-hover-bg hover:border-components-option-card-option-hover-border flex h-8 flex-[1] grow cursor-pointer items-center justify-center gap-2 rounded-lg border border-components-option-card-option-border bg-components-option-card-option-bg p-2 text-text-secondary system-sm-regular',
value === option.value && 'border-components-option-card-option-selected-border bg-components-option-card-option-selected-bg text-text-primary shadow-xs',
disabled && 'cursor-not-allowed opacity-50',
inputClassName,
@ -315,7 +317,7 @@ const BaseField = ({
}
{fieldState?.validateStatus && [FormItemValidateStatusEnum.Error, FormItemValidateStatusEnum.Warning].includes(fieldState?.validateStatus) && (
<div className={cn(
'system-xs-regular mt-1 px-0 py-[2px]',
'mt-1 px-0 py-[2px] system-xs-regular',
VALIDATE_STATUS_STYLE_MAP[fieldState?.validateStatus].textClassName,
)}
>
@ -325,21 +327,21 @@ const BaseField = ({
</div>
</div>
{description && (
<div className="system-xs-regular mt-4 text-text-tertiary">
<div className="mt-4 text-text-tertiary system-xs-regular">
{translatedDescription}
</div>
)}
{
url && (
<a
className="system-xs-regular mt-4 flex items-center text-text-accent"
className="mt-4 flex items-center text-text-accent system-xs-regular"
href={url}
target="_blank"
>
<span className="break-all">
{translatedHelp}
</span>
<RiExternalLinkLine className="ml-1 h-3 w-3 shrink-0" />
<div className="i-ri-external-link-line ml-1 h-3 w-3 shrink-0" />
</a>
)
}

View File

@ -147,4 +147,32 @@ describe('input-field scenario schema generator', () => {
other: { key: 'value' },
}).success).toBe(false)
})
it('should ignore constraints for irrelevant field types', () => {
const schema = generateZodSchema([
{
type: InputFieldType.numberInput,
variable: 'num',
label: 'Num',
required: true,
maxLength: 10, // maxLength is for textInput, should be ignored
showConditions: [],
},
{
type: InputFieldType.textInput,
variable: 'text',
label: 'Text',
required: true,
min: 1, // min is for numberInput, should be ignored
max: 5, // max is for numberInput, should be ignored
showConditions: [],
},
])
// Should still work based on their base types
// num: 12345678901 (violates maxLength: 10 if it were applied)
// text: 'long string here' (violates max: 5 if it were applied)
expect(schema.safeParse({ num: 12345678901, text: 'long string here' }).success).toBe(true)
expect(schema.safeParse({ num: 'not a number', text: 'hello' }).success).toBe(false)
})
})

View File

@ -28,18 +28,21 @@ describe('useCheckValidated', () => {
expect(mockNotify).not.toHaveBeenCalled()
})
it('should notify and return false when visible field has errors', () => {
it.each([
{ fieldName: 'name', label: 'Name', message: 'Name is required' },
{ fieldName: 'field1', label: 'Field 1', message: 'Field is required' },
])('should notify and return false when visible field has errors (show_on: []) for $fieldName', ({ fieldName, label, message }) => {
const form = {
getAllErrors: () => ({
fields: {
name: { errors: ['Name is required'] },
[fieldName]: { errors: [message] },
},
}),
state: { values: {} },
}
const schemas = [{
name: 'name',
label: 'Name',
name: fieldName,
label,
required: true,
type: FormTypeEnum.textInput,
show_on: [],
@ -50,7 +53,7 @@ describe('useCheckValidated', () => {
expect(result.current.checkValidated()).toBe(false)
expect(mockNotify).toHaveBeenCalledWith({
type: 'error',
message: 'Name is required',
message,
})
})
@ -102,4 +105,208 @@ describe('useCheckValidated', () => {
message: 'Secret is required',
})
})
it('should notify with first error when multiple fields have errors', () => {
const form = {
getAllErrors: () => ({
fields: {
name: { errors: ['Name error'] },
email: { errors: ['Email error'] },
},
}),
state: { values: {} },
}
const schemas = [
{
name: 'name',
label: 'Name',
required: true,
type: FormTypeEnum.textInput,
show_on: [],
},
{
name: 'email',
label: 'Email',
required: true,
type: FormTypeEnum.textInput,
show_on: [],
},
]
const { result } = renderHook(() => useCheckValidated(form as unknown as AnyFormApi, schemas))
expect(result.current.checkValidated()).toBe(false)
expect(mockNotify).toHaveBeenCalledWith({
type: 'error',
message: 'Name error',
})
expect(mockNotify).toHaveBeenCalledTimes(1)
})
it('should notify when multiple conditions all match', () => {
const form = {
getAllErrors: () => ({
fields: {
advancedOption: { errors: ['Advanced is required'] },
},
}),
state: { values: { enabled: 'true', level: 'advanced' } },
}
const schemas = [{
name: 'advancedOption',
label: 'Advanced Option',
required: true,
type: FormTypeEnum.textInput,
show_on: [
{ variable: 'enabled', value: 'true' },
{ variable: 'level', value: 'advanced' },
],
}]
const { result } = renderHook(() => useCheckValidated(form as unknown as AnyFormApi, schemas))
expect(result.current.checkValidated()).toBe(false)
expect(mockNotify).toHaveBeenCalledWith({
type: 'error',
message: 'Advanced is required',
})
})
it('should ignore error when one of multiple conditions does not match', () => {
const form = {
getAllErrors: () => ({
fields: {
advancedOption: { errors: ['Advanced is required'] },
},
}),
state: { values: { enabled: 'true', level: 'basic' } },
}
const schemas = [{
name: 'advancedOption',
label: 'Advanced Option',
required: true,
type: FormTypeEnum.textInput,
show_on: [
{ variable: 'enabled', value: 'true' },
{ variable: 'level', value: 'advanced' },
],
}]
const { result } = renderHook(() => useCheckValidated(form as unknown as AnyFormApi, schemas))
expect(result.current.checkValidated()).toBe(true)
expect(mockNotify).not.toHaveBeenCalled()
})
it('should handle field with error when schema is not found', () => {
const form = {
getAllErrors: () => ({
fields: {
unknownField: { errors: ['Unknown error'] },
},
}),
state: { values: {} },
}
const schemas = [{
name: 'knownField',
label: 'Known Field',
required: true,
type: FormTypeEnum.textInput,
show_on: [],
}]
const { result } = renderHook(() => useCheckValidated(form as unknown as AnyFormApi, schemas))
expect(result.current.checkValidated()).toBe(false)
expect(mockNotify).toHaveBeenCalledWith({
type: 'error',
message: 'Unknown error',
})
expect(mockNotify).toHaveBeenCalledTimes(1)
})
it('should handle field with multiple errors and notify only first one', () => {
const form = {
getAllErrors: () => ({
fields: {
field1: { errors: ['First error', 'Second error'] },
},
}),
state: { values: {} },
}
const schemas = [{
name: 'field1',
label: 'Field 1',
required: true,
type: FormTypeEnum.textInput,
show_on: [],
}]
const { result } = renderHook(() => useCheckValidated(form as unknown as AnyFormApi, schemas))
expect(result.current.checkValidated()).toBe(false)
expect(mockNotify).toHaveBeenCalledWith({
type: 'error',
message: 'First error',
})
})
it('should return true when all visible fields have no errors', () => {
const form = {
getAllErrors: () => ({
fields: {
visibleField: { errors: [] },
hiddenField: { errors: [] },
},
}),
state: { values: { showHidden: 'false' } },
}
const schemas = [
{
name: 'visibleField',
label: 'Visible Field',
required: true,
type: FormTypeEnum.textInput,
show_on: [],
},
{
name: 'hiddenField',
label: 'Hidden Field',
required: true,
type: FormTypeEnum.textInput,
show_on: [{ variable: 'showHidden', value: 'true' }],
},
]
const { result } = renderHook(() => useCheckValidated(form as unknown as AnyFormApi, schemas))
expect(result.current.checkValidated()).toBe(true)
expect(mockNotify).not.toHaveBeenCalled()
})
it('should properly evaluate show_on conditions with different values', () => {
const form = {
getAllErrors: () => ({
fields: {
numericField: { errors: ['Numeric error'] },
},
}),
state: { values: { threshold: '100' } },
}
const schemas = [{
name: 'numericField',
label: 'Numeric Field',
required: true,
type: FormTypeEnum.textInput,
show_on: [{ variable: 'threshold', value: '100' }],
}]
const { result } = renderHook(() => useCheckValidated(form as unknown as AnyFormApi, schemas))
expect(result.current.checkValidated()).toBe(false)
expect(mockNotify).toHaveBeenCalledWith({
type: 'error',
message: 'Numeric error',
})
})
})

View File

@ -71,4 +71,149 @@ describe('useGetFormValues', () => {
isCheckValidated: false,
})
})
it('should return raw values when validation passes but no transformation is requested', () => {
const form = {
store: { state: { values: { email: 'test@example.com' } } },
}
const schemas = [{
name: 'email',
label: 'Email',
required: true,
type: FormTypeEnum.textInput,
}]
mockCheckValidated.mockReturnValue(true)
const { result } = renderHook(() => useGetFormValues(form as unknown as AnyFormApi, schemas))
expect(result.current.getFormValues({
needCheckValidatedValues: true,
needTransformWhenSecretFieldIsPristine: false,
})).toEqual({
values: { email: 'test@example.com' },
isCheckValidated: true,
})
expect(mockTransform).not.toHaveBeenCalled()
})
it('should return raw values when validation passes and transformation is undefined', () => {
const form = {
store: { state: { values: { username: 'john_doe' } } },
}
const schemas = [{
name: 'username',
label: 'Username',
required: true,
type: FormTypeEnum.textInput,
}]
mockCheckValidated.mockReturnValue(true)
const { result } = renderHook(() => useGetFormValues(form as unknown as AnyFormApi, schemas))
expect(result.current.getFormValues({
needCheckValidatedValues: true,
needTransformWhenSecretFieldIsPristine: undefined,
})).toEqual({
values: { username: 'john_doe' },
isCheckValidated: true,
})
expect(mockTransform).not.toHaveBeenCalled()
})
it('should handle empty form values when validation check is disabled', () => {
const form = {
store: { state: { values: {} } },
}
const { result } = renderHook(() => useGetFormValues(form as unknown as AnyFormApi, []))
expect(result.current.getFormValues({ needCheckValidatedValues: false })).toEqual({
values: {},
isCheckValidated: true,
})
expect(mockCheckValidated).not.toHaveBeenCalled()
})
it('should handle null form values gracefully', () => {
const form = {
store: { state: { values: null } },
}
const { result } = renderHook(() => useGetFormValues(form as unknown as AnyFormApi, []))
expect(result.current.getFormValues({ needCheckValidatedValues: false })).toEqual({
values: {},
isCheckValidated: true,
})
})
it('should call transform with correct arguments when transformation is requested', () => {
const form = {
store: { state: { values: { password: 'secret' } } },
}
const schemas = [{
name: 'password',
label: 'Password',
required: true,
type: FormTypeEnum.secretInput,
}]
mockCheckValidated.mockReturnValue(true)
mockTransform.mockReturnValue({ password: 'encrypted' })
const { result } = renderHook(() => useGetFormValues(form as unknown as AnyFormApi, schemas))
result.current.getFormValues({
needCheckValidatedValues: true,
needTransformWhenSecretFieldIsPristine: true,
})
expect(mockTransform).toHaveBeenCalledWith(schemas, form)
})
it('should return validation failure before attempting transformation', () => {
const form = {
store: { state: { values: { password: 'secret' } } },
}
const schemas = [{
name: 'password',
label: 'Password',
required: true,
type: FormTypeEnum.secretInput,
}]
mockCheckValidated.mockReturnValue(false)
const { result } = renderHook(() => useGetFormValues(form as unknown as AnyFormApi, schemas))
expect(result.current.getFormValues({
needCheckValidatedValues: true,
needTransformWhenSecretFieldIsPristine: true,
})).toEqual({
values: {},
isCheckValidated: false,
})
expect(mockTransform).not.toHaveBeenCalled()
})
it('should handle complex nested values with validation check disabled', () => {
const form = {
store: {
state: {
values: {
user: { name: 'Alice', age: 30 },
settings: { theme: 'dark' },
},
},
},
}
const { result } = renderHook(() => useGetFormValues(form as unknown as AnyFormApi, []))
expect(result.current.getFormValues({ needCheckValidatedValues: false })).toEqual({
values: {
user: { name: 'Alice', age: 30 },
settings: { theme: 'dark' },
},
isCheckValidated: true,
})
})
})

View File

@ -75,4 +75,59 @@ describe('useGetValidators', () => {
expect(changeMessage).toContain('"field":"Workspace"')
expect(nonRequiredValidators).toBeUndefined()
})
it('should return undefined when value is truthy (onMount, onChange, onBlur)', () => {
const { result } = renderHook(() => useGetValidators())
const validators = result.current.getValidators({
name: 'username',
label: 'Username',
required: true,
type: FormTypeEnum.textInput,
})
expect(validators?.onMount?.({ value: 'some value' })).toBeUndefined()
expect(validators?.onChange?.({ value: 'some value' })).toBeUndefined()
expect(validators?.onBlur?.({ value: 'some value' })).toBeUndefined()
})
it('should handle null/missing labels correctly', () => {
const { result } = renderHook(() => useGetValidators())
// Explicitly test fallback to name when label is missing
const validators = result.current.getValidators({
name: 'id_field',
label: null as unknown as string,
required: true,
type: FormTypeEnum.textInput,
})
const mountMessage = validators?.onMount?.({ value: '' })
expect(mountMessage).toContain('"field":"id_field"')
})
it('should handle onChange message with fallback to name', () => {
const { result } = renderHook(() => useGetValidators())
const validators = result.current.getValidators({
name: 'desc',
label: createElement('span'), // results in '' label
required: true,
type: FormTypeEnum.textInput,
})
const changeMessage = validators?.onChange?.({ value: '' })
expect(changeMessage).toContain('"field":"desc"')
})
it('should handle onBlur message specifically', () => {
const { result } = renderHook(() => useGetValidators())
const validators = result.current.getValidators({
name: 'email',
label: 'Email Address',
required: true,
type: FormTypeEnum.textInput,
})
const blurMessage = validators?.onBlur?.({ value: '' })
expect(blurMessage).toContain('"field":"Email Address"')
})
})

View File

@ -24,6 +24,28 @@ describe('zodSubmitValidator', () => {
})
})
it('should only keep the first error when multiple errors occur for the same field', () => {
// Both string() empty check and email() validation will fail here conceptually,
// but Zod aborts early on type errors sometimes. Let's use custom refinements that both trigger
const schema = z.object({
email: z.string().superRefine((val, ctx) => {
if (!val.includes('@')) {
ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Invalid email format' })
}
if (val.length < 10) {
ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Email too short' })
}
}),
})
const validator = zodSubmitValidator(schema)
// "bad" triggers both missing '@' and length < 10
expect(validator({ value: { email: 'bad' } })).toEqual({
fields: {
email: 'Invalid email format',
},
})
})
it('should ignore root-level issues without a field path', () => {
const schema = z.object({ value: z.number() }).superRefine((_value, ctx) => {
ctx.addIssue({

View File

@ -51,4 +51,64 @@ describe('secret input utilities', () => {
apiKey: 'secret',
})
})
it('should not mask when secret name is not in the values object', () => {
expect(transformFormSchemasSecretInput(['missing'], {
apiKey: 'secret',
})).toEqual({
apiKey: 'secret',
})
})
it('should not mask falsy values like 0 or null', () => {
expect(transformFormSchemasSecretInput(['zeroVal', 'nullVal'], {
zeroVal: 0,
nullVal: null,
})).toEqual({
zeroVal: 0,
nullVal: null,
})
})
it('should return empty object when form values are undefined', () => {
const formSchemas = [
{ name: 'apiKey', type: FormTypeEnum.secretInput, label: 'API Key', required: true },
]
const form = {
store: { state: { values: undefined } },
getFieldMeta: () => ({ isPristine: true }),
}
expect(getTransformedValuesWhenSecretInputPristine(formSchemas, form as unknown as AnyFormApi)).toEqual({})
})
it('should handle fieldMeta being undefined', () => {
const formSchemas = [
{ name: 'apiKey', type: FormTypeEnum.secretInput, label: 'API Key', required: true },
]
const form = {
store: { state: { values: { apiKey: 'secret' } } },
getFieldMeta: () => undefined,
}
expect(getTransformedValuesWhenSecretInputPristine(formSchemas, form as unknown as AnyFormApi)).toEqual({
apiKey: 'secret',
})
})
it('should skip non-secretInput schema types entirely', () => {
const formSchemas = [
{ name: 'name', type: FormTypeEnum.textInput, label: 'Name', required: true },
{ name: 'desc', type: FormTypeEnum.textInput, label: 'Desc', required: false },
]
const form = {
store: { state: { values: { name: 'Alice', desc: 'Test' } } },
getFieldMeta: () => ({ isPristine: true }),
}
expect(getTransformedValuesWhenSecretInputPristine(formSchemas, form as unknown as AnyFormApi)).toEqual({
name: 'Alice',
desc: 'Test',
})
})
})

View File

@ -224,6 +224,35 @@ describe('ChatImageUploader', () => {
expect(queryFileInput()).toBeInTheDocument()
})
it('should close popover when local upload calls closePopover in mixed mode', async () => {
const user = userEvent.setup()
const settings = createSettings({
transfer_methods: [TransferMethod.local_file, TransferMethod.remote_url],
})
mocks.handleLocalFileUpload.mockImplementation((file) => {
mocks.hookArgs?.onUpload({
type: TransferMethod.local_file,
_id: 'mixed-local-upload-id',
fileId: '',
progress: 0,
url: 'data:image/png;base64,mixed',
file,
} as ImageFile)
})
render(<ChatImageUploader settings={settings} onUpload={defaultOnUpload} />)
await user.click(screen.getByRole('button'))
expect(screen.getByRole('textbox')).toBeInTheDocument()
const localInput = getFileInput()
const file = new File(['hello'], 'mixed.png', { type: 'image/png' })
await user.upload(localInput, file)
expect(screen.queryByRole('textbox')).not.toBeInTheDocument()
})
it('should toggle local-upload hover style in mixed transfer mode', async () => {
const user = userEvent.setup()
const settings = createSettings({

View File

@ -424,5 +424,50 @@ describe('ImagePreview', () => {
expect(image).toHaveStyle({ transform: 'scale(1) translate(0px, 0px)' })
})
})
it('should zoom out below 1 without resetting position', async () => {
const user = userEvent.setup()
render(
<ImagePreview
url="https://example.com/image.png"
title="Preview Image"
onCancel={vi.fn()}
/>,
)
const image = screen.getByRole('img', { name: 'Preview Image' })
await user.click(getZoomOutButton())
await waitFor(() => {
expect(image).toHaveStyle({ transform: 'scale(0.8333333333333334) translate(0px, 0px)' })
})
})
it('should keep drag move stable when rect data is unavailable', async () => {
const user = userEvent.setup()
render(
<ImagePreview
url="https://example.com/image.png"
title="Preview Image"
onCancel={vi.fn()}
/>,
)
const overlay = getOverlay()
const image = screen.getByRole('img', { name: 'Preview Image' }) as HTMLImageElement
const imageParent = image.parentElement
if (!imageParent)
throw new Error('Image parent element not found')
vi.spyOn(image, 'getBoundingClientRect').mockReturnValue(undefined as unknown as DOMRect)
vi.spyOn(imageParent, 'getBoundingClientRect').mockReturnValue(undefined as unknown as DOMRect)
await user.click(getZoomInButton())
act(() => {
overlay.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, clientX: 10, clientY: 10 }))
overlay.dispatchEvent(new MouseEvent('mousemove', { bubbles: true, clientX: 120, clientY: 60 }))
})
expect(image).toHaveStyle({ transform: 'scale(1.2) translate(0px, 0px)' })
})
})
})

View File

@ -17,7 +17,12 @@ const ImageLinkInput: FC<ImageLinkInputProps> = ({
const { t } = useTranslation()
const [imageLink, setImageLink] = useState('')
const placeholder = t('imageUploader.pasteImageLinkInputPlaceholder', { ns: 'common' })
/* v8 ignore next -- defensive i18n fallback; translation key resolves to non-empty text in normal runtime/test setup, so empty-placeholder branch is not exercised without forcing i18n internals. @preserve */
const safeText = placeholder || ''
const handleClick = () => {
/* v8 ignore next 2 -- same condition drives Button.disabled; when true, click does not invoke onClick in user-level flow. @preserve */
if (disabled)
return
@ -39,7 +44,7 @@ const ImageLinkInput: FC<ImageLinkInputProps> = ({
className="mr-0.5 h-[18px] grow appearance-none bg-transparent px-1 text-[13px] text-text-primary outline-none"
value={imageLink}
onChange={e => setImageLink(e.target.value)}
placeholder={t('imageUploader.pasteImageLinkInputPlaceholder', { ns: 'common' }) || ''}
placeholder={safeText}
data-testid="image-link-input"
/>
<Button

View File

@ -1,6 +1,4 @@
import { fireEvent, render, screen } from '@testing-library/react'
import * as React from 'react'
import { createReactI18nextMock } from '@/test/i18n-mock'
import InputWithCopy from '../index'
// Create a controllable mock for useClipboard
@ -16,14 +14,6 @@ vi.mock('foxact/use-clipboard', () => ({
}),
}))
// Mock the i18n hook with custom translations for test assertions
vi.mock('react-i18next', () => createReactI18nextMock({
'operation.copy': 'Copy',
'operation.copied': 'Copied',
'overview.appInfo.embedded.copy': 'Copy',
'overview.appInfo.embedded.copied': 'Copied',
}))
describe('InputWithCopy component', () => {
beforeEach(() => {
vi.clearAllMocks()
@ -145,4 +135,98 @@ describe('InputWithCopy component', () => {
// Input should maintain focus after copy
expect(input).toHaveFocus()
})
it('converts non-string value to string for copying', () => {
const mockOnChange = vi.fn()
// number value triggers String(value || '') branch where typeof value !== 'string'
render(<InputWithCopy value={12345} onChange={mockOnChange} />)
const copyButton = screen.getByRole('button')
fireEvent.click(copyButton)
expect(mockCopy).toHaveBeenCalledWith('12345')
})
it('handles undefined value by converting to empty string', () => {
const mockOnChange = vi.fn()
// undefined value triggers String(value || '') where value is falsy
render(<InputWithCopy value={undefined} onChange={mockOnChange} />)
const copyButton = screen.getByRole('button')
fireEvent.click(copyButton)
expect(mockCopy).toHaveBeenCalledWith('')
})
it('shows copied tooltip text when copied state is true', () => {
mockCopied = true
const mockOnChange = vi.fn()
render(<InputWithCopy value="test value" onChange={mockOnChange} />)
// The tooltip content should use the 'copied' translation
const copyButton = screen.getByRole('button')
expect(copyButton).toBeInTheDocument()
// Verify the filled clipboard icon is rendered (not the line variant)
const filledIcon = screen.getByTestId('copied-icon')
expect(filledIcon).toBeInTheDocument()
})
it('shows copy tooltip text when copied state is false', () => {
mockCopied = false
const mockOnChange = vi.fn()
render(<InputWithCopy value="test value" onChange={mockOnChange} />)
const copyButton = screen.getByRole('button')
expect(copyButton).toBeInTheDocument()
const lineIcon = screen.getByTestId('copy-icon')
expect(lineIcon).toBeInTheDocument()
})
it('calls reset on mouse leave from copy button wrapper', () => {
const mockOnChange = vi.fn()
render(<InputWithCopy value="test value" onChange={mockOnChange} />)
const wrapper = screen.getByTestId('copy-button-wrapper')
expect(wrapper).toBeInTheDocument()
fireEvent.mouseLeave(wrapper)
expect(mockReset).toHaveBeenCalled()
})
it('applies wrapperClassName to the outer container', () => {
const mockOnChange = vi.fn()
const { container } = render(
<InputWithCopy value="test" onChange={mockOnChange} wrapperClassName="my-wrapper" />,
)
const outerDiv = container.firstChild as HTMLElement
expect(outerDiv).toHaveClass('my-wrapper')
})
it('copies copyValue over non-string input value when both provided', () => {
const mockOnChange = vi.fn()
render(
<InputWithCopy value={42} onChange={mockOnChange} copyValue="override-copy" />,
)
const copyButton = screen.getByRole('button')
fireEvent.click(copyButton)
expect(mockCopy).toHaveBeenCalledWith('override-copy')
})
it('invokes onCopy with copyValue when copyValue is provided', () => {
const onCopyMock = vi.fn()
const mockOnChange = vi.fn()
render(
<InputWithCopy value="display" onChange={mockOnChange} copyValue="custom" onCopy={onCopyMock} />,
)
const copyButton = screen.getByRole('button')
fireEvent.click(copyButton)
expect(onCopyMock).toHaveBeenCalledWith('custom')
})
})

View File

@ -1,6 +1,5 @@
'use client'
import type { InputProps } from '../input'
import { RiClipboardFill, RiClipboardLine } from '@remixicon/react'
import { useClipboard } from 'foxact/use-clipboard'
import * as React from 'react'
import { useTranslation } from 'react-i18next'
@ -39,13 +38,19 @@ const InputWithCopy = React.forwardRef<HTMLInputElement, InputWithCopyProps>((
onCopy?.(finalCopyValue)
}
const tooltipText = copied
? t(`${prefixEmbedded}.copied`, { ns: 'appOverview' })
: t(`${prefixEmbedded}.copy`, { ns: 'appOverview' })
/* v8 ignore next -- i18n test mock always returns a non-empty string; runtime fallback is defensive. -- @preserve */
const safeTooltipText = tooltipText || ''
return (
<div className={cn('relative w-full', wrapperClassName)}>
<input
ref={ref}
className={cn(
'w-full appearance-none border border-transparent bg-components-input-bg-normal py-[7px] text-components-input-text-filled caret-primary-600 outline-none placeholder:text-components-input-text-placeholder hover:border-components-input-border-hover hover:bg-components-input-bg-hover focus:border-components-input-border-active focus:bg-components-input-bg-active focus:shadow-xs',
'radius-md system-sm-regular px-3',
'px-3 system-sm-regular radius-md',
showCopyButton && 'pr-8',
inputProps.disabled && 'cursor-not-allowed border-transparent bg-components-input-bg-disabled text-components-input-text-filled-disabled hover:border-transparent hover:bg-components-input-bg-disabled',
inputProps.className,
@ -57,13 +62,10 @@ const InputWithCopy = React.forwardRef<HTMLInputElement, InputWithCopyProps>((
<div
className="absolute right-2 top-1/2 -translate-y-1/2"
onMouseLeave={reset}
data-testid="copy-button-wrapper"
>
<Tooltip
popupContent={
(copied
? t(`${prefixEmbedded}.copied`, { ns: 'appOverview' })
: t(`${prefixEmbedded}.copy`, { ns: 'appOverview' })) || ''
}
popupContent={safeTooltipText}
>
<ActionButton
size="xs"
@ -71,12 +73,8 @@ const InputWithCopy = React.forwardRef<HTMLInputElement, InputWithCopyProps>((
className="hover:bg-components-button-ghost-bg-hover"
>
{copied
? (
<RiClipboardFill className="h-3.5 w-3.5 text-text-tertiary" />
)
: (
<RiClipboardLine className="h-3.5 w-3.5 text-text-tertiary" />
)}
? (<span className="i-ri-clipboard-fill h-3.5 w-3.5 text-text-tertiary" data-testid="copied-icon" />)
: (<span className="i-ri-clipboard-line h-3.5 w-3.5 text-text-tertiary" data-testid="copy-icon" />)}
</ActionButton>
</Tooltip>
</div>

View File

@ -115,6 +115,41 @@ describe('Input component', () => {
expect(input).toBeInTheDocument()
})
describe('Additional Layout Branches', () => {
it('applies pl-7 when showLeftIcon and size is large', () => {
render(<Input showLeftIcon size="large" />)
const input = screen.getByRole('textbox')
expect(input).toHaveClass('pl-7')
})
it('applies pr-7 when showClearIcon, has value, and size is large', () => {
render(<Input showClearIcon value="123" size="large" onChange={vi.fn()} />)
const input = screen.getByRole('textbox')
expect(input).toHaveClass('pr-7')
})
it('applies pr-7 when destructive and size is large', () => {
render(<Input destructive size="large" />)
const input = screen.getByRole('textbox')
expect(input).toHaveClass('pr-7')
})
it('shows copy icon and applies pr-[26px] when showCopyIcon is true', () => {
render(<Input showCopyIcon />)
const input = screen.getByRole('textbox')
expect(input).toHaveClass('pr-[26px]')
// Assert that CopyFeedbackNew wrapper is present
const copyWrapper = document.querySelector('.group.absolute.right-0')
expect(copyWrapper).toBeInTheDocument()
})
it('shows copy icon and applies pr-7 when showCopyIcon and size is large', () => {
render(<Input showCopyIcon size="large" value="my-val" onChange={vi.fn()} />)
const input = screen.getByRole('textbox')
expect(input).toHaveClass('pr-7')
})
})
describe('Number Input Formatting', () => {
it('removes leading zeros on change when current value is zero', () => {
let changedValue = ''
@ -130,6 +165,17 @@ describe('Input component', () => {
expect(changedValue).toBe('42')
})
it('does not normalize when value is 0 and input value is already normalized', () => {
const onChange = vi.fn()
render(<Input type="number" value={0} onChange={onChange} />)
const input = screen.getByRole('spinbutton') as HTMLInputElement
// The event value ('1') is already normalized, preventing e.target.value reassignment
fireEvent.change(input, { target: { value: '1' } })
expect(onChange).toHaveBeenCalledTimes(1)
})
it('keeps typed value on change when current value is not zero', () => {
let changedValue = ''
const onChange = vi.fn((e: React.ChangeEvent<HTMLInputElement>) => {

View File

@ -25,4 +25,9 @@ describe('Loading Component', () => {
const svgElement = container.querySelector('svg')
expect(svgElement).toHaveClass('spin-animation')
})
it('handles undefined props correctly', () => {
const { container } = render(Loading() as unknown as React.ReactElement)
expect(container.firstChild).toHaveClass('flex w-full items-center justify-center')
})
})

View File

@ -118,4 +118,11 @@ describe('MarkdownButton (integration)', () => {
const comp = MarkdownButton as NamedExoticComponent<{ node: unknown }>
expect(comp.displayName).toBe('MarkdownButton')
})
it('falls back to empty label when first child value is missing', () => {
const node: TestNode = { properties: {}, children: [{}] }
renderWithCtx(node)
expect(screen.getByRole('button')).toHaveTextContent('')
})
})

View File

@ -59,6 +59,11 @@ vi.mock('@/hooks/use-theme', () => ({
default: () => mockUseTheme(),
}))
vi.mock('@/app/components/base/mermaid', () => ({
__esModule: true,
default: ({ PrimitiveCode }: { PrimitiveCode: string }) => <div data-testid="mock-mermaid">{PrimitiveCode}</div>,
}))
const findEchartsHost = async () => {
await waitFor(() => {
expect(document.querySelector('.echarts-for-react')).toBeInTheDocument()
@ -159,6 +164,12 @@ describe('CodeBlock', () => {
// expect(await screen.findByTestId('classic')).toBeInTheDocument()
// expect(screen.getByText('Mermaid')).toBeInTheDocument()
// })
it('should render mermaid block when language is mermaid', async () => {
render(<CodeBlock className="language-mermaid">{'graph TD; A-->B;'}</CodeBlock>)
expect(screen.getByText('Mermaid')).toBeInTheDocument()
expect(await screen.findByTestId('mock-mermaid')).toHaveTextContent('graph TD; A-->B;')
})
it('should render abc section header when language is abc', () => {
render(<CodeBlock className="language-abc">X:1\nT:test</CodeBlock>)
@ -351,5 +362,15 @@ describe('CodeBlock', () => {
unmount()
})
it('should cleanup echarts resize listener when no debounce timer is pending', async () => {
const { rerender, unmount } = render(<CodeBlock className="language-echarts">{'{"a":1}'}</CodeBlock>)
await findEchartsHost()
rerender(<CodeBlock className="language-javascript">const x = 1;</CodeBlock>)
rerender(<CodeBlock className="language-echarts">{'{"a":2}'}</CodeBlock>)
unmount()
})
})
})

View File

@ -43,6 +43,23 @@ describe('Link component', () => {
expect(mockOnSend).toHaveBeenCalledWith('hello world')
})
it('renders abbr with empty fallback title/value when child value is missing', () => {
const node = {
properties: {
href: 'abbr:hi',
},
children: [{}],
}
const { container } = render(<Link node={node} />)
const abbr = container.querySelector('abbr')
expect(abbr).toBeTruthy()
expect(abbr?.tagName).toBe('ABBR')
fireEvent.click(abbr as HTMLElement)
expect(mockOnSend).toHaveBeenCalledWith('hi')
})
// --------------------------
// HASH SCROLL LINK
// --------------------------
@ -79,6 +96,40 @@ describe('Link component', () => {
expect(scrollIntoView).toHaveBeenCalled()
})
it('does not throw when hash link is clicked outside chat-answer-container', () => {
const node = {
properties: {
href: '#section2',
},
}
render(<Link node={node}>Outside</Link>)
expect(() => {
fireEvent.click(screen.getByText('Outside'))
}).not.toThrow()
})
it('does not scroll when hash target element is missing', () => {
const scrollIntoView = vi.fn()
Element.prototype.scrollIntoView = scrollIntoView
const node = {
properties: {
href: '#missing-target',
},
}
render(
<div className="chat-answer-container">
<Link node={node}>Missing</Link>
</div>,
)
fireEvent.click(screen.getByText('Missing'))
expect(scrollIntoView).not.toHaveBeenCalled()
})
// --------------------------
// INVALID URL
// --------------------------

View File

@ -1,16 +1,46 @@
import { render, screen } from '@testing-library/react'
import { render } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import ErrorBoundary from '@/app/components/base/markdown/error-boundary'
import MarkdownMusic from '../music'
const mockSetTune = vi.fn()
const mockLoad = vi.fn()
const mockInit = vi.fn().mockResolvedValue(undefined)
const mockRenderAbc = vi.fn().mockReturnValue([{}])
vi.mock('abcjs', () => ({
__esModule: true,
default: {
renderAbc: (...args: unknown[]) => mockRenderAbc(...args),
synth: {
SynthController: class {
load(...args: unknown[]) {
mockLoad(...args)
}
setTune(...args: unknown[]) {
mockSetTune(...args)
}
},
CreateSynth: class {
init(...args: unknown[]) {
return mockInit(...args)
}
},
},
},
}))
const loadMarkdownMusic = async () => (await import('../music')).default
describe('MarkdownMusic', () => {
beforeEach(() => {
vi.resetModules()
vi.clearAllMocks()
})
// Base rendering behavior for the component shell.
describe('Rendering', () => {
it('should render wrapper and two internal container nodes', () => {
it('should render wrapper and two internal container nodes', async () => {
const MarkdownMusic = await loadMarkdownMusic()
const { container } = render(<MarkdownMusic><span>child</span></MarkdownMusic>)
const topLevel = container.firstElementChild as HTMLElement | null
@ -21,26 +51,59 @@ describe('MarkdownMusic', () => {
})
})
// String input triggers abcjs execution in jsdom; verify error is safely catchable.
// String input should trigger abcjs rendering and synth initialization.
describe('String Input', () => {
it('should render fallback when abcjs audio initialization fails in test environment', async () => {
render(
<ErrorBoundary>
<MarkdownMusic>{'X:1\nT:Test\nK:C\nC D E F|'}</MarkdownMusic>
</ErrorBoundary>,
)
it('should render music notation and initialize synth when children is a string', async () => {
const MarkdownMusic = await loadMarkdownMusic()
render(<MarkdownMusic>{'X:1\nT:Test\nK:C\nC D E F|'}</MarkdownMusic>)
expect(await screen.findByText(/Oops! An error occurred./i)).toBeInTheDocument()
expect(mockRenderAbc).toHaveBeenCalledTimes(1)
expect(mockLoad).toHaveBeenCalledTimes(1)
expect(mockInit).toHaveBeenCalledTimes(1)
await Promise.resolve()
expect(mockSetTune).toHaveBeenCalledTimes(1)
})
it('should not render fallback when children is not a string', () => {
render(
<ErrorBoundary>
<MarkdownMusic><span>not a string</span></MarkdownMusic>
</ErrorBoundary>,
)
it('should not render fallback when children is not a string', async () => {
const MarkdownMusic = await loadMarkdownMusic()
render(<MarkdownMusic><span>not a string</span></MarkdownMusic>)
expect(mockRenderAbc).not.toHaveBeenCalled()
expect(mockLoad).not.toHaveBeenCalled()
expect(mockInit).not.toHaveBeenCalled()
})
expect(screen.queryByText(/Oops! An error occurred./i)).not.toBeInTheDocument()
it('should call abcjs renderer with expected options for string input', async () => {
const MarkdownMusic = await loadMarkdownMusic()
render(<MarkdownMusic>{'X:1\nT:Opts\nK:C\nC D E F|'}</MarkdownMusic>)
expect(mockRenderAbc).toHaveBeenCalledWith(
expect.any(HTMLDivElement),
'X:1\nT:Opts\nK:C\nC D E F|',
expect.objectContaining({
add_classes: true,
responsive: 'resize',
}),
)
})
it('should skip initialization when refs are unavailable', async () => {
vi.doMock('react', async (importOriginal) => {
const actual = await importOriginal<typeof import('react')>()
return {
...actual,
useEffect: (effect: () => void) => {
effect()
},
}
})
const MarkdownMusic = await loadMarkdownMusic()
render(<MarkdownMusic>{'X:1\nT:NoRef\nK:C\nC D E F|'}</MarkdownMusic>)
expect(mockRenderAbc).not.toHaveBeenCalled()
expect(mockLoad).not.toHaveBeenCalled()
expect(mockInit).not.toHaveBeenCalled()
vi.doUnmock('react')
})
})
})

View File

@ -7,10 +7,17 @@ const { mockReactMarkdownWrapper } = vi.hoisted(() => ({
mockReactMarkdownWrapper: vi.fn(),
}))
vi.mock('../react-markdown-wrapper', () => ({
ReactMarkdownWrapper: () => null,
}))
vi.mock('next/dynamic', () => ({
default: () => (props: { latexContent: string }) => {
mockReactMarkdownWrapper(props)
return <div data-testid="react-markdown-wrapper">{props.latexContent}</div>
default: (loader: () => Promise<unknown>) => {
void loader()
return (props: { latexContent: string }) => {
mockReactMarkdownWrapper(props)
return <div data-testid="react-markdown-wrapper">{props.latexContent}</div>
}
},
}))

View File

@ -30,6 +30,12 @@ describe('preprocessLaTeX', () => {
expect(out).toContain('$$x^2 + 1$$')
})
it('converts multiline \\[ ... \\] blocks into $$ ... $$', () => {
const input = 'Block:\n\\[\na+b=c\n\\]'
const out = mod.preprocessLaTeX(input)
expect(out).toContain('$$\na+b=c\n$$')
})
it('converts \\( ... \\) into $$ ... $$', () => {
const input = 'Inline: \\(a+b\\)'
const out = mod.preprocessLaTeX(input)
@ -91,6 +97,14 @@ describe('preprocessThinkTag', () => {
const endCount = (out.match(/\[ENDTHINKFLAG\]<\/details>/g) || []).length
expect(endCount).toBe(2)
})
it('normalizes repeated think tags to a single details pair', () => {
const input = '<think><think>deep</think></think>'
const out = mod.preprocessThinkTag(input)
expect((out.match(/<details data-think=true>/g) || []).length).toBe(1)
expect((out.match(/\[ENDTHINKFLAG\]<\/details>/g) || []).length).toBe(1)
})
})
describe('customUrlTransform', () => {

View File

@ -42,7 +42,7 @@ export const Markdown = memo((props: MarkdownProps) => {
const latexContent = useMemo(() => preprocess(content), [content])
return (
<div className={cn('markdown-body', '!text-text-primary', className)}>
<div className={cn('markdown-body', '!text-text-primary', className)} data-testid="markdown-body">
<StreamdownWrapper
pluginInfo={pluginInfo}
latexContent={latexContent}

View File

@ -4,9 +4,11 @@ import { useStore } from '@/app/components/app/store'
import MessageLogModal from '../index'
let clickAwayHandler: (() => void) | null = null
let clickAwayHandlers: (() => void)[] = []
vi.mock('ahooks', () => ({
useClickAway: (fn: () => void) => {
clickAwayHandler = fn
clickAwayHandlers.push(fn)
},
}))
@ -38,6 +40,7 @@ describe('MessageLogModal', () => {
beforeEach(() => {
vi.clearAllMocks()
clickAwayHandler = null
clickAwayHandlers = []
// eslint-disable-next-line ts/no-explicit-any
vi.mocked(useStore).mockImplementation((selector: any) => selector({
appDetail: { id: 'app-1' },
@ -100,5 +103,12 @@ describe('MessageLogModal', () => {
clickAwayHandler!()
expect(onCancel).toHaveBeenCalledTimes(1)
})
it('does not call onCancel when clicked away if not mounted', () => {
render(<MessageLogModal width={800} onCancel={onCancel} currentLogItem={mockLog} />)
expect(clickAwayHandlers.length).toBeGreaterThan(0)
clickAwayHandlers[0]() // This is the closure from the initial render, where mounted is false
expect(onCancel).not.toHaveBeenCalled()
})
})
})

Some files were not shown because too many files have changed in this diff Show More