diff --git a/frontend/src/api/opencode.test.ts b/frontend/src/api/opencode.test.ts index 55db0c97..99d4542e 100644 --- a/frontend/src/api/opencode.test.ts +++ b/frontend/src/api/opencode.test.ts @@ -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') diff --git a/frontend/src/api/opencode.ts b/frontend/src/api/opencode.ts index 96528a3b..22693982 100644 --- a/frontend/src/api/opencode.ts +++ b/frontend/src/api/opencode.ts @@ -241,6 +241,8 @@ export async function removeShell(id: string, directory: string): Promise } export async function stageRevert(sessionID: string, messageID: string): Promise { + await interruptSession(sessionID) + await callOpenCode((api) => api.session.wait({ sessionID })) return callOpenCode((api) => api.session.revert.stage({ sessionID, messageID })) } diff --git a/frontend/src/components/session/SessionPickerDialog.test.tsx b/frontend/src/components/session/SessionPickerDialog.test.tsx index 86a248a4..747b29aa 100644 --- a/frontend/src/components/session/SessionPickerDialog.test.tsx +++ b/frontend/src/components/session/SessionPickerDialog.test.tsx @@ -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> = {}) { const onOpenChange = vi.fn() const onSelectSession = vi.fn() - const onActiveSessionDeleted = vi.fn() + const onDeleteActiveSession = vi.fn() render( , ) - 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((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()} /> ) diff --git a/frontend/src/components/session/SessionPickerDialog.tsx b/frontend/src/components/session/SessionPickerDialog.tsx index 37fb490c..7994aa19 100644 --- a/frontend/src/components/session/SessionPickerDialog.tsx +++ b/frontend/src/components/session/SessionPickerDialog.tsx @@ -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 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(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} /> diff --git a/frontend/src/pages/SessionDetail.tsx b/frontend/src/pages/SessionDetail.tsx index 9dd33d04..9c435772 100644 --- a/frontend/src/pages/SessionDetail.tsx +++ b/frontend/src/pages/SessionDetail.tsx @@ -840,7 +840,7 @@ export function SessionDetail() { } setSessionsDialogOpen(false) }} - onActiveSessionDeleted={() => { + onDeleteActiveSession={() => { setSessionsDialogOpen(false) handleCloseSession() }}