-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(files): image drag-reorder move + unwedge post-stream editor + bullet/image Backspace fixes #5608
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
Merged
Merged
fix(files): image drag-reorder move + unwedge post-stream editor + bullet/image Backspace fixes #5608
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
064edb1
fix(rich-markdown-editor): stop Backspace on an empty bullet from des…
waleedlatif1 adcbe0a
fix(files): poll the content query while the post-stream reconcile wa…
waleedlatif1 fcec190
test(files): cover the refetchInterval passthrough against real react…
waleedlatif1 28fdba0
fix(files): degrade reconcile polling to a slow cadence instead of st…
waleedlatif1 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
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
167 changes: 167 additions & 0 deletions
167
...p/workspace/[workspaceId]/files/components/file-viewer/use-editable-file-content.test.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,167 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| * | ||
| * The post-stream reconcile must poll the content query until a fetch shows the server content | ||
| * advanced past the pre-stream baseline — its exit is data-driven, and without the poll a single | ||
| * refetch racing the agent's write (or an invalidation that never reaches this surface) wedged the | ||
| * editor read-only until a window refocus or full reload. These drive the real | ||
| * `useEditableFileContent` engine through stream → settle → advance and assert the | ||
| * `refetchInterval` handed to the content query flips on and off with the `reconciling` phase. | ||
| */ | ||
| import { act } from 'react' | ||
| import { createRoot, type Root } from 'react-dom/client' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { queryState } = vi.hoisted(() => ({ | ||
| queryState: { | ||
| fetched: undefined as string | undefined, | ||
| lastOptions: undefined as | ||
| | { refetchInterval?: number | false | (() => number | false) } | ||
| | undefined, | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock('@/hooks/queries/workspace-files', () => ({ | ||
| useWorkspaceFileContent: ( | ||
| _workspaceId: string, | ||
| _fileId: string, | ||
| _key: string, | ||
| _raw?: boolean, | ||
| options?: { refetchInterval?: number | false | (() => number | false) } | ||
| ) => { | ||
| queryState.lastOptions = options | ||
| return { data: queryState.fetched, isLoading: queryState.fetched === undefined, error: null } | ||
| }, | ||
| useUpdateWorkspaceFileContent: () => ({ mutateAsync: vi.fn(async () => ({ success: true })) }), | ||
| })) | ||
|
|
||
| vi.mock('idb-keyval', () => ({ | ||
| get: vi.fn(async () => undefined), | ||
| set: vi.fn(async () => {}), | ||
| del: vi.fn(async () => {}), | ||
| })) | ||
|
|
||
| import { | ||
| RECONCILING_REFETCH_INTERVAL_MS, | ||
| RECONCILING_REFETCH_SLOW_INTERVAL_MS, | ||
| RECONCILING_REFETCH_WINDOW_MS, | ||
| useEditableFileContent, | ||
| } from './use-editable-file-content' | ||
|
|
||
| const FILE = { | ||
| id: 'f1', | ||
| key: 'workspace/ws-1/123-abc-doc.md', | ||
| name: 'doc.md', | ||
| type: 'text/markdown', | ||
| folderId: null, | ||
| } as any | ||
|
|
||
| interface ProbeProps { | ||
| streamingContent?: string | ||
| isAgentEditing?: boolean | ||
| } | ||
|
|
||
| let container: HTMLDivElement | null = null | ||
| let root: Root | null = null | ||
| let latest: ReturnType<typeof useEditableFileContent> | null = null | ||
|
|
||
| function Probe(props: ProbeProps) { | ||
| latest = useEditableFileContent({ | ||
| file: FILE, | ||
| workspaceId: 'ws-1', | ||
| canEdit: true, | ||
| streamingContent: props.streamingContent, | ||
| isAgentEditing: props.isAgentEditing, | ||
| }) | ||
| return null | ||
| } | ||
|
|
||
| function render(props: ProbeProps) { | ||
| act(() => { | ||
| root?.render(<Probe {...props} />) | ||
| }) | ||
| } | ||
|
|
||
| /** The interval value react-query would currently be using (resolving the function form). */ | ||
| function currentInterval(): number | false { | ||
| const raw = queryState.lastOptions?.refetchInterval | ||
| return typeof raw === 'function' ? raw() : (raw ?? false) | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true | ||
| queryState.fetched = undefined | ||
| queryState.lastOptions = undefined | ||
| container = document.createElement('div') | ||
| document.body.appendChild(container) | ||
| root = createRoot(container) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| if (root) act(() => root?.unmount()) | ||
| container?.remove() | ||
| container = null | ||
| root = null | ||
| latest = null | ||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| describe('reconcile refetch polling', () => { | ||
| it('polls only while reconciling, and stops the moment the fetched content advances', () => { | ||
| const BASELINE = '# Doc\n\nstart\n' | ||
| const STREAMED = '# Doc\n\nstart\n\n\n' | ||
|
|
||
| // Mount mid-stream (fetch not resolved yet): no polling. | ||
| render({ streamingContent: '', isAgentEditing: true }) | ||
| expect(currentInterval()).toBe(false) | ||
|
|
||
| // Baseline fetch resolves during the stream; chunks arrive: still no polling. | ||
| queryState.fetched = BASELINE | ||
| render({ streamingContent: '# Doc\n\nstart\n\n | ||
| render({ streamingContent: STREAMED, isAgentEditing: true }) | ||
| expect(currentInterval()).toBe(false) | ||
| expect(latest?.isStreamInteractionLocked).toBe(true) | ||
|
|
||
| // Stream settles but the cached fetch still shows the pre-stream baseline: reconciling → poll. | ||
| render({ streamingContent: undefined, isAgentEditing: false }) | ||
| expect(latest?.isStreamInteractionLocked).toBe(true) | ||
| expect(currentInterval()).toBe(RECONCILING_REFETCH_INTERVAL_MS) | ||
|
|
||
| // A poll returns the advanced server content: finalize, unlock, polling off. | ||
| queryState.fetched = STREAMED | ||
| render({ streamingContent: undefined, isAgentEditing: false }) | ||
| expect(latest?.isStreamInteractionLocked).toBe(false) | ||
| expect(latest?.content).toBe(STREAMED) | ||
| expect(currentInterval()).toBe(false) | ||
| }) | ||
|
|
||
| it('degrades to the slow cadence after the fast window — never stops outright while reconciling', () => { | ||
| vi.useFakeTimers() | ||
| vi.setSystemTime(1_000_000) | ||
| const BASELINE = '# Doc\n\nstart\n' | ||
|
|
||
| render({ streamingContent: '', isAgentEditing: true }) | ||
| queryState.fetched = BASELINE | ||
| render({ streamingContent: `${BASELINE}\nmore`, isAgentEditing: true }) | ||
| render({ streamingContent: undefined, isAgentEditing: false }) | ||
| expect(currentInterval()).toBe(RECONCILING_REFETCH_INTERVAL_MS) | ||
|
|
||
| vi.setSystemTime(1_000_000 + RECONCILING_REFETCH_WINDOW_MS - 1) | ||
| expect(currentInterval()).toBe(RECONCILING_REFETCH_INTERVAL_MS) | ||
|
|
||
| // A write landing late (slow job, replica catch-up) must still be picked up automatically — | ||
| // the poll degrades in rate but the editor can never end up with no recovery path at all. | ||
| vi.setSystemTime(1_000_000 + RECONCILING_REFETCH_WINDOW_MS) | ||
| expect(currentInterval()).toBe(RECONCILING_REFETCH_SLOW_INTERVAL_MS) | ||
| }) | ||
|
|
||
| it('never polls during plain at-rest editing (no stream involved)', () => { | ||
| queryState.fetched = '# Plain\n\ndoc\n' | ||
| render({}) | ||
| expect(latest?.isStreamInteractionLocked).toBe(false) | ||
| expect(currentInterval()).toBe(false) | ||
| act(() => latest?.setDraftContent('# Plain\n\ndoc edited\n')) | ||
| render({}) | ||
| expect(currentInterval()).toBe(false) | ||
| }) | ||
| }) |
Oops, something went wrong.
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.