Skip to content

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

TypeScript
6.2 KB
Raw Blame
// @vitest-environment jsdom
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { cleanup, render, screen, waitFor } from '@testing-library/react'
import { GFS_IMAGE_PREVIEW_MAX_BYTES } from '@constants/gfsImagePreview'
import { GfsImagePreviewBody } from './Body'

// The producer (`window.clerum.gfs.downloadPreview`) returns `{ bytes: ArrayBuffer }`
// (renderer.d.ts); the body reads only `.bytes`, so the mock mirrors that shape.
function stubDownload(bytes: ArrayBuffer) {
  const downloadPreview = vi.fn(async () => ({ bytes }))
  Object.defineProperty(window, 'clerum', {
    configurable: true,
    value: { gfs: { downloadPreview } },
  })
  return downloadPreview
}

describe('GfsImagePreviewBody', () => {
  beforeEach(() => {
    Object.defineProperty(URL, 'createObjectURL', {
      configurable: true,
      value: vi.fn(() => 'blob:gfs-image-preview'),
    })
    Object.defineProperty(URL, 'revokeObjectURL', { configurable: true, value: vi.fn() })
  })

  afterEach(() => {
    cleanup()
    vi.restoreAllMocks()
  })

  it('downloads by gfsUri and renders the image without a modal', async () => {
    const downloadPreview = stubDownload(new Uint8Array([1, 2, 3]).buffer)

    render(
      <GfsImagePreviewBody
        byteLength={3}
        fileName="diagram.PNG"
        gfsUri="gfs://main/image-1"
        mimeType="image/png"
      />
    )

    const img = await screen.findByAltText('Preview of diagram.PNG')
    expect(img.tagName).toBe('IMG')
    expect(img.getAttribute('src')).toBe('blob:gfs-image-preview')
    // The preview download carries its per-kind ceiling so the main process can
    // bound the fetch (R1-H4 Part B).
    expect(downloadPreview).toHaveBeenCalledWith('gfs://main/image-1', GFS_IMAGE_PREVIEW_MAX_BYTES)
    // No portal/backdrop/dialog chrome — this is the de-modalized body.
    expect(screen.queryByRole('dialog')).toBeNull()
    expect(screen.queryByRole('presentation')).toBeNull()
  })

  it('renders an inline base64 source without a GFS download (chat attachments)', async () => {
    // BUG-176: chat image attachments already carry their bytes — the body must
    // decode them locally instead of reaching for `gfs.downloadPreview`.
    const downloadPreview = vi.fn(async () => ({ bytes: new Uint8Array([9]).buffer }))
    Object.defineProperty(window, 'clerum', {
      configurable: true,
      value: { gfs: { downloadPreview } },
    })

    render(
      <GfsImagePreviewBody
        byteLength={3}
        fileName="Screenshot 2026-09-21 at 13.32.30.png"
        dataBase64="AQID"
        mimeType="image/png"
      />
    )

    const img = await screen.findByAltText('Preview of Screenshot 2026-09-21 at 13.32.30.png')
    expect(img.getAttribute('src')).toBe('blob:gfs-image-preview')
    expect(downloadPreview).not.toHaveBeenCalled()
  })

  it('rejects missing and ambiguous sources before decoding or downloading', async () => {
    const downloadPreview = stubDownload(new Uint8Array([1]).buffer)
    const atobSpy = vi.spyOn(window, 'atob')
    render(<GfsImagePreviewBody byteLength={1} fileName="image.png" mimeType="image/png" />)
    expect(await screen.findByText('Image preview requires exactly one source')).toBeTruthy()
    cleanup()
    render(
      <GfsImagePreviewBody
        byteLength={1}
        fileName="image.png"
        mimeType="image/png"
        gfsUri="gfs://main/image-1"
        dataBase64="AQ=="
      />
    )
    expect(await screen.findByText('Image preview requires exactly one source')).toBeTruthy()
    expect(downloadPreview).not.toHaveBeenCalled()
    expect(atobSpy).not.toHaveBeenCalled()
  })

  it('rejects oversized inline base64 before calling atob even with a small size hint', async () => {
    const atobSpy = vi.spyOn(window, 'atob')
    render(
      <GfsImagePreviewBody
        byteLength={1}
        fileName="image.png"
        mimeType="image/png"
        dataBase64={'A'.repeat(Math.ceil((GFS_IMAGE_PREVIEW_MAX_BYTES + 1) / 3) * 4)}
      />
    )
    expect(await screen.findByText(/Image previews are limited to 10 MB/)).toBeTruthy()
    expect(atobSpy).not.toHaveBeenCalled()
  })

  it('renders the heading at the requested level (a page uses h2)', async () => {
    stubDownload(new Uint8Array([1, 2, 3]).buffer)
    render(
      <GfsImagePreviewBody
        byteLength={3}
        fileName="diagram.PNG"
        gfsUri="gfs://main/image-1"
        mimeType="image/png"
        headingLevel={2}
      />
    )
    expect(await screen.findByRole('heading', { name: 'diagram.PNG', level: 2 })).toBeTruthy()
  })

  it('rejects an oversized image from metadata before downloading', async () => {
    const downloadPreview = stubDownload(new Uint8Array([1]).buffer)
    render(
      <GfsImagePreviewBody
        byteLength={GFS_IMAGE_PREVIEW_MAX_BYTES + 1}
        fileName="oversized.png"
        gfsUri="gfs://main/oversized"
        mimeType="image/png"
      />
    )
    expect(await screen.findByText(/Image previews are limited to 10 MB/)).toBeTruthy()
    expect(downloadPreview).not.toHaveBeenCalled()
  })

  it('fails closed on a download error: shows the reason and notifies onDownloadError', async () => {
    const onDownloadError = vi.fn()
    const downloadPreview = vi.fn(async () => {
      throw new Error('403 Forbidden')
    })
    Object.defineProperty(window, 'clerum', {
      configurable: true,
      value: { gfs: { downloadPreview } },
    })

    render(
      <GfsImagePreviewBody
        byteLength={3}
        fileName="secret.png"
        gfsUri="gfs://main/secret"
        mimeType="image/png"
        onDownloadError={onDownloadError}
      />
    )

    expect(await screen.findByText('403 Forbidden')).toBeTruthy()
    expect(screen.queryByAltText('Preview of secret.png')).toBeNull()
    await waitFor(() => expect(onDownloadError).toHaveBeenCalled())
  })

  it('renders the copy affordance in the header', async () => {
    stubDownload(new Uint8Array([1, 2, 3]).buffer)
    render(
      <GfsImagePreviewBody
        byteLength={3}
        fileName="diagram.PNG"
        gfsUri="gfs://main/image-1"
        mimeType="image/png"
      />
    )
    const copyButton = await screen.findByRole('button', { name: /Copy image to clipboard/i })
    expect(copyButton.textContent).toContain('Copy')
  })
})