mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
* fix: stabilize scheduled runs and background task tracking * fix: complete scheduled runs and harden background task status - Route directory-less OpenCode session events (session.execution.*, session.usage.updated) to backend listeners by remembering each session's directory in the SSE aggregator, so scheduled runs finish and failure push notifications fire; browser delivery is unchanged. - Resolve background shell status from the transcript completion notice when OpenCode no longer lists the shell, keep the first terminal status so a kill is not reported as completed, and share one status icon and colour mapping between the transcript and background bar. - Reconcile child sessions only while unknown or running, trigger it from the EventProvider alone, refresh children that drop to unknown after a poll, and forget deleted sessions. - Fetch the model list and default concurrently while waiting for the schedule model, share configured-model selection, and consolidate schedule startup cancellation cleanup. - Reduce background work re-renders, prune shell exit records, remove dead exports, and document background work in the chat guide. * fix: hide finished background tasks and clip long dialog titles
162 lines
5.0 KiB
TypeScript
162 lines
5.0 KiB
TypeScript
import { act, renderHook, waitFor } from '@testing-library/react'
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import type { ReactNode } from 'react'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { useKillShell, useShell } from './useSessionShells'
|
|
import { applyShellExit, clearShellExitRecord, recordShellExit, type ShellRecord } from '@/lib/backgroundWork'
|
|
|
|
const api = vi.hoisted(() => ({
|
|
listShells: vi.fn(),
|
|
removeShell: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/api/opencode', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('@/api/opencode')>()),
|
|
...api,
|
|
}))
|
|
|
|
vi.mock('@/lib/toast', () => ({
|
|
showToast: { error: vi.fn(), success: vi.fn(), info: vi.fn(), loading: vi.fn() },
|
|
}))
|
|
|
|
const shell = (id: string, sessionID = 'session-1'): ShellRecord => ({
|
|
id,
|
|
status: 'running',
|
|
command: `npm run ${id}`,
|
|
cwd: '/repo',
|
|
shell: 'zsh',
|
|
file: `/tmp/${id}.log`,
|
|
metadata: { sessionID },
|
|
time: { started: 1 },
|
|
})
|
|
|
|
const createWrapper = (queryClient: QueryClient) => {
|
|
return ({ children }: { children: ReactNode }) => (
|
|
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
)
|
|
}
|
|
|
|
const createQueryClient = () => new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
|
|
|
describe('useKillShell', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
api.listShells.mockResolvedValue([])
|
|
api.removeShell.mockResolvedValue(undefined)
|
|
})
|
|
|
|
it('marks a killed shell terminal without erasing it', async () => {
|
|
const queryClient = createQueryClient()
|
|
queryClient.setQueryData(['opencode', 'shells', '/repo'], [shell('dev')])
|
|
|
|
const { result } = renderHook(() => useKillShell('/repo'), {
|
|
wrapper: createWrapper(queryClient),
|
|
})
|
|
|
|
await act(async () => {
|
|
await result.current.mutateAsync('dev')
|
|
})
|
|
|
|
const [updated] = queryClient.getQueryData<ShellRecord[]>(['opencode', 'shells', '/repo']) ?? []
|
|
expect(updated?.status).toBe('killed')
|
|
expect(updated?.time.completed).toBeDefined()
|
|
clearShellExitRecord('/repo', 'dev')
|
|
})
|
|
|
|
it('records a killed shell so a later refetch keeps it killed', async () => {
|
|
const queryClient = createQueryClient()
|
|
const key = ['opencode', 'shells', '/repo']
|
|
queryClient.setQueryData(key, [shell('dev')])
|
|
|
|
const { result } = renderHook(
|
|
() => ({ kill: useKillShell('/repo'), shell: useShell('dev', '/repo') }),
|
|
{ wrapper: createWrapper(queryClient) },
|
|
)
|
|
|
|
await act(async () => {
|
|
await result.current.kill.mutateAsync('dev')
|
|
})
|
|
|
|
queryClient.setQueryData(key, [])
|
|
api.listShells.mockResolvedValue([shell('dev')])
|
|
|
|
await act(async () => {
|
|
await queryClient.invalidateQueries({ queryKey: key })
|
|
})
|
|
|
|
const [refetched] = queryClient.getQueryData<ShellRecord[]>(key) ?? []
|
|
expect(refetched?.status).toBe('killed')
|
|
clearShellExitRecord('/repo', 'dev')
|
|
})
|
|
|
|
it('keeps a killed shell killed through a later exit event', async () => {
|
|
const queryClient = createQueryClient()
|
|
const key = ['opencode', 'shells', '/repo']
|
|
queryClient.setQueryData(key, [shell('dev')])
|
|
|
|
const { result } = renderHook(() => useKillShell('/repo'), {
|
|
wrapper: createWrapper(queryClient),
|
|
})
|
|
|
|
await act(async () => {
|
|
await result.current.mutateAsync('dev')
|
|
})
|
|
|
|
await act(async () => {
|
|
recordShellExit('/repo', { id: 'dev', status: 'exited', exit: 0 })
|
|
queryClient.setQueryData<ShellRecord[]>(key, (current) =>
|
|
current ? applyShellExit(current, { id: 'dev', status: 'exited', exit: 0 }) : current,
|
|
)
|
|
})
|
|
|
|
const [after] = queryClient.getQueryData<ShellRecord[]>(key) ?? []
|
|
expect(after?.status).toBe('killed')
|
|
clearShellExitRecord('/repo', 'dev')
|
|
})
|
|
})
|
|
|
|
describe('useShell', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
api.listShells.mockResolvedValue([])
|
|
api.removeShell.mockResolvedValue(undefined)
|
|
})
|
|
|
|
it('returns only the matching shell record once the list loads', async () => {
|
|
const queryClient = createQueryClient()
|
|
api.listShells.mockResolvedValue([shell('dev'), shell('other')])
|
|
|
|
const { result } = renderHook(() => useShell('dev', '/repo'), {
|
|
wrapper: createWrapper(queryClient),
|
|
})
|
|
|
|
await waitFor(() => expect(result.current.shell?.id).toBe('dev'))
|
|
expect(result.current.listLoaded).toBe(true)
|
|
})
|
|
|
|
it('does not re-render when an unrelated shell record changes', async () => {
|
|
const queryClient = createQueryClient()
|
|
const dev = shell('dev')
|
|
const other = shell('other')
|
|
api.listShells.mockResolvedValue([dev, other])
|
|
|
|
let renderCount = 0
|
|
const { result } = renderHook(
|
|
() => {
|
|
renderCount += 1
|
|
return useShell('dev', '/repo')
|
|
},
|
|
{ wrapper: createWrapper(queryClient) },
|
|
)
|
|
|
|
await waitFor(() => expect(result.current.shell).toBe(dev))
|
|
const rendersAfterLoad = renderCount
|
|
|
|
await act(async () => {
|
|
queryClient.setQueryData(['opencode', 'shells', '/repo'], [dev, { ...other, command: 'changed' }])
|
|
})
|
|
|
|
expect(renderCount).toBe(rendersAfterLoad)
|
|
})
|
|
})
|