Skip to content

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

TypeScript
8.8 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { cleanup, render, screen } from '@testing-library/react'
import * as hook from '../../lib/hooks/usePublishScope'
import { Sidebar } from '../Sidebar'
import { activeSidebarChildHref } from '../Sidebar/activeChild'
import { SIDEBAR_TABS } from '../Sidebar/constants'

const navigationState = vi.hoisted(() => ({ pathname: '/agents' }))
const refreshPublishScope = vi.fn()

function publishScopeState(state: Omit<hook.PublishScopeState, 'refresh'>): hook.PublishScopeState {
  return { ...state, refresh: refreshPublishScope }
}

vi.mock('next/navigation', () => ({
  usePathname: () => navigationState.pathname,
}))

vi.mock('../../lib/hooks/usePublishScope', async orig => {
  const actual = await orig<typeof import('../../lib/hooks/usePublishScope')>()
  return { ...actual, usePublishScope: vi.fn() }
})

afterEach(() => {
  cleanup()
  navigationState.pathname = '/agents'
})
beforeEach(() => vi.clearAllMocks())

describe('Sidebar publisher gating', () => {
  it('does not render a Publisher entry (folded into the Marketplace org tab)', () => {
    vi.mocked(hook.usePublishScope).mockReturnValue(
      publishScopeState({
        scope: { scope: 'acme', curator: false, orgName: 'Acme' },
        loading: false,
        error: false,
      })
    )
    render(<Sidebar currentTab="hosts" />)
    // Publisher was folded into the org-named Marketplace tab (design spec §4).
    expect(screen.queryByRole('link', { name: /publisher/i })).toBeNull()
    expect(screen.getByRole('link', { name: /marketplace/i })).toBeInTheDocument()
  })

  it('still renders the other navigation entries', () => {
    vi.mocked(hook.usePublishScope).mockReturnValue(
      publishScopeState({ scope: null, loading: false, error: true })
    )
    render(<Sidebar currentTab="hosts" />)
    expect(screen.getByRole('link', { name: /agents/i })).toBeInTheDocument()
  })

  it('keeps Traces hidden and renders visible navigation in the defined order', () => {
    vi.mocked(hook.usePublishScope).mockReturnValue(
      publishScopeState({ scope: null, loading: false, error: false })
    )
    render(<Sidebar currentTab="traces" />)

    expect(screen.queryByText('Traces')).not.toBeInTheDocument()
    const nav = screen.getByRole('navigation', { name: 'Main sections' })
    const labels = Array.from(nav.children).map(item =>
      item.querySelector('.cu-sidebar__label')?.textContent?.trim()
    )
    expect(labels).toEqual([
      'Users & Teams',
      'Agents',
      'Marketplace',
      'Installed Connectors',
      'Installed Plugins',
      'Installed Guardrails',
      'Files',
      'External Channels',
      'LLM Models',
      'Secrets',
      'Cost & Usage',
    ])
  })

  it('keeps Settings in the footer on its canonical route', () => {
    vi.mocked(hook.usePublishScope).mockReturnValue(
      publishScopeState({ scope: null, loading: false, error: false })
    )
    render(<Sidebar currentTab="settings" />)

    expect(screen.getByRole('link', { name: 'Settings' })).toHaveAttribute('href', '/settings/ui')
  })

  it.each(['/llm-models', '/llm-models/model-id/edit', '/llm-models/discovery'])(
    'keeps the merged LLM Models entry selected for %s',
    pathname => {
      vi.mocked(hook.usePublishScope).mockReturnValue(
        publishScopeState({ scope: null, loading: false, error: false })
      )
      navigationState.pathname = pathname
      render(<Sidebar currentTab="llm-models" />)

      const entry = screen.getByRole('link', { name: 'LLM Models' })
      expect(entry).toHaveAttribute('href', '/llm-models')
      expect(entry).toHaveAttribute('data-active', 'true')
      expect(entry).toHaveAttribute('aria-current', 'page')
    }
  )

  it.each([
    ['/agent-outputs/recipe-artifacts', 'Agent Outputs', '/agent-outputs/recipe-artifacts'],
    ['/agent-outputs/desktop-app-artifacts', 'Agent Outputs', '/agent-outputs/recipe-artifacts'],
    ['/global-file-system', 'EvenDrive', '/global-file-system'],
  ])('selects the matching Files child for %s', (pathname, label, href) => {
    vi.mocked(hook.usePublishScope).mockReturnValue(
      publishScopeState({ scope: null, loading: false, error: false })
    )
    navigationState.pathname = pathname
    render(<Sidebar currentTab="directories" />)

    const group = screen.getByRole('button', { name: 'Files' })
    expect(group).toHaveAttribute('aria-expanded', 'true')
    expect(group).toHaveAttribute('data-active', 'true')
    const child = screen.getByRole('link', { name: label })
    expect(child).toHaveAttribute('href', href)
    expect(child).toHaveAttribute('aria-current', 'page')
  })

  it('renders a thin icon for every visible child route', () => {
    vi.mocked(hook.usePublishScope).mockReturnValue(
      publishScopeState({ scope: null, loading: false, error: false })
    )
    render(<Sidebar currentTab="directories" />)

    for (const label of ['Agent Outputs', 'EvenDrive']) {
      const child = screen.getByRole('link', { name: label })
      expect(child.querySelector('.cu-sidebar__subitem-icon svg')).toBeInTheDocument()
    }
  })

  it('uses a borderless solid output arrow for Agent Outputs', () => {
    vi.mocked(hook.usePublishScope).mockReturnValue(
      publishScopeState({ scope: null, loading: false, error: false })
    )
    render(<Sidebar currentTab="directories" />)

    const agentOutputs = screen.getByRole('link', { name: 'Agent Outputs' })
    const icon = agentOutputs.querySelector('.cu-sidebar__subitem-icon svg')
    expect(icon).toHaveAttribute('data-solid', 'true')
    expect(icon).toHaveAttribute('stroke-width', '0')
    expect(icon?.querySelectorAll('path')).toHaveLength(1)
    expect(icon?.querySelector('path')).toHaveAttribute(
      'd',
      'm17 17 5-5-5-5-1.41 1.41L18.17 11H9v2h9.17l-2.58 2.59z'
    )
  })

  it('renders an icon for the Files group', () => {
    vi.mocked(hook.usePublishScope).mockReturnValue(
      publishScopeState({ scope: null, loading: false, error: false })
    )
    render(<Sidebar currentTab="directories" />)

    const directories = screen.getByRole('button', { name: 'Files' })
    const icon = directories.querySelector('.cu-sidebar__icon svg')
    expect(icon).toBeInTheDocument()
    expect(icon).toHaveAttribute('width', '18')
    expect(icon).toHaveAttribute('height', '18')
    expect(icon).toHaveAttribute('viewBox', '0 0 512 512')
    expect(icon?.querySelector('path')).toHaveAttribute(
      'd',
      'M464 128H272l-64-64H48C21.49 64 0 85.49 0 112v288c0 26.51 21.49 48 48 48h416c26.51 0 48-21.49 48-48V176c0-26.51-21.49-48-48-48z'
    )
  })

  it('hides Agent Files from the sidebar without changing its route', () => {
    vi.mocked(hook.usePublishScope).mockReturnValue(
      publishScopeState({ scope: null, loading: false, error: false })
    )
    navigationState.pathname = '/agent-files/example'
    render(<Sidebar currentTab="directories" />)

    expect(screen.queryByRole('link', { name: 'Agent Files' })).not.toBeInTheDocument()
    expect(SIDEBAR_TABS.directories.href).toBe('/agent-files')
  })

  it('uses the shared Desktop hard-drive glyph for EvenDrive', () => {
    vi.mocked(hook.usePublishScope).mockReturnValue(
      publishScopeState({ scope: null, loading: false, error: false })
    )
    navigationState.pathname = '/global-file-system'
    render(<Sidebar currentTab="directories" />)

    const globalFileSystem = screen.getByRole('link', { name: 'EvenDrive' })
    expect(globalFileSystem.querySelector('path')).toHaveAttribute(
      'd',
      'M5.45 5.11 2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z'
    )
    expect(globalFileSystem.querySelectorAll('line')).toHaveLength(3)
  })

  it('defines an icon for every sidebar child route, including hidden groups', () => {
    for (const item of Object.values(SIDEBAR_TABS)) {
      expect(item.children?.every(child => Boolean(child.icon)) ?? true).toBe(true)
    }
  })

  it('sorts standard sidebar groups by the displayed child label', () => {
    for (const [tab, item] of Object.entries(SIDEBAR_TABS)) {
      if (tab === 'cost' || tab === 'directories') continue
      const labels = item.children?.map(child => child.label) ?? []
      expect(labels).toEqual([...labels].sort((first, second) => first.localeCompare(second)))
    }

    expect(SIDEBAR_TABS.cost.children?.map(child => child.label)).toEqual([
      'Usage',
      'Token Budgets',
      'LLM Prices',
    ])
    expect(SIDEBAR_TABS.directories.children?.map(child => child.label)).toEqual([
      'EvenDrive',
      'Agent Outputs',
    ])
  })

  it('selects hidden Trace children by their nested routes', () => {
    expect(
      activeSidebarChildHref('/traces/infrastructure/event-id', SIDEBAR_TABS.traces.children ?? [])
    ).toBe('/traces/infrastructure')
    expect(
      activeSidebarChildHref(
        '/traces/sessions/host-id/session-id',
        SIDEBAR_TABS.traces.children ?? []
      )
    ).toBe('/traces')
  })
})