import { describe, expect, it, vi } from "vitest"; import { createChatStreamHandlers } from "../createChatStreamHandlers"; describe("createChatStreamHandlers", () => { it("preserves whitespace across streamed text deltas", () => { vi.useFakeTimers(); let text = ""; const onDone = vi.fn(); const onError = vi.fn(); const cancelStreamingFlushesRef = { current: null } as { current: (() => void) | null }; const { handlers } = createChatStreamHandlers({ sessionId: "s-1", tempUserMessageId: "temp-1", setStreamingText: (value) => { text = typeof value === "function" ? value(text) : value; }, setStreamingThinking: vi.fn(), setStreamingToolCalls: vi.fn(), cancelStreamingFlushesRef, onDone, onError, }); handlers.onText("Hello."); handlers.onText(""); handlers.onText(" World."); vi.advanceTimersToNextTimer(); expect(text).toBe("Hello. World."); handlers.onDone({ messageId: "m-1" }); expect(onDone).toHaveBeenCalledWith({ messageId: "m-1", message: undefined, accumulated: { text: "Hello. World.", thinking: "", toolCalls: [], fallbackInfo: undefined, }, }); expect(onError).not.toHaveBeenCalled(); vi.useRealTimers(); }); });