perf(webui): bound reasoning previews and pace streaming updates

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-12 18:33:39 +08:00
1 parent 9e71087a2a
commit 7cede64f07
5 files changed
+167 -12

No files matched your search

+31
View File
@@ -20,3 +20,34 @@ it("shows the reasoning preview in the shared tooltip on hover and keyboard focu
await user.keyboard("{Escape}");
expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();
});
it("bounds reasoning text without repainting a truncated label and keeps the complete tooltip current", async () => {
const user = userEvent.setup();
const text = "A long reasoning paragraph. ".repeat(2_000) + "original tail";
const { container, rerender } = render(<ReasoningRow text={text} streaming />);
const line = screen.getByTestId("activity-line");
expect(line.textContent).toBe(text.slice(0, 512) + "…");
expect(container.querySelector("[data-sheen-text]")).toBeNull();
expect(screen.getByTestId("activity-reasoning-marker")).toHaveAttribute("data-state", "thinking");
expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();
await user.tab();
expect(line).toHaveFocus();
expect(await screen.findByRole("tooltip")).toHaveTextContent(text);
rerender(<ReasoningRow text={text + " latest tail"} streaming />);
expect(screen.getByRole("tooltip")).toHaveTextContent(text + " latest tail");
await user.keyboard("{Escape}");
expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();
expect(line.textContent).toBe(text.slice(0, 512) + "…");
});
it("keeps the existing text animation for short active reasoning", () => {
const { container } = render(<ReasoningRow text="Short active reasoning" streaming />);
expect(container.querySelector("[data-sheen-text]")).toHaveAttribute("data-sheen-text", "Short active reasoning");
});
it("does not split a surrogate pair at the reasoning preview boundary", () => {
render(<ReasoningRow text={"x".repeat(511) + "🚀 tail"} streaming />);
expect(screen.getByTestId("activity-line").textContent).toBe("x".repeat(511) + "…");
});
+89
View File
@@ -296,6 +296,95 @@ describe("useNanobotStream", () => {
requestFrame.mockRestore();
});
it("paces visible updates and immediately flushes the final ordered text", () => {
vi.useFakeTimers();
const now = vi.spyOn(performance, "now").mockReturnValue(1_000);
const frames: FrameRequestCallback[] = [];
const requestFrame = vi.spyOn(window, "requestAnimationFrame").mockImplementation((callback) => {
frames.push(callback);
return frames.length;
});
const fake = fakeClient();
const { result, unmount } = renderHook(() => useNanobotStream("paced", EMPTY_MESSAGES), {
wrapper: wrap(fake.client),
});
try {
act(() => fake.emit("paced", { event: "delta", chat_id: "paced", text: "first" }));
expect(frames).toHaveLength(1);
act(() => frames.shift()!(1_000));
expect(result.current.messages[0].content).toBe("first");
act(() => {
fake.emit("paced", { event: "delta", chat_id: "paced", text: " 中文" });
fake.emit("paced", { event: "delta", chat_id: "paced", text: "🚀" });
vi.advanceTimersByTime(49);
});
expect(frames).toHaveLength(0);
expect(result.current.messages[0].content).toBe("first");
now.mockReturnValue(1_050);
act(() => vi.advanceTimersByTime(1));
expect(frames).toHaveLength(1);
act(() => frames.shift()!(1_050));
expect(result.current.messages[0].content).toBe("first 中文🚀");
act(() => {
fake.emit("paced", { event: "delta", chat_id: "paced", text: " tail" });
fake.emit("paced", { event: "stream_end", chat_id: "paced" });
});
expect(result.current.messages[0].content).toBe("first 中文🚀 tail");
act(() => fake.emit("paced", { event: "turn_end", chat_id: "paced" }));
expect(result.current.messages[0].isStreaming).toBe(false);
expect(vi.getTimerCount()).toBe(0);
} finally {
unmount();
requestFrame.mockRestore();
now.mockRestore();
vi.useRealTimers();
}
});
it.each(["frame", "timer"] as const)("moves a pending visible %s to background cadence and flushes on return", (pending) => {
vi.useFakeTimers();
const descriptor = Object.getOwnPropertyDescriptor(document, "visibilityState");
const now = vi.spyOn(performance, "now").mockReturnValue(1_000);
const frames: FrameRequestCallback[] = [];
const requestFrame = vi.spyOn(window, "requestAnimationFrame").mockImplementation((callback) => {
frames.push(callback);
return frames.length;
});
const fake = fakeClient();
const { result, unmount } = renderHook(() => useNanobotStream("visibility", EMPTY_MESSAGES), {
wrapper: wrap(fake.client),
});
const visibility = (value: string) => {
Object.defineProperty(document, "visibilityState", { configurable: true, value });
document.dispatchEvent(new Event("visibilitychange"));
};
try {
act(() => fake.emit("visibility", { event: "delta", chat_id: "visibility", text: "first" }));
if (pending === "timer") act(() => frames.shift()!(1_000));
act(() => {
fake.emit("visibility", { event: "delta", chat_id: "visibility", text: " hidden" });
visibility("hidden");
vi.advanceTimersByTime(999);
});
expect(result.current.messages[0]?.content).toBe(pending === "timer" ? "first" : undefined);
act(() => vi.advanceTimersByTime(1));
expect(result.current.messages[0].content).toBe("first hidden");
act(() => {
fake.emit("visibility", { event: "delta", chat_id: "visibility", text: " returned" });
visibility("visible");
});
expect(result.current.messages[0].content).toBe("first hidden returned");
expect(vi.getTimerCount()).toBe(0);
} finally {
unmount();
requestFrame.mockRestore();
now.mockRestore();
if (descriptor) Object.defineProperty(document, "visibilityState", descriptor);
else delete (document as Document & { visibilityState?: string }).visibilityState;
vi.useRealTimers();
}
});
it("coalesces hidden-tab deltas without scheduling paint frames", () => {
vi.useFakeTimers();
const visibilityDescriptor = Object.getOwnPropertyDescriptor(document, "visibilityState");