Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions web-ui/src/App.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { describe, it, expect, vi, afterEach } from 'vitest'
import { render, screen, fireEvent, within } from '@testing-library/react'
import App from './App'
import { mockGateway, defaultRoutes, jsonResponse } from './test/gatewayMock'

describe('App', () => {
afterEach(() => {
vi.unstubAllGlobals()
vi.restoreAllMocks()
localStorage.clear()
})

it('renders the dashboard with live gateway status on load', async () => {
mockGateway(defaultRoutes())
render(<App />)

expect(screen.getByRole('heading', { name: 'Dashboard' })).toBeInTheDocument()
expect(await screen.findByText('Healthy')).toBeInTheDocument()
expect(screen.getByText('3.11')).toBeInTheDocument()
expect(screen.getByText('3.12')).toBeInTheDocument()
// Sidebar shows the module version reported by /version, not its fallback.
expect(await screen.findByText('v9.8.7')).toBeInTheDocument()
})

it('shows the login prompt when the gateway rejects the health check', async () => {
mockGateway({ ...defaultRoutes(), '/api/v1/health': () => jsonResponse({ error: 'Unauthorized' }, 401) })
render(<App />)

expect(await screen.findByRole('heading', { name: 'Authentication Required' })).toBeInTheDocument()
expect(screen.getByRole('link', { name: 'Log In to Gateway' })).toHaveAttribute('href', '/web/login')
})

it.each([
['Scripts', 'Scripts', 'TankLevels'],
['Python Versions', 'Python Versions', 'Python 3.13'],
['Packages', 'Packages', 'numpy'],
['Diagnostics', 'Diagnostics', 'Python3ProcessPool started with 3 executors'],
])('navigates to %s from the sidebar', async (navLabel, heading, content) => {
mockGateway(defaultRoutes())
render(<App />)

const nav = screen.getByRole('navigation')
fireEvent.click(within(nav).getByRole('button', { name: navLabel }))

expect(await screen.findByRole('heading', { name: heading })).toBeInTheDocument()
expect(await screen.findByText(content)).toBeInTheDocument()
expect(within(nav).getByRole('button', { name: navLabel })).toHaveAttribute('aria-current', 'page')
})

it('navigates to the IDE from the dashboard quick action', async () => {
mockGateway(defaultRoutes())
render(<App />)

fireEvent.click(screen.getByRole('button', { name: /New Script/ }))
expect(await screen.findByRole('button', { name: 'Run script' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Load Script' })).toBeInTheDocument()
})
})
43 changes: 43 additions & 0 deletions web-ui/src/components/DashboardView.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, it, expect, vi, afterEach } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import DashboardView from './DashboardView'
import { mockGateway, defaultRoutes, callsTo, GATEWAY_PREFIX } from '../test/gatewayMock'

const GATEWAY_URL = window.location.origin + GATEWAY_PREFIX

describe('DashboardView', () => {
afterEach(() => {
vi.unstubAllGlobals()
vi.restoreAllMocks()
})

it('shows health, pool and version cards from the gateway', async () => {
mockGateway(defaultRoutes())
render(<DashboardView gatewayUrl={GATEWAY_URL} onNavigate={() => {}} />)

expect(await screen.findByText('Healthy')).toBeInTheDocument()
expect(screen.getByText('Process Pool')).toBeInTheDocument()
expect(screen.getByTitle('33% utilization')).toBeInTheDocument() // 1 in use of 3
expect(screen.getByText('3.11')).toBeInTheDocument()
expect(screen.getByText(/^Last updated /)).toBeInTheDocument()
})

it('shows Down and the empty versions message when the gateway is unreachable', async () => {
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new TypeError('Failed to fetch')))
render(<DashboardView gatewayUrl={GATEWAY_URL} onNavigate={() => {}} />)

expect(await screen.findByText('Down')).toBeInTheDocument()
expect(screen.getByText('No versions detected')).toBeInTheDocument()
})

it('refetches every card when Refresh is pressed', async () => {
const fetchMock = mockGateway(defaultRoutes())
render(<DashboardView gatewayUrl={GATEWAY_URL} onNavigate={() => {}} />)
await screen.findByText('Healthy')
expect(callsTo(fetchMock, '/api/v1/health')).toHaveLength(1)

fireEvent.click(screen.getByRole('button', { name: 'Refresh dashboard' }))
await waitFor(() => expect(callsTo(fetchMock, '/api/v1/health')).toHaveLength(2))
expect(callsTo(fetchMock, '/api/v1/pool-stats')).toHaveLength(2)
})
})
76 changes: 76 additions & 0 deletions web-ui/src/components/DiagnosticsView.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { describe, it, expect, vi, afterEach } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import DiagnosticsView from './DiagnosticsView'
import {
mockGateway, defaultRoutes, callsTo, jsonResponse, GATEWAY_PREFIX,
} from '../test/gatewayMock'

const GATEWAY_URL = window.location.origin + GATEWAY_PREFIX

describe('DiagnosticsView', () => {
afterEach(() => {
vi.unstubAllGlobals()
vi.restoreAllMocks()
})

it('shows pool stats, health and module log entries', async () => {
mockGateway(defaultRoutes())
render(<DiagnosticsView gatewayUrl={GATEWAY_URL} />)

expect(screen.getByRole('heading', { name: 'Diagnostics' })).toBeInTheDocument()
expect(await screen.findByRole('button', { name: 'Pool size 3, click to change' })).toBeInTheDocument()
expect(await screen.findByText('Executor 2 slow to respond')).toBeInTheDocument()
expect(screen.getByText('2 entries (module only)')).toBeInTheDocument()
})

it('shows each panel’s empty message when the gateway answers 500', async () => {
mockGateway({ '/api/v1/': () => jsonResponse({ success: false, error: 'Internal error' }, 500) })
render(<DiagnosticsView gatewayUrl={GATEWAY_URL} />)

expect(await screen.findByText('No pool data available.')).toBeInTheDocument()
expect(screen.getByText('No metrics data available.')).toBeInTheDocument()
expect(screen.getByText('No log entries found')).toBeInTheDocument()
fireEvent.click(screen.getByRole('button', { name: /Raw Diagnostics/ }))
expect(screen.getByText('(no diagnostics data)')).toBeInTheDocument()
})

it('requests only ERROR logs when the ERROR filter is chosen', async () => {
const fetchMock = mockGateway(defaultRoutes())
render(<DiagnosticsView gatewayUrl={GATEWAY_URL} />)
await screen.findByText('Executor 2 slow to respond')

fireEvent.click(screen.getByRole('button', { name: 'ERROR' }))

await waitFor(() => {
const levels = callsTo(fetchMock, '/api/v1/logs').map(([u]) => new URL(String(u)).searchParams.get('level'))
expect(levels).toContain('ERROR')
})
const logCalls = callsTo(fetchMock, '/api/v1/logs')
const last = logCalls[logCalls.length - 1]
const params = new URL(String(last[0])).searchParams
expect(params.get('filter')).toBe('Python3')
expect(params.get('lines')).toBe('100')
})

it('validates the pool size and POSTs a valid resize to /pool-size', async () => {
const fetchMock = mockGateway({
...defaultRoutes(),
'/api/v1/pool-size': { success: true, poolSize: 5, message: 'Pool size changed to 5' },
})
render(<DiagnosticsView gatewayUrl={GATEWAY_URL} />)

fireEvent.click(await screen.findByRole('button', { name: 'Pool size 3, click to change' }))
const input = screen.getByRole('spinbutton')
fireEvent.change(input, { target: { value: '25' } })
fireEvent.keyDown(input, { key: 'Enter' })
expect(await screen.findByText('Must be 1–20')).toBeInTheDocument()
expect(callsTo(fetchMock, '/api/v1/pool-size')).toHaveLength(0)

fireEvent.change(input, { target: { value: '5' } })
fireEvent.keyDown(input, { key: 'Enter' })
await waitFor(() => expect(callsTo(fetchMock, '/api/v1/pool-size')).toHaveLength(1))
const [call] = callsTo(fetchMock, '/api/v1/pool-size')
expect(call[1]?.method).toBe('POST')
expect(JSON.parse(String(call[1]?.body))).toEqual({ size: 5 })
})
})
50 changes: 50 additions & 0 deletions web-ui/src/components/IDEView.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, it, expect, vi, afterEach, beforeAll } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import IDEView from './IDEView'
import { mockGateway, defaultRoutes, callsTo, GATEWAY_PREFIX } from '../test/gatewayMock'

const GATEWAY_URL = window.location.origin + GATEWAY_PREFIX

// jsdom has no layout engine; CodeMirror measures text ranges after each update.
beforeAll(() => {
const emptyRects = () => Object.assign([], { item: () => null }) as unknown as DOMRectList
Range.prototype.getClientRects = emptyRects
Range.prototype.getBoundingClientRect = () => new DOMRect(0, 0, 0, 0)
})

describe('IDEView', () => {
afterEach(() => {
vi.unstubAllGlobals()
vi.restoreAllMocks()
localStorage.clear()
})

it('runs the editor contents via POST /exec and shows the result', async () => {
const fetchMock = mockGateway({
...defaultRoutes(),
'/api/v1/exec': { success: true, result: 'hello from python' },
})
render(<IDEView gatewayUrl={GATEWAY_URL} />)
expect(screen.getByText('Run a script to see output here. (Ctrl+Enter)')).toBeInTheDocument()

fireEvent.click(screen.getByRole('button', { name: 'Run script' }))

expect(await screen.findByText('hello from python')).toBeInTheDocument()
const [call] = callsTo(fetchMock, '/api/v1/exec')
expect(call[1]?.method).toBe('POST')
expect(JSON.parse(String(call[1]?.body))).toMatchObject({
code: '# Create or load a script to get started\n',
variables: {},
})
})

it('lists saved scripts in the Load Script menu', async () => {
const fetchMock = mockGateway(defaultRoutes())
render(<IDEView gatewayUrl={GATEWAY_URL} />)

fireEvent.click(screen.getByRole('button', { name: 'Load Script' }))
await waitFor(() => expect(callsTo(fetchMock, '/api/v1/scripts/list')).toHaveLength(1))
expect(await screen.findByText('TankLevels')).toBeInTheDocument()
expect(screen.getByText('Housekeeping')).toBeInTheDocument()
})
})
70 changes: 70 additions & 0 deletions web-ui/src/components/PackagesView.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { describe, it, expect, vi, afterEach } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import PackagesView from './PackagesView'
import {
mockGateway, defaultRoutes, callsTo, headersOf, textResponse, GATEWAY_PREFIX,
} from '../test/gatewayMock'

const GATEWAY_URL = window.location.origin + GATEWAY_PREFIX

describe('PackagesView', () => {
afterEach(() => {
vi.unstubAllGlobals()
vi.restoreAllMocks()
})

it('renders the catalogue map with installed status', async () => {
mockGateway(defaultRoutes())
render(<PackagesView gatewayUrl={GATEWAY_URL} showToast={() => {}} />)

expect(await screen.findByText('numpy')).toBeInTheDocument()
expect(screen.getByText('pandas')).toBeInTheDocument()
expect(screen.getByText('1 installed · 2 total')).toBeInTheDocument()
expect(screen.getByTitle('Uninstall numpy')).toBeInTheDocument()
expect(screen.getByTitle('Install pandas')).toBeInTheDocument()
})

it('shows the gateway error text and empty state when the catalogue fails', async () => {
mockGateway({
...defaultRoutes(),
'/api/v1/packages/catalog': () => textResponse('Package manager not initialised', 500),
})
render(<PackagesView gatewayUrl={GATEWAY_URL} showToast={() => {}} />)

expect(await screen.findByText('Package manager not initialised')).toBeInTheDocument()
expect(screen.getByText('No packages found.')).toBeInTheDocument()
})

it('filters installed packages and reports no match', async () => {
mockGateway(defaultRoutes())
render(<PackagesView gatewayUrl={GATEWAY_URL} showToast={() => {}} />)
await screen.findByText('numpy')

const search = screen.getByPlaceholderText('Search installed packages…')
fireEvent.change(search, { target: { value: 'array' } })
expect(screen.getByText('numpy')).toBeInTheDocument()
expect(screen.queryByText('pandas')).not.toBeInTheDocument()

fireEvent.change(search, { target: { value: 'scipy' } })
expect(screen.getByText('No packages match "scipy"')).toBeInTheDocument()
})

it('Install POSTs to /packages/install/<name> with auth headers', async () => {
const fetchMock = mockGateway({
...defaultRoutes(),
'/api/v1/packages/install/': { success: true, message: 'Installed pandas' },
})
const showToast = vi.fn()
render(<PackagesView gatewayUrl={GATEWAY_URL} showToast={showToast} />)

fireEvent.click(await screen.findByTitle('Install pandas'))

await waitFor(() => expect(showToast).toHaveBeenCalledWith('Package installed successfully', 'success'))
const [call] = callsTo(fetchMock, '/api/v1/packages/install/pandas')
expect(call[1]?.method).toBe('POST')
expect(headersOf(call[1])).toMatchObject({
Authorization: 'Bearer api-token-456',
'X-CSRF-Token': 'csrf-token-123',
})
})
})
72 changes: 72 additions & 0 deletions web-ui/src/components/ScriptsView.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, it, expect, vi, afterEach } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import ScriptsView from './ScriptsView'
import {
mockGateway, defaultRoutes, callsTo, jsonResponse, GATEWAY_PREFIX,
} from '../test/gatewayMock'

const GATEWAY_URL = window.location.origin + GATEWAY_PREFIX

describe('ScriptsView', () => {
afterEach(() => {
vi.unstubAllGlobals()
vi.restoreAllMocks()
})

it('lists saved scripts and their folders', async () => {
mockGateway(defaultRoutes())
render(<ScriptsView gatewayUrl={GATEWAY_URL} showToast={() => {}} />)

expect(screen.getByRole('heading', { name: 'Scripts' })).toBeInTheDocument()
expect(await screen.findByText('TankLevels')).toBeInTheDocument()
expect(screen.getByText('Housekeeping')).toBeInTheDocument()
expect(screen.getByText('Reports')).toBeInTheDocument()
})

it('shows the empty message when no scripts are saved', async () => {
mockGateway({ ...defaultRoutes(), '/api/v1/scripts/list': { success: true, scripts: [] } })
render(<ScriptsView gatewayUrl={GATEWAY_URL} showToast={() => {}} />)

expect(await screen.findByText('No scripts saved yet.')).toBeInTheDocument()
})

it('shows the load error when the list endpoint answers 500', async () => {
vi.spyOn(console, 'error').mockImplementation(() => {})
mockGateway({
...defaultRoutes(),
'/api/v1/scripts/list': () => jsonResponse({ success: false, error: 'Script repository not initialized' }, 500),
})
render(<ScriptsView gatewayUrl={GATEWAY_URL} showToast={() => {}} />)

expect(await screen.findByText('Failed to load scripts: Error: HTTP 500')).toBeInTheDocument()
})

it('New Script saves an empty script with the prompted name', async () => {
vi.spyOn(window, 'prompt').mockReturnValue(' PumpReport ')
const fetchMock = mockGateway({
...defaultRoutes(),
'/api/v1/scripts/save': { success: true, message: 'Script saved' },
})
const showToast = vi.fn()
render(<ScriptsView gatewayUrl={GATEWAY_URL} showToast={showToast} />)
await screen.findByText('TankLevels')

fireEvent.click(screen.getByTitle('New Script'))

await waitFor(() => expect(showToast).toHaveBeenCalledWith('Script created successfully', 'success'))
const [call] = callsTo(fetchMock, '/api/v1/scripts/save')
expect(call[1]?.method).toBe('POST')
expect(JSON.parse(String(call[1]?.body))).toEqual({ name: 'PumpReport', code: '', description: '' })
})

it('New Folder rejects a folder name that already exists', async () => {
vi.spyOn(window, 'prompt').mockReturnValue('Reports')
mockGateway(defaultRoutes())
const showToast = vi.fn()
render(<ScriptsView gatewayUrl={GATEWAY_URL} showToast={showToast} />)
await screen.findByText('TankLevels')

fireEvent.click(screen.getByTitle('New Folder'))
expect(showToast).toHaveBeenCalledWith('Folder "Reports" already exists.', 'error')
})
})
Loading
Loading