Skip to content

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

TypeScript
6.6 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'
import {
  getControlUISettingsMe,
  requestControlUISettingsEmailChange,
  updateControlUISettingsPassword,
  updateControlUISettingsUsername,
} from '@lib/api'
import { ControlSettingsPanel } from '../ControlSettingsPanel'

const checkAuth = vi.fn()
const logout = vi.fn()
const showToast = vi.fn()

vi.mock('next/navigation', () => ({
  useSearchParams: () => new URLSearchParams(),
}))

vi.mock('../AuthContext', () => ({
  useAuth: () => ({ checkAuth, logout }),
}))

vi.mock('../AdminBridgeAlerts', () => ({
  hasControlAdminBridgeAlertOverrides: () => false,
  resetControlAdminBridgeAlerts: vi.fn(),
}))

vi.mock('../ConfirmDialog', () => ({
  useConfirmDialog: () => ({ confirm: vi.fn(), confirmDialog: null }),
}))

vi.mock('../ThemeContext', () => ({
  useTheme: () => ({ setThemeMode: vi.fn(), themeMode: 'dark' }),
}))

vi.mock('../Toast', () => ({
  useToast: () => ({ showToast }),
}))

vi.mock('@lib/api', () => ({
  getControlUISettingsMe: vi.fn(),
  requestControlUISettingsEmailChange: vi.fn(),
  updateControlUISettingsPassword: vi.fn(),
  updateControlUISettingsUsername: vi.fn(),
}))

const mockGetSettings = vi.mocked(getControlUISettingsMe)
const mockRequestEmailChange = vi.mocked(requestControlUISettingsEmailChange)
const mockUpdateUsername = vi.mocked(updateControlUISettingsUsername)
const mockUpdatePassword = vi.mocked(updateControlUISettingsPassword)

function accountRow(label: string): HTMLElement {
  const row = screen.getByText(label).closest('.cu-settings-row')
  if (!(row instanceof HTMLElement)) throw new Error(`Could not find ${label} settings row`)
  return row
}

describe('ControlSettingsPanel scalar dialogs', () => {
  beforeEach(() => {
    vi.clearAllMocks()
    mockGetSettings.mockResolvedValue({
      me: {
        email: '[email protected]',
        username: 'admin',
        pendingEmailChange: null,
      },
    } as Awaited<ReturnType<typeof getControlUISettingsMe>>)
  })

  afterEach(cleanup)

  it('keeps username read-only until Edit and discards dismissed drafts', async () => {
    render(<ControlSettingsPanel />)

    await screen.findByText('admin')
    expect(screen.queryByLabelText('Username')).not.toBeInTheDocument()

    fireEvent.click(within(accountRow('Username')).getByRole('button', { name: 'Edit' }))
    const dialog = screen.getByRole('dialog', { name: 'Edit username' })
    const input = within(dialog).getByLabelText('Username')
    expect(input).toHaveValue('admin')
    fireEvent.change(input, { target: { value: 'discarded-name' } })
    fireEvent.mouseDown(screen.getByTestId('dialog-backdrop'))

    expect(screen.queryByRole('dialog', { name: 'Edit username' })).not.toBeInTheDocument()
    fireEvent.click(within(accountRow('Username')).getByRole('button', { name: 'Edit' }))
    expect(screen.getByLabelText('Username')).toHaveValue('admin')
  })

  it('preserves the username draft on failure and adopts the authoritative success response', async () => {
    mockUpdateUsername
      .mockRejectedValueOnce(new Error('Username is already in use.'))
      .mockResolvedValueOnce({
        me: {
          email: '[email protected]',
          username: 'server-normalized',
          pendingEmailChange: null,
        },
      } as Awaited<ReturnType<typeof updateControlUISettingsUsername>>)
    render(<ControlSettingsPanel />)

    await screen.findByText('admin')
    fireEvent.click(within(accountRow('Username')).getByRole('button', { name: 'Edit' }))
    fireEvent.change(screen.getByLabelText('Username'), { target: { value: 'new-admin' } })
    fireEvent.click(screen.getByRole('button', { name: 'Save' }))

    expect(await screen.findByRole('alert')).toHaveTextContent('Username is already in use.')
    expect(screen.getByLabelText('Username')).toHaveValue('new-admin')

    fireEvent.click(screen.getByRole('button', { name: 'Save' }))
    await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
    expect(mockUpdateUsername).toHaveBeenNthCalledWith(1, 'new-admin')
    expect(mockUpdateUsername).toHaveBeenNthCalledWith(2, 'new-admin')
    expect(screen.getByText('server-normalized')).toBeInTheDocument()
    expect(screen.getByText('[email protected]')).toBeInTheDocument()
    expect(checkAuth).toHaveBeenCalledTimes(1)
  })

  it('stages email selection and preserves the verification workflow on save', async () => {
    mockRequestEmailChange.mockResolvedValue({
      confirmation: {
        email: '[email protected]',
        createdAt: '2026-09-22T10:00:00.000Z',
        expiresAt: '2026-09-22T11:00:00.000Z',
      },
    } as Awaited<ReturnType<typeof requestControlUISettingsEmailChange>>)
    render(<ControlSettingsPanel />)

    await screen.findByText('[email protected]')
    fireEvent.click(within(accountRow('Email')).getByRole('button', { name: 'Edit' }))
    expect(screen.getByLabelText('Email')).toHaveValue('[email protected]')
    fireEvent.change(screen.getByLabelText('Email'), { target: { value: '[email protected]' } })
    expect(mockRequestEmailChange).not.toHaveBeenCalled()
    fireEvent.click(screen.getByRole('button', { name: 'Send confirmation' }))

    await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
    expect(mockRequestEmailChange).toHaveBeenCalledWith('[email protected]')
    expect(screen.getByText('[email protected]')).toBeInTheDocument()
    expect(screen.getByText('Confirmation pending for [email protected].')).toBeInTheDocument()
    expect(showToast).toHaveBeenCalledWith('Confirmation email sent.', { tone: 'success' })
  })

  it('uses the shared simple dialog and preserves password reauthentication', async () => {
    mockUpdatePassword.mockResolvedValue(undefined)
    render(<ControlSettingsPanel />)

    await screen.findByText('admin')
    fireEvent.click(screen.getByRole('button', { name: 'Change password' }))
    const dialog = screen.getByRole('dialog', { name: 'Change password' })
    fireEvent.change(within(dialog).getByLabelText(/^Current password/), {
      target: { value: 'current-password' },
    })
    fireEvent.change(within(dialog).getByLabelText(/^New password/), {
      target: { value: 'next-password' },
    })
    fireEvent.change(within(dialog).getByLabelText(/^Confirm new password/), {
      target: { value: 'next-password' },
    })
    fireEvent.click(within(dialog).getByRole('button', { name: 'Save password' }))

    await waitFor(() =>
      expect(mockUpdatePassword).toHaveBeenCalledWith({
        currentPassword: 'current-password',
        newPassword: 'next-password',
      })
    )
    expect(logout).toHaveBeenCalledTimes(1)
  })
})