mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
fix(sessions): settle session before revert stage and notify active-session delete eagerly
This commit is contained in:
1 parent
2d14972fad
commit
7bc71d7123
5 files changed
+54
-29
No files matched your search
@@ -555,15 +555,27 @@ describe('OpenCode facade', () => {
|
||||
})
|
||||
|
||||
it('stages, commits, and clears a session revert', async () => {
|
||||
fetchMock.mockResolvedValue(jsonResponse({ data: { messageID: 'msg_1' } }))
|
||||
fetchMock.mockImplementation((input: RequestInfo | URL) => {
|
||||
const url = String(input)
|
||||
if (url.endsWith('/interrupt')) return Promise.resolve(jsonResponse({ data: { interrupted: true } }))
|
||||
if (url.endsWith('/wait')) return Promise.resolve(new Response(null, { status: 204 }))
|
||||
if (url.endsWith('/revert/stage')) return Promise.resolve(jsonResponse({ data: { messageID: 'msg_1' } }))
|
||||
return Promise.resolve(new Response(null, { status: 204 }))
|
||||
})
|
||||
|
||||
await stageRevert('ses_1', 'msg_1')
|
||||
|
||||
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/revert/stage')
|
||||
expect(lastRequest().init.method).toBe('POST')
|
||||
expect(lastRequest().init.body).toBe(JSON.stringify({ messageID: 'msg_1' }))
|
||||
|
||||
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
|
||||
const stageRequests = fetchMock.mock.calls.map(([input, init]) => ({
|
||||
url: String(input),
|
||||
init: init as RequestInit,
|
||||
}))
|
||||
expect(stageRequests.map((request) => request.url)).toEqual([
|
||||
'http://localhost/api/opencode/api/session/ses_1/interrupt',
|
||||
'http://localhost/api/opencode/api/experimental/session/ses_1/wait',
|
||||
'http://localhost/api/opencode/api/session/ses_1/revert/stage',
|
||||
])
|
||||
expect(stageRequests.map((request) => request.init.method)).toEqual(['POST', 'POST', 'POST'])
|
||||
expect(stageRequests[2].init.body).toBe(JSON.stringify({ messageID: 'msg_1' }))
|
||||
|
||||
await commitRevert('ses_1')
|
||||
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/revert/commit')
|
||||
|
||||
@@ -241,6 +241,8 @@ export async function removeShell(id: string, directory: string): Promise<void>
|
||||
}
|
||||
|
||||
export async function stageRevert(sessionID: string, messageID: string): Promise<SessionRevert> {
|
||||
await interruptSession(sessionID)
|
||||
await callOpenCode((api) => api.session.wait({ sessionID }))
|
||||
return callOpenCode((api) => api.session.revert.stage({ sessionID, messageID }))
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { useState } from 'react'
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
|
||||
import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import type { Repo, Session } from '@/api/types'
|
||||
import { stubMatchMedia } from '@/test/test-utils'
|
||||
@@ -88,18 +88,18 @@ function repo(id: number, name: string, fullPath: string): Repo {
|
||||
function renderPicker(overrides: Partial<React.ComponentProps<typeof SessionPickerDialog>> = {}) {
|
||||
const onOpenChange = vi.fn()
|
||||
const onSelectSession = vi.fn()
|
||||
const onActiveSessionDeleted = vi.fn()
|
||||
const onDeleteActiveSession = vi.fn()
|
||||
render(
|
||||
<SessionPickerDialog
|
||||
open
|
||||
onOpenChange={onOpenChange}
|
||||
currentRepo={mocks.repos[0]}
|
||||
onSelectSession={onSelectSession}
|
||||
onActiveSessionDeleted={onActiveSessionDeleted}
|
||||
onDeleteActiveSession={onDeleteActiveSession}
|
||||
{...overrides}
|
||||
/>,
|
||||
)
|
||||
return { onOpenChange, onSelectSession, onActiveSessionDeleted }
|
||||
return { onOpenChange, onSelectSession, onDeleteActiveSession }
|
||||
}
|
||||
|
||||
const combobox = () => screen.getByRole('combobox')
|
||||
@@ -225,7 +225,7 @@ describe('SessionPickerDialog', () => {
|
||||
onOpenChange={setOpen}
|
||||
currentRepo={mocks.repos[0]}
|
||||
onSelectSession={vi.fn()}
|
||||
onActiveSessionDeleted={vi.fn()}
|
||||
onDeleteActiveSession={vi.fn()}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
@@ -245,7 +245,7 @@ describe('SessionPickerDialog', () => {
|
||||
session('ses_1', 'First', '/w/a', 3000),
|
||||
session('ses_2', 'Second', '/w/a', 2000),
|
||||
]
|
||||
renderPicker()
|
||||
const { onDeleteActiveSession } = renderPicker()
|
||||
|
||||
fireEvent.keyDown(combobox(), { key: 'd', ctrlKey: true })
|
||||
expect(screen.getByText('Press Ctrl+D again to confirm')).toBeInTheDocument()
|
||||
@@ -259,20 +259,32 @@ describe('SessionPickerDialog', () => {
|
||||
|
||||
fireEvent.keyDown(combobox(), { key: 'd', ctrlKey: true })
|
||||
expect(mocks.deleteSessionMock).toHaveBeenCalledWith({ id: 'ses_2', directory: '/w/a' })
|
||||
expect(onDeleteActiveSession).not.toHaveBeenCalled()
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.queryByText('Press Ctrl+D again to confirm')).not.toBeInTheDocument(),
|
||||
)
|
||||
})
|
||||
|
||||
it('notifies when the active session is deleted', async () => {
|
||||
it('notifies when the active session is deleted before the delete completes', async () => {
|
||||
mocks.sessionsData = [session('ses_active', 'Active', '/w/a', 3000)]
|
||||
const { onActiveSessionDeleted } = renderPicker({ activeSessionID: 'ses_active' })
|
||||
let resolveDelete: () => void = () => undefined
|
||||
mocks.deleteSessionMock.mockReturnValue(
|
||||
new Promise<void>((resolve) => {
|
||||
resolveDelete = resolve
|
||||
}),
|
||||
)
|
||||
const { onDeleteActiveSession } = renderPicker({ activeSessionID: 'ses_active' })
|
||||
|
||||
fireEvent.keyDown(combobox(), { key: 'd', ctrlKey: true })
|
||||
fireEvent.keyDown(combobox(), { key: 'd', ctrlKey: true })
|
||||
|
||||
await waitFor(() => expect(onActiveSessionDeleted).toHaveBeenCalled())
|
||||
expect(mocks.deleteSessionMock).toHaveBeenCalledWith({ id: 'ses_active', directory: '/w/a' })
|
||||
await waitFor(() => expect(onDeleteActiveSession).toHaveBeenCalled())
|
||||
|
||||
await act(async () => {
|
||||
resolveDelete()
|
||||
})
|
||||
})
|
||||
|
||||
it('moves the cursor when a mouse pointer moves over a row', () => {
|
||||
@@ -331,7 +343,7 @@ describe('SessionPickerDialog', () => {
|
||||
currentRepo={mocks.repos[0]}
|
||||
activeSessionID="ses_active"
|
||||
onSelectSession={vi.fn()}
|
||||
onActiveSessionDeleted={vi.fn()}
|
||||
onDeleteActiveSession={vi.fn()}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
|
||||
@@ -36,7 +36,7 @@ interface SessionPickerDialogProps {
|
||||
currentRepo: Repo | undefined
|
||||
activeSessionID?: string
|
||||
onSelectSession: (session: Session, repo: Repo) => void
|
||||
onActiveSessionDeleted: () => void
|
||||
onDeleteActiveSession: () => void
|
||||
}
|
||||
|
||||
interface SessionPickerRow {
|
||||
@@ -227,7 +227,7 @@ interface SessionPickerContentProps {
|
||||
currentRepo: Repo | undefined
|
||||
activeSessionID?: string
|
||||
onSelectSession: (session: Session, repo: Repo) => void
|
||||
onActiveSessionDeleted: () => void
|
||||
onDeleteActiveSession: () => void
|
||||
searchInputRef: React.RefObject<HTMLInputElement | null>
|
||||
hasFinePointer: boolean
|
||||
}
|
||||
@@ -237,7 +237,7 @@ function SessionPickerContent({
|
||||
currentRepo,
|
||||
activeSessionID,
|
||||
onSelectSession,
|
||||
onActiveSessionDeleted,
|
||||
onDeleteActiveSession,
|
||||
searchInputRef,
|
||||
hasFinePointer,
|
||||
}: SessionPickerContentProps) {
|
||||
@@ -349,14 +349,13 @@ function SessionPickerContent({
|
||||
)
|
||||
|
||||
const performDelete = useCallback(
|
||||
(session: Session) =>
|
||||
deleteSession
|
||||
(session: Session) => {
|
||||
if (session.id === activeSessionID) onDeleteActiveSession()
|
||||
return deleteSession
|
||||
.mutateAsync({ id: session.id, directory: session.location.directory })
|
||||
.then(() => {
|
||||
if (session.id === activeSessionID) onActiveSessionDeleted()
|
||||
})
|
||||
.catch(() => undefined),
|
||||
[deleteSession, activeSessionID, onActiveSessionDeleted],
|
||||
.catch(() => undefined)
|
||||
},
|
||||
[deleteSession, activeSessionID, onDeleteActiveSession],
|
||||
)
|
||||
|
||||
const deleteRow = useCallback(
|
||||
@@ -621,7 +620,7 @@ export function SessionPickerDialog({
|
||||
currentRepo,
|
||||
activeSessionID,
|
||||
onSelectSession,
|
||||
onActiveSessionDeleted,
|
||||
onDeleteActiveSession,
|
||||
}: SessionPickerDialogProps) {
|
||||
const searchInputRef = useRef<HTMLInputElement>(null)
|
||||
const hasFinePointer = useMediaQuery(FINE_POINTER_MEDIA_QUERY)
|
||||
@@ -646,7 +645,7 @@ export function SessionPickerDialog({
|
||||
currentRepo={currentRepo}
|
||||
activeSessionID={activeSessionID}
|
||||
onSelectSession={onSelectSession}
|
||||
onActiveSessionDeleted={onActiveSessionDeleted}
|
||||
onDeleteActiveSession={onDeleteActiveSession}
|
||||
searchInputRef={searchInputRef}
|
||||
hasFinePointer={hasFinePointer}
|
||||
/>
|
||||
|
||||
@@ -840,7 +840,7 @@ export function SessionDetail() {
|
||||
}
|
||||
setSessionsDialogOpen(false)
|
||||
}}
|
||||
onActiveSessionDeleted={() => {
|
||||
onDeleteActiveSession={() => {
|
||||
setSessionsDialogOpen(false)
|
||||
handleCloseSession()
|
||||
}}
|
||||
|
||||
Reference in new issue
Block a user