Skip to content

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

TypeScript
11.7 KB
Raw Blame
import type { ReactNode } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { cleanup, fireEvent, render as rtlRender, screen, waitFor } from '@testing-library/react'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import * as api from '../../lib/api'
import type { LlmAllowedModel } from '../../lib/api'
import { LlmDiscoveryPanel } from '../LlmDiscoveryPanel'
import { ToastProvider } from '../Toast'

vi.mock('../../lib/api', async importOriginal => {
  const actual = await importOriginal<typeof import('../../lib/api')>()
  return {
    ...actual,
    deleteLlmModel: vi.fn(),
    getDiscoveryStatus: vi.fn(),
    syncDiscovery: vi.fn(),
    updateLlmModel: vi.fn(),
  }
})

const reviewModel: LlmAllowedModel = {
  id: 'review-openai',
  provider: 'openai',
  model: 'gpt-5',
  vendor: 'OpenAI',
  display_name: 'GPT-5',
  context_window_tokens: 400000,
  enabled: false,
  source: 'discovery',
  stale: false,
  discovered_at: '2026-01-01T00:00:00.000Z',
  last_seen_at: '2026-01-02T00:00:00.000Z',
  created_at: '2026-01-01T00:00:00.000Z',
  updated_at: '2026-01-02T00:00:00.000Z',
}

const staleModel: LlmAllowedModel = {
  ...reviewModel,
  id: 'stale-anthropic',
  provider: 'claude',
  model: 'claude-retired',
  vendor: 'Anthropic',
  enabled: true,
  stale: true,
}

function render(children: ReactNode) {
  return rtlRender(<ToastProvider>{children}</ToastProvider>)
}

function cssRuleBody(selector: string) {
  const css = readFileSync(resolve(__dirname, '../../app/globals.css'), 'utf8')
  const escaped = selector.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  const match = css.match(new RegExp(`(?:^|\\n)${escaped}\\s*\\{([^}]*)\\}`, 'm'))
  return match?.[1] ?? ''
}

beforeEach(() => {
  vi.mocked(api.getDiscoveryStatus).mockResolvedValue({
    ranAt: '2026-07-29T12:00:00.000Z',
    source: 'live',
    added: 3,
    updated: 14,
    staled: 1,
  })
  vi.mocked(api.updateLlmModel).mockResolvedValue(reviewModel)
  vi.mocked(api.syncDiscovery).mockResolvedValue({
    fetchedAt: '2026-07-29T12:59:59.000Z',
    ranAt: '2026-07-29T13:00:00.000Z',
    source: 'live',
    added: 2,
    updated: 12,
    staled: 0,
  })
})

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

describe('LlmDiscoveryPanel merged lifecycle workflow', () => {
  it('shows review records with provider identity and excludes stale rows', async () => {
    render(
      <LlmDiscoveryPanel
        items={[reviewModel, staleModel]}
        loading={false}
        onRefresh={vi.fn().mockResolvedValue(undefined)}
      />
    )

    expect(screen.getByText('LLM Models')).toBeInTheDocument()
    expect(screen.getByText(/Newly synced models land here disabled/)).toBeInTheDocument()

    expect(screen.getByRole('columnheader', { name: /Provider/i })).toBeInTheDocument()
    expect(screen.getByRole('button', { name: 'Expand OpenAI review models' })).toHaveAttribute(
      'aria-expanded',
      'false'
    )
    const summaryRow = screen
      .getByRole('button', { name: 'Expand OpenAI review models' })
      .closest('tr')
    expect(Array.from(summaryRow?.cells ?? []).map(cell => cell.colSpan)).toEqual([1, 1, 1, 1])
    fireEvent.click(screen.getByRole('button', { name: 'Expand OpenAI review models' }))
    const childTable = screen.getByText('gpt-5').closest('table')
    expect(childTable).toHaveClass('cu-llm-review-table__children')
    expect(childTable?.querySelectorAll(':scope > thead > tr > th')).toHaveLength(5)
    expect(screen.getByText('gpt-5')).toBeInTheDocument()
    expect(screen.queryByText('claude-retired')).toBeNull()

    await waitFor(() => expect(screen.getByText('Live catalog')).toBeInTheDocument())
    expect(screen.getByText('+3 new')).toBeInTheDocument()
    expect(screen.getByText('14 refreshed')).toBeInTheDocument()
    expect(screen.getByText('1 stale')).toBeInTheDocument()
  })

  it('enables a reviewed row and refreshes the shared inventory', async () => {
    const onRefresh = vi.fn().mockResolvedValue(undefined)
    render(<LlmDiscoveryPanel items={[reviewModel]} loading={false} onRefresh={onRefresh} />)

    fireEvent.click(screen.getByRole('button', { name: 'Expand OpenAI review models' }))
    fireEvent.click(screen.getByRole('button', { name: 'Enable' }))

    expect(api.updateLlmModel).not.toHaveBeenCalled()
    expect(screen.getByRole('alertdialog')).toHaveTextContent('Enable OpenAI/gpt-5?')
    fireEvent.click(screen.getByRole('button', { name: 'Enable OpenAI/gpt-5' }))

    await waitFor(() =>
      expect(api.updateLlmModel).toHaveBeenCalledWith('review-openai', { enabled: true })
    )
    expect(onRefresh).toHaveBeenCalledTimes(1)
  })

  it('cancels a single-model confirmation without mutation', () => {
    render(
      <LlmDiscoveryPanel
        items={[reviewModel]}
        loading={false}
        onRefresh={vi.fn().mockResolvedValue(undefined)}
      />
    )
    fireEvent.click(screen.getByRole('button', { name: 'Expand OpenAI review models' }))
    fireEvent.click(screen.getByRole('button', { name: 'Enable' }))
    fireEvent.click(screen.getByRole('button', { name: 'Cancel' }))
    expect(api.updateLlmModel).not.toHaveBeenCalled()
  })

  it('converges selected actions into the header in the required order', () => {
    render(
      <LlmDiscoveryPanel
        items={[reviewModel]}
        loading={false}
        onRefresh={vi.fn().mockResolvedValue(undefined)}
      />
    )
    fireEvent.click(screen.getByRole('button', { name: 'Expand OpenAI review models' }))
    fireEvent.click(screen.getByRole('checkbox', { name: 'Select OpenAI/gpt-5' }))
    const actions = document.querySelector('.cu-table-panel__actions')
    expect(
      Array.from(actions?.querySelectorAll('button') ?? []).map(button => button.textContent)
    ).toEqual(['Unselect all', 'Enable 1 selected', '', 'Sync catalog'])
    expect(screen.queryByText('1 selected')).not.toBeInTheDocument()
  })

  it('confirms bulk enable by count and unselects without mutation', () => {
    render(
      <LlmDiscoveryPanel
        items={[reviewModel]}
        loading={false}
        onRefresh={vi.fn().mockResolvedValue(undefined)}
      />
    )
    fireEvent.click(screen.getByRole('button', { name: 'Expand OpenAI review models' }))
    fireEvent.click(screen.getByRole('checkbox', { name: 'Select OpenAI/gpt-5' }))
    fireEvent.click(screen.getByRole('button', { name: 'Enable 1 selected' }))
    expect(screen.getByRole('alertdialog')).toHaveTextContent('Enable 1 selected models?')
    expect(screen.getByRole('alertdialog')).not.toHaveTextContent('gpt-5')
    fireEvent.click(screen.getByRole('button', { name: 'Cancel' }))
    fireEvent.click(screen.getByRole('button', { name: 'Unselect all' }))
    expect(api.updateLlmModel).not.toHaveBeenCalled()
    expect(screen.queryByRole('button', { name: 'Enable 1 selected' })).not.toBeInTheDocument()
  })

  it('keeps the per-row enable action cell wide enough for its button', () => {
    render(
      <LlmDiscoveryPanel
        items={[reviewModel]}
        loading={false}
        onRefresh={vi.fn().mockResolvedValue(undefined)}
      />
    )

    fireEvent.click(screen.getByRole('button', { name: 'Expand OpenAI review models' }))
    const actionCell = screen.getByRole('button', { name: 'Enable' }).closest('td')
    expect(actionCell).toHaveClass('cu-px-actions')
    expect(actionCell).not.toHaveClass('cu-table__cell-actions')
    expect(cssRuleBody('.cu-px-actions')).not.toMatch(/\b(?:max-width|min-width|width)\s*:/)
    expect(cssRuleBody('.cu-tb-actions')).not.toMatch(/\b(?:max-width|min-width|width)\s*:/)
    expect(cssRuleBody('.cu-table__cell-actions')).toMatch(/\bwidth:\s*3\.5rem\b/)
  })

  it('keeps the nested discovery model cell inset after the selection checkbox', () => {
    const selector = '.cu-llm-review-table__children .cu-llm-model-row > .cu-px-model'

    expect(cssRuleBody(selector)).toMatch(/\bpadding-left:\s*var\(--cu-space-2\)/)
  })

  it('does not render a dedicated stale-model management section', () => {
    render(
      <LlmDiscoveryPanel
        items={[reviewModel, staleModel]}
        loading={false}
        onRefresh={vi.fn().mockResolvedValue(undefined)}
      />
    )

    expect(screen.queryByText(/Stale models/)).toBeNull()
    expect(screen.queryByRole('button', { name: /Anthropic stale models/ })).toBeNull()
  })

  it('keeps the sync status when an older mount-time status request resolves afterwards', async () => {
    let resolveInitialStatus!: (value: Awaited<ReturnType<typeof api.getDiscoveryStatus>>) => void
    vi.mocked(api.getDiscoveryStatus).mockReset()
    vi.mocked(api.getDiscoveryStatus).mockReturnValueOnce(
      new Promise(resolve => {
        resolveInitialStatus = resolve
      })
    )
    const onRefresh = vi.fn().mockResolvedValue(undefined)

    render(<LlmDiscoveryPanel items={[reviewModel]} loading={false} onRefresh={onRefresh} />)
    fireEvent.click(screen.getByRole('button', { name: 'Sync catalog' }))

    await waitFor(() => expect(onRefresh).toHaveBeenCalledTimes(1))
    expect(screen.getByText('+2 new')).toBeInTheDocument()
    await waitFor(() =>
      expect(screen.getByRole('button', { name: 'Reload discovery review' })).toBeEnabled()
    )

    resolveInitialStatus({
      ranAt: '2026-07-01T00:00:00.000Z',
      source: 'vendored',
      added: 99,
      updated: 99,
      staled: 99,
    })

    await waitFor(() => expect(screen.getByText('+2 new')).toBeInTheDocument())
    expect(screen.queryByText('+99 new')).toBeNull()
    expect(screen.getByRole('button', { name: 'Reload discovery review' })).toBeEnabled()
  })
})

describe('LlmDiscoveryPanel sorting', () => {
  function rowOrder(): string[] {
    const rows = document.querySelectorAll('tr.cu-llm-model-row')
    return Array.from(rows).map(row => {
      const cell = row.querySelector('td.cu-px-model')
      return cell?.textContent?.trim() ?? ''
    })
  }

  function expandOpenAi() {
    fireEvent.click(screen.getByRole('button', { name: 'Expand OpenAI review models' }))
  }

  const discoveryModel = (overrides: Partial<LlmAllowedModel>): LlmAllowedModel => ({
    ...reviewModel,
    id: overrides.id ?? reviewModel.id,
    enabled: false,
    source: 'discovery',
    stale: false,
    ...overrides,
  })

  it('keeps the static LLM Models title for the discovery panel', () => {
    render(
      <LlmDiscoveryPanel
        items={[reviewModel]}
        loading={false}
        onRefresh={vi.fn().mockResolvedValue(undefined)}
      />
    )
    expect(screen.getByRole('heading', { name: /LLM Models/ })).toBeInTheDocument()
  })

  it('sorts queued models by model name ascending when clicked', () => {
    const alpha = discoveryModel({ id: 'a', model: 'alpha' })
    const bravo = discoveryModel({ id: 'b', model: 'bravo' })
    const charlie = discoveryModel({ id: 'c', model: 'charlie' })
    render(
      <LlmDiscoveryPanel
        items={[charlie, alpha, bravo]}
        loading={false}
        onRefresh={vi.fn().mockResolvedValue(undefined)}
      />
    )
    expandOpenAi()

    fireEvent.click(screen.getByRole('button', { name: 'Sort by Model ascending' }))

    expect(rowOrder()).toEqual(['alpha', 'bravo', 'charlie'])
  })

  it('sorts context window descending by default and pushes null values to the end', () => {
    const small = discoveryModel({ id: 's', model: 'small', context_window_tokens: 8_000 })
    const large = discoveryModel({ id: 'l', model: 'large', context_window_tokens: 1_000_000 })
    const unknown = discoveryModel({ id: 'u', model: 'unknown', context_window_tokens: null })
    render(
      <LlmDiscoveryPanel
        items={[small, large, unknown]}
        loading={false}
        onRefresh={vi.fn().mockResolvedValue(undefined)}
      />
    )
    expandOpenAi()

    fireEvent.click(screen.getByRole('button', { name: 'Sort by Context window descending' }))

    expect(rowOrder()).toEqual(['large', 'small', 'unknown'])
  })
})