mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
* feat(session): paginated session list with infinite scroll and search Replace per-directory useQueries session listing with a combined cursor-based useInfiniteQuery (25 per page, loads more on scroll) and move search into the API query instead of client-side filtering. Add OpenCodeClient.listSessionsPage backed by the v2 /api/session endpoint plus a toLegacySession adapter. Add invalidateSessionListCaches to centralize session-list cache invalidation for infinite queries, and update EventContext and useSSE to handle the new infinite-query cache shape. * fix(session): centralize and debounce session-list cache invalidation Migrate ResetPermissionsDialog off the stale per-directory cache key (which no longer matches the infinite-query key shape and hardcoded the opencode URL) to the shared invalidateSessionListCaches helper, and drop the now-unused repoDirectory prop and its call sites. Debounce session-list invalidation on SSE session.updated events in EventContext and useSSE so a burst of updates coalesces into a single refetch instead of one per event. Fix the directory fallback in EventContext getClient so it never returns the joined directory key. * fix(permissions): scope instance dispose to the repo directory Drop the session-list cache invalidation from ResetPermissionsDialog: disposing the OpenCode instance does not change the persisted session list, so the refetch was unnecessary, and the prior cache key no longer matched the paginated shape. Guard the reset-permissions route against an empty repo directory so it never forwards /instance/dispose without a directory, which would dispose the entire server instead of only the repo's instance.
278 lines
8.1 KiB
TypeScript
278 lines
8.1 KiB
TypeScript
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { act, renderHook, waitFor } from '@testing-library/react'
|
|
import type { ReactNode } from 'react'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { useSSE } from './useSSE'
|
|
import { useSessionStatus } from '../stores/sessionStatusStore'
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
getSessionStatuses: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/api/opencode', () => ({
|
|
OpenCodeClient: vi.fn(() => ({
|
|
getSessionStatuses: mocks.getSessionStatuses,
|
|
})),
|
|
}))
|
|
|
|
vi.mock('@/api/settings', () => ({
|
|
settingsApi: {
|
|
reloadOpenCodeConfig: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
vi.mock('@/lib/toast', () => ({
|
|
showToast: {
|
|
dismiss: vi.fn(),
|
|
error: vi.fn(),
|
|
info: vi.fn(),
|
|
loading: vi.fn(),
|
|
success: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
class MockEventSource {
|
|
static instances: MockEventSource[] = []
|
|
|
|
onopen: (() => void) | null = null
|
|
onerror: (() => void) | null = null
|
|
onmessage: ((event: MessageEvent) => void) | null = null
|
|
private listeners = new Map<string, Array<(event: MessageEvent) => void>>()
|
|
|
|
constructor() {
|
|
MockEventSource.instances.push(this)
|
|
}
|
|
|
|
addEventListener(type: string, listener: (event: MessageEvent) => void) {
|
|
const listeners = this.listeners.get(type) ?? []
|
|
listeners.push(listener)
|
|
this.listeners.set(type, listeners)
|
|
}
|
|
|
|
close() {}
|
|
|
|
emit(type: string, data: unknown) {
|
|
const event = { data: JSON.stringify(data) } as MessageEvent
|
|
this.listeners.get(type)?.forEach((listener) => listener(event))
|
|
if (type === 'message' && this.onmessage) {
|
|
this.onmessage(event)
|
|
}
|
|
}
|
|
}
|
|
|
|
describe('useSSE', () => {
|
|
const originalEventSource = globalThis.EventSource
|
|
const originalFetch = globalThis.fetch
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
MockEventSource.instances = []
|
|
mocks.getSessionStatuses.mockResolvedValue({})
|
|
useSessionStatus.getState().replaceStatuses({})
|
|
globalThis.EventSource = MockEventSource as unknown as typeof EventSource
|
|
globalThis.fetch = vi.fn(() => Promise.resolve({ ok: true } as Response))
|
|
})
|
|
|
|
afterEach(() => {
|
|
useSessionStatus.getState().replaceStatuses({})
|
|
globalThis.EventSource = originalEventSource
|
|
globalThis.fetch = originalFetch
|
|
})
|
|
|
|
it('invalidates active session data after reconnecting', async () => {
|
|
const queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: { retry: false },
|
|
},
|
|
})
|
|
const invalidateQueries = vi.spyOn(queryClient, 'invalidateQueries')
|
|
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
)
|
|
|
|
const { result, unmount } = renderHook(
|
|
() => useSSE('http://localhost:5551', '/repo', 'session-1'),
|
|
{ wrapper }
|
|
)
|
|
|
|
await waitFor(() => expect(MockEventSource.instances).toHaveLength(1))
|
|
|
|
act(() => {
|
|
MockEventSource.instances[0].emit('connected', { clientId: 'client-1' })
|
|
})
|
|
|
|
await waitFor(() => expect(result.current.isConnected).toBe(true))
|
|
invalidateQueries.mockClear()
|
|
|
|
act(() => {
|
|
MockEventSource.instances[0].onerror?.()
|
|
})
|
|
|
|
await waitFor(() => expect(result.current.isConnected).toBe(false))
|
|
|
|
act(() => {
|
|
window.dispatchEvent(new Event('focus'))
|
|
})
|
|
|
|
await waitFor(() => expect(MockEventSource.instances).toHaveLength(2))
|
|
|
|
act(() => {
|
|
MockEventSource.instances[1].emit('connected', { clientId: 'client-2' })
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(invalidateQueries).toHaveBeenCalledWith({
|
|
queryKey: ['opencode', 'session', 'http://localhost:5551', 'session-1', '/repo'],
|
|
})
|
|
expect(invalidateQueries).toHaveBeenCalledWith({
|
|
queryKey: ['opencode', 'messages', 'http://localhost:5551', 'session-1', '/repo'],
|
|
})
|
|
})
|
|
|
|
unmount()
|
|
})
|
|
|
|
it('clears stale active statuses from the initial status snapshot', async () => {
|
|
const queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: { retry: false },
|
|
},
|
|
})
|
|
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
)
|
|
|
|
useSessionStatus.getState().setStatus('session-1', { type: 'busy' })
|
|
|
|
const { unmount } = renderHook(
|
|
() => useSSE('http://localhost:5551', '/repo', 'session-1'),
|
|
{ wrapper }
|
|
)
|
|
|
|
await waitFor(() => expect(MockEventSource.instances).toHaveLength(1))
|
|
|
|
act(() => {
|
|
MockEventSource.instances[0].emit('connected', { clientId: 'client-1' })
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(useSessionStatus.getState().getStatus('session-1')).toEqual({ type: 'idle' })
|
|
})
|
|
|
|
unmount()
|
|
})
|
|
|
|
it('ignores stale status snapshots after the directory changes', async () => {
|
|
const queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: { retry: false },
|
|
},
|
|
})
|
|
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
)
|
|
|
|
let resolveRepoA: (value: Record<string, { type: 'busy' }>) => void = () => {}
|
|
let resolveRepoB: (value: Record<string, { type: 'busy' }>) => void = () => {}
|
|
mocks.getSessionStatuses
|
|
.mockImplementationOnce(() => new Promise((resolve) => { resolveRepoA = resolve }))
|
|
.mockImplementationOnce(() => new Promise((resolve) => { resolveRepoB = resolve }))
|
|
|
|
const { rerender, unmount } = renderHook(
|
|
({ directory }) => useSSE('http://localhost:5551', directory, 'session-1'),
|
|
{ wrapper, initialProps: { directory: '/repo-a' } }
|
|
)
|
|
|
|
await waitFor(() => expect(MockEventSource.instances).toHaveLength(1))
|
|
|
|
act(() => {
|
|
MockEventSource.instances[0].emit('connected', { clientId: 'client-1' })
|
|
})
|
|
|
|
rerender({ directory: '/repo-b' })
|
|
|
|
await waitFor(() => expect(MockEventSource.instances).toHaveLength(2))
|
|
|
|
act(() => {
|
|
MockEventSource.instances[1].emit('connected', { clientId: 'client-2' })
|
|
})
|
|
|
|
await act(async () => {
|
|
resolveRepoB({ 'session-b': { type: 'busy' } })
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(useSessionStatus.getState().getStatus('session-b')).toEqual({ type: 'busy' })
|
|
})
|
|
|
|
await act(async () => {
|
|
resolveRepoA({ 'session-a': { type: 'busy' } })
|
|
})
|
|
|
|
expect(useSessionStatus.getState().getStatus('session-b')).toEqual({ type: 'busy' })
|
|
expect(useSessionStatus.getState().getStatus('session-a')).toEqual({ type: 'idle' })
|
|
|
|
unmount()
|
|
})
|
|
|
|
it('sets single-session cache and invalidates session list on session.updated', async () => {
|
|
const queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: { retry: false },
|
|
},
|
|
})
|
|
const invalidateQueriesSpy = vi.spyOn(queryClient, 'invalidateQueries')
|
|
const setQueryDataSpy = vi.spyOn(queryClient, 'setQueryData')
|
|
|
|
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
)
|
|
|
|
const { result, unmount } = renderHook(
|
|
() => useSSE('http://localhost:5551', '/repo', 'session-1'),
|
|
{ wrapper }
|
|
)
|
|
|
|
await waitFor(() => expect(MockEventSource.instances).toHaveLength(1))
|
|
|
|
act(() => {
|
|
MockEventSource.instances[0].emit('connected', { clientId: 'client-1' })
|
|
})
|
|
|
|
await waitFor(() => expect(result.current.isConnected).toBe(true))
|
|
|
|
// Clear initial connection-related calls
|
|
invalidateQueriesSpy.mockClear()
|
|
setQueryDataSpy.mockClear()
|
|
|
|
const sessionData = {
|
|
id: 'session-2',
|
|
projectID: 'proj-1',
|
|
title: 'Updated Session',
|
|
time: { created: 1000, updated: 2000 },
|
|
}
|
|
|
|
act(() => {
|
|
MockEventSource.instances[0].emit('message', {
|
|
type: 'session.updated',
|
|
properties: { info: sessionData },
|
|
})
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(setQueryDataSpy).toHaveBeenCalledWith(
|
|
['opencode', 'session', 'http://localhost:5551', 'session-2', '/repo'],
|
|
sessionData,
|
|
)
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(invalidateQueriesSpy).toHaveBeenCalledWith(
|
|
expect.objectContaining({ predicate: expect.any(Function) }),
|
|
)
|
|
})
|
|
|
|
unmount()
|
|
})
|
|
})
|