diff --git a/packages/db/src/test-global-setup.ts b/packages/db/src/test-global-setup.ts index 53af64e9..51b354b0 100644 --- a/packages/db/src/test-global-setup.ts +++ b/packages/db/src/test-global-setup.ts @@ -1,7 +1,6 @@ import { PostgreSqlContainer } from '@testcontainers/postgresql' import { exec } from 'node:child_process' import { promisify } from 'node:util' -import { Wait } from 'testcontainers' const execAsync = promisify(exec) @@ -9,13 +8,7 @@ let dbContainer: import('@testcontainers/postgresql').StartedPostgreSqlContainer export async function globalTestSetup() { console.log('Starting Testcontainers for global setup...') - dbContainer = await new PostgreSqlContainer('pgvector/pgvector:pg18') - .withWaitStrategy( - // casting to any due to testcontainers wait strategy type mismatch - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Wait.forLogMessage('database system is ready to accept connections', 1) as any, - ) - .start() + dbContainer = await new PostgreSqlContainer('pgvector/pgvector:pg18').start() const databaseUrl = dbContainer.getConnectionUri() console.log('Database started at:', databaseUrl) diff --git a/packages/webui/components/chat/message-input.test.tsx b/packages/webui/components/chat/message-input.test.tsx index 7af9390f..f2efe9c0 100644 --- a/packages/webui/components/chat/message-input.test.tsx +++ b/packages/webui/components/chat/message-input.test.tsx @@ -141,3 +141,111 @@ describe('ChatInput mention navigation', () => { expect(mentionNode.textContent).toContain('David Human') }) }) + +describe('ChatInput paste handling', () => { + afterEach(() => { + cleanup() + vi.clearAllMocks() + }) + + it('pastes plain text only and strips HTML formatting when styled HTML is in clipboard', async () => { + const queryClient = new QueryClient({ + defaultOptions: { queries: { retry: false } }, + }) + + const mockSendMessage = vi.fn() + const mockChangeText = vi.fn() + + const { container } = render( + + + , + ) + + const editor = container.querySelector('[contenteditable="true"]') as HTMLDivElement + expect(editor).toBeTruthy() + + // Focus editor and set selection + editor.focus() + const range = document.createRange() + range.selectNodeContents(editor) + range.collapse(false) + const sel = window.getSelection()! + sel.removeAllRanges() + sel.addRange(range) + + // Simulate pasting HTML with plain text fallback + fireEvent.paste(editor, { + clipboardData: { + getData: (format: string) => { + if (format === 'text/plain') return 'Clean plain text' + if (format === 'text/html') + return 'Clean plain text' + return '' + }, + }, + }) + + expect(editor.innerHTML).not.toContain(' { + const queryClient = new QueryClient({ + defaultOptions: { queries: { retry: false } }, + }) + + const mockSendMessage = vi.fn() + + const { container } = render( + + + , + ) + + const editor = container.querySelector('[contenteditable="true"]') as HTMLDivElement + expect(editor).toBeTruthy() + + editor.focus() + const range = document.createRange() + range.selectNodeContents(editor) + range.collapse(false) + const sel = window.getSelection()! + sel.removeAllRanges() + sel.addRange(range) + + const multilineText = 'First line\nSecond line\nThird line' + + fireEvent.paste(editor, { + clipboardData: { + getData: (format: string) => { + if (format === 'text/plain') return multilineText + return `

First line

Second line

Third line

` + }, + }, + }) + + expect(editor.textContent).toBe(multilineText) + + // Send message and verify multiline content is preserved in onSendMessage + fireEvent.keyDown(editor, { key: 'Enter' }) + expect(mockSendMessage).toHaveBeenCalledWith(multilineText, [], [], undefined, undefined, []) + }) +}) diff --git a/packages/webui/components/chat/message-input.tsx b/packages/webui/components/chat/message-input.tsx index c254f4b4..99949425 100644 --- a/packages/webui/components/chat/message-input.tsx +++ b/packages/webui/components/chat/message-input.tsx @@ -432,6 +432,52 @@ export const ChatInput = React.forwardRef( editorRef.current.focus() } + const handlePaste = (e: React.ClipboardEvent) => { + e.preventDefault() + const text = e.clipboardData.getData('text/plain') + if (!text) return + + let selection = window.getSelection() + if ( + !selection || + selection.rangeCount === 0 || + !editorRef.current?.contains(selection.anchorNode) + ) { + if (editorRef.current) { + editorRef.current.focus() + const range = document.createRange() + range.selectNodeContents(editorRef.current) + range.collapse(false) + selection = window.getSelection() + selection?.removeAllRanges() + selection?.addRange(range) + } + } + + const success = + typeof document.execCommand === 'function' && + document.queryCommandSupported?.('insertText') && + document.execCommand('insertText', false, text) + + if (!success) { + const sel = window.getSelection() + if (sel && sel.rangeCount > 0) { + const range = sel.getRangeAt(0) + range.deleteContents() + const textNode = document.createTextNode(text) + range.insertNode(textNode) + range.setStartAfter(textNode) + range.collapse(true) + sel.removeAllRanges() + sel.addRange(range) + } else if (editorRef.current) { + editorRef.current.innerText += text + } + } + + handleInput() + } + const handleKeyDown = (e: React.KeyboardEvent) => { if (e.nativeEvent.isComposing) return @@ -889,10 +935,11 @@ export const ChatInput = React.forwardRef( suppressContentEditableWarning onInput={handleInput} onKeyDown={handleKeyDown} + onPaste={handlePaste} data-placeholder={ placeholder || (replyingTo ? m.reply_placeholder() : m.message_placeholder()) } - className={`bg-transparent border-none focus:ring-0 resize-none min-h-[40px] leading-relaxed py-2 focus:outline-none block ${paddingLeftClass}`} + className={`bg-transparent border-none focus:ring-0 resize-none min-h-[40px] leading-relaxed py-2 focus:outline-none block whitespace-pre-wrap break-words ${paddingLeftClass}`} />