Skip to content

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

test(chat): cover conversation rendering and chat service

Steven Enamakel8675ac5f39bf
diff --git a/app/src/pages/__tests__/Conversations.render.test.tsx b/app/src/pages/__tests__/Conversations.render.test.tsx
index 9c45d996ce..952c2ae918 100644
--- a/app/src/pages/__tests__/Conversations.render.test.tsx
+++ b/app/src/pages/__tests__/Conversations.render.test.tsx
@@ -1293,6 +1293,80 @@ describe('Conversations — smoke render (#1123 welcome-lock removal)', () => {
expect(threadApi.appendMessage).not.toHaveBeenCalled();
});
+ it('re-issues a Stop pressed before the core had registered the turn', async () => {
+ // The send RPC is still in flight, so the core has no turn yet and the
+ // first cancel finds nothing. Once the RPC returns the turn exists, and the
+ // Stop must be sent again rather than letting the turn run to completion.
+ let resolveSend: (() => void) | undefined;
+ vi.mocked(chatSend).mockImplementationOnce(
+ () =>
+ new Promise<string | undefined>(resolve => {
+ resolveSend = () => resolve('req-late');
+ })
+ );
+ vi.mocked(chatCancel)
+ .mockResolvedValueOnce({ accepted: true, turnCancelled: false })
+ .mockResolvedValueOnce({ accepted: true, turnCancelled: true });
+ const { textarea, thread } = await renderSelectedConversation();
+
+ await act(async () => {
+ setComposerText(textarea, 'stop me early');
+ });
+ await act(async () => {
+ fireEvent.click(screen.getByRole('button', { name: 'Send message' }));
+ });
+ const stopButton = await screen.findByRole('button', { name: 'Stop generating' });
+ await act(async () => {
+ fireEvent.click(stopButton);
+ });
+ await act(async () => {
+ await Promise.resolve();
+ });
+ expect(chatCancel).toHaveBeenCalledTimes(1);
+
+ await act(async () => {
+ resolveSend?.();
+ await Promise.resolve();
+ });
+
+ expect(chatCancel).toHaveBeenCalledTimes(2);
+ expect(chatCancel).toHaveBeenNthCalledWith(2, thread.id);
+ });
+
+ it.each([
+ ['the cancel event never arrives', { accepted: true, turnCancelled: true }],
+ ['the cancel RPC fails', { accepted: false, turnCancelled: false }],
+ ])('settles the running state after a Stop when %s', async (_label, outcome) => {
+ vi.mocked(chatCancel).mockResolvedValueOnce(outcome);
+ const { thread, store } = await renderStreamingConversation({
+ streamingContent: 'half a thought',
+ });
+ const stopButton = await screen.findByRole('button', { name: 'Stop generating' });
+
+ vi.useFakeTimers();
+ try {
+ await act(async () => {
+ fireEvent.click(stopButton);
+ await Promise.resolve();
+ });
+ // Still waiting on the core's confirmation.
+ expect(store.getState().chatRuntime.inferenceTurnLifecycleByThread[thread.id]).toBe(
+ 'streaming'
+ );
+
+ await act(async () => {
+ vi.advanceTimersByTime(5_000);
+ });
+ } finally {
+ vi.useRealTimers();
+ }
+
+ expect(chatCancel).toHaveBeenCalledWith(thread.id);
+ expect(store.getState().chatRuntime.inferenceTurnLifecycleByThread[thread.id]).toBeUndefined();
+ expect(store.getState().thread.activeThreadIds[thread.id]).toBeUndefined();
+ expect(screen.queryByRole('button', { name: 'Stop generating' })).toBeNull();
+ });
+
it('does not locally persist a partial across repeated Stop clicks (#4862)', async () => {
const { thread } = await renderStreamingConversation({ streamingContent: 'half a thought' });
diff --git a/app/src/services/__tests__/chatService.test.ts b/app/src/services/__tests__/chatService.test.ts
index 729b9ee1a7..7c8b574520 100644
--- a/app/src/services/__tests__/chatService.test.ts
+++ b/app/src/services/__tests__/chatService.test.ts
@@ -1,6 +1,12 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
-import { chatCancel, chatClearQueue, chatSend, subscribeChatEvents } from '../chatService';
+import {
+ chatCancel,
+ chatClearQueue,
+ chatSend,
+ NO_SOCKET_CANCEL_CLIENT_ID,
+ subscribeChatEvents,
+} from '../chatService';
import { socketService } from '../socketService';
const mockCallCoreRpc = vi.fn();
@@ -487,9 +493,16 @@ describe('chatService.chatCancel', () => {
expect(await chatCancel('thread-9')).toEqual({ accepted: false, turnCancelled: false });
});
- it('is not accepted without a socket id', async () => {
+ it('still sends the cancel when the socket has no id', async () => {
+ // A dropped or reconnecting socket is when a turn most often looks stuck;
+ // the Stop must still reach the core over RPC.
vi.mocked(socketService.getSocket).mockReturnValue(null as never);
- expect(await chatCancel('thread-9')).toEqual({ accepted: false, turnCancelled: false });
- expect(mockCallCoreRpc).not.toHaveBeenCalled();
+ mockCallCoreRpc.mockResolvedValue({ result: { cancelled: true, request_id: 'req-2' } });
+
+ expect(await chatCancel('thread-9')).toEqual({ accepted: true, turnCancelled: true });
+ expect(mockCallCoreRpc).toHaveBeenCalledWith({
+ method: 'openhuman.channel_web_cancel',
+ params: { client_id: NO_SOCKET_CANCEL_CLIENT_ID, thread_id: 'thread-9' },
+ });
});
});