Repository navigation
fix(editor): keep file ownership through host focus changes #25429
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
nwparker
wants to merge
5
commits into
main
Choose a base branch
from
nwparker/editor-file-host-focus
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.
+598
−19
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9969240
fix(lint): distinguish internal hook confirmation from browser dialogs
nwparker d314158
fix(editor): preserve file owner across host focus
Pr1p cad7903
fix(editor): retain legacy runtime owner fallback
Pr1p 7a89e6f
test(editor): isolate file owner focus coverage
Pr1p 64a234a
fix(editor): preserve folder ownership through host focus changes
nwparker 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
180 changes: 180 additions & 0 deletions
180
src/renderer/src/components/editor/editor-file-host-focus.operations.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,180 @@ | ||
| // @vitest-environment happy-dom | ||
| import { act, useLayoutEffect, useRef, useState } from 'react' | ||
| import { createRoot } from 'react-dom/client' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
| import { useAppStore } from '@/store' | ||
| import { makeWorktree, TEST_REPO } from '@/store/slices/store-test-helpers' | ||
| import type { OpenFile } from '@/store/slices/editor' | ||
| import type * as RuntimeFileClient from '@/runtime/runtime-file-client' | ||
| import { createGlobalSettingsFixture } from '../../../../shared/global-settings-test-fixture' | ||
| import type { ExecutionHostId } from '../../../../shared/execution-host' | ||
| import type { FileContent } from './editor-panel-content-types' | ||
| import { createEditorSaveQueue } from './editor-save-queue' | ||
| import { | ||
| useEditorPanelFileContentLoader, | ||
| type EditorPanelFileContentLoader | ||
| } from './useEditorPanelFileContentLoader' | ||
|
|
||
| const calls = vi.hoisted(() => ({ read: vi.fn(), write: vi.fn() })) | ||
| vi.mock('@/runtime/runtime-file-client', async (importOriginal) => ({ | ||
| ...(await importOriginal<typeof RuntimeFileClient>()), | ||
| readRuntimeFileContent: calls.read, | ||
| writeRuntimeFile: calls.write | ||
| })) | ||
|
|
||
| function deferred<T>() { | ||
| let resolve!: (value: T) => void | ||
| const promise = new Promise<T>((done) => { | ||
| resolve = done | ||
| }) | ||
| return { promise, resolve } | ||
| } | ||
|
|
||
| function ReadProbe({ | ||
| file, | ||
| ready | ||
| }: { | ||
| file: OpenFile | ||
| ready: (load: EditorPanelFileContentLoader) => void | ||
| }) { | ||
| const [, setFileContents] = useState<Record<string, FileContent>>({}) | ||
| const load = useEditorPanelFileContentLoader({ | ||
| fileLoadRetryAttemptsRef: useRef({}), | ||
| fileReadGenerationCounterRef: useRef(0), | ||
| fileReadGenerationRef: useRef({}), | ||
| openFilesRef: useRef([file]), | ||
| outstandingFileReadsRef: useRef({}), | ||
| setFileContents | ||
| }) | ||
| useLayoutEffect(() => { | ||
| ready(load) | ||
| }, [load, ready]) | ||
| return null | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| useAppStore.setState(useAppStore.getInitialState(), true) | ||
| calls.read.mockReset() | ||
| calls.write.mockReset() | ||
| }) | ||
|
|
||
| describe('editor operations across host focus changes', () => { | ||
| it.each([ | ||
| { host: 'local', environment: null, connection: undefined }, | ||
| { host: 'runtime:file-host', environment: 'file-host', connection: undefined }, | ||
| { host: 'ssh:file-target', environment: null, connection: 'file-target' } | ||
| ] satisfies { | ||
| host: ExecutionHostId | ||
| environment: string | null | ||
| connection: string | undefined | ||
| }[])('keeps delayed read and save on $host', async ({ host, environment, connection }) => { | ||
| globalThis.IS_REACT_ACT_ENVIRONMENT = true | ||
| useAppStore.setState(useAppStore.getInitialState(), true) | ||
| const worktreeId = 'repo1::/workspace/repo' | ||
| useAppStore.setState({ | ||
| repos: [{ ...TEST_REPO, path: '/workspace/repo', connectionId: connection }], | ||
| worktreesByRepo: { | ||
| repo1: [ | ||
| makeWorktree({ | ||
| id: worktreeId, | ||
| repoId: 'repo1', | ||
| path: '/workspace/repo', | ||
| ...(host === 'local' ? {} : { hostId: host }), | ||
| ...(environment ? { runtimeOwnerEnvironmentId: environment } : {}) | ||
| }) | ||
| ] | ||
| }, | ||
| settings: createGlobalSettingsFixture({ activeRuntimeEnvironmentId: null }), | ||
| activeWorktreeId: worktreeId, | ||
| activeWorkspaceExecutionHostId: 'runtime:other-host', | ||
| sshConnectionStates: new Map([ | ||
| [ | ||
| 'file-target', | ||
| { | ||
| targetId: 'file-target', | ||
| status: 'connected', | ||
| error: null, | ||
| reconnectAttempt: 0, | ||
| connectionGeneration: 7 | ||
| } | ||
| ] | ||
| ]), | ||
| recordFeatureInteraction: vi.fn() | ||
| }) | ||
| const fileId = useAppStore.getState().openFile({ | ||
| filePath: '/workspace/repo/file.txt', | ||
| relativePath: 'file.txt', | ||
| worktreeId, | ||
| language: 'plaintext', | ||
| mode: 'edit' | ||
| }) | ||
| const file = useAppStore.getState().openFiles.find((entry) => entry.id === fileId) | ||
| if (!file) { | ||
| throw new Error('Expected the opened file') | ||
| } | ||
| const read = deferred<FileContent>() | ||
| const write = deferred<void>() | ||
| calls.read.mockReturnValueOnce(read.promise) | ||
| calls.write.mockReturnValueOnce(write.promise) | ||
| const container = document.body.appendChild(document.createElement('div')) | ||
| const root = createRoot(container) | ||
| const queue = createEditorSaveQueue(useAppStore) | ||
| const probe: { load?: EditorPanelFileContentLoader } = {} | ||
| try { | ||
| act(() => | ||
| root.render( | ||
| <ReadProbe | ||
| file={file} | ||
| ready={(load) => { | ||
| probe.load = load | ||
| }} | ||
| /> | ||
| ) | ||
| ) | ||
| const load = probe.load | ||
| if (!load) { | ||
| throw new Error('Expected the content loader') | ||
| } | ||
| const pendingRead = load(file.filePath, file.id, worktreeId, file.relativePath) | ||
| await vi.waitFor(() => expect(calls.read).toHaveBeenCalledTimes(1)) | ||
| useAppStore.setState({ | ||
| activeWorkspaceExecutionHostId: 'runtime:third-host', | ||
| settings: createGlobalSettingsFixture({ activeRuntimeEnvironmentId: 'third-host' }) | ||
| }) | ||
| expect(calls.read.mock.calls[0]?.[0]).toMatchObject({ | ||
| settings: { activeRuntimeEnvironmentId: environment }, | ||
| connectionId: connection | ||
| }) | ||
| await act(async () => { | ||
| read.resolve({ content: 'original', isBinary: false }) | ||
| await pendingRead | ||
| }) | ||
| useAppStore.getState().setEditorDraft(fileId, 'saved draft') | ||
| useAppStore.getState().markFileDirty(fileId, true) | ||
| const pendingSave = queue.queueSave(file, 'saved draft') | ||
| await vi.waitFor(() => expect(calls.write).toHaveBeenCalledTimes(1)) | ||
| useAppStore.setState({ activeWorkspaceExecutionHostId: 'runtime:fourth-host' }) | ||
| expect(calls.write.mock.calls[0]?.[0]).toMatchObject({ | ||
| settings: { activeRuntimeEnvironmentId: environment }, | ||
| ...(connection ? { connectionId: connection } : {}), | ||
| expectedExecutionHostId: connection ? host : 'local' | ||
| }) | ||
| expect(calls.write.mock.calls[0]?.slice(1, 3)).toEqual([file.filePath, 'saved draft']) | ||
| write.resolve() | ||
| await pendingSave | ||
| expect(useAppStore.getState().openFiles.find((entry) => entry.id === fileId)?.isDirty).toBe( | ||
| false | ||
| ) | ||
| expect( | ||
| useAppStore | ||
| .getState() | ||
| .unifiedTabsByWorktree[worktreeId]?.find((tab) => tab.entityId === fileId) | ||
| ?.executionHostId | ||
| ).toBe(host) | ||
| } finally { | ||
| queue.dispose() | ||
| act(() => root.unmount()) | ||
| container.remove() | ||
| } | ||
| }) | ||
| }) |
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
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.
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.
Nulling the active host here also removes the only disambiguator for a folder workspace id that exists on more than one host, so editor capture for such a folder now throws instead of resolving.
Technical details