Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

test: add comprehensive component and store tests

Boschf7dec30a4835
diff --git a/dashboard/src/components/atoms/Badge.test.tsx b/dashboard/src/components/atoms/Badge.test.tsx
new file mode 100644
index 00000000..fb8213a3
--- /dev/null
+++ b/dashboard/src/components/atoms/Badge.test.tsx
@@ -0,0 +1,46 @@
+import { render, screen } from '@testing-library/react'
+import { describe, it, expect } from 'vitest'
+import Badge from './Badge'
+
+describe('Badge', () => {
+ it('renders children', () => {
+ render(<Badge>Status</Badge>)
+ expect(screen.getByText('Status')).toBeInTheDocument()
+ })
+
+ it('applies default variant styles', () => {
+ render(<Badge>Default</Badge>)
+ const badge = screen.getByText('Default')
+ expect(badge).toHaveClass('bg-bg-elevated', 'text-text-secondary')
+ })
+
+ it('applies success variant styles', () => {
+ render(<Badge variant="success">Running</Badge>)
+ const badge = screen.getByText('Running')
+ expect(badge).toHaveClass('text-success')
+ })
+
+ it('applies danger variant styles', () => {
+ render(<Badge variant="danger">Error</Badge>)
+ const badge = screen.getByText('Error')
+ expect(badge).toHaveClass('text-destructive')
+ })
+
+ it('applies warning variant styles', () => {
+ render(<Badge variant="warning">Warning</Badge>)
+ const badge = screen.getByText('Warning')
+ expect(badge).toHaveClass('text-warning')
+ })
+
+ it('applies info variant styles', () => {
+ render(<Badge variant="info">Info</Badge>)
+ const badge = screen.getByText('Info')
+ expect(badge).toHaveClass('text-info')
+ })
+
+ it('has correct base styles', () => {
+ render(<Badge>Test</Badge>)
+ const badge = screen.getByText('Test')
+ expect(badge).toHaveClass('inline-flex', 'items-center', 'rounded', 'text-xs', 'font-medium')
+ })
+})
diff --git a/dashboard/src/components/atoms/StatusDot.test.tsx b/dashboard/src/components/atoms/StatusDot.test.tsx
new file mode 100644
index 00000000..d100d244
--- /dev/null
+++ b/dashboard/src/components/atoms/StatusDot.test.tsx
@@ -0,0 +1,52 @@
+import { render, screen } from '@testing-library/react'
+import { describe, it, expect } from 'vitest'
+import StatusDot from './StatusDot'
+
+describe('StatusDot', () => {
+ it('renders online status with success color', () => {
+ const { container } = render(<StatusDot status="online" />)
+ const dot = container.querySelector('.rounded-full')
+ expect(dot).toHaveClass('bg-success')
+ })
+
+ it('renders offline status with muted color', () => {
+ const { container } = render(<StatusDot status="offline" />)
+ const dot = container.querySelector('.rounded-full')
+ expect(dot).toHaveClass('bg-text-muted')
+ })
+
+ it('renders warning status with warning color', () => {
+ const { container } = render(<StatusDot status="warning" />)
+ const dot = container.querySelector('.rounded-full')
+ expect(dot).toHaveClass('bg-warning')
+ })
+
+ it('renders loading status with pulse animation', () => {
+ const { container } = render(<StatusDot status="loading" />)
+ const dot = container.querySelector('.rounded-full')
+ expect(dot).toHaveClass('bg-primary', 'animate-pulse')
+ })
+
+ it('renders label when provided', () => {
+ render(<StatusDot status="online" label="Connected" />)
+ expect(screen.getByText('Connected')).toBeInTheDocument()
+ })
+
+ it('does not render label when not provided', () => {
+ const { container } = render(<StatusDot status="online" />)
+ const label = container.querySelector('.text-xs')
+ expect(label).not.toBeInTheDocument()
+ })
+
+ it('applies small size', () => {
+ const { container } = render(<StatusDot status="online" size="sm" />)
+ const dot = container.querySelector('.rounded-full')
+ expect(dot).toHaveClass('h-2', 'w-2')
+ })
+
+ it('applies medium size by default', () => {
+ const { container } = render(<StatusDot status="online" />)
+ const dot = container.querySelector('.rounded-full')
+ expect(dot).toHaveClass('h-2.5', 'w-2.5')
+ })
+})
diff --git a/dashboard/src/components/molecules/ProfileCard.test.tsx b/dashboard/src/components/molecules/ProfileCard.test.tsx
new file mode 100644
index 00000000..19ee6870
--- /dev/null
+++ b/dashboard/src/components/molecules/ProfileCard.test.tsx
@@ -0,0 +1,170 @@
+import { render, screen } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+import { describe, it, expect, vi } from 'vitest'
+import ProfileCard from './ProfileCard'
+import type { Profile, Instance } from '../../types'
+
+const mockProfile: Profile = {
+ id: 'prof_123',
+ name: 'test-profile',
+ created: new Date().toISOString(),
+ lastUsed: new Date().toISOString(),
+ diskUsage: 1024 * 1024 * 100, // 100MB
+ sizeMB: 100,
+ running: false,
+}
+
+const mockRunningInstance: Instance = {
+ id: 'inst_123',
+ profileId: 'prof_123',
+ profileName: 'test-profile',
+ port: '9868',
+ headless: false,
+ status: 'running',
+ startTime: new Date().toISOString(),
+}
+
+const mockErrorInstance: Instance = {
+ ...mockRunningInstance,
+ status: 'error',
+ error: 'Failed to start Chrome',
+}
+
+describe('ProfileCard', () => {
+ it('renders profile name', () => {
+ render(<ProfileCard profile={mockProfile} onLaunch={() => {}} />)
+ expect(screen.getByText('test-profile')).toBeInTheDocument()
+ })
+
+ it('shows stopped badge when no instance', () => {
+ render(<ProfileCard profile={mockProfile} onLaunch={() => {}} />)
+ expect(screen.getByText('stopped')).toBeInTheDocument()
+ })
+
+ it('shows port badge when running', () => {
+ render(
+ <ProfileCard
+ profile={mockProfile}
+ instance={mockRunningInstance}
+ onLaunch={() => {}}
+ />
+ )
+ expect(screen.getByText(':9868')).toBeInTheDocument()
+ })
+
+ it('shows error badge when errored', () => {
+ render(
+ <ProfileCard
+ profile={mockProfile}
+ instance={mockErrorInstance}
+ onLaunch={() => {}}
+ />
+ )
+ expect(screen.getByText('error')).toBeInTheDocument()
+ })
+
+ it('shows error message when errored', () => {
+ render(
+ <ProfileCard
+ profile={mockProfile}
+ instance={mockErrorInstance}
+ onLaunch={() => {}}
+ />
+ )
+ expect(screen.getByText('Failed to start Chrome')).toBeInTheDocument()
+ })
+
+ it('displays size in MB', () => {
+ render(<ProfileCard profile={mockProfile} onLaunch={() => {}} />)
+ expect(screen.getByText('100 MB')).toBeInTheDocument()
+ })
+
+ it('displays dash for missing size', () => {
+ const noSizeProfile = { ...mockProfile, sizeMB: undefined }
+ render(<ProfileCard profile={noSizeProfile} onLaunch={() => {}} />)
+ expect(screen.getAllByText('—')).toHaveLength(2) // Size and Account both show —
+ })
+
+ it('displays account email', () => {
+ const profileWithEmail = { ...mockProfile, accountEmail: '[email protected]' }
+ render(<ProfileCard profile={profileWithEmail} onLaunch={() => {}} />)
+ expect(screen.getByText('[email protected]')).toBeInTheDocument()
+ })
+
+ it('displays account name when no email', () => {
+ const profileWithName = { ...mockProfile, accountName: 'Test User' }
+ render(<ProfileCard profile={profileWithName} onLaunch={() => {}} />)
+ expect(screen.getByText('Test User')).toBeInTheDocument()
+ })
+
+ it('displays useWhen text', () => {
+ const profileWithUseWhen = { ...mockProfile, useWhen: 'For testing purposes' }
+ render(<ProfileCard profile={profileWithUseWhen} onLaunch={() => {}} />)
+ expect(screen.getByText('For testing purposes')).toBeInTheDocument()
+ })
+
+ it('shows Start button when stopped', () => {
+ render(<ProfileCard profile={mockProfile} onLaunch={() => {}} />)
+ expect(screen.getByRole('button', { name: 'Start' })).toBeInTheDocument()
+ })
+
+ it('shows Stop button when running', () => {
+ render(
+ <ProfileCard
+ profile={mockProfile}
+ instance={mockRunningInstance}
+ onLaunch={() => {}}
+ onStop={() => {}}
+ />
+ )
+ expect(screen.getByRole('button', { name: 'Stop' })).toBeInTheDocument()
+ })
+
+ it('calls onLaunch when Start clicked', async () => {
+ const handleLaunch = vi.fn()
+ render(<ProfileCard profile={mockProfile} onLaunch={handleLaunch} />)
+
+ await userEvent.click(screen.getByRole('button', { name: 'Start' }))
+ expect(handleLaunch).toHaveBeenCalledTimes(1)
+ })
+
+ it('calls onStop when Stop clicked', async () => {
+ const handleStop = vi.fn()
+ render(
+ <ProfileCard
+ profile={mockProfile}
+ instance={mockRunningInstance}
+ onLaunch={() => {}}
+ onStop={handleStop}
+ />
+ )
+
+ await userEvent.click(screen.getByRole('button', { name: 'Stop' }))
+ expect(handleStop).toHaveBeenCalledTimes(1)
+ })
+
+ it('shows Details button when onDetails provided', () => {
+ render(
+ <ProfileCard
+ profile={mockProfile}
+ onLaunch={() => {}}
+ onDetails={() => {}}
+ />
+ )
+ expect(screen.getByRole('button', { name: 'Details' })).toBeInTheDocument()
+ })
+
+ it('calls onDetails when Details clicked', async () => {
+ const handleDetails = vi.fn()
+ render(
+ <ProfileCard
+ profile={mockProfile}
+ onLaunch={() => {}}
+ onDetails={handleDetails}
+ />
+ )
+
+ await userEvent.click(screen.getByRole('button', { name: 'Details' }))
+ expect(handleDetails).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/dashboard/src/components/molecules/TabsChart.test.tsx b/dashboard/src/components/molecules/TabsChart.test.tsx
new file mode 100644
index 00000000..17e1abb8
--- /dev/null
+++ b/dashboard/src/components/molecules/TabsChart.test.tsx
@@ -0,0 +1,80 @@
+import { render, screen } from '@testing-library/react'
+import { describe, it, expect, beforeAll } from 'vitest'
+import TabsChart from './TabsChart'
+import type { TabDataPoint } from '../../stores/useAppStore'
+
+// Mock ResizeObserver for Recharts ResponsiveContainer
+beforeAll(() => {
+ class ResizeObserverMock {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+ }
+ window.ResizeObserver = ResizeObserverMock as any
+})
+
+const mockInstances = [
+ { id: 'inst_1', profileName: 'profile-1' },
+ { id: 'inst_2', profileName: 'profile-2' },
+]
+
+const mockData: TabDataPoint[] = [
+ { timestamp: Date.now() - 60000, inst_1: 3, inst_2: 5 },
+ { timestamp: Date.now() - 30000, inst_1: 4, inst_2: 6 },
+ { timestamp: Date.now(), inst_1: 5, inst_2: 4 },
+]
+
+describe('TabsChart', () => {
+ describe('empty states', () => {
+ it('shows "No running instances" when instances array is empty', () => {
+ render(
+ <TabsChart
+ data={mockData}
+ instances={[]}
+ selectedInstanceId={null}
+ onSelectInstance={() => {}}
+ />
+ )
+ expect(screen.getByText('No running instances')).toBeInTheDocument()
+ })
+
+ it('shows "Collecting data..." when data array is empty', () => {
+ render(
+ <TabsChart
+ data={[]}
+ instances={mockInstances}
+ selectedInstanceId={null}
+ onSelectInstance={() => {}}
+ />
+ )
+ expect(screen.getByText('Collecting data...')).toBeInTheDocument()
+ })
+
+ it('shows "No running instances" when both are empty (instances takes priority)', () => {
+ render(
+ <TabsChart
+ data={[]}
+ instances={[]}
+ selectedInstanceId={null}
+ onSelectInstance={() => {}}
+ />
+ )
+ expect(screen.getByText('No running instances')).toBeInTheDocument()
+ })
+ })
+
+ describe('chart rendering', () => {
+ it('renders chart container when data and instances exist', () => {
+ const { container } = render(
+ <TabsChart
+ data={mockData}
+ instances={mockInstances}
+ selectedInstanceId={null}
+ onSelectInstance={() => {}}
+ />
+ )
+ // Should render the chart container (not the empty state)
+ expect(container.querySelector('.recharts-responsive-container')).toBeInTheDocument()
+ })
+ })
+})
diff --git a/dashboard/src/stores/useAppStore.test.ts b/dashboard/src/stores/useAppStore.test.ts
new file mode 100644
index 00000000..81133782
--- /dev/null
+++ b/dashboard/src/stores/useAppStore.test.ts
@@ -0,0 +1,144 @@
+import { describe, it, expect, beforeEach } from 'vitest'
+import { useAppStore } from './useAppStore'
+
+describe('useAppStore', () => {
+ beforeEach(() => {
+ // Reset store between tests
+ useAppStore.setState({
+ profiles: [],
+ profilesLoading: false,
+ instances: [],
+ instancesLoading: false,
+ tabsChartData: [],
+ currentTabs: {},
+ agents: [],
+ selectedAgentId: null,
+ events: [],
+ eventFilter: 'all',
+ serverInfo: null,
+ })
+ })
+
+ describe('profiles', () => {
+ it('sets profiles', () => {
+ const profiles = [{ name: 'test', id: 'prof_123' }] as any
+ useAppStore.getState().setProfiles(profiles)
+ expect(useAppStore.getState().profiles).toEqual(profiles)
+ })
+
+ it('sets profiles loading state', () => {
+ useAppStore.getState().setProfilesLoading(true)
+ expect(useAppStore.getState().profilesLoading).toBe(true)
+ })
+ })
+
+ describe('instances', () => {
+ it('sets instances', () => {
+ const instances = [{ id: 'inst_123', profileName: 'test' }] as any
+ useAppStore.getState().setInstances(instances)
+ expect(useAppStore.getState().instances).toEqual(instances)
+ })
+ })
+
+ describe('chart data', () => {
+ it('adds chart data point', () => {
+ const point = { timestamp: Date.now(), inst_123: 5 }
+ useAppStore.getState().addChartDataPoint(point)
+ expect(useAppStore.getState().tabsChartData).toHaveLength(1)
+ expect(useAppStore.getState().tabsChartData[0]).toEqual(point)
+ })
+
+ it('keeps only last 60 data points', () => {
+ // Add 65 points
+ for (let i = 0; i < 65; i++) {
+ useAppStore.getState().addChartDataPoint({ timestamp: i, inst_123: i })
+ }
+
+ const data = useAppStore.getState().tabsChartData
+ expect(data).toHaveLength(60)
+ // Should have points 5-64 (the last 60)
+ expect(data[0].timestamp).toBe(5)
+ expect(data[59].timestamp).toBe(64)
+ })
+
+ it('sets current tabs', () => {
+ const tabs = { inst_123: [{ id: 'tab_1', url: 'https://example.com' }] } as any
+ useAppStore.getState().setCurrentTabs(tabs)
+ expect(useAppStore.getState().currentTabs).toEqual(tabs)
+ })
+ })
+
+ describe('events', () => {
+ it('adds event to beginning of list', () => {
+ const event1 = { type: 'action', timestamp: '2024-01-01' } as any
+ const event2 = { type: 'action', timestamp: '2024-01-02' } as any
+
+ useAppStore.getState().addEvent(event1)
+ useAppStore.getState().addEvent(event2)
+
+ const events = useAppStore.getState().events
+ expect(events[0]).toEqual(event2) // Most recent first
+ expect(events[1]).toEqual(event1)
+ })
+
+ it('limits events to 100', () => {
+ // Add 105 events
+ for (let i = 0; i < 105; i++) {
+ useAppStore.getState().addEvent({ type: 'action', id: i } as any)
+ }
+
+ const events = useAppStore.getState().events
+ expect(events).toHaveLength(100)
+ // Most recent should be id: 104
+ expect(events[0].id).toBe(104)
+ })
+
+ it('clears events', () => {
+ useAppStore.getState().addEvent({ type: 'action' } as any)
+ useAppStore.getState().clearEvents()
+ expect(useAppStore.getState().events).toHaveLength(0)
+ })
+
+ it('sets event filter', () => {
+ useAppStore.getState().setEventFilter('errors')
+ expect(useAppStore.getState().eventFilter).toBe('errors')
+ })
+ })
+
+ describe('agents', () => {
+ it('sets agents', () => {
+ const agents = [{ id: 'agent_1', name: 'Test Agent' }] as any
+ useAppStore.getState().setAgents(agents)
+ expect(useAppStore.getState().agents).toEqual(agents)
+ })
+
+ it('sets selected agent id', () => {
+ useAppStore.getState().setSelectedAgentId('agent_1')
+ expect(useAppStore.getState().selectedAgentId).toBe('agent_1')
+ })
+
+ it('clears selected agent id', () => {
+ useAppStore.getState().setSelectedAgentId('agent_1')
+ useAppStore.getState().setSelectedAgentId(null)
+ expect(useAppStore.getState().selectedAgentId).toBeNull()
+ })
+ })
+
+ describe('settings', () => {
+ it('has default settings', () => {
+ const settings = useAppStore.getState().settings
+ expect(settings.stealth).toBe('light')
+ expect(settings.screencast?.fps).toBe(1)
+ })
+
+ it('updates settings', () => {
+ const newSettings = {
+ screencast: { fps: 5, quality: 50, maxWidth: 1024 },
+ stealth: 'strict' as const,
+ browser: { blockImages: true, blockMedia: true, noAnimations: true },
+ }
+ useAppStore.getState().setSettings(newSettings)
+ expect(useAppStore.getState().settings).toEqual(newSettings)
+ })
+ })
+})