mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
feat(session): paginated session list with infinite scroll and assistant mode improvements (#243)
* 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.
This commit is contained in:
1 parent
78d934772e
commit
eedbbfb94e
17 files changed
+931
-116
No files matched your search
@@ -1,8 +1,8 @@
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { act, renderHook } from '@testing-library/react'
|
||||
import { act, renderHook, waitFor } from '@testing-library/react'
|
||||
import type { ReactNode } from 'react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { useDeleteSession } from './useOpenCode'
|
||||
import { useDeleteSession, useSessionsAcrossDirectories } from './useOpenCode'
|
||||
|
||||
vi.mock('../lib/toast', () => ({
|
||||
showToast: {
|
||||
@@ -82,3 +82,185 @@ describe('useDeleteSession', () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('useSessionsAcrossDirectories', () => {
|
||||
const fetchMock = vi.fn()
|
||||
|
||||
beforeEach(() => {
|
||||
fetchMock.mockReset()
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
it('fetches first page of sessions with v2 pagination and adapted items', async () => {
|
||||
fetchMock.mockResolvedValueOnce(
|
||||
new Response(JSON.stringify({
|
||||
items: [
|
||||
{ id: 'session-1', projectID: 'proj-1', title: 'Test Session', time: { created: 1000, updated: 1000 } },
|
||||
],
|
||||
cursor: { next: 'cursor_abc' },
|
||||
}), { headers: { 'Content-Type': 'application/json' } }),
|
||||
)
|
||||
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } })
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
)
|
||||
|
||||
const { result } = renderHook(() => useSessionsAcrossDirectories('/api/opencode', ['/repo']), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
expect(result.current.data).toHaveLength(1)
|
||||
expect(result.current.data[0].id).toBe('session-1')
|
||||
expect(result.current.data[0].title).toBe('Test Session')
|
||||
expect(result.current.hasNextPage).toBe(true)
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1)
|
||||
expect(fetchMock).toHaveBeenCalledWith(
|
||||
'http://localhost/api/opencode/api/session?limit=25&order=desc&directory=%2Frepo',
|
||||
expect.objectContaining({ credentials: 'include' }),
|
||||
)
|
||||
})
|
||||
|
||||
it('fetches next page via cursor when fetchNextPage is called and flattens items', async () => {
|
||||
fetchMock
|
||||
.mockResolvedValueOnce(
|
||||
new Response(JSON.stringify({
|
||||
items: [
|
||||
{ id: 'session-1', projectID: 'proj-1', title: 'Page 1', time: { created: 1000, updated: 1000 } },
|
||||
],
|
||||
cursor: { next: 'cursor_next' },
|
||||
}), { headers: { 'Content-Type': 'application/json' } }),
|
||||
)
|
||||
.mockResolvedValueOnce(
|
||||
new Response(JSON.stringify({
|
||||
items: [
|
||||
{ id: 'session-2', projectID: 'proj-1', title: 'Page 2', time: { created: 2000, updated: 2000 } },
|
||||
],
|
||||
}), { headers: { 'Content-Type': 'application/json' } }),
|
||||
)
|
||||
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } })
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
)
|
||||
|
||||
const { result } = renderHook(() => useSessionsAcrossDirectories('/api/opencode', ['/repo']), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
expect(result.current.data).toHaveLength(1)
|
||||
expect(result.current.data[0].id).toBe('session-1')
|
||||
|
||||
await act(async () => {
|
||||
result.current.fetchNextPage()
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.data).toHaveLength(2)
|
||||
})
|
||||
|
||||
expect(result.current.data[0].id).toBe('session-1')
|
||||
expect(result.current.data[1].id).toBe('session-2')
|
||||
expect(fetchMock).toHaveBeenCalledTimes(2)
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'http://localhost/api/opencode/api/session?cursor=cursor_next',
|
||||
expect.objectContaining({ credentials: 'include' }),
|
||||
)
|
||||
})
|
||||
|
||||
it('handles multi-directory cursors and only fetches directories with nextCursor', async () => {
|
||||
fetchMock
|
||||
.mockResolvedValueOnce(
|
||||
new Response(JSON.stringify({
|
||||
items: [
|
||||
{ id: 'session-a1', projectID: 'proj-1', title: 'Session A1', time: { created: 1000, updated: 1000 } },
|
||||
],
|
||||
cursor: { next: 'cursor_a' },
|
||||
}), { headers: { 'Content-Type': 'application/json' } }),
|
||||
)
|
||||
.mockResolvedValueOnce(
|
||||
new Response(JSON.stringify({
|
||||
items: [
|
||||
{ id: 'session-b1', projectID: 'proj-1', title: 'Session B1', time: { created: 1000, updated: 1000 } },
|
||||
],
|
||||
}), { headers: { 'Content-Type': 'application/json' } }),
|
||||
)
|
||||
.mockResolvedValueOnce(
|
||||
new Response(JSON.stringify({
|
||||
items: [
|
||||
{ id: 'session-a2', projectID: 'proj-1', title: 'Session A2', time: { created: 2000, updated: 2000 } },
|
||||
],
|
||||
}), { headers: { 'Content-Type': 'application/json' } }),
|
||||
)
|
||||
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } })
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
)
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useSessionsAcrossDirectories('/api/opencode', ['/w/a', '/w/b']),
|
||||
{ wrapper },
|
||||
)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
expect(result.current.data).toHaveLength(2)
|
||||
expect(result.current.hasNextPage).toBe(true)
|
||||
|
||||
await act(async () => {
|
||||
result.current.fetchNextPage()
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.data).toHaveLength(3)
|
||||
})
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledTimes(3)
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(
|
||||
3,
|
||||
'http://localhost/api/opencode/api/session?cursor=cursor_a',
|
||||
expect.objectContaining({ credentials: 'include' }),
|
||||
)
|
||||
})
|
||||
|
||||
it('sends search parameter when search option is provided', async () => {
|
||||
fetchMock.mockResolvedValueOnce(
|
||||
new Response(JSON.stringify({
|
||||
items: [
|
||||
{ id: 'session-1', projectID: 'proj-1', title: 'Deploy Session', time: { created: 1000, updated: 1000 } },
|
||||
],
|
||||
}), { headers: { 'Content-Type': 'application/json' } }),
|
||||
)
|
||||
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } })
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
)
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useSessionsAcrossDirectories('/api/opencode', ['/repo'], { search: 'deploy' }),
|
||||
{ wrapper },
|
||||
)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledWith(
|
||||
'http://localhost/api/opencode/api/session?limit=25&order=desc&search=deploy&directory=%2Frepo',
|
||||
expect.objectContaining({ credentials: 'include' }),
|
||||
)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user