|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act, useRef } from 'react' |
| 5 | +import type { Virtualizer } from '@tanstack/react-virtual' |
| 6 | +import { createRoot } from 'react-dom/client' |
| 7 | +import { renderToStaticMarkup } from 'react-dom/server' |
| 8 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 9 | +import { ChatContent } from '@/app/workspace/[workspaceId]/home/components/message-content/components/chat-content/chat-content' |
| 10 | +import { useChatFind } from '@/app/workspace/[workspaceId]/home/components/mothership-chat/use-chat-find' |
| 11 | +import type { ChatMessage } from '@/app/workspace/[workspaceId]/home/types' |
| 12 | + |
| 13 | +type ChatFind = ReturnType<typeof useChatFind> |
| 14 | + |
| 15 | +interface HarnessProps { |
| 16 | + chatId?: string |
| 17 | + messages: ChatMessage[] |
| 18 | + onRender: (find: ChatFind) => void |
| 19 | +} |
| 20 | + |
| 21 | +// double-cast-allowed: the hook only calls scrollToIndex on the virtualizer |
| 22 | +const virtualizer = { scrollToIndex: () => {} } as unknown as Virtualizer<HTMLDivElement, Element> |
| 23 | + |
| 24 | +function Harness({ chatId, messages, onRender }: HarnessProps) { |
| 25 | + const containerRef = useRef<HTMLDivElement>(null) |
| 26 | + const scrollElementRef = useRef<HTMLDivElement>(null) |
| 27 | + const find = useChatFind({ |
| 28 | + chatId, |
| 29 | + messages, |
| 30 | + hiddenUserByIndex: [], |
| 31 | + containerRef, |
| 32 | + scrollElementRef, |
| 33 | + virtualizer, |
| 34 | + }) |
| 35 | + onRender(find) |
| 36 | + return ( |
| 37 | + <div ref={containerRef} tabIndex={-1} onKeyDown={find.onKeyDown}> |
| 38 | + <div ref={scrollElementRef}> |
| 39 | + {messages.map((message, index) => ( |
| 40 | + <div |
| 41 | + key={message.id} |
| 42 | + data-index={index} |
| 43 | + dangerouslySetInnerHTML={{ |
| 44 | + __html: renderToStaticMarkup(<ChatContent content={message.content} />), |
| 45 | + }} |
| 46 | + /> |
| 47 | + ))} |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | +const cleanups: (() => void)[] = [] |
| 54 | + |
| 55 | +function renderFind(props: Omit<HarnessProps, 'onRender'>) { |
| 56 | + let find: ChatFind | undefined |
| 57 | + const host = document.createElement('div') |
| 58 | + document.body.append(host) |
| 59 | + const root = createRoot(host) |
| 60 | + const render = (next: Omit<HarnessProps, 'onRender'>) => |
| 61 | + act(() => root.render(<Harness {...next} onRender={(value) => (find = value)} />)) |
| 62 | + cleanups.push(() => { |
| 63 | + act(() => root.unmount()) |
| 64 | + host.remove() |
| 65 | + }) |
| 66 | + render(props) |
| 67 | + return { |
| 68 | + get current() { |
| 69 | + if (!find) throw new Error('Hook was not rendered') |
| 70 | + return find |
| 71 | + }, |
| 72 | + rerender: render, |
| 73 | + search(query: string) { |
| 74 | + act(() => { |
| 75 | + host.firstElementChild?.dispatchEvent( |
| 76 | + new KeyboardEvent('keydown', { key: 'f', metaKey: true, bubbles: true }) |
| 77 | + ) |
| 78 | + }) |
| 79 | + act(() => find?.onQueryChange(query)) |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function assistant(id: string, content: string): ChatMessage { |
| 85 | + return { id, role: 'assistant', content } |
| 86 | +} |
| 87 | + |
| 88 | +class TestHighlight extends Set<Range> {} |
| 89 | + |
| 90 | +beforeEach(() => { |
| 91 | + vi.stubGlobal('Highlight', TestHighlight) |
| 92 | + vi.stubGlobal('CSS', { highlights: new Map<string, TestHighlight>() }) |
| 93 | + Range.prototype.getBoundingClientRect = () => new DOMRect() |
| 94 | +}) |
| 95 | + |
| 96 | +afterEach(() => { |
| 97 | + for (const cleanup of cleanups.splice(0)) cleanup() |
| 98 | + vi.unstubAllGlobals() |
| 99 | + Reflect.deleteProperty(Range.prototype, 'getBoundingClientRect') |
| 100 | +}) |
| 101 | + |
| 102 | +describe('useChatFind', () => { |
| 103 | + it('keeps the open search when a pending chat is persisted, and resets on a chat switch', () => { |
| 104 | + const messages = [assistant('a', 'The answer is here.')] |
| 105 | + const find = renderFind({ messages }) |
| 106 | + find.search('answer') |
| 107 | + expect(find.current).toMatchObject({ isOpen: true, query: 'answer', count: 1 }) |
| 108 | + |
| 109 | + find.rerender({ chatId: 'chat-1', messages }) |
| 110 | + expect(find.current).toMatchObject({ isOpen: true, query: 'answer', count: 1 }) |
| 111 | + |
| 112 | + find.rerender({ chatId: 'chat-2', messages }) |
| 113 | + expect(find.current).toMatchObject({ isOpen: false, query: '', count: 0 }) |
| 114 | + }) |
| 115 | + |
| 116 | + it('counts the same footnoted matches in the index as it highlights in the rendered message', async () => { |
| 117 | + const find = renderFind({ |
| 118 | + chatId: 'chat', |
| 119 | + messages: [assistant('a', 'Chapter 1 cites this[^1].\n\n[^1]: Page 1.')], |
| 120 | + }) |
| 121 | + find.search('1') |
| 122 | + await act(() => new Promise<void>((resolve) => requestAnimationFrame(() => resolve()))) |
| 123 | + |
| 124 | + const highlighted = CSS.highlights.get('chat-find') |
| 125 | + expect(find.current.count).toBe(1) |
| 126 | + expect(highlighted?.size).toBe(find.current.count) |
| 127 | + }) |
| 128 | +}) |
0 commit comments