Files
opencode-manager/frontend/src/components/navigation/DesktopSessionTree.test.tsx
T
Chris Scott 36b8c1b200 feat(servers): switch the sidebar listing between local and linked servers
The Servers section becomes a Local/server switcher shared by the desktop sidebar, collapsed rail and mobile repo sheet. Selecting a linked server lists its projects and sessions with the same navigation as local repos, and a remote project page mirrors the local repo page.
2026-10-19 10:16:32 -04:00

466 lines
18 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { MemoryRouter, useLocation } from 'react-router-dom'
import type { ReactNode } from 'react'
import { TooltipProvider } from '@/components/ui/tooltip'
import { LOCAL_OPENCODE_SERVER_ID } from '@opencode-manager/shared/schemas'
import { DesktopSessionTree } from './DesktopSessionTree'
import { useSidebarServerStore } from '@/stores/sidebarServerStore'
import type { Repo } from '@/api/types'
type SessionFixture = {
id: string
projectID: string
title: string
time: { created: number; updated: number; archived?: number }
location: { directory: string }
parentID?: string
cost: number
tokens: { input: number; output: number; reasoning: number; cache: { read: number; write: number } }
}
type SessionsHookCall = { directories: string[]; options?: { search?: string; limit?: number } }
type SessionPinFixture = { sessionId: string; directory: string; pinnedAt: number }
const {
listReposMock,
getRepoSiblingsMock,
sessionsData,
sessionsHookCalls,
createSessionCalls,
permissionSessions,
formSessions,
emptyOnSearch,
getSessionMock,
sessionPinsData,
sessionsHasNextPage,
searchError,
} = vi.hoisted(() => ({
listReposMock: vi.fn(),
getRepoSiblingsMock: vi.fn(),
sessionsData: [] as SessionFixture[],
sessionsHookCalls: [] as SessionsHookCall[],
createSessionCalls: [] as Array<{ directory?: string; vars?: unknown }>,
permissionSessions: { current: new Set<string>() },
formSessions: { current: new Set<string>() },
emptyOnSearch: { current: false },
getSessionMock: vi.fn(),
sessionPinsData: { current: [] as SessionPinFixture[] },
sessionsHasNextPage: { current: false },
searchError: { current: false },
}))
vi.mock('@/api/repos', async (importOriginal) => {
const actual = await importOriginal<typeof import('@/api/repos')>()
return { ...actual, listRepos: listReposMock, getRepoSiblings: getRepoSiblingsMock }
})
vi.mock('@/api/opencode', async (importOriginal) => {
const actual = await importOriginal<typeof import('@/api/opencode')>()
return { ...actual, getSession: getSessionMock }
})
vi.mock('@/hooks/useOpenCode', async (importOriginal) => {
const actual = await importOriginal<typeof import('@/hooks/useOpenCode')>()
return {
...actual,
useSessionsAcrossDirectories: (directories: string[], options?: { search?: string; limit?: number }) => {
if (directories.length > 0) sessionsHookCalls.push({ directories, options })
const data =
options?.search && emptyOnSearch.current
? []
: sessionsData.filter((session) => directories.includes(session.location.directory))
return {
data,
isLoading: false,
isError: Boolean(options?.search) && searchError.current,
hasNextPage: sessionsHasNextPage.current,
}
},
useCreateSession: (directory?: string) => ({
mutate: (vars?: unknown) => {
createSessionCalls.push({ directory, vars })
},
}),
}
})
vi.mock('@/hooks/useSessionPins', () => ({
useSessionPins: () => ({ data: sessionPinsData.current }),
}))
vi.mock('@/hooks/useOpenCodeServers', () => ({
useOpenCodeServers: () => ({ servers: [], isLoading: false }),
useOpenCodeServerStatuses: () => ({ statuses: [], statusById: {}, isLoading: false }),
}))
vi.mock('@/contexts/EventContext', () => ({
usePermissions: () => ({
hasForSession: (sessionID: string) => permissionSessions.current.has(sessionID),
}),
useForms: () => ({
hasForSession: (sessionID: string) => formSessions.current.has(sessionID),
}),
}))
vi.mock('@/hooks/useActiveWork', () => ({
useRepoActivityState: () => null,
}))
function createRepo(overrides: Partial<Repo> & { id: number; fullPath: string }): Repo {
return {
localPath: `repos/${overrides.id}`,
defaultBranch: 'main',
cloneStatus: 'ready',
clonedAt: 0,
...overrides,
}
}
function createSession(id: string, directory: string, updated = Date.now()): SessionFixture {
return {
id,
projectID: 'proj-1',
title: `Session ${id}`,
time: { created: updated - 10000, updated },
location: { directory },
cost: 0,
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
}
}
const repoA = createRepo({ id: 1, fullPath: '/repos/a', name: 'Alpha', lastAccessedAt: 20 })
const repoB = createRepo({ id: 2, fullPath: '/repos/b', name: 'Beta', lastAccessedAt: 10 })
const repoC = createRepo({ id: 3, fullPath: '/repos/c', name: 'Gamma', lastAccessedAt: 30 })
function LocationDisplay() {
const location = useLocation()
return <div data-testid="location">{location.pathname}</div>
}
function createWrapper(initialEntries: string[]) {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
})
return ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={queryClient}>
<TooltipProvider>
<MemoryRouter initialEntries={initialEntries}>
{children}
<LocationDisplay />
</MemoryRouter>
</TooltipProvider>
</QueryClientProvider>
)
}
const toggleFor = (name: string) => screen.getByRole('button', { name: `Show sessions in ${name}` })
const fetchedDirectories = () => new Set(sessionsHookCalls.flatMap((call) => call.directories))
describe('DesktopSessionTree', () => {
beforeEach(() => {
localStorage.clear()
useSidebarServerStore.setState({ serverId: LOCAL_OPENCODE_SERVER_ID })
listReposMock.mockReset()
listReposMock.mockResolvedValue([repoA, repoB, repoC])
getRepoSiblingsMock.mockReset()
getRepoSiblingsMock.mockResolvedValue([])
sessionsData.splice(
0,
sessionsData.length,
createSession('a1', '/repos/a'),
createSession('b1', '/repos/b'),
createSession('c1', '/repos/c'),
)
sessionsHookCalls.splice(0, sessionsHookCalls.length)
createSessionCalls.splice(0, createSessionCalls.length)
permissionSessions.current = new Set()
formSessions.current = new Set()
emptyOnSearch.current = false
getSessionMock.mockReset()
getSessionMock.mockResolvedValue(undefined)
sessionPinsData.current = []
sessionsHasNextPage.current = false
searchError.current = false
})
it('lists every repo by last access, even inside a repo, with no Recent section', async () => {
render(<DesktopSessionTree />, { wrapper: createWrapper(['/repos/1']) })
await screen.findByText('Alpha')
const names = screen
.getAllByRole('button', { name: /^Show sessions in / })
.map((button) => button.getAttribute('aria-label'))
expect(names).toEqual(['Show sessions in Gamma', 'Show sessions in Alpha', 'Show sessions in Beta'])
expect(screen.queryByText('Recent')).toBeNull()
})
it('opens the active repo by default and lazily fetches only its sessions', async () => {
render(<DesktopSessionTree />, { wrapper: createWrapper(['/repos/1']) })
expect(await screen.findByRole('button', { name: /^Session a1/ })).toBeTruthy()
expect(toggleFor('Alpha')).toHaveAttribute('aria-expanded', 'true')
expect(toggleFor('Beta')).toHaveAttribute('aria-expanded', 'false')
expect(screen.queryByRole('button', { name: /^Session b1/ })).toBeNull()
expect(fetchedDirectories()).toEqual(new Set(['/repos/a']))
})
it('shows a worktree session under its repo with the worktree branch tag', async () => {
getRepoSiblingsMock.mockResolvedValue([
{ fullPath: '/repos/a/.worktrees/feat', currentBranch: 'feat/x', worktreeSource: 'git' },
])
sessionsData.push(createSession('w1', '/repos/a/.worktrees/feat', Date.now() - 1000))
render(<DesktopSessionTree />, { wrapper: createWrapper(['/repos/1']) })
const row = await screen.findByRole('button', { name: /^Session w1/ })
expect(row).toHaveTextContent('feat/x')
})
it('opens nothing outside a repo', async () => {
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
await screen.findByText('Alpha')
expect(screen.queryByRole('button', { name: /^Session / })).toBeNull()
expect(sessionsHookCalls).toHaveLength(0)
})
it('keeps only one repo open at a time', async () => {
const user = userEvent.setup()
render(<DesktopSessionTree />, { wrapper: createWrapper(['/repos/1']) })
await screen.findByRole('button', { name: /^Session a1/ })
await user.click(toggleFor('Beta'))
expect(toggleFor('Alpha')).toHaveAttribute('aria-expanded', 'false')
expect(toggleFor('Beta')).toHaveAttribute('aria-expanded', 'true')
expect(screen.getByRole('button', { name: /^Session b1/ })).toBeTruthy()
expect(screen.queryByRole('button', { name: /^Session a1/ })).toBeNull()
await user.click(toggleFor('Beta'))
expect(toggleFor('Beta')).toHaveAttribute('aria-expanded', 'false')
expect(screen.queryByRole('button', { name: /^Session / })).toBeNull()
})
it('opens the repo page when the repo name is clicked', async () => {
const user = userEvent.setup()
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
await user.click(await screen.findByRole('button', { name: 'Beta' }))
expect(screen.getByTestId('location').textContent).toBe('/repos/2')
})
it('navigates to a session and marks it current', async () => {
const user = userEvent.setup()
sessionsData.push(createSession('a2', '/repos/a', Date.now() - 1000))
render(<DesktopSessionTree />, { wrapper: createWrapper(['/repos/1/sessions/a1']) })
expect(await screen.findByRole('button', { name: /^Session a1/ })).toHaveAttribute('aria-current', 'page')
await user.click(screen.getByRole('button', { name: /^Session a2/ }))
expect(screen.getByTestId('location').textContent).toBe('/repos/1/sessions/a2')
expect(screen.getByRole('button', { name: /^Session a2/ })).toHaveAttribute('aria-current', 'page')
expect(screen.getByRole('button', { name: /^Session a1/ })).not.toHaveAttribute('aria-current')
})
it('offers All sessions when there is a next page', async () => {
const user = userEvent.setup()
sessionsHasNextPage.current = true
render(<DesktopSessionTree />, { wrapper: createWrapper(['/repos/1/sessions/a1']) })
await user.click(await screen.findByText('All sessions'))
expect(screen.getByTestId('location').textContent).toBe('/repos/1')
})
it('hides All sessions when there is no next page', async () => {
render(<DesktopSessionTree />, { wrapper: createWrapper(['/repos/1']) })
await screen.findByRole('button', { name: /^Session a1/ })
expect(screen.queryByText('All sessions')).toBeNull()
})
it('shows a pinned session that is outside the first page', async () => {
sessionsData.splice(
0,
sessionsData.length,
createSession('a1', '/repos/a', Date.now()),
createSession('a2', '/repos/a', Date.now() - 1000),
)
sessionPinsData.current = [{ sessionId: 'old', directory: '/repos/a', pinnedAt: 1 }]
getSessionMock.mockImplementation((id: string) =>
Promise.resolve(createSession(id, '/repos/a', Date.now() - 100000)),
)
render(<DesktopSessionTree />, { wrapper: createWrapper(['/repos/1']) })
await screen.findByRole('button', { name: /Session old/ })
const rows = screen.getAllByRole('button', { name: /Session / })
expect(rows[0]).toHaveTextContent('Session old')
expect(rows[0].querySelector('[aria-label="Pinned"]')).not.toBeNull()
expect(getSessionMock).toHaveBeenCalledWith('old', 'local')
})
it('keeps a non-ready repo navigable without actions or session requests', async () => {
const user = userEvent.setup()
const notReady = createRepo({ id: 4, fullPath: '/repos/d', name: 'Delta', cloneStatus: 'cloning' })
listReposMock.mockResolvedValue([repoA, notReady])
render(<DesktopSessionTree />, { wrapper: createWrapper(['/repos/4']) })
await screen.findByText('Delta')
expect(toggleFor('Delta')).toHaveAttribute('aria-expanded', 'true')
expect(screen.queryByRole('button', { name: 'New session in Delta' })).toBeNull()
expect(await screen.findByText('Repository not ready')).toBeTruthy()
expect(fetchedDirectories()).not.toContain('/repos/d')
await user.click(screen.getByRole('button', { name: 'Delta' }))
expect(screen.getByTestId('location').textContent).toBe('/repos/4')
})
it('searches as you type across ready repos and clears with Escape', async () => {
const user = userEvent.setup()
listReposMock.mockResolvedValue([repoA, repoB, createRepo({ id: 4, fullPath: '/repos/d', name: 'Delta', cloneStatus: 'cloning' })])
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
const input = await screen.findByLabelText('Search sessions')
await screen.findByText('Alpha')
await user.type(input, 'Session')
expect(await screen.findByRole('button', { name: /^Session a1/ })).toBeTruthy()
await waitFor(() =>
expect(sessionsHookCalls.some((call) => call.options?.search === 'Session')).toBe(true),
)
const searchCall = sessionsHookCalls.find((call) => call.options?.search === 'Session')
expect(searchCall?.directories).toEqual(['/repos/a', '/repos/b'])
expect(sessionsHookCalls.some((call) => call.options?.search === 'Sess')).toBe(false)
expect(screen.getByRole('button', { name: /^Session b1/ })).toBeTruthy()
expect(screen.queryByRole('button', { name: /^Show sessions in / })).toBeNull()
await user.type(input, '{Escape}')
expect(input).toHaveValue('')
await waitFor(() => expect(screen.queryByRole('button', { name: /^Session / })).toBeNull())
expect(toggleFor('Alpha')).toBeTruthy()
})
it('filters loaded sessions by title immediately', async () => {
const user = userEvent.setup()
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
const input = await screen.findByLabelText('Search sessions')
await screen.findByText('Alpha')
await user.type(input, 'a1')
expect(await screen.findByRole('button', { name: /^Session a1/ })).toBeTruthy()
expect(screen.queryByRole('button', { name: /^Session b1/ })).toBeNull()
expect(screen.queryByRole('button', { name: /^Session c1/ })).toBeNull()
})
it('clears an active search with the clear button', async () => {
const user = userEvent.setup()
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
const input = await screen.findByLabelText('Search sessions')
await user.type(input, 'Session')
await screen.findByRole('button', { name: /^Session a1/ })
await user.click(screen.getByLabelText('Clear search'))
expect(input).toHaveValue('')
expect(screen.queryByLabelText('Clear search')).toBeNull()
expect(toggleFor('Alpha')).toBeTruthy()
})
it('matches repo names and shows their recent sessions', async () => {
const user = userEvent.setup()
emptyOnSearch.current = true
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
const input = await screen.findByLabelText('Search sessions')
await user.type(input, 'bet')
expect(await screen.findByRole('button', { name: /^Session b1/ })).toBeTruthy()
expect(screen.queryByRole('button', { name: /^Session a1/ })).toBeNull()
await waitFor(() => expect(sessionsHookCalls.some((call) => call.options?.search === 'bet')).toBe(true))
const searchCall = sessionsHookCalls.find((call) => call.options?.search === 'bet')
expect(searchCall?.directories).toEqual(['/repos/c', '/repos/a'])
})
it('restores the full list when the input is emptied', async () => {
const user = userEvent.setup()
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
const input = await screen.findByLabelText('Search sessions')
await user.type(input, 'Session')
await screen.findByRole('button', { name: /^Session a1/ })
await user.clear(input)
await waitFor(() => expect(screen.queryByRole('button', { name: /^Session / })).toBeNull())
expect(toggleFor('Alpha')).toBeTruthy()
})
it('shows the empty search state when a search returns nothing', async () => {
const user = userEvent.setup()
emptyOnSearch.current = true
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
const input = await screen.findByLabelText('Search sessions')
await user.type(input, 'nothing')
expect(await screen.findByText('No sessions found')).toBeTruthy()
})
it('shows the error state when the session search fails', async () => {
const user = userEvent.setup()
searchError.current = true
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
const input = await screen.findByLabelText('Search sessions')
await user.type(input, 'Session')
expect(await screen.findByText('Failed to load sessions')).toBeTruthy()
})
it('shows the error state when the worktree siblings lookup fails', async () => {
getRepoSiblingsMock.mockRejectedValue(new Error('siblings failed'))
render(<DesktopSessionTree />, { wrapper: createWrapper(['/repos/1']) })
expect(await screen.findByText('Failed to load sessions')).toBeTruthy()
})
it('creates a session in the repo full path', async () => {
const user = userEvent.setup()
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
await user.click(await screen.findByRole('button', { name: 'New session in Beta' }))
expect(createSessionCalls).toContainEqual({ directory: '/repos/b', vars: { agent: undefined } })
})
it('renders permission and form badges for sessions with pending requests', async () => {
sessionsData.push(createSession('a2', '/repos/a', Date.now() - 1000))
permissionSessions.current = new Set(['a1'])
formSessions.current = new Set(['a2'])
render(<DesktopSessionTree />, { wrapper: createWrapper(['/repos/1']) })
await screen.findByRole('button', { name: /^Session a1/ })
expect(screen.getByLabelText('Pending permission')).toBeTruthy()
expect(screen.getByLabelText('Pending form')).toBeTruthy()
})
})