The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
test(chat): pin the three outcomes reconnect recovery reads\n\n`subscribeThread`'s resolved value decides whether the runtime re-reads a\nthread and whether it keeps that thread queued for the next connection, and\nthe provider tests mock the module out entirely — so nothing covered the ack,\nthe bounded wait for a core that never acknowledges, or the disconnected case\nthat must leave the thread queued rather than emitting.\n
Shanue1bac3733a20
diff --git a/app/src/services/__tests__/socketService.subscribeThread.test.ts b/app/src/services/__tests__/socketService.subscribeThread.test.tsnew file mode 100644index 0000000000..c85173f7c6--- /dev/null+++ b/app/src/services/__tests__/socketService.subscribeThread.test.ts@@ -0,0 +1,84 @@+import { afterEach, describe, expect, it, vi } from 'vitest';++import { socketService } from '../socketService';++/**+ * `subscribeThread` is what orders the reconnect recovery in #6034: the chat+ * runtime re-reads a thread only after the room join is acknowledged, and it+ * keeps a thread queued for the next connection when the join never happened.+ * Both of those decisions read this function's resolved value, so the three+ * outcomes are pinned here rather than through the provider (which mocks this+ * module out entirely).+ */++type FakeSocket = { connected: boolean; emit: ReturnType<typeof vi.fn> };++/** Install a stand-in for the private socket the singleton holds. */+function withSocket(socket: FakeSocket | null) {+ (socketService as unknown as { socket: FakeSocket | null }).socket = socket;+}++afterEach(() => {+ withSocket(null);+ vi.useRealTimers();+});++describe('socketService.subscribeThread (#6034)', () => {+ it('resolves true once the server acknowledges the room join', async () => {+ const emit = vi.fn((_event: string, _payload: unknown, ack: () => void) => ack());+ withSocket({ connected: true, emit });++ await expect(socketService.subscribeThread('t-1')).resolves.toBe(true);+ expect(emit).toHaveBeenCalledWith(+ 'thread:subscribe',+ { thread_id: 't-1' },+ expect.any(Function)+ );+ });++ it('resolves false without emitting when the socket is not connected', async () => {+ const emit = vi.fn();+ withSocket({ connected: false, emit });++ // The caller keeps the thread queued for the next connection on a false —+ // clearing it here is what stranded a thread whose socket dropped again.+ await expect(socketService.subscribeThread('t-2')).resolves.toBe(false);+ expect(emit).not.toHaveBeenCalled();+ });++ it('resolves false when the acknowledgement never arrives', async () => {+ vi.useFakeTimers();+ // A core without the ack handler never calls back. Recovery must still+ // proceed rather than hang, so the wait is bounded.+ const emit = vi.fn();+ withSocket({ connected: true, emit });++ const pending = socketService.subscribeThread('t-3', 50);+ await vi.advanceTimersByTimeAsync(60);+ await expect(pending).resolves.toBe(false);+ expect(emit).toHaveBeenCalledTimes(1);+ });++ it('resolves false for an empty thread id and sends nothing', async () => {+ const emit = vi.fn();+ withSocket({ connected: true, emit });++ await expect(socketService.subscribeThread('')).resolves.toBe(false);+ expect(emit).not.toHaveBeenCalled();+ });++ it('does not resolve twice when the ack lands after the timeout', async () => {+ vi.useFakeTimers();+ let late: (() => void) | undefined;+ const emit = vi.fn((_event: string, _payload: unknown, ack: () => void) => {+ late = ack;+ });+ withSocket({ connected: true, emit });++ const pending = socketService.subscribeThread('t-4', 50);+ await vi.advanceTimersByTimeAsync(60);+ late?.();++ await expect(pending).resolves.toBe(false);+ });+});