-
Notifications
You must be signed in to change notification settings - Fork 4.2k
fix(frontend): Ask spinner never stops under React Strict Mode #1235
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
zivkidd1
wants to merge
2
commits into
lfnovo:main
Choose a base branch
from
zivkidd1:fix/ask-mounted-ref-strict-mode
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.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| import { StrictMode } from 'react' | ||
| import { renderHook, act } from '@testing-library/react' | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest' | ||
| import { useAsk } from './use-ask' | ||
| import { searchApi } from '@/lib/api/search' | ||
|
|
||
| vi.mock('@/lib/api/search', () => ({ | ||
| searchApi: { askKnowledgeBase: vi.fn() }, | ||
| })) | ||
|
|
||
| vi.mock('sonner', () => ({ | ||
| toast: { error: vi.fn(), success: vi.fn() }, | ||
| })) | ||
|
|
||
| const MODELS = { | ||
| strategy: 'model:strategy', | ||
| answer: 'model:answer', | ||
| finalAnswer: 'model:final', | ||
| } | ||
|
|
||
| // Build a ReadableStream of SSE frames shaped like the backend's | ||
| // `data: {json}\n\n` output (api/routers/search.py stream_ask_response). | ||
| function sseStream(events: Array<Record<string, unknown>>) { | ||
| const encoder = new TextEncoder() | ||
| return new ReadableStream<Uint8Array>({ | ||
| start(controller) { | ||
| for (const event of events) { | ||
| controller.enqueue(encoder.encode(`data: ${JSON.stringify(event)}\n\n`)) | ||
| } | ||
| controller.close() | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| // Regression coverage for the mountedRef guard. StrictMode mounts, unmounts and | ||
| // remounts, which is exactly the sequence that used to leave mountedRef.current | ||
| // false forever and turn every state update in this hook into a no-op. These | ||
| // assertions fail without the `mountedRef.current = true` setup assignment. | ||
| describe('useAsk under React StrictMode', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('clears the loading state and exposes the answer when the stream completes', async () => { | ||
| vi.mocked(searchApi.askKnowledgeBase).mockResolvedValue( | ||
| sseStream([ | ||
| { type: 'strategy', reasoning: 'because', searches: [{ term: 't', instructions: 'i' }] }, | ||
| { type: 'answer', content: 'partial answer' }, | ||
| { type: 'final_answer', content: 'the final answer' }, | ||
| { type: 'complete', final_answer: 'the final answer' }, | ||
| ]) as any | ||
| ) | ||
|
|
||
| const { result } = renderHook(() => useAsk(), { wrapper: StrictMode }) | ||
|
|
||
| await act(async () => { | ||
| await result.current.sendAsk('why?', MODELS) | ||
| }) | ||
|
|
||
| expect(result.current.isStreaming).toBe(false) | ||
| expect(result.current.finalAnswer).toBe('the final answer') | ||
| expect(result.current.strategy?.searches).toHaveLength(1) | ||
| expect(result.current.answers).toEqual(['partial answer']) | ||
| }) | ||
|
|
||
| it('surfaces an in-band error event instead of loading forever', async () => { | ||
| vi.mocked(searchApi.askKnowledgeBase).mockResolvedValue( | ||
| sseStream([ | ||
| { type: 'strategy', reasoning: 'because', searches: [] }, | ||
| { type: 'error', message: 'The AI provider is temporarily unavailable.' }, | ||
| ]) as any | ||
| ) | ||
|
|
||
| const { result } = renderHook(() => useAsk(), { wrapper: StrictMode }) | ||
|
|
||
| await act(async () => { | ||
| await result.current.sendAsk('why?', MODELS) | ||
| }) | ||
|
|
||
| expect(result.current.isStreaming).toBe(false) | ||
| expect(result.current.error).toBe('The AI provider is temporarily unavailable.') | ||
| }) | ||
| }) |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: The Strict Mode fix itself is correct and minimal. Since this is framed as a bug fix and the project convention requires a regression test with real evidence for bug fixes, consider adding a test that exercises the mount → unmount → remount sequence and asserts that the mountedRef guards still allow the end-of-loading paths (stopStreaming on final_answer/complete, idle watchdog, error clear of isStreaming) to run. The current PR only notes that the existing 140-test suite still passes, which cannot catch this dev-only regression.
Prompt for AI agents