Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
6.6 KB
Raw Blame
// @vitest-environment jsdom
import { createRef } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { readFileSync } from 'node:fs'
import path from 'node:path'
import { ChatDrawer } from '.'

const stylesheet = readFileSync(path.join(process.cwd(), 'ui', 'src', 'styles.css'), 'utf8')

beforeEach(() => {
  const style = document.createElement('style')
  style.dataset.testStyles = 'chat-drawer'
  style.textContent = `${readFileSync(
    path.join(process.cwd(), 'ui', 'src', 'styles', 'tokens.css'),
    'utf8'
  )}
${readFileSync(path.join(process.cwd(), 'ui', 'src', 'styles.css'), 'utf8')}`
  document.head.append(style)
})

afterEach(() => {
  cleanup()
  document.querySelector('[data-test-styles="chat-drawer"]')?.remove()
})

function renderDrawer(props: Partial<React.ComponentProps<typeof ChatDrawer>> = {}) {
  const onNewChat = props.onNewChat ?? vi.fn()
  const onToggle = props.onToggle ?? vi.fn()
  const onExpandFullScreen = props.onExpandFullScreen ?? vi.fn()
  const onResizeHandleMouseDown = props.onResizeHandleMouseDown ?? vi.fn()
  const onResizeHandleKeyDown = props.onResizeHandleKeyDown ?? vi.fn()
  const containerRef = props.containerRef ?? createRef<HTMLElement>()
  const utils = render(
    <ChatDrawer
      header={props.header ?? <span className="chat-drawer__title">Drawer chat</span>}
      onNewChat={onNewChat}
      onExpandFullScreen={onExpandFullScreen}
      onToggle={onToggle}
      containerRef={containerRef}
      ready={props.ready ?? true}
      onResizeHandleMouseDown={onResizeHandleMouseDown}
      onResizeHandleKeyDown={onResizeHandleKeyDown}
      width={props.width ?? 525}
      resizing={props.resizing ?? false}
    >
      <div data-testid="drawer-chat-page">chat page</div>
    </ChatDrawer>
  )
  return {
    ...utils,
    onNewChat,
    onExpandFullScreen,
    onToggle,
    onResizeHandleMouseDown,
    onResizeHandleKeyDown,
  }
}

describe('ChatDrawer', () => {
  it('renders the header slot, the embedded chat page, and its actions', () => {
    renderDrawer()
    expect(screen.getByText('Drawer chat')).toBeTruthy()
    expect(screen.getByTestId('drawer-chat-page')).toBeTruthy()
    expect(screen.getByRole('button', { name: 'New chat' })).toBeTruthy()
    expect(screen.getByRole('button', { name: 'Open chat in full screen' })).toBeTruthy()
    expect(screen.getByRole('button', { name: 'Collapse chat drawer' })).toBeTruthy()
  })

  it('wires the new-chat, full-screen, and collapse actions', () => {
    const { onNewChat, onExpandFullScreen, onToggle } = renderDrawer()
    fireEvent.click(screen.getByRole('button', { name: 'New chat' }))
    fireEvent.click(screen.getByRole('button', { name: 'Open chat in full screen' }))
    fireEvent.click(screen.getByRole('button', { name: 'Collapse chat drawer' }))
    expect(onNewChat).toHaveBeenCalledTimes(1)
    expect(onExpandFullScreen).toHaveBeenCalledTimes(1)
    expect(onToggle).toHaveBeenCalledTimes(1)
  })

  it('sizes the header CTA icons to the app standard (16px), not the raw viewBox', () => {
    const { container } = renderDrawer()
    const svgs = container.querySelectorAll('.chat-drawer__header-actions .ui-icon-button svg')
    expect(svgs.length).toBe(3)
    svgs.forEach(svg => {
      expect(getComputedStyle(svg as Element).width).toBe('16px')
      expect(getComputedStyle(svg as Element).height).toBe('16px')
    })
  })

  it('stays hidden and inert until the embed acknowledges its shrunk bounds', () => {
    const { container } = renderDrawer({ ready: false })
    const drawer = container.querySelector('.chat-drawer') as HTMLElement
    expect(drawer.getAttribute('data-ready')).toBe('false')
    expect(drawer.classList.contains('is-ready')).toBe(false)
    expect(getComputedStyle(drawer).opacity).toBe('0')
    expect(getComputedStyle(drawer).pointerEvents).toBe('none')
    // pointer-events:none only stops the mouse; `inert` is what also removes the
    // focusable handle/CTAs from the tab order while the drawer is not shown.
    expect(drawer.hasAttribute('inert')).toBe(true)
  })

  it('confines the composer reference submenu to the drawer interior, off the embed rect', () => {
    // Right-docked drawer: the embed is to its LEFT and paints above all DOM, so
    // a submenu crossing the drawer's left edge would be occluded. The submenu is
    // now portaled to document.body and positioned with a fixed rect clamped to
    // the drawer's rect in JS (useFlyoutPosition) — a submenu clamped inside the
    // drawer is by construction off the embed. Pin `position: fixed` and prove the
    // old in-flow rightward override was removed so it can't reintroduce the
    // clipped/occluded in-flow layout.
    const rule = stylesheet.match(/(?:^|[};/])\s*\.composer-reference-submenu\s*\{([^}]*)\}/)?.[1]
    expect(rule).toBeTruthy()
    expect(rule).toMatch(/position:\s*fixed/)
    expect(stylesheet).not.toContain('.chat-drawer .composer-reference-submenu')
  })

  it('exposes a left-edge resize handle that fires the drag callback on mousedown', () => {
    const { container, onResizeHandleMouseDown } = renderDrawer()
    const handle = container.querySelector('.chat-drawer__resize-handle') as HTMLElement
    expect(handle).toBeTruthy()
    expect(handle.getAttribute('aria-orientation')).toBe('vertical')
    expect(getComputedStyle(handle).cursor).toBe('col-resize')
    fireEvent.mouseDown(handle)
    expect(onResizeHandleMouseDown).toHaveBeenCalledTimes(1)
  })

  it('makes the resize handle keyboard-operable per the ARIA separator pattern', () => {
    const { container, onResizeHandleKeyDown } = renderDrawer({ width: 512 })
    const handle = container.querySelector('.chat-drawer__resize-handle') as HTMLElement
    // Focusable, with the value semantics the separator role requires.
    expect(handle.getAttribute('tabindex')).toBe('0')
    expect(handle.getAttribute('aria-valuemin')).toBe('340')
    expect(handle.getAttribute('aria-valuemax')).toBe('1000')
    expect(handle.getAttribute('aria-valuenow')).toBe('512')
    fireEvent.keyDown(handle, { key: 'ArrowLeft' })
    expect(onResizeHandleKeyDown).toHaveBeenCalledTimes(1)
  })

  it('reveals once ready', () => {
    const { container } = renderDrawer({ ready: true })
    const drawer = container.querySelector('.chat-drawer') as HTMLElement
    expect(drawer.getAttribute('data-ready')).toBe('true')
    expect(drawer.classList.contains('is-ready')).toBe(true)
    expect(getComputedStyle(drawer).opacity).toBe('1')
    expect(getComputedStyle(drawer).pointerEvents).toBe('auto')
    expect(drawer.hasAttribute('inert')).toBe(false)
  })
})