mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-24 04:47:03 +00:00
148 lines
8.4 KiB
TypeScript
148 lines
8.4 KiB
TypeScript
import { AssistantRuntimeProvider, useLocalRuntime, useAuiState, type AssistantRuntime, type AttachmentAdapter, type ChatModelAdapter } from '@assistant-ui/react'
|
|
import { act, fireEvent, render, screen, within } from '@testing-library/react'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
import { SortableComposerAttachments } from './SortableComposerAttachments'
|
|
import { reconcileAttachmentOrder } from '@/lib/attachmentOrder'
|
|
|
|
const previewUrl = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jP1sAAAAASUVORK5CYII='
|
|
const model: ChatModelAdapter = { async *run() {} }
|
|
const attachmentAdapter: AttachmentAdapter = {
|
|
accept: 'image/*,text/plain',
|
|
async add({ file }) {
|
|
return {
|
|
id: file.name, name: file.name, type: file.type.startsWith('image/') ? 'image' : 'file', contentType: file.type, file,
|
|
status: { type: 'requires-action', reason: 'composer-send' },
|
|
...(file.type.startsWith('image/') ? { previewUrl } : {}),
|
|
}
|
|
},
|
|
async remove() {},
|
|
async send(attachment) {
|
|
return { ...attachment, status: { type: 'complete' }, content: [{ type: 'image', image: previewUrl }] }
|
|
},
|
|
}
|
|
|
|
type HarnessProps = {
|
|
disabled?: boolean
|
|
order?: string[]
|
|
onReorder?: (activeId: string, targetId: string, position: 'before' | 'after') => void
|
|
runtimeRef: { current?: AssistantRuntime }
|
|
}
|
|
const noop = () => {}
|
|
|
|
function Attachments(props: HarnessProps) {
|
|
const attachments = useAuiState((s) => s.composer.attachments)
|
|
// HappyComposer also reconciles order on every render, returning a fresh array.
|
|
const order = reconcileAttachmentOrder(props.order ?? [], attachments.map((a) => a.id))
|
|
return <SortableComposerAttachments attachments={attachments} orderedAttachmentIds={order} disabled={props.disabled} onReorder={props.onReorder ?? noop} />
|
|
}
|
|
|
|
function Harness(props: HarnessProps) {
|
|
const runtime = useLocalRuntime(model, { adapters: { attachments: attachmentAdapter } })
|
|
props.runtimeRef.current = runtime
|
|
return <AssistantRuntimeProvider runtime={runtime}><Attachments {...props} /></AssistantRuntimeProvider>
|
|
}
|
|
|
|
async function setup() {
|
|
const runtimeRef: HarnessProps['runtimeRef'] = {}
|
|
const view = render(<Harness runtimeRef={runtimeRef} />)
|
|
await act(async () => {
|
|
for (const name of ['first.png', 'second.png']) {
|
|
await runtimeRef.current!.thread.composer.addAttachment(new File(['image'], name, { type: 'image/png' }))
|
|
}
|
|
})
|
|
fireEvent.click(screen.getAllByTitle('Click to zoom')[0]!)
|
|
return { ...view, runtimeRef }
|
|
}
|
|
|
|
describe('sortable composer image preview with the real attachment runtime', () => {
|
|
it.each([
|
|
['only.png', 'image/png'],
|
|
['only.txt', 'text/plain'],
|
|
])('shows drag handles only while %s has another attachment to reorder', async (name, type) => {
|
|
const runtimeRef: HarnessProps['runtimeRef'] = {}
|
|
const { container } = render(<Harness runtimeRef={runtimeRef} />)
|
|
const composer = runtimeRef.current!.thread.composer
|
|
await act(async () => {
|
|
await composer.addAttachment(new File(['attachment'], name, { type }))
|
|
})
|
|
expect(screen.queryByTestId('attachment-drag-handle')).not.toBeInTheDocument()
|
|
expect(screen.getByRole('button', { name: 'Remove attachment' })).toBeInTheDocument()
|
|
|
|
await act(async () => {
|
|
await composer.addAttachment(new File(['second'], 'second.txt', { type: 'text/plain' }))
|
|
})
|
|
expect(screen.getAllByTestId('attachment-drag-handle')).toHaveLength(2)
|
|
expect(screen.getAllByRole('button', { name: 'Remove attachment' })).toHaveLength(2)
|
|
|
|
const second = container.querySelector('[data-hapi-composer-attachment-id="second.txt"]')!
|
|
await act(async () => fireEvent.click(within(second as HTMLElement).getByRole('button', { name: 'Remove attachment' })))
|
|
expect(screen.queryByTestId('attachment-drag-handle')).not.toBeInTheDocument()
|
|
expect(composer.getState().attachments.map((attachment) => attachment.name)).toEqual([name])
|
|
await act(async () => fireEvent.click(screen.getByRole('button', { name: 'Remove attachment' })))
|
|
expect(composer.getState().attachments).toHaveLength(0)
|
|
})
|
|
|
|
it('keeps an open single-image viewer and its zoom as the attachment count changes', async () => {
|
|
const runtimeRef: HarnessProps['runtimeRef'] = {}
|
|
const { container } = render(<Harness runtimeRef={runtimeRef} />)
|
|
const composer = runtimeRef.current!.thread.composer
|
|
await act(async () => composer.addAttachment(new File(['image'], 'first.png', { type: 'image/png' })))
|
|
fireEvent.click(screen.getByTitle('Click to zoom'))
|
|
fireEvent.click(within(screen.getByRole('dialog')).getByTitle('Zoom in'))
|
|
const dialog = screen.getByRole('dialog', { name: 'first.png' })
|
|
|
|
await act(async () => composer.addAttachment(new File(['second'], 'second.txt', { type: 'text/plain' })))
|
|
expect(screen.getByRole('dialog', { name: 'first.png' })).toBe(dialog)
|
|
expect(within(dialog).getByTitle('Reset zoom')).toHaveTextContent('125%')
|
|
const second = container.querySelector('[data-hapi-composer-attachment-id="second.txt"]')!
|
|
await act(async () => fireEvent.click(within(second as HTMLElement).getByRole('button', { name: 'Remove attachment', hidden: true })))
|
|
expect(screen.getByRole('dialog', { name: 'first.png' })).toBe(dialog)
|
|
expect(within(dialog).getByTitle('Reset zoom')).toHaveTextContent('125%')
|
|
expect(screen.queryByTestId('attachment-drag-handle')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('keeps the active image and zoom across draft and parent updates', async () => {
|
|
const { rerender, runtimeRef } = await setup()
|
|
fireEvent.click(within(screen.getByRole('dialog')).getByRole('button', { name: 'Next image' }))
|
|
fireEvent.click(within(screen.getByRole('dialog')).getByTitle('Zoom in'))
|
|
|
|
act(() => runtimeRef.current!.thread.composer.setText('draft update'))
|
|
rerender(<Harness runtimeRef={runtimeRef} onReorder={vi.fn()} />)
|
|
|
|
const dialog = screen.getByRole('dialog', { name: 'second.png' })
|
|
expect(within(dialog).getByTitle('Reset zoom')).toHaveTextContent('125%')
|
|
expect(within(dialog).getByRole('img')).toHaveAttribute('src', previewUrl)
|
|
})
|
|
|
|
it('keeps the preview while controls are disabled and re-enabled', async () => {
|
|
const { rerender, runtimeRef } = await setup()
|
|
rerender(<Harness runtimeRef={runtimeRef} disabled />)
|
|
expect(screen.getByRole('dialog', { name: 'first.png' })).toBeInTheDocument()
|
|
expect(screen.queryByRole('button', { name: 'Reorder attachment first.png', hidden: true })).not.toBeInTheDocument()
|
|
rerender(<Harness runtimeRef={runtimeRef} />)
|
|
expect(screen.getByRole('dialog', { name: 'first.png' })).toBeInTheDocument()
|
|
expect(screen.getByRole('button', { name: 'Reorder attachment first.png', hidden: true })).toBeInTheDocument()
|
|
})
|
|
|
|
it('preserves the viewer across reordering and uses the latest keyboard callback', async () => {
|
|
const { rerender, runtimeRef, container } = await setup()
|
|
const onReorder = vi.fn()
|
|
rerender(<Harness runtimeRef={runtimeRef} order={['second.png', 'first.png']} onReorder={onReorder} />)
|
|
expect(screen.getByRole('dialog', { name: 'first.png' })).toBeInTheDocument()
|
|
fireEvent.keyDown(window, { key: 'Escape' })
|
|
fireEvent.keyDown(screen.getByRole('button', { name: 'Reorder attachment first.png' }), { key: 'ArrowLeft' })
|
|
expect(onReorder).toHaveBeenCalledWith('first.png', 'second.png', 'before')
|
|
expect(Array.from(container.querySelectorAll<HTMLElement>('[data-hapi-composer-attachment-id]')).map((el) => el.dataset.hapiComposerAttachmentId)).toEqual(['second.png', 'first.png'])
|
|
})
|
|
|
|
it('still closes explicitly and removes an attachment through the real runtime', async () => {
|
|
const { runtimeRef, container } = await setup()
|
|
fireEvent.click(within(screen.getByRole('dialog')).getByTitle('Close'))
|
|
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
|
|
fireEvent.click(screen.getAllByTitle('Click to zoom')[0]!)
|
|
await act(async () => runtimeRef.current!.thread.composer.clearAttachments())
|
|
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
|
|
expect(container.querySelector('[data-hapi-composer-attachment-id]')).toBeNull()
|
|
})
|
|
})
|