mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
feat: add LSP server status display UI with dialog list (#154)
* feat: add LSP server status display Co-authored-by: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com>
This commit is contained in:
1 parent
5746033fde
commit
a77d9e0371
11 files changed
+743
-2
No files matched your search
@@ -13,8 +13,10 @@ type ShellRequest = NonNullable<paths['/session/{sessionID}/shell']['post']['req
|
||||
type AgentListResponse = paths['/agent']['get']['responses']['200']['content']['application/json']
|
||||
type QuestionListResponse = paths['/question']['get']['responses']['200']['content']['application/json']
|
||||
type SendPromptResponse = paths['/session/{sessionID}/message']['post']['responses']['200']['content']['application/json']
|
||||
type LspStatusResponse = paths['/lsp']['get']['responses']['200']['content']['application/json']
|
||||
type LspStatus = LspStatusResponse[number]
|
||||
|
||||
export type { SendPromptResponse }
|
||||
export type { SendPromptResponse, LspStatus }
|
||||
|
||||
export class OpenCodeClient {
|
||||
private baseURL: string
|
||||
@@ -121,6 +123,12 @@ export class OpenCodeClient {
|
||||
})
|
||||
}
|
||||
|
||||
async getLSPStatus() {
|
||||
return fetchWrapper<LspStatusResponse>(`${this.baseURL}/lsp`, {
|
||||
params: this.getParams(),
|
||||
})
|
||||
}
|
||||
|
||||
async updateConfig(config: Partial<ConfigResponse>) {
|
||||
return fetchWrapper<ConfigResponse>(`${this.baseURL}/config`, {
|
||||
method: 'PATCH',
|
||||
|
||||
@@ -179,6 +179,11 @@ export interface SSESessionErrorEvent {
|
||||
}
|
||||
}
|
||||
|
||||
export interface SSELspUpdatedEvent {
|
||||
type: 'lsp.updated'
|
||||
properties: Record<string, never>
|
||||
}
|
||||
|
||||
export interface SSESSHHostKeyRequestEvent {
|
||||
type: 'ssh.host-key-request'
|
||||
properties: SSHHostKeyRequest
|
||||
@@ -203,6 +208,7 @@ export type SSEEvent =
|
||||
| SSEQuestionRejectedEvent
|
||||
| SSEInstallationUpdatedEvent
|
||||
| SSEInstallationUpdateAvailableEvent
|
||||
| SSELspUpdatedEvent
|
||||
| SSESSHHostKeyRequestEvent
|
||||
|
||||
export type ContentPart =
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { render, screen, fireEvent } from '@testing-library/react'
|
||||
import { LspStatusButton } from './LspStatusButton'
|
||||
import type { LspStatus } from '@/api/opencode'
|
||||
import { useLSPStatus } from '@/hooks/useLSPStatus'
|
||||
|
||||
vi.mock('@/hooks/useLSPStatus')
|
||||
|
||||
describe('LspStatusButton', () => {
|
||||
const mockOnClick = vi.fn()
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('should render button with LSP text', () => {
|
||||
vi.mocked(useLSPStatus).mockReturnValue({ data: undefined } as any)
|
||||
|
||||
render(<LspStatusButton opcodeUrl="http://localhost:5551" directory="/test" onClick={mockOnClick} />)
|
||||
|
||||
expect(screen.getByText('LSP')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should call onClick when clicked', () => {
|
||||
vi.mocked(useLSPStatus).mockReturnValue({ data: undefined } as any)
|
||||
|
||||
render(<LspStatusButton opcodeUrl="http://localhost:5551" directory="/test" onClick={mockOnClick} />)
|
||||
|
||||
const button = screen.getByRole('button')
|
||||
fireEvent.click(button)
|
||||
|
||||
expect(mockOnClick).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('should show green icon when has active servers', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'typescript-language-server', status: 'connected', root: '/project' }
|
||||
]
|
||||
vi.mocked(useLSPStatus).mockReturnValue({ data: mockData } as any)
|
||||
|
||||
const { container } = render(
|
||||
<LspStatusButton opcodeUrl="http://localhost:5551" directory="/test" onClick={mockOnClick} />
|
||||
)
|
||||
|
||||
const icon = container.querySelector('.text-green-500')
|
||||
expect(icon).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should show default icon when no active servers', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'typescript-language-server', status: 'error', root: '/project' }
|
||||
]
|
||||
vi.mocked(useLSPStatus).mockReturnValue({ data: mockData } as any)
|
||||
|
||||
const { container } = render(
|
||||
<LspStatusButton opcodeUrl="http://localhost:5551" directory="/test" onClick={mockOnClick} />
|
||||
)
|
||||
|
||||
const icon = container.querySelector('.text-green-500')
|
||||
expect(icon).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should show default icon when data is empty', () => {
|
||||
vi.mocked(useLSPStatus).mockReturnValue({ data: [] } as any)
|
||||
|
||||
const { container } = render(
|
||||
<LspStatusButton opcodeUrl="http://localhost:5551" directory="/test" onClick={mockOnClick} />
|
||||
)
|
||||
|
||||
const icon = container.querySelector('.text-green-500')
|
||||
expect(icon).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should show default icon when data is undefined', () => {
|
||||
vi.mocked(useLSPStatus).mockReturnValue({ data: undefined } as any)
|
||||
|
||||
const { container } = render(
|
||||
<LspStatusButton opcodeUrl="http://localhost:5551" directory="/test" onClick={mockOnClick} />
|
||||
)
|
||||
|
||||
const icon = container.querySelector('.text-green-500')
|
||||
expect(icon).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should handle multiple servers with mixed statuses', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'typescript-language-server', status: 'connected', root: '/project' },
|
||||
{ id: '2', name: 'python-lsp-server', status: 'error', root: '/project' }
|
||||
]
|
||||
vi.mocked(useLSPStatus).mockReturnValue({ data: mockData } as any)
|
||||
|
||||
const { container } = render(
|
||||
<LspStatusButton opcodeUrl="http://localhost:5551" directory="/test" onClick={mockOnClick} />
|
||||
)
|
||||
|
||||
const icon = container.querySelector('.text-green-500')
|
||||
expect(icon).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should hide on mobile screens (hidden md:flex)', () => {
|
||||
vi.mocked(useLSPStatus).mockReturnValue({ data: undefined } as any)
|
||||
|
||||
const { container } = render(
|
||||
<LspStatusButton opcodeUrl="http://localhost:5551" directory="/test" onClick={mockOnClick} />
|
||||
)
|
||||
|
||||
const button = container.querySelector('button')
|
||||
expect(button).toHaveClass('hidden')
|
||||
expect(button).toHaveClass('md:flex')
|
||||
})
|
||||
|
||||
it('should apply correct variant and size classes', () => {
|
||||
vi.mocked(useLSPStatus).mockReturnValue({ data: undefined } as any)
|
||||
|
||||
const { container } = render(
|
||||
<LspStatusButton opcodeUrl="http://localhost:5551" directory="/test" onClick={mockOnClick} />
|
||||
)
|
||||
|
||||
const button = container.querySelector('button')
|
||||
expect(button).toHaveClass('border')
|
||||
expect(button).toHaveClass('bg-background')
|
||||
})
|
||||
|
||||
it('should apply transition and hover classes', () => {
|
||||
vi.mocked(useLSPStatus).mockReturnValue({ data: undefined } as any)
|
||||
|
||||
const { container } = render(
|
||||
<LspStatusButton opcodeUrl="http://localhost:5551" directory="/test" onClick={mockOnClick} />
|
||||
)
|
||||
|
||||
const button = container.querySelector('button')
|
||||
expect(button).toHaveClass('transition-all')
|
||||
expect(button).toHaveClass('duration-200')
|
||||
expect(button).toHaveClass('hover:scale-105')
|
||||
})
|
||||
|
||||
it('should render Code icon', () => {
|
||||
vi.mocked(useLSPStatus).mockReturnValue({ data: undefined } as any)
|
||||
|
||||
const { container } = render(
|
||||
<LspStatusButton opcodeUrl="http://localhost:5551" directory="/test" onClick={mockOnClick} />
|
||||
)
|
||||
|
||||
const svg = container.querySelector('svg')
|
||||
expect(svg).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should hide LSP text on small screens', () => {
|
||||
vi.mocked(useLSPStatus).mockReturnValue({ data: undefined } as any)
|
||||
|
||||
const { container } = render(
|
||||
<LspStatusButton opcodeUrl="http://localhost:5551" directory="/test" onClick={mockOnClick} />
|
||||
)
|
||||
|
||||
const textSpan = container.querySelector('.hidden.sm\\:inline')
|
||||
expect(textSpan).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,27 @@
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Code } from 'lucide-react'
|
||||
import { useLSPStatus } from '@/hooks/useLSPStatus'
|
||||
|
||||
interface LspStatusButtonProps {
|
||||
opcodeUrl: string | null | undefined
|
||||
directory?: string
|
||||
onClick: () => void
|
||||
}
|
||||
|
||||
export function LspStatusButton({ opcodeUrl, directory, onClick }: LspStatusButtonProps) {
|
||||
const { data } = useLSPStatus(opcodeUrl, directory)
|
||||
|
||||
const hasActiveServers = data && data.length > 0 && data.some(server => server.status === 'connected')
|
||||
|
||||
return (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={onClick}
|
||||
className="hidden md:flex text-foreground border-border hover:bg-accent transition-all duration-200 hover:scale-105"
|
||||
>
|
||||
<Code className={`w-4 h-4 sm:mr-2 ${hasActiveServers ? 'text-green-500' : ''}`} />
|
||||
<span className="hidden sm:inline">LSP</span>
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
||||
import { RepoLspServerList } from './RepoLspServerList'
|
||||
import { useLSPStatus } from '@/hooks/useLSPStatus'
|
||||
|
||||
interface RepoLspDialogProps {
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
opcodeUrl: string | null | undefined
|
||||
directory?: string
|
||||
}
|
||||
|
||||
export function RepoLspDialog({ open, onOpenChange, opcodeUrl, directory }: RepoLspDialogProps) {
|
||||
const { isLoading, data } = useLSPStatus(opcodeUrl, directory)
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="sm:max-w-[600px] w-full">
|
||||
<DialogHeader>
|
||||
<DialogTitle>LSP Servers</DialogTitle>
|
||||
</DialogHeader>
|
||||
<RepoLspServerList isLoading={isLoading} data={data} />
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import { RepoLspServerList } from './RepoLspServerList'
|
||||
import type { LspStatus } from '@/api/opencode'
|
||||
|
||||
describe('RepoLspServerList', () => {
|
||||
describe('loading state', () => {
|
||||
it('should show loading spinner when isLoading is true', () => {
|
||||
const { container } = render(<RepoLspServerList isLoading={true} data={undefined} />)
|
||||
|
||||
expect(screen.getByText('Loading...')).toBeInTheDocument()
|
||||
expect(container.querySelector('.animate-spin')).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
describe('empty state', () => {
|
||||
it('should show empty message when no servers', () => {
|
||||
render(<RepoLspServerList isLoading={false} data={[]} />)
|
||||
|
||||
expect(screen.getByText('No LSP servers active')).toBeInTheDocument()
|
||||
expect(screen.getByText(/LSP servers will activate automatically/)).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should show empty message when data is undefined', () => {
|
||||
render(<RepoLspServerList isLoading={false} data={undefined} />)
|
||||
|
||||
expect(screen.getByText('No LSP servers active')).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
describe('server list', () => {
|
||||
it('should render all servers', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'typescript-language-server', status: 'connected', root: '/project' },
|
||||
{ id: '2', name: 'python-lsp-server', status: 'error', root: '/project/src' }
|
||||
]
|
||||
|
||||
render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
expect(screen.getByText('Typescript language server')).toBeInTheDocument()
|
||||
expect(screen.getByText('Python lsp server')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should format server names correctly with hyphens', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'typescript-language-server', status: 'connected', root: '/project' }
|
||||
]
|
||||
|
||||
render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
expect(screen.getByText('Typescript language server')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should format server names correctly with underscores', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'python_lsp_server', status: 'connected', root: '/project' }
|
||||
]
|
||||
|
||||
render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
expect(screen.getByText('Python lsp server')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should capitalize first letter of server name', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'rust-analyzer', status: 'connected', root: '/project' }
|
||||
]
|
||||
|
||||
render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
expect(screen.getByText('Rust analyzer')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should show Active badge for connected servers', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'typescript-language-server', status: 'connected', root: '/project' }
|
||||
]
|
||||
|
||||
render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
expect(screen.getByText('Active')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should show Error badge for error servers', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'typescript-language-server', status: 'error', root: '/project' }
|
||||
]
|
||||
|
||||
render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
expect(screen.getByText('Error')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should display server root path', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'typescript-language-server', status: 'connected', root: '/project/src' }
|
||||
]
|
||||
|
||||
render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
expect(screen.getByText('/project/src')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should handle mixed status servers', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'typescript-language-server', status: 'connected', root: '/project' },
|
||||
{ id: '2', name: 'python-lsp-server', status: 'error', root: '/project/src' },
|
||||
{ id: '3', name: 'rust-analyzer', status: 'connected', root: '/project/core' }
|
||||
]
|
||||
|
||||
render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
const activeBadges = screen.getAllByText('Active')
|
||||
const errorBadges = screen.getAllByText('Error')
|
||||
|
||||
expect(activeBadges).toHaveLength(2)
|
||||
expect(errorBadges).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('should truncate long server names', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'very-long-server-name-that-should-be-truncated', status: 'connected', root: '/project' }
|
||||
]
|
||||
|
||||
const { container } = render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
const nameElement = container.querySelector('.truncate')
|
||||
expect(nameElement).toHaveClass('truncate')
|
||||
})
|
||||
|
||||
it('should truncate long root paths', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'typescript-language-server', status: 'connected', root: '/very/long/path/that/should/be/truncated/in/the/ui' }
|
||||
]
|
||||
|
||||
const { container } = render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
const pathElements = container.querySelectorAll('.truncate')
|
||||
expect(pathElements.length).toBeGreaterThan(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('formatServerName utility', () => {
|
||||
it('should convert hyphen-separated names to spaced format', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'my-custom-language-server', status: 'connected', root: '/project' }
|
||||
]
|
||||
|
||||
render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
expect(screen.getByText('My custom language server')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should convert underscore-separated names to spaced format', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'my_custom_language_server', status: 'connected', root: '/project' }
|
||||
]
|
||||
|
||||
render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
expect(screen.getByText('My custom language server')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should handle mixed separators', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'my-custom_language-server', status: 'connected', root: '/project' }
|
||||
]
|
||||
|
||||
render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
expect(screen.getByText('My custom language server')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should capitalize first letter', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'server', status: 'connected', root: '/project' }
|
||||
]
|
||||
|
||||
render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
expect(screen.getByText('Server')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('should handle single word names', () => {
|
||||
const mockData: LspStatus[] = [
|
||||
{ id: '1', name: 'analyzer', status: 'connected', root: '/project' }
|
||||
]
|
||||
|
||||
render(<RepoLspServerList isLoading={false} data={mockData} />)
|
||||
|
||||
expect(screen.getByText('Analyzer')).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,77 @@
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { CheckCircle2, AlertCircle, Code, Loader2 } from 'lucide-react'
|
||||
import type { LspStatus } from '@/api/opencode'
|
||||
|
||||
interface RepoLspServerListProps {
|
||||
isLoading: boolean
|
||||
data: LspStatus[] | undefined
|
||||
}
|
||||
|
||||
export function RepoLspServerList({ isLoading, data }: RepoLspServerListProps) {
|
||||
const formatServerName = (name: string): string => {
|
||||
const formatted = name.replace(/[-_]/g, ' ')
|
||||
return formatted.charAt(0).toUpperCase() + formatted.slice(1)
|
||||
}
|
||||
|
||||
const getStatusBadge = (status: 'connected' | 'error') => {
|
||||
if (status === 'connected') {
|
||||
return (
|
||||
<Badge variant="default" className="text-xs bg-green-600">
|
||||
<CheckCircle2 className="h-3 w-3 mr-1" />
|
||||
Active
|
||||
</Badge>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<Badge variant="destructive" className="text-xs">
|
||||
<AlertCircle className="h-3 w-3 mr-1" />
|
||||
Error
|
||||
</Badge>
|
||||
)
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center py-8">
|
||||
<Loader2 className="w-4 h-4 animate-spin text-blue-600 dark:text-blue-400" />
|
||||
<span className="ml-2 text-sm text-muted-foreground">Loading...</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (!data || data.length === 0) {
|
||||
return (
|
||||
<div className="text-center py-6 text-muted-foreground">
|
||||
<Code className="w-10 h-10 mx-auto mb-3 opacity-50" />
|
||||
<p className="text-sm">No LSP servers active</p>
|
||||
<p className="text-xs mt-1">LSP servers will activate automatically when you open files</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="px-4 sm:px-6 py-3 sm:py-4 flex-1 overflow-y-auto min-h-0">
|
||||
<div className="space-y-3">
|
||||
{data.map((server) => (
|
||||
<div
|
||||
key={server.id}
|
||||
className="flex items-center justify-between gap-3 p-3 rounded-lg border border-border bg-card"
|
||||
>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<p className="text-sm font-medium truncate">
|
||||
{formatServerName(server.name)}
|
||||
</p>
|
||||
{getStatusBadge(server.status)}
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground truncate">
|
||||
{server.root}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -386,6 +386,11 @@ export function EventProvider({ children }: { children: React.ReactNode }) {
|
||||
})
|
||||
}
|
||||
break
|
||||
case 'lsp.updated':
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['opencode', 'lsp']
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { renderHook, waitFor } from '@testing-library/react'
|
||||
import { useLSPStatus } from './useLSPStatus'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import type { LspStatus } from '@/api/opencode'
|
||||
import { useOpenCodeClient } from './useOpenCode'
|
||||
|
||||
vi.mock('./useOpenCode')
|
||||
|
||||
const mockGetLSPStatus = vi.fn()
|
||||
let mockClient: any
|
||||
|
||||
const createWrapper = () => {
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
mutations: { retry: false }
|
||||
}
|
||||
})
|
||||
return ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
)
|
||||
}
|
||||
|
||||
describe('useLSPStatus', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockClient = {
|
||||
getLSPStatus: mockGetLSPStatus
|
||||
}
|
||||
})
|
||||
|
||||
it('should not fetch when client is not available', async () => {
|
||||
vi.mocked(useOpenCodeClient).mockReturnValue(null)
|
||||
|
||||
const { result } = renderHook(() => useLSPStatus(null, '/test'), {
|
||||
wrapper: createWrapper()
|
||||
})
|
||||
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
expect(mockGetLSPStatus).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should fetch LSP status when client is available', async () => {
|
||||
const mockServers: LspStatus[] = [
|
||||
{ id: '1', name: 'typescript-language-server', status: 'connected', root: '/project' }
|
||||
]
|
||||
mockGetLSPStatus.mockResolvedValue(mockServers)
|
||||
vi.mocked(useOpenCodeClient).mockReturnValue(mockClient)
|
||||
|
||||
const { result } = renderHook(() => useLSPStatus('http://localhost:5551', '/test'), {
|
||||
wrapper: createWrapper()
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true)
|
||||
})
|
||||
|
||||
expect(mockGetLSPStatus).toHaveBeenCalledTimes(1)
|
||||
expect(result.current.data).toEqual(mockServers)
|
||||
})
|
||||
|
||||
it('should use correct query key', async () => {
|
||||
mockGetLSPStatus.mockResolvedValue([])
|
||||
vi.mocked(useOpenCodeClient).mockReturnValue(mockClient)
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } }
|
||||
})
|
||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
)
|
||||
|
||||
const { result } = renderHook(() => useLSPStatus('http://localhost:5551', '/test'), {
|
||||
wrapper
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true)
|
||||
})
|
||||
|
||||
const queryState = queryClient.getQueryState(['opencode', 'lsp', 'http://localhost:5551', '/test'])
|
||||
expect(queryState).toBeTruthy()
|
||||
expect(queryState?.status).toBe('success')
|
||||
})
|
||||
|
||||
it('should return empty array when no servers active', async () => {
|
||||
mockGetLSPStatus.mockResolvedValue([])
|
||||
vi.mocked(useOpenCodeClient).mockReturnValue(mockClient)
|
||||
|
||||
const { result } = renderHook(() => useLSPStatus('http://localhost:5551', '/test'), {
|
||||
wrapper: createWrapper()
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true)
|
||||
})
|
||||
|
||||
expect(result.current.data).toEqual([])
|
||||
})
|
||||
|
||||
it('should return server list with mixed statuses', async () => {
|
||||
const mockServers: LspStatus[] = [
|
||||
{ id: '1', name: 'typescript-language-server', status: 'connected', root: '/project' },
|
||||
{ id: '2', name: 'python-lsp-server', status: 'error', root: '/project' },
|
||||
{ id: '3', name: 'rust-analyzer', status: 'connected', root: '/project/src' }
|
||||
]
|
||||
mockGetLSPStatus.mockResolvedValue(mockServers)
|
||||
vi.mocked(useOpenCodeClient).mockReturnValue(mockClient)
|
||||
|
||||
const { result } = renderHook(() => useLSPStatus('http://localhost:5551', '/test'), {
|
||||
wrapper: createWrapper()
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true)
|
||||
})
|
||||
|
||||
expect(result.current.data).toHaveLength(3)
|
||||
expect(result.current.data?.[0].status).toBe('connected')
|
||||
expect(result.current.data?.[1].status).toBe('error')
|
||||
expect(result.current.data?.[2].status).toBe('connected')
|
||||
})
|
||||
|
||||
it('should handle API errors', async () => {
|
||||
mockGetLSPStatus.mockRejectedValue(new Error('Failed to fetch LSP status'))
|
||||
vi.mocked(useOpenCodeClient).mockReturnValue(mockClient)
|
||||
|
||||
const { result } = renderHook(() => useLSPStatus('http://localhost:5551', '/test'), {
|
||||
wrapper: createWrapper()
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isError).toBe(true)
|
||||
})
|
||||
|
||||
expect(result.current.error).toBeTruthy()
|
||||
})
|
||||
|
||||
it('should have 30s refetch interval', async () => {
|
||||
mockGetLSPStatus.mockResolvedValue([])
|
||||
vi.mocked(useOpenCodeClient).mockReturnValue(mockClient)
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } }
|
||||
})
|
||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
)
|
||||
|
||||
const { result } = renderHook(() => useLSPStatus('http://localhost:5551', '/test'), {
|
||||
wrapper
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true)
|
||||
})
|
||||
|
||||
const observer = queryClient.getQueryCache().find({ queryKey: ['opencode', 'lsp', 'http://localhost:5551', '/test'] })
|
||||
expect((observer?.options as any).refetchInterval).toBe(30000)
|
||||
})
|
||||
|
||||
it('should have 10s stale time', async () => {
|
||||
mockGetLSPStatus.mockResolvedValue([])
|
||||
vi.mocked(useOpenCodeClient).mockReturnValue(mockClient)
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } }
|
||||
})
|
||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
)
|
||||
|
||||
const { result } = renderHook(() => useLSPStatus('http://localhost:5551', '/test'), {
|
||||
wrapper
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true)
|
||||
})
|
||||
|
||||
const observer = queryClient.getQueryCache().find({ queryKey: ['opencode', 'lsp', 'http://localhost:5551', '/test'] })
|
||||
expect((observer?.options as any).staleTime).toBe(10000)
|
||||
})
|
||||
|
||||
it('should refetch on window focus', async () => {
|
||||
mockGetLSPStatus.mockResolvedValue([])
|
||||
vi.mocked(useOpenCodeClient).mockReturnValue(mockClient)
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } }
|
||||
})
|
||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
)
|
||||
|
||||
const { result } = renderHook(() => useLSPStatus('http://localhost:5551', '/test'), {
|
||||
wrapper
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true)
|
||||
})
|
||||
|
||||
const observer = queryClient.getQueryCache().find({ queryKey: ['opencode', 'lsp', 'http://localhost:5551', '/test'] })
|
||||
expect((observer?.options as any).refetchOnWindowFocus).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,15 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { useOpenCodeClient } from './useOpenCode'
|
||||
|
||||
export function useLSPStatus(opcodeUrl: string | null | undefined, directory?: string) {
|
||||
const client = useOpenCodeClient(opcodeUrl, directory)
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['opencode', 'lsp', opcodeUrl, directory],
|
||||
queryFn: () => client!.getLSPStatus(),
|
||||
enabled: !!client,
|
||||
refetchInterval: 30000,
|
||||
staleTime: 10000,
|
||||
refetchOnWindowFocus: true,
|
||||
})
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import { useQuery } from "@tanstack/react-query";
|
||||
import { getRepo } from "@/api/repos";
|
||||
import { MessageThread } from "@/components/message/MessageThread";
|
||||
import { PromptInput, type PromptInputHandle } from "@/components/message/PromptInput";
|
||||
import { X, VolumeX, FolderOpen, Plug, Settings, CornerUpLeft, GitCommitHorizontal, Brain, ShieldOff } from "lucide-react";
|
||||
import { X, VolumeX, FolderOpen, Plug, Settings, CornerUpLeft, GitCommitHorizontal, Brain, ShieldOff, Code } from "lucide-react";
|
||||
import { ModelSelectDialog } from "@/components/model/ModelSelectDialog";
|
||||
import { Header } from "@/components/ui/header";
|
||||
import { SessionList } from "@/components/session/SessionList";
|
||||
@@ -34,6 +34,8 @@ import { showToast } from "@/lib/toast";
|
||||
import { getRepoDisplayName } from "@/lib/utils";
|
||||
import { RepoMcpDialog } from "@/components/repo/RepoMcpDialog";
|
||||
import { ResetPermissionsDialog } from "@/components/repo/ResetPermissionsDialog";
|
||||
import { LspStatusButton } from "@/components/repo/LspStatusButton";
|
||||
import { RepoLspDialog } from "@/components/repo/RepoLspDialog";
|
||||
import { createOpenCodeClient } from "@/api/opencode";
|
||||
import { useSessionStatus, useSessionStatusForSession } from "@/stores/sessionStatusStore";
|
||||
import { useQuestions } from "@/contexts/EventContext";
|
||||
@@ -60,6 +62,7 @@ export function SessionDetail() {
|
||||
const [modelDialogOpen, setModelDialogOpen] = useState(false);
|
||||
const [sessionsDialogOpen, setSessionsDialogOpen] = useState(false);
|
||||
const [fileBrowserOpen, setFileBrowserOpen] = useState(false);
|
||||
const [lspDialogOpen, setLspDialogOpen] = useState(false);
|
||||
const [mcpDialogOpen, setMcpDialogOpen] = useState(false);
|
||||
const [sourceControlOpen, setSourceControlOpen] = useState(false);
|
||||
const [resetPermissionsOpen, setResetPermissionsOpen] = useState(false);
|
||||
@@ -376,6 +379,11 @@ export function SessionDetail() {
|
||||
<FolderOpen className="w-4 h-4 sm:mr-2" />
|
||||
<span className="hidden sm:inline">Files</span>
|
||||
</Button>
|
||||
<LspStatusButton
|
||||
opcodeUrl={opcodeUrl}
|
||||
directory={repoDirectory}
|
||||
onClick={() => setLspDialogOpen(true)}
|
||||
/>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
@@ -431,6 +439,9 @@ export function SessionDetail() {
|
||||
<DropdownMenuItem onClick={() => setMcpDialogOpen(true)}>
|
||||
<Plug className="w-4 h-4 mr-2" /> MCP
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => setLspDialogOpen(true)}>
|
||||
<Code className="w-4 h-4 mr-2" /> LSP
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => setFileBrowserOpen(true)}>
|
||||
<FolderOpen className="w-4 h-4 mr-2" /> Files
|
||||
</DropdownMenuItem>
|
||||
@@ -566,6 +577,13 @@ export function SessionDetail() {
|
||||
initialSelectedFile={selectedFilePath}
|
||||
/>
|
||||
|
||||
<RepoLspDialog
|
||||
open={lspDialogOpen}
|
||||
onOpenChange={setLspDialogOpen}
|
||||
opcodeUrl={opcodeUrl}
|
||||
directory={repoDirectory}
|
||||
/>
|
||||
|
||||
<RepoMcpDialog
|
||||
open={mcpDialogOpen}
|
||||
onOpenChange={setMcpDialogOpen}
|
||||
|
||||
Reference in new issue
Block a user