Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

13.0 KB
Raw Blame
import { screen, fireEvent } from '@testing-library/react'
import { render } from './test/test-utils'
import { render as rtlRender } from '@testing-library/react'
import { MemoryRouter } from 'react-router-dom' // eslint-disable-line no-unused-vars
import { ThemeProvider } from './contexts/ThemeContext' // eslint-disable-line no-unused-vars
import App from './App' // eslint-disable-line no-unused-vars
import { useFirstRun } from './hooks/useFirstRun'
import { useSystemStatus } from './hooks/useSystemStatus'
import { useVersion } from './hooks/useVersion'
import { getInternalRoutes } from './plugins/registry'

vi.mock('./hooks/useSystemStatus', () => ({
  useSystemStatus: vi.fn(() => ({
    status: { gpu: null, services: [], model: null, bootstrap: null, uptime: 0, version: '1.0.0' },
    loading: false,
    error: null
  }))
}))

vi.mock('./hooks/useVersion', () => ({
  useVersion: vi.fn(() => ({
    version: { current: '1.0.0', update_available: false },
    loading: false,
    error: null,
    dismissUpdate: vi.fn()
  }))
}))

// Server-side first-run gating — the hook drives whether SetupWizard mounts.
// Tests below override the mock per case.
vi.mock('./hooks/useFirstRun', () => ({
  useFirstRun: vi.fn(() => ({ firstRun: false, loading: false, error: null, refresh: vi.fn() })),
}))

vi.mock('./plugins/registry', () => ({
  getInternalRoutes: vi.fn(() => []),
  getSidebarNavItems: vi.fn(() => []),
  getSidebarExternalLinks: vi.fn(() => [])
}))

// FirstBoot is lazy-imported in App.jsx and rendered fullscreen when
// firstRun=true. Mock it as a sync component so tests don't need to
// await Suspense.
vi.mock('./pages/FirstBoot', () => ({
  default: ({ onComplete }) => (
    <div data-testid="first-boot">
      <button onClick={onComplete}>Complete</button>
    </div>
  )
}))

vi.mock('./pages/ODSTalk', () => ({
  default: () => <div data-testid="ods-talk">ODS Talk Portal</div>,
}))
vi.mock('./pages/Pixel', () => ({ default: () => <input aria-label="Portal draft" /> }))
vi.mock('./components/SettingsModal', () => ({ default: () => <input aria-label="Settings draft" /> }))

// InstallPromptBanner depends on browser PWA events we don't simulate
// in these App-level tests; render nothing so it doesn't interfere.
vi.mock('./components/InstallPromptBanner', () => ({
  default: () => null,
}))

describe('App', () => {
  test('opens legacy Pixel settings in the common utility panel', async () => {
    rtlRender(<MemoryRouter initialEntries={['/pixel/settings']}><ThemeProvider><App /></ThemeProvider></MemoryRouter>)
    expect(await screen.findByLabelText('Settings draft')).toBeVisible()
    expect(screen.getByRole('complementary', {name:'ODS navigation'})).toBeVisible()
    expect(screen.queryByRole('complementary', {name:'Pixel navigation'})).toBeNull()
    expect(screen.queryByRole('dialog')).toBeNull()
    expect(screen.getByLabelText('Portal draft')).toBeVisible()
  })
  test('opens settings inside the workspace and preserves both drafts when collapsed', async () => {
    rtlRender(<MemoryRouter initialEntries={['/settings']}><ThemeProvider><App /></ThemeProvider></MemoryRouter>)
    const chat = await screen.findByLabelText('Portal draft')
    const settings = await screen.findByLabelText('Settings draft')
    fireEvent.change(chat, {target:{value:'Chat draft'}})
    fireEvent.change(settings, {target:{value:'Unsaved settings'}})
    expect(screen.queryByRole('dialog')).toBeNull()
    expect(screen.getByRole('complementary', {name:'Workspace panel'})).toContainElement(settings)
    fireEvent.click(screen.getByRole('button', {name:'Collapse workspace panel'}))
    expect(settings).not.toBeVisible()
    expect(chat).toBeVisible()
    fireEvent.click(screen.getByRole('button', {name:'Expand workspace panel'}))
    expect(settings).toHaveValue('Unsaved settings')
    fireEvent.click(screen.getByRole('button', {name:'Close workspace panel'}))
    expect(screen.queryByLabelText('Settings draft')).toBeNull()
    expect(chat).toHaveValue('Chat draft')
  })
  test('keeps the portal draft mounted while a panel collapses and closes', async () => {
    getInternalRoutes.mockReturnValue([{id:'dashboard',path:'/dashboard',label:'Dashboard',component:() => <p>Panel readings</p>}])
    rtlRender(<MemoryRouter initialEntries={['/dashboard']}><ThemeProvider><App /></ThemeProvider></MemoryRouter>)
    const input = await screen.findByLabelText('Portal draft')
    fireEvent.change(input, {target:{value:'Keep this message'}})
    expect(await screen.findByText('Panel readings')).toBeVisible()
    fireEvent.click(screen.getByRole('button',{name:'Collapse workspace panel'}))
    expect(screen.getByText('Panel readings')).not.toBeVisible()
    expect(screen.getByLabelText('Portal draft')).toBe(input)
    fireEvent.click(screen.getByRole('button',{name:'Expand workspace panel'}))
    expect(screen.getByText('Panel readings')).toBeVisible()
    fireEvent.click(screen.getByRole('button',{name:'Close workspace panel'}))
    expect(screen.queryByText('Panel readings')).toBeNull()
    expect(screen.getByLabelText('Portal draft')).toBe(input)
    expect(input).toHaveValue('Keep this message')
  })
  beforeEach(() => {
    useSystemStatus.mockReturnValue({
      status: { gpu: null, services: [], model: null, bootstrap: null, uptime: 0, version: '1.0.0' },
      loading: false,
      error: null,
    })
    useVersion.mockReturnValue({ version:{current:'2.6.0',update_available:false}, showUpdate:false, dismissUpdate:vi.fn() })
    getInternalRoutes.mockReturnValue([])
    vi.stubGlobal('fetch', vi.fn(() =>
      Promise.resolve({ ok: true, json: () => Promise.resolve({}) })
    ))
    globalThis.localStorage.removeItem('ods-sidebar-collapsed')
    useFirstRun.mockReturnValue({ firstRun: false, loading: false, error: null, refresh: vi.fn() })
  })

  afterEach(() => {
    vi.restoreAllMocks()
  })

  test('renders without crashing', () => {
    render(<App />)
    expect(document.querySelector('aside')).toBeInTheDocument()
  })

  test.each([
    ['starting', 'Preparing Full Model', 'Preparing the full model download…'],
    ['verifying', 'Verifying Full Model', 'Checking the downloaded model before activation…'],
    ['swapping', 'Activating Full Model', 'Switching chat to the full model…'],
  ])('shows the %s model phase instead of a download ETA', (phase, title, detail) => {
    useSystemStatus.mockReturnValue({
      status: {
        gpu: null, services: [], model: null, uptime: 0, version: '1.0.0',
        bootstrap: {
          active: true, phase, model: 'Qwen 3.5 9B', percent: 100,
          bytesDownloaded: 5.68e9, bytesTotal: 5.68e9, speedMbps: 25, eta: null,
        },
      },
      loading: false,
      error: null,
    })
    render(<App />)
    expect(screen.getByText(title)).toBeInTheDocument()
    expect(screen.getByText(detail)).toBeInTheDocument()
    if (phase === 'swapping') {
      expect(screen.queryByText(/Chat now with lightweight model/)).toBeNull()
      expect(screen.getByText(/Chat pauses during activation/)).toBeVisible()
    } else expect(screen.getByText(/Chat now with lightweight model/)).toBeVisible()
    expect(screen.queryByText(/ETA:/)).toBeNull()
    expect(screen.queryByText('25.0 MB/s')).toBeNull()
    if (phase !== 'starting') expect(screen.getByText('Download 100%')).toBeInTheDocument()
  })

  test('keeps download progress for older clients without a phase', () => {
    useSystemStatus.mockReturnValue({
      status: {
        gpu: null, services: [], model: null, uptime: 0, version: '1.0.0',
        bootstrap: {
          active: true, model: 'Qwen 3.5 9B', percent: 50,
          bytesDownloaded: 3e9, bytesTotal: 6e9, speedMbps: 25, eta: 120,
        },
      },
      loading: false,
      error: null,
    })
    render(<App />)
    expect(screen.getByText('Downloading Full Model')).toBeInTheDocument()
    expect(screen.getByText('50.0%')).toBeInTheDocument()
    expect(screen.getByText(/ETA: 2m 0s/)).toBeInTheDocument()
    expect(screen.getByText('25.0 MB/s')).toBeInTheDocument()
  })

  // bootstrap-upgrade.sh writes bytesTotal 0 when the HEAD request returns no
  // Content-Length, and /api/status sends that unknown percent as 0.
  test.each([
    [3.1e9, '3.1 GB downloaded · Total size unavailable'],
    [0, 'Progress unavailable'],
  ])('shows an unknown download total as indeterminate (%s bytes downloaded)', (bytesDownloaded, detail) => {
    useSystemStatus.mockReturnValue({
      status: {
        gpu: null, services: [], model: null, uptime: 0, version: '1.0.0',
        bootstrap: {
          active: true, phase: 'downloading', model: 'Qwen 3.5 9B', percent: 0,
          bytesDownloaded, bytesTotal: 0, speedMbps: 25, eta: null,
        },
      },
      loading: false,
      error: null,
    })
    render(<App />)
    expect(screen.getByText('Downloading Full Model')).toBeInTheDocument()
    expect(screen.queryByText('0.0%')).toBeNull()
    expect(screen.queryByText(/\/ 0 GB/)).toBeNull()
    expect(screen.queryByText(/ETA:/)).toBeNull()
    expect(screen.getByText(detail)).toBeInTheDocument()
    expect(screen.getByRole('progressbar', { name: 'Full model download progress' })).not.toHaveAttribute('aria-valuenow')
    expect(screen.getByText('25.0 MB/s')).toBeInTheDocument()
  })

  test('keeps the percentage and bar for a known total at 0%', () => {
    useSystemStatus.mockReturnValue({
      status: {
        gpu: null, services: [], model: null, uptime: 0, version: '1.0.0',
        bootstrap: {
          active: true, phase: 'downloading', model: 'Qwen 3.5 9B', percent: 0,
          bytesDownloaded: 0, bytesTotal: 6e9, speedMbps: null, eta: null,
        },
      },
      loading: false,
      error: null,
    })
    render(<App />)
    expect(screen.getByText('0.0%')).toBeInTheDocument()
    expect(screen.getByRole('progressbar', { name: 'Full model download progress' })).toHaveAttribute('aria-valuenow', '0')
    expect(screen.getByText(/ETA: calculating\.\.\. • 0 \/ 6\.0 GB/)).toBeInTheDocument()
  })

  test('opens update details from the confirmed-release notice without replacing the conversation', async () => {
    useVersion.mockReturnValue({ version:{current:'2.6.0',latest:'2.7.0',update_available:true,check_status:'checked'}, showUpdate:true, dismissUpdate:vi.fn() })
    render(<App />)
    const draft = await screen.findByLabelText('Portal draft')
    fireEvent.change(draft, {target:{value:'Keep my message'}})
    fireEvent.click(screen.getByRole('button', {name:'View update'}))
    expect(await screen.findByLabelText('Settings draft')).toBeVisible()
    expect(screen.getByLabelText('Portal draft')).toBe(draft)
    expect(draft).toHaveValue('Keep my message')
  })

  test('shows FirstBoot when server reports first_run=true', async () => {
    useFirstRun.mockReturnValue({ firstRun: true, loading: false, error: null, refresh: vi.fn() })
    render(<App />)
    // FirstBoot is lazy-loaded under Suspense; await its appearance.
    expect(await screen.findByTestId('first-boot')).toBeInTheDocument()
    // Sidebar must NOT render during onboarding — the wizard owns the screen.
    expect(document.querySelector('aside')).not.toBeInTheDocument()
  })

  test('hides FirstBoot when server reports first_run=false', () => {
    useFirstRun.mockReturnValue({ firstRun: false, loading: false, error: null, refresh: vi.fn() })
    render(<App />)
    expect(screen.queryByTestId('first-boot')).not.toBeInTheDocument()
  })

  test('renders sidebar', () => {
    render(<App />)
    expect(document.querySelector('aside')).toBeInTheDocument()
    expect(document.querySelector('main')).toBeInTheDocument()
  })

  test('uses the compact shell offset below the desktop sidebar breakpoint', () => {
    render(<App />)

    expect(document.querySelector('aside')).toHaveClass('pixel-sidebar')
    expect(document.querySelector('main')).toHaveClass('pixel-workspace')
  })

  test('keeps the compact shell offset when the desktop sidebar is collapsed', () => {
    globalThis.localStorage.setItem('ods-sidebar-collapsed', 'true')
    render(<App />)

    expect(document.querySelector('aside')).toHaveClass('is-collapsed')
    expect(document.querySelector('.pixel-app')).toHaveClass('sidebar-collapsed')
  })

  test('renders ODS Talk without dashboard chrome on /talk', async () => {
    rtlRender(
      <MemoryRouter initialEntries={['/talk']}>
        <ThemeProvider>
          <App />
        </ThemeProvider>
      </MemoryRouter>,
    )
    expect(await screen.findByTestId('ods-talk')).toBeInTheDocument()
    expect(document.querySelector('aside')).not.toBeInTheDocument()
    expect(globalThis.fetch).not.toHaveBeenCalledWith('/api/auth/admin-session', expect.anything())
  })
})

test.each(['localStorage', 'sessionStorage'])('opens the beta workspace when %s access is denied', async storage => {
  const getter = vi.spyOn(window, storage, 'get').mockImplementation(() => {
    throw new window.DOMException('Storage denied', 'SecurityError')
  })
  try {
    render(<App />)
    expect(await screen.findByLabelText('Portal draft')).toBeVisible()
    fireEvent.click(screen.getByRole('button', {name:'Collapse sidebar'}))
    expect(screen.getByRole('button', {name:'Expand sidebar'})).toBeVisible()
  } finally { getter.mockRestore() }
})