Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(conversations): update chatCancel mock to return structured response

Steven Enamakel570698820611
diff --git a/app/src/features/conversations/Conversations.processSourceCommand.test.tsx b/app/src/features/conversations/Conversations.processSourceCommand.test.tsx
index c90fb1b1fb..a233cebfc8 100644
--- a/app/src/features/conversations/Conversations.processSourceCommand.test.tsx
+++ b/app/src/features/conversations/Conversations.processSourceCommand.test.tsx
@@ -45,7 +45,7 @@ const { mockGetThreads, mockGetThreadMessages, mockUseUsageState } = vi.hoisted(
}));
vi.mock('../../services/chatService', () => ({
- chatCancel: vi.fn().mockResolvedValue(true),
+ chatCancel: vi.fn().mockResolvedValue({ accepted: true, turnCancelled: true }),
chatClearQueue: vi.fn().mockResolvedValue(0),
chatSend: vi.fn().mockResolvedValue(undefined),
subscribeChatEvents: vi.fn(() => () => {}),
diff --git a/app/src/features/conversations/components/__tests__/WorkerThreadRefCard.test.tsx b/app/src/features/conversations/components/__tests__/WorkerThreadRefCard.test.tsx
index 93126f3ca2..8d0e2f165f 100644
--- a/app/src/features/conversations/components/__tests__/WorkerThreadRefCard.test.tsx
+++ b/app/src/features/conversations/components/__tests__/WorkerThreadRefCard.test.tsx
@@ -3,6 +3,7 @@ import { Provider } from 'react-redux';
import { describe, expect, it, vi } from 'vitest';
import { store } from '../../../../store';
+import { clearThreadInferenceActive } from '../../../../store/threadSlice';
import { WorkerThreadRefCard } from '../WorkerThreadRefCard';
// Issue #1624: the worker-thread surface card must render a live
@@ -70,19 +71,15 @@ describe('WorkerThreadRefCard — status badge', () => {
});
describe('WorkerThreadRefCard — navigation', () => {
- it('dispatches setActiveThread with the worker thread id when clicked', () => {
- const dispatch = vi.spyOn(store, 'dispatch');
+ it('selects the worker thread without marking it as in-flight', () => {
+ store.dispatch(clearThreadInferenceActive(REF.threadId));
renderInStore(<WorkerThreadRefCard ref={REF} status="running" />);
fireEvent.click(screen.getByRole('button'));
- const calls = dispatch.mock.calls;
- expect(calls.length).toBeGreaterThan(0);
- const action = calls[calls.length - 1][0] as { type: string; payload?: unknown };
- // Mirrors `setActiveThread`'s slice action: payload is the worker
- // thread id, which the Conversations page uses to swap the active
- // thread (parent → worker navigation).
- expect(action.payload).toBe('t-worker-1');
- dispatch.mockRestore();
+ expect(store.getState().thread.selectedThreadId).toBe('t-worker-1');
+ // Opening a worker must not fabricate an in-flight turn: that phantom state
+ // showed a Stop button with no turn behind it to cancel.
+ expect(store.getState().thread.activeThreadIds['t-worker-1']).toBeUndefined();
});
});
diff --git a/app/src/pages/__tests__/Conversations.auiComposerSurfaces.test.tsx b/app/src/pages/__tests__/Conversations.auiComposerSurfaces.test.tsx
index 85eff75f97..42f76be8d6 100644
--- a/app/src/pages/__tests__/Conversations.auiComposerSurfaces.test.tsx
+++ b/app/src/pages/__tests__/Conversations.auiComposerSurfaces.test.tsx
@@ -71,7 +71,7 @@ const { mockGetThreads, mockGetThreadMessages, mockUseUsageState, mockFlowApprov
// ── Module mocks ───────────────────────────────────────────────────────────
vi.mock('../../services/chatService', () => ({
- chatCancel: vi.fn().mockResolvedValue(true),
+ chatCancel: vi.fn().mockResolvedValue({ accepted: true, turnCancelled: true }),
chatClearQueue: vi.fn().mockResolvedValue(0),
chatSend: vi.fn().mockResolvedValue(undefined),
aiRegenerate: vi.fn().mockResolvedValue(undefined),
diff --git a/app/src/pages/__tests__/Conversations.render.test.tsx b/app/src/pages/__tests__/Conversations.render.test.tsx
index 7c76674c8e..ca43ed4777 100644
--- a/app/src/pages/__tests__/Conversations.render.test.tsx
+++ b/app/src/pages/__tests__/Conversations.render.test.tsx
@@ -60,7 +60,7 @@ const { mockGetThreads, mockGetThreadMessages, mockUseUsageState } = vi.hoisted(
// ── Module mocks ───────────────────────────────────────────────────────────
vi.mock('../../services/chatService', () => ({
- chatCancel: vi.fn().mockResolvedValue(true),
+ chatCancel: vi.fn().mockResolvedValue({ accepted: true, turnCancelled: true }),
chatClearQueue: vi.fn().mockResolvedValue(0),
chatSend: vi.fn().mockResolvedValue(undefined),
subscribeChatEvents: vi.fn(() => () => {}),
@@ -1218,10 +1218,10 @@ describe('Conversations — smoke render (#1123 welcome-lock removal)', () => {
});
it('does not persist a stopped reply when the cancel is rejected (#4862)', async () => {
- // Socket down / RPC rejected → chatCancel resolves false. The original turn
- // may keep running and append its own final response, so we must NOT leave a
- // misleading partial bubble behind.
- vi.mocked(chatCancel).mockResolvedValueOnce(false);
+ // Socket down / RPC rejected → chatCancel resolves not-accepted. The original
+ // turn may keep running and append its own final response, so we must NOT
+ // leave a misleading partial bubble behind.
+ vi.mocked(chatCancel).mockResolvedValueOnce({ accepted: false, turnCancelled: false });
const { thread } = await renderStreamingConversation({ streamingContent: 'half a thought' });
const stopButton = await screen.findByRole('button', { name: 'Stop generating' });
@@ -1237,6 +1237,32 @@ describe('Conversations — smoke render (#1123 welcome-lock removal)', () => {
expect(threadApi.appendMessage).not.toHaveBeenCalled();
});
+ it('settles a phantom running state when the core has no turn to cancel', async () => {
+ // The core accepted the Stop but had nothing in flight for the thread (the
+ // turn's terminal event was lost, or the marker was never a real turn). No
+ // `cancelled` chat_error will ever arrive, so the composer must leave the
+ // generating state on its own instead of offering a Stop that can't work.
+ vi.mocked(chatCancel).mockResolvedValueOnce({ accepted: true, turnCancelled: false });
+ const { thread, store } = await renderStreamingConversation({
+ streamingContent: 'half a thought',
+ });
+
+ const stopButton = await screen.findByRole('button', { name: 'Stop generating' });
+ await act(async () => {
+ fireEvent.click(stopButton);
+ });
+ await act(async () => {
+ await Promise.resolve();
+ });
+
+ expect(chatCancel).toHaveBeenCalledWith(thread.id);
+ expect(store.getState().thread.activeThreadIds[thread.id]).toBeUndefined();
+ expect(store.getState().chatRuntime.streamingAssistantByThread[thread.id]).toBeUndefined();
+ expect(screen.queryByRole('button', { name: 'Stop generating' })).toBeNull();
+ // Nothing was cancelled, so no partial is persisted as a stopped reply.
+ expect(threadApi.appendMessage).not.toHaveBeenCalled();
+ });
+
it('persists the stopped reply only once across repeated Stop clicks (#4862)', async () => {
const { thread } = await renderStreamingConversation({ streamingContent: 'half a thought' });
diff --git a/app/src/pages/__tests__/Conversations.unroutedApproval.test.tsx b/app/src/pages/__tests__/Conversations.unroutedApproval.test.tsx
index 1a92e4442f..bd49f6f2e3 100644
--- a/app/src/pages/__tests__/Conversations.unroutedApproval.test.tsx
+++ b/app/src/pages/__tests__/Conversations.unroutedApproval.test.tsx
@@ -40,7 +40,7 @@ vi.mock('../../services/chatService', async importOriginal => {
const actual = await importOriginal<typeof import('../../services/chatService')>();
return {
...actual,
- chatCancel: vi.fn().mockResolvedValue(true),
+ chatCancel: vi.fn().mockResolvedValue({ accepted: true, turnCancelled: true }),
chatClearQueue: vi.fn().mockResolvedValue(0),
chatSend: vi.fn().mockResolvedValue(undefined),
};