Skip to content

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

TypeScript
4.8 KB
Raw Blame
// @vitest-environment jsdom
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'
import { GfsImagePreview } from './index'

describe('GfsImagePreview layout', () => {
  let sidebar: HTMLElement
  let sidebarRight = 74
  let resizeObserverCallback: (() => void) | null = null

  beforeEach(() => {
    Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1280 })
    Object.defineProperty(window, 'clerum', {
      configurable: true,
      value: {
        gfs: {
          downloadPreview: vi.fn(async () => ({ bytes: new Uint8Array([1, 2, 3]).buffer })),
        },
      },
    })
    Object.defineProperty(URL, 'createObjectURL', {
      configurable: true,
      value: vi.fn(() => 'blob:gfs-image-preview'),
    })
    Object.defineProperty(URL, 'revokeObjectURL', {
      configurable: true,
      value: vi.fn(),
    })

    sidebar = document.createElement('aside')
    sidebar.className = 'left-nav collapsed'
    vi.spyOn(sidebar, 'getBoundingClientRect').mockImplementation(
      () =>
        ({
          right: sidebarRight,
          width: sidebarRight,
          height: 800,
        }) as DOMRect
    )
    document.body.appendChild(sidebar)

    vi.stubGlobal(
      'ResizeObserver',
      class {
        constructor(callback: () => void) {
          resizeObserverCallback = callback
        }

        observe() {}

        disconnect() {}
      }
    )
  })

  afterEach(() => {
    cleanup()
    sidebar.remove()
    resizeObserverCallback = null
    vi.unstubAllGlobals()
    vi.restoreAllMocks()
  })

  it('centers in the workspace and follows sidebar expansion changes', async () => {
    render(
      <GfsImagePreview
        byteLength={3}
        fileName="diagram.png"
        gfsUri="gfs://main/image-1"
        mimeType="image/png"
        onClose={vi.fn()}
      />
    )

    const modal = screen.getByRole('presentation')
    const copyButton = screen.getByRole('button', { name: 'Copy image to clipboard' })
    expect(copyButton.classList.contains('ui-button')).toBe(true)
    expect(copyButton.classList.contains('da-gfs-image-preview-dialog__copy')).toBe(true)
    expect(copyButton.textContent).toContain('Copy')
    const closeButton = screen.getByRole('button', { name: 'Close image preview' })
    expect(closeButton.classList.contains('ui-button')).toBe(true)
    expect(closeButton.classList.contains('da-gfs-image-preview-dialog__close')).toBe(true)
    const headerMain = screen
      .getByRole('dialog', { name: 'diagram.png' })
      .querySelector('.da-gfs-image-preview-dialog__header-main')
    expect(headerMain?.contains(copyButton)).toBe(true)
    expect(headerMain?.firstElementChild).toBe(copyButton)
    expect(headerMain?.contains(closeButton)).toBe(false)
    expect((modal as HTMLElement).style.left).toBe('74px')
    expect((modal as HTMLElement).style.right).toBe('0px')

    sidebarRight = 328
    sidebar.classList.remove('collapsed')
    resizeObserverCallback?.()

    await waitFor(() => {
      expect((modal as HTMLElement).style.left).toBe('328px')
      expect((modal as HTMLElement).style.right).toBe('0px')
    })
  })

  it('keeps the unavailable image dialog named and closable', async () => {
    const downloadPreview = vi.fn(async () => {
      throw new Error('404 File unavailable')
    })
    window.clerum.gfs.downloadPreview = downloadPreview
    render(
      <GfsImagePreview
        byteLength={3}
        fileName="diagram.png"
        gfsUri="gfs://main/image-1"
        mimeType="image/png"
        onClose={vi.fn()}
      />
    )

    expect(await screen.findByText(/File unavailable/)).toBeTruthy()
    const dialog = screen.getByRole('dialog', { name: 'diagram.png' })
    const title = screen.getByRole('heading', { name: 'diagram.png' })
    expect(dialog.getAttribute('aria-labelledby')).toBe(title.id)
    expect(screen.getByRole('button', { name: 'Close image preview' })).toBeTruthy()
  })

  it('keeps the authoritative unavailable shell named and closable without fetching bytes', () => {
    const onClose = vi.fn()
    const downloadPreview = vi.mocked(window.clerum.gfs.downloadPreview)
    render(
      <GfsImagePreview
        byteLength={3}
        fileName="diagram.png"
        gfsUri="gfs://main/image-1"
        mimeType="image/png"
        onClose={onClose}
        unavailable
      />
    )

    const dialog = screen.getByRole('dialog', { name: 'File unavailable' })
    const title = within(dialog).getByRole('heading', { name: 'File unavailable', level: 3 })
    expect(dialog.getAttribute('aria-labelledby')).toBe(title.id)
    fireEvent.click(within(dialog).getByRole('button', { name: 'Close image preview' }))
    expect(onClose).toHaveBeenCalledOnce()
    expect(downloadPreview).not.toHaveBeenCalled()
  })
})