Skip to content

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

TypeScript
4.4 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { vi } from 'vitest'
import { wireMainWindowRendererReadiness } from '../mainWindowReadiness.js'

describe('main window renderer readiness', () => {
  function makeWebContents() {
    const listeners = new Map<string, (...args: unknown[]) => void>()
    return {
      listeners,
      webContents: {
        isDestroyed: vi.fn(() => false),
        on: vi.fn((event: string, listener: (...args: unknown[]) => void) => {
          listeners.set(event, listener)
        }),
        reload: vi.fn(),
      },
    }
  }

  it('marks the renderer not ready only after a navigation commits', () => {
    const { listeners, webContents } = makeWebContents()
    const markNotReady = vi.fn()

    wireMainWindowRendererReadiness({
      webContents: webContents as never,
      isCurrentWindow: () => true,
      markNotReady,
      sandboxUi: { closeSandboxUi: vi.fn(async () => undefined) },
    })

    expect(listeners.has('did-start-navigation')).toBe(false)
    expect(listeners.has('did-navigate')).toBe(true)

    listeners.get('did-navigate')?.()
    expect(markNotReady).toHaveBeenCalledOnce()
  })

  it('reloads a crashed renderer so it can perform a new ready handshake', () => {
    const { listeners, webContents } = makeWebContents()
    const markNotReady = vi.fn()

    wireMainWindowRendererReadiness({
      webContents: webContents as never,
      isCurrentWindow: () => true,
      markNotReady,
      sandboxUi: { closeSandboxUi: vi.fn(async () => undefined) },
    })

    listeners.get('render-process-gone')?.({}, { reason: 'crashed' })

    expect(markNotReady).toHaveBeenCalledOnce()
    expect(webContents.reload).toHaveBeenCalledOnce()
  })

  it('does not reload a clean exit or react to an obsolete window', () => {
    const clean = makeWebContents()
    wireMainWindowRendererReadiness({
      webContents: clean.webContents as never,
      isCurrentWindow: () => true,
      markNotReady: vi.fn(),
      sandboxUi: { closeSandboxUi: vi.fn(async () => undefined) },
    })
    clean.listeners.get('render-process-gone')?.({}, { reason: 'clean-exit' })
    expect(clean.webContents.reload).not.toHaveBeenCalled()

    const obsolete = makeWebContents()
    const markNotReady = vi.fn()
    wireMainWindowRendererReadiness({
      webContents: obsolete.webContents as never,
      isCurrentWindow: () => false,
      markNotReady,
      sandboxUi: { closeSandboxUi: vi.fn(async () => undefined) },
    })
    obsolete.listeners.get('render-process-gone')?.({}, { reason: 'crashed' })
    expect(markNotReady).not.toHaveBeenCalled()
    expect(obsolete.webContents.reload).not.toHaveBeenCalled()
  })

  it('closes the sandbox-ui embed once per replaced renderer of the current window', () => {
    const navigated = makeWebContents()
    const closeOnNavigate = vi.fn(async () => undefined)
    wireMainWindowRendererReadiness({
      webContents: navigated.webContents as never,
      isCurrentWindow: () => true,
      markNotReady: vi.fn(),
      sandboxUi: { closeSandboxUi: closeOnNavigate },
    })
    navigated.listeners.get('did-navigate')?.()
    expect(closeOnNavigate).toHaveBeenCalledOnce()
    expect(closeOnNavigate).toHaveBeenCalledWith({ reason: 'renderer_replaced' })

    // A clean exit is not reloaded, but the renderer that owned the embed is gone.
    const exited = makeWebContents()
    const closeOnExit = vi.fn(async () => undefined)
    wireMainWindowRendererReadiness({
      webContents: exited.webContents as never,
      isCurrentWindow: () => true,
      markNotReady: vi.fn(),
      sandboxUi: { closeSandboxUi: closeOnExit },
    })
    exited.listeners.get('render-process-gone')?.({}, { reason: 'clean-exit' })
    expect(closeOnExit).toHaveBeenCalledOnce()
    expect(closeOnExit).toHaveBeenCalledWith({ reason: 'renderer_replaced' })
  })

  it('never closes the sandbox-ui embed for an obsolete window', () => {
    const obsolete = makeWebContents()
    const closeSandboxUi = vi.fn(async () => undefined)
    wireMainWindowRendererReadiness({
      webContents: obsolete.webContents as never,
      isCurrentWindow: () => false,
      markNotReady: vi.fn(),
      sandboxUi: { closeSandboxUi },
    })

    obsolete.listeners.get('did-navigate')?.()
    obsolete.listeners.get('render-process-gone')?.({}, { reason: 'crashed' })
    obsolete.listeners.get('render-process-gone')?.({}, { reason: 'clean-exit' })

    expect(closeSandboxUi).not.toHaveBeenCalled()
  })
})