mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
feat: add session status store with optimistic active state and SSE polling (#272)
* feat: add session status store with optimistic active state and SSE polling - Adds sessionStatusStore with setOptimisticActive (auto-timeout), statusCache deduplication, and replaceStatuses/clearStatus - Integrates store in useSendPrompt (set optimistic on send, clear on error) - Adds session.status SSE event handling in useSSE with 5s polling fallback - Refactors test to mock the store instead of using real store instance - Fixes repoId null check in useRepoActivity * feat: replace event-source-polyfill with native SSE for PromptInput * fix: keep stop button visible and hide clear prompt button during active session * feat: restore failed queued prompts and preserve optimistic active status during poll * test: add MessageThread tests for optimistic editing state and resend flow * fix: handle queued prompt edge cases during pending submission and SSE errors
This commit is contained in:
1 parent
931f1215e6
commit
4e32fd5b00
16 files changed
+833
-61
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { renderHook } from '@testing-library/react'
|
||||
import { renderHook, waitFor } from '@testing-library/react'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { createElement } from 'react'
|
||||
import { useSendPrompt } from './useOpenCode'
|
||||
@@ -7,6 +7,8 @@ import { FetchError } from '../api/fetchWrapper'
|
||||
|
||||
const mockSendPrompt = vi.fn()
|
||||
const mockSendPromptAsync = vi.fn()
|
||||
const mockSetOptimisticActive = vi.fn()
|
||||
const mockClearStatus = vi.fn()
|
||||
|
||||
vi.mock('../api/opencode', async () => {
|
||||
const actual = await vi.importActual('../api/opencode')
|
||||
@@ -20,7 +22,12 @@ vi.mock('../api/opencode', async () => {
|
||||
})
|
||||
|
||||
vi.mock('@/stores/sessionStatusStore', () => ({
|
||||
useSessionStatus: vi.fn(() => vi.fn()),
|
||||
useSessionStatus: Object.assign(vi.fn(() => vi.fn()), {
|
||||
getState: () => ({
|
||||
setOptimisticActive: mockSetOptimisticActive,
|
||||
clearStatus: mockClearStatus,
|
||||
}),
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('../lib/toast', () => ({
|
||||
@@ -37,12 +44,18 @@ vi.mock('../lib/opencode-errors', () => ({
|
||||
|
||||
const mockClearError = vi.fn()
|
||||
const mockSetError = vi.fn()
|
||||
const mockSetQueuedPrompt = vi.fn()
|
||||
const mockClearQueuedPrompt = vi.fn()
|
||||
const mockFailQueuedPrompt = vi.fn()
|
||||
|
||||
vi.mock('../stores/sendErrorStore', () => ({
|
||||
useSendErrorStore: {
|
||||
getState: () => ({
|
||||
clearError: mockClearError,
|
||||
setError: mockSetError,
|
||||
setQueuedPrompt: mockSetQueuedPrompt,
|
||||
clearQueuedPrompt: mockClearQueuedPrompt,
|
||||
failQueuedPrompt: mockFailQueuedPrompt,
|
||||
getError: vi.fn(),
|
||||
}),
|
||||
},
|
||||
@@ -178,6 +191,63 @@ describe('useSendPrompt', () => {
|
||||
).resolves.toEqual(expect.objectContaining({ queued: true }))
|
||||
|
||||
expect(mockClearError).toHaveBeenCalledWith('session-1')
|
||||
expect(mockSetQueuedPrompt).toHaveBeenCalledWith('session-1', 'Hello')
|
||||
})
|
||||
|
||||
it('stores the raw prompt for queued restoration when parts omit file mentions', async () => {
|
||||
const { result } = renderHookWithProviders()
|
||||
|
||||
await expect(
|
||||
result.current.mutateAsync({
|
||||
sessionID: 'session-raw',
|
||||
prompt: 'please inspect @App.tsx',
|
||||
parts: [
|
||||
{ type: 'text', content: 'please inspect ' },
|
||||
{ type: 'file', path: '/repo/src/App.tsx', name: 'App.tsx' },
|
||||
],
|
||||
queued: true,
|
||||
})
|
||||
).resolves.toEqual(expect.objectContaining({ queued: true }))
|
||||
|
||||
expect(mockSetQueuedPrompt).toHaveBeenCalledWith('session-raw', 'please inspect @App.tsx')
|
||||
})
|
||||
|
||||
it('stores queued prompt before the async queue request resolves', async () => {
|
||||
let resolveQueued: () => void = () => {}
|
||||
mockSendPromptAsync.mockImplementationOnce(() => new Promise<void>((resolve) => {
|
||||
resolveQueued = resolve
|
||||
}))
|
||||
|
||||
const { result } = renderHookWithProviders()
|
||||
|
||||
const queued = result.current.mutateAsync({
|
||||
sessionID: 'session-pending',
|
||||
prompt: 'pending queued prompt',
|
||||
queued: true,
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockSetQueuedPrompt).toHaveBeenCalledWith('session-pending', 'pending queued prompt')
|
||||
})
|
||||
|
||||
resolveQueued()
|
||||
await expect(queued).resolves.toEqual(expect.objectContaining({ queued: true }))
|
||||
})
|
||||
|
||||
it('clears queued prompt when the async queue request fails', async () => {
|
||||
mockSendPromptAsync.mockRejectedValueOnce(new Error('Queue failed'))
|
||||
|
||||
const { result } = renderHookWithProviders()
|
||||
|
||||
await expect(
|
||||
result.current.mutateAsync({
|
||||
sessionID: 'session-failed',
|
||||
prompt: 'failed queued prompt',
|
||||
queued: true,
|
||||
})
|
||||
).rejects.toThrow('Queue failed')
|
||||
|
||||
expect(mockClearQueuedPrompt).toHaveBeenCalledWith('session-failed')
|
||||
})
|
||||
|
||||
it('clears stored send error on successful non-queued response', async () => {
|
||||
|
||||
Reference in new issue
Block a user