Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/elements/__tests__/reasoning.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,47 @@ describe("reasoningTrigger", () => {
expect(screen.getByText("Thought for a few seconds")).toBeInTheDocument();
});

it("does not warn when duration is set after streaming ends", () => {
const spy = vi.spyOn(console, "warn").mockImplementation(vi.fn());
const { rerender } = render(
<Reasoning duration={undefined} isStreaming>
<ReasoningTrigger />
</Reasoning>
);

rerender(
<Reasoning duration={3} isStreaming={false}>
<ReasoningTrigger />
</Reasoning>
);

expect(screen.getByText("Thought for 3 seconds")).toBeInTheDocument();
expect(spy).not.toHaveBeenCalled();
spy.mockRestore();
});

it("prefers the duration prop over the measured duration", () => {
vi.useFakeTimers();
const { rerender } = render(
<Reasoning isStreaming>
<ReasoningTrigger />
</Reasoning>
);

act(() => {
vi.advanceTimersByTime(2000);
});

rerender(
<Reasoning duration={9} isStreaming={false}>
<ReasoningTrigger />
</Reasoning>
);

expect(screen.getByText("Thought for 9 seconds")).toBeInTheDocument();
vi.useRealTimers();
});

it("renders custom children", () => {
render(
<Reasoning>
Expand Down
11 changes: 7 additions & 4 deletions packages/elements/src/reasoning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ export const Reasoning = memo(
onChange: onOpenChange,
prop: open,
});
const [duration, setDuration] = useControllableState<number | undefined>({
defaultProp: undefined,
prop: durationProp,
});
// Kept in plain state: `duration` has no change callback, and
// `useControllableState` warns when `durationProp` goes from undefined
// (while streaming) to a number (once reasoning finishes).
const [measuredDuration, setDuration] = useState<number | undefined>(
undefined
);
const duration = durationProp ?? measuredDuration;

const hasEverStreamedRef = useRef(isStreaming);
const [hasAutoClosed, setHasAutoClosed] = useState(false);
Expand Down