-
Notifications
You must be signed in to change notification settings - Fork 192
fix(webview): anchor thinking timer to message timestamp to survive Virtuoso remounts #881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
umi008
wants to merge
4
commits into
Zoo-Code-Org:main
Choose a base branch
from
umi008:fix/656-reasoning-timer-reset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+159
−3
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b84aa59
fix(webview): anchor thinking timer to message timestamp to survive r…
umi008 0fce8f2
Merge branch 'main' into fix/656-reasoning-timer-reset
umi008 17e1593
Merge branch 'main' into fix/656-reasoning-timer-reset
umi008 7898c54
test(webview): add ReasoningBlock timer remount coverage
umi008 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
webview-ui/src/components/chat/__tests__/ReasoningBlock.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| // npx vitest src/components/chat/__tests__/ReasoningBlock.spec.tsx | ||
|
|
||
| import React from "react" | ||
| import { render, screen, act } from "@/utils/test-utils" | ||
|
|
||
| import { ReasoningBlock } from "../ReasoningBlock" | ||
|
|
||
| // Mock i18n | ||
| vi.mock("react-i18next", () => ({ | ||
| useTranslation: () => ({ | ||
| t: (key: string, options?: any) => { | ||
| if (key === "chat:reasoning.seconds") { | ||
| return `${options?.count ?? 0}s` | ||
| } | ||
| return key | ||
| }, | ||
| }), | ||
| initReactI18next: { | ||
| type: "3rdParty", | ||
| init: vi.fn(), | ||
| }, | ||
| })) | ||
|
|
||
| // Mock ExtensionStateContext | ||
| const mockExtensionState = { | ||
| reasoningBlockCollapsed: false, | ||
| } | ||
|
|
||
| vi.mock("@src/context/ExtensionStateContext", () => ({ | ||
| useExtensionState: () => mockExtensionState, | ||
| })) | ||
|
|
||
| const defaultProps = { | ||
| content: "Let me think about this...", | ||
| ts: Date.now() - 30_000, // 30 seconds ago | ||
| isStreaming: false, | ||
| isLast: false, | ||
| } as const | ||
|
|
||
| describe("ReasoningBlock", () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers() | ||
| vi.setSystemTime(Date.now()) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| it("renders the thinking label", () => { | ||
| render(<ReasoningBlock {...defaultProps} />) | ||
| expect(screen.getByText("chat:reasoning.thinking")).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it("shows elapsed time for non-streaming blocks anchored to ts", () => { | ||
| const thirtySecAgo = Date.now() - 30_000 | ||
| render(<ReasoningBlock {...defaultProps} ts={thirtySecAgo} isStreaming={false} />) | ||
|
|
||
| // Should show ~30s since the timestamp | ||
| expect(screen.getByText("30s")).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it("starts timer at 0 when streaming and is last message", () => { | ||
| const ts = Date.now() | ||
| render(<ReasoningBlock {...defaultProps} ts={ts} isStreaming={true} isLast={true} content="" />) | ||
|
|
||
| // Initially 0 while streaming (just started) — no label shown when elapsed is 0 | ||
| expect(screen.queryByText(/^\d+s$/)).not.toBeInTheDocument() | ||
|
|
||
| // Advance time by 5 seconds | ||
| act(() => { | ||
| vi.advanceTimersByTime(5000) | ||
| }) | ||
|
|
||
| expect(screen.getByText("5s")).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it("preserves elapsed time on remount with same ts (Virtuoso recycle)", () => { | ||
| // ts is 1 second in the past so elapsed starts at 1s (not 0, since 0 hides the label) | ||
| const ts = Date.now() - 1000 | ||
|
|
||
| const { unmount } = render(<ReasoningBlock {...defaultProps} ts={ts} isStreaming={false} isLast={false} />) | ||
|
|
||
| // Initially 1s since ts is 1s ago | ||
| expect(screen.getByText("1s")).toBeInTheDocument() | ||
|
|
||
| // Advance time by 10 seconds | ||
| act(() => { | ||
| vi.advanceTimersByTime(10_000) | ||
| }) | ||
|
|
||
| // Unmount (simulating Virtuoso recycling the component) | ||
| unmount() | ||
|
|
||
| // Remount with the same ts | ||
| render(<ReasoningBlock {...defaultProps} ts={ts} isStreaming={false} isLast={false} />) | ||
|
|
||
| // Should still show ~11s, not 1 — timer survived the remount | ||
| // (elapsed is computed from Date.now() - ts on each mount, and fake timers keep advancing) | ||
| expect(screen.getByText("11s")).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it("stops timer when streaming ends", () => { | ||
| const ts = Date.now() - 5000 | ||
|
|
||
| const { rerender } = render( | ||
| <ReasoningBlock {...defaultProps} ts={ts} isStreaming={true} isLast={true} content="" />, | ||
| ) | ||
|
|
||
| // Streaming — timer is active | ||
| expect(screen.getByText("5s")).toBeInTheDocument() | ||
|
|
||
| // Advance time while streaming | ||
| act(() => { | ||
| vi.advanceTimersByTime(3000) | ||
| }) | ||
|
|
||
| expect(screen.getByText("8s")).toBeInTheDocument() | ||
|
|
||
| // Streaming stops — rerender with isStreaming=false | ||
| rerender(<ReasoningBlock {...defaultProps} ts={ts} isStreaming={false} isLast={false} />) | ||
|
|
||
| // Timer should stop at current value | ||
| act(() => { | ||
| vi.advanceTimersByTime(5000) | ||
| }) | ||
|
|
||
| // Should NOT have advanced | ||
| expect(screen.getByText("8s")).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it("collapses when reasoningBlockCollapsed is true", () => { | ||
| mockExtensionState.reasoningBlockCollapsed = true | ||
|
|
||
| render(<ReasoningBlock {...defaultProps} />) | ||
|
|
||
| // Content and markdown should be hidden | ||
| expect(screen.queryByText(defaultProps.content)).not.toBeInTheDocument() | ||
|
|
||
| // Reset for other tests | ||
| mockExtensionState.reasoningBlockCollapsed = false | ||
| }) | ||
|
|
||
| it("hides elapsed label when elapsed is 0", () => { | ||
| const ts = Date.now() | ||
| render(<ReasoningBlock {...defaultProps} ts={ts} isStreaming={false} isLast={false} />) | ||
|
|
||
| // No seconds label because elapsed is 0 | ||
| expect(screen.queryByText(/^\d+s$/)).not.toBeInTheDocument() | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.