deer-flow/frontend/tests/unit/core/threads/stream-throttle.test.ts
Wu JiaCheng e317f7b8d9
perf(streaming): batch main thread stream updates (#4410)
* perf(streaming): batch stream updates

* docs(streaming): clarify throttle runtime default

---------

Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
2026-07-29 14:10:51 +08:00

63 lines
1.6 KiB
TypeScript

import { expect, rs, test } from "@rstest/core";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { I18nContext } from "@/core/i18n/context";
import { DEFAULT_LOCAL_SETTINGS } from "@/core/settings/local";
const streamMockState = rs.hoisted(() => ({
options: undefined as { throttle?: boolean | number } | undefined,
}));
rs.mock("@langchain/langgraph-sdk/react", () => ({
useStream: (options: { throttle?: boolean | number }) => {
streamMockState.options = options;
return {
isLoading: false,
messages: [],
stop: async () => undefined,
submit: async () => undefined,
values: {
artifacts: [],
messages: [],
title: "",
todos: [],
},
};
},
}));
test("batches stream subscription updates in a macrotask", async () => {
const { useThreadStream } = await import("@/core/threads/hooks");
const queryClient = new QueryClient();
function StreamProbe() {
useThreadStream({
context: DEFAULT_LOCAL_SETTINGS.context,
isMock: true,
threadId: "thread-1",
});
return null;
}
renderToStaticMarkup(
createElement(
QueryClientProvider,
{ client: queryClient },
createElement(
I18nContext.Provider,
{
value: {
locale: "en-US",
setLocale: () => undefined,
},
},
createElement(StreamProbe),
),
),
);
expect(streamMockState.options?.throttle).toBe(true);
});