Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

3.3 KB
Raw Blame
// @vitest-environment jsdom
/*
 * Modified by BoxLite AI, 2026
 * SPDX-License-Identifier: AGPL-3.0
 */

import { act } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { ErrorBoundaryFallback } from './ErrorBoundaryFallback'
import { BoxliteError } from '@/api/errors'
import { RoutePath } from '@/enums/RoutePath'

let container: HTMLDivElement
let root: Root

beforeAll(() => {
  ;(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
})

beforeEach(() => {
  container = document.createElement('div')
  document.body.appendChild(container)
  root = createRoot(container)
})

afterEach(() => {
  act(() => root.unmount())
  container.remove()
  vi.restoreAllMocks()
})

function render(error: unknown) {
  act(() => root.render(<ErrorBoundaryFallback error={error} resetErrorBoundary={vi.fn()} />))
  // The dialog portals out of `container`, so assert against the whole document.
  return document.body.textContent ?? ''
}

describe('ErrorBoundaryFallback', () => {
  it('shows the crash report for an uncoded error', () => {
    const text = render(new BoxliteError('Runner is unreachable'))

    expect(text).toContain('Something went wrong')
    expect(text).toContain('Runner is unreachable')
  })

  it.each([
    ['disposable_email', 'temporary email addresses'],
    ['email_required', "didn't share an email address"],
    ['invalid_email', 'invalid email address'],
  ])('explains %s as an account creation refusal', (code, reason) => {
    const assign = vi.fn()
    vi.spyOn(window, 'location', 'get').mockReturnValue({ ...window.location, assign } as unknown as Location)

    const text = render(BoxliteError.fromString('Account creation refused', { code }))

    expect(text).toContain('Use a different email address')
    expect(text).toContain(reason)
    expect(text).toContain('Use another account')
    expect(text).not.toContain('Something went wrong')
    expect(text).not.toContain('Stack Trace')
    expect(text).not.toContain('Try Again')
    expect(text).not.toContain('Reload Page')
    expect(assign).not.toHaveBeenCalled()
  })

  it('ends the rejected account session only when the user chooses another email', () => {
    const assign = vi.fn()
    vi.spyOn(window, 'location', 'get').mockReturnValue({ ...window.location, assign } as unknown as Location)
    render(BoxliteError.fromString('Account creation refused', { code: 'disposable_email' }))

    const button = Array.from(document.querySelectorAll('button')).find((item) =>
      item.textContent?.includes('Use another account'),
    )
    if (!button) throw new Error('registration refusal must offer a sign-out action')

    act(() => button.dispatchEvent(new MouseEvent('click', { bubbles: true })))

    expect(assign).toHaveBeenCalledOnce()
    expect(assign).toHaveBeenCalledWith(expect.stringContaining(`${RoutePath.LOGOUT}?switch=1&returnTo=`))
  })

  it('keeps an ordinary forbidden response on the existing error screen', () => {
    const text = render(BoxliteError.fromString('Forbidden', { code: 'permission_denied' }))

    expect(text).toContain('Something went wrong')
    expect(text).toContain('Forbidden')
    expect(text).not.toContain('Use another account')
  })
})