Files
opencode-manager/frontend/src/components/ui/sidebar.test.tsx
T
Chris Scott 015a44a5f3 feat(sidebar): add repo-grouped session navigation with live status (#361)
* feat(sidebar): add repo-grouped session navigation with live status

- Add a searchable, collapsible session tree to the desktop sidebar, grouped by repository with pinned and time-bucketed ordering.
- Track per-session busy, retry, and compact status in a shared store fed by the SSE event stream and scoped by directory.
- Extract shared navigation components reused by the desktop tree and the mobile repo quick-switch sheet.
- Show a Git tab and hide Schedules inside a repo on the mobile tab bar; add Repos and Assistant entries to the session More drawer.
- Replace the CommitsTab local relative-time formatter with a shared formatShortRelativeTime util.

* refactor(sidebar): unify session status tracking and navigation helpers

* feat(sidebar): add collapsible sessions and menu sections

* fix(sessions): keep specific live status over snapshot busy
2026-09-26 21:50:23 -04:00

197 lines
5.2 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
import { Sidebar, SidebarCollapsibleSection, SidebarSection, SidebarItem, SidebarCollapseToggle } from './sidebar'
import { TooltipProvider } from './tooltip'
import { Folder } from 'lucide-react'
describe('Sidebar', () => {
it('renders children', () => {
render(
<Sidebar collapsed={false} onToggle={vi.fn()}>
<div data-testid="child">Content</div>
</Sidebar>
)
expect(screen.getByTestId('child')).toBeInTheDocument()
})
it('applies collapsed width class when collapsed', () => {
const { container } = render(
<Sidebar collapsed={true} onToggle={vi.fn()}>
<div>Content</div>
</Sidebar>
)
expect(container.firstChild).toHaveClass('w-14')
})
it('applies expanded width class when not collapsed', () => {
const { container } = render(
<Sidebar collapsed={false} onToggle={vi.fn()}>
<div>Content</div>
</Sidebar>
)
expect(container.firstChild).toHaveClass('w-60')
})
})
describe('SidebarSection', () => {
it('renders label when not collapsed', () => {
render(
<SidebarSection label="Test Section" collapsed={false}>
<div>Content</div>
</SidebarSection>
)
expect(screen.getByText('Test Section')).toBeInTheDocument()
})
it('hides label when collapsed', () => {
render(
<SidebarSection label="Test Section" collapsed={true}>
<div>Content</div>
</SidebarSection>
)
expect(screen.queryByText('Test Section')).not.toBeInTheDocument()
})
})
describe('SidebarCollapsibleSection', () => {
it('renders label and children when expanded', () => {
render(
<SidebarCollapsibleSection label="Sessions" collapsed={false} onToggle={vi.fn()}>
<div>Content</div>
</SidebarCollapsibleSection>
)
expect(screen.getByRole('button', { name: 'Sessions' })).toHaveAttribute('aria-expanded', 'true')
expect(screen.getByText('Content')).toBeInTheDocument()
})
it('hides children when collapsed', () => {
render(
<SidebarCollapsibleSection label="Sessions" collapsed={true} onToggle={vi.fn()}>
<div>Content</div>
</SidebarCollapsibleSection>
)
expect(screen.getByRole('button', { name: 'Sessions' })).toHaveAttribute('aria-expanded', 'false')
expect(screen.queryByText('Content')).not.toBeInTheDocument()
})
it('calls onToggle when the header is clicked', () => {
const handleToggle = vi.fn()
render(
<SidebarCollapsibleSection label="Menu" collapsed={false} onToggle={handleToggle}>
<div>Content</div>
</SidebarCollapsibleSection>
)
fireEvent.click(screen.getByRole('button', { name: 'Menu' }))
expect(handleToggle).toHaveBeenCalledTimes(1)
})
it('applies contentClassName to the content wrapper', () => {
render(
<SidebarCollapsibleSection
label="Menu"
collapsed={false}
onToggle={vi.fn()}
contentClassName="overflow-y-auto"
>
<div>Content</div>
</SidebarCollapsibleSection>
)
expect(screen.getByText('Content').parentElement).toHaveClass('overflow-y-auto')
})
})
describe('SidebarItem', () => {
it('fires onClick when clicked', () => {
const handleClick = vi.fn()
render(
<SidebarItem
icon={Folder}
label="Test Item"
collapsed={false}
onClick={handleClick}
/>
)
fireEvent.click(screen.getByRole('button'))
expect(handleClick).toHaveBeenCalledTimes(1)
})
it('applies active styles when active', () => {
render(
<SidebarItem
icon={Folder}
label="Test Item"
collapsed={false}
active={true}
/>
)
expect(screen.getByRole('button')).toHaveClass('bg-accent')
})
it('uses title attribute when collapsed', () => {
render(
<TooltipProvider>
<SidebarItem
icon={Folder}
label="Test Item"
collapsed={true}
/>
</TooltipProvider>
)
expect(screen.getByRole('button')).toHaveAttribute('title', 'Test Item')
})
})
describe('SidebarCollapseToggle', () => {
it('calls onToggle when clicked', () => {
const handleToggle = vi.fn()
render(
<SidebarCollapseToggle collapsed={false} onToggle={handleToggle} />
)
fireEvent.click(screen.getByRole('button'))
expect(handleToggle).toHaveBeenCalledTimes(1)
})
it('calls onToggle when clicked while collapsed', () => {
const handleToggle = vi.fn()
render(
<SidebarCollapseToggle collapsed={true} onToggle={handleToggle} />
)
fireEvent.click(screen.getByRole('button', { name: 'Expand sidebar' }))
expect(handleToggle).toHaveBeenCalledTimes(1)
})
it('exposes expanded state via accessible name and aria-expanded', () => {
const { rerender } = render(
<SidebarCollapseToggle collapsed={false} onToggle={vi.fn()} />
)
expect(
screen.getByRole('button', { name: 'Collapse sidebar' })
).toHaveAttribute('aria-expanded', 'true')
rerender(<SidebarCollapseToggle collapsed={true} onToggle={vi.fn()} />)
expect(
screen.getByRole('button', { name: 'Expand sidebar' })
).toHaveAttribute('aria-expanded', 'false')
})
})