forked from internxt/drive-desktop
-
Notifications
You must be signed in to change notification settings - Fork 4
[_]: Decouple release callback logic #277
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
AlexisMora
wants to merge
5
commits into
main
Choose a base branch
from
fix/br-1151-error-opening-files
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
798dbbc
Fix: centralize temporal drive folder path + add jsdoc for TemporalFile
AlexisMora 41047b7
Fix: Decouple fuse callback with logic and make tests
AlexisMora db4d8fc
fix: comments pr
AlexisMora fcbac0c
Merge remote-tracking branch 'origin/main' into fix/br-1151-error-ope…
AlexisMora c906160
fix: format
AlexisMora 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { describe, it, vi } from 'vitest'; | ||
| import { call } from '../../../../../tests/vitest/utils.helper'; | ||
| import { ReleaseCallback } from './ReleaseCallback'; | ||
| import { right } from '../../../../context/shared/domain/Either'; | ||
| import * as openFlagsTracker from './open-flags-tracker'; | ||
|
Check failure on line 5 in src/apps/drive/fuse/callbacks/ReleaseCallback.test.ts
|
||
| import * as handleReleaseModule from '../../../../backend/features/fuse/on-release/handle-release-callback'; | ||
| import { partialSpyOn } from '../../../../../tests/vitest/utils.helper'; | ||
|
|
||
| vi.mock(import('@internxt/drive-desktop-core/build/backend')); | ||
|
|
||
| describe('ReleaseCallback', () => { | ||
| const onReleaseSpy = partialSpyOn(openFlagsTracker, 'onRelease'); | ||
| const handleReleaseSpy = partialSpyOn(handleReleaseModule, 'handleReleaseCallback'); | ||
|
|
||
| const container = { get: vi.fn() } as any; | ||
| const releaseCallback = new ReleaseCallback(container); | ||
| it('should call onRelease to clean up open flags tracker', async () => { | ||
| handleReleaseSpy.mockResolvedValue(right(undefined)); | ||
|
|
||
| await releaseCallback.execute('/Documents/file.pdf', 3); | ||
|
|
||
| call(onReleaseSpy).toBe('/Documents/file.pdf'); | ||
| }); | ||
|
|
||
| it('should delegate to handleReleaseCallback', async () => { | ||
| handleReleaseSpy.mockResolvedValue(right(undefined)); | ||
|
|
||
| await releaseCallback.execute('/Documents/file.pdf', 3); | ||
|
|
||
| call(handleReleaseSpy).toMatchObject({ path: '/Documents/file.pdf' }); | ||
| }); | ||
|
|
||
| it('should return the result from handleReleaseCallback', async () => { | ||
| handleReleaseSpy.mockResolvedValue(right(undefined)); | ||
|
|
||
| const result = await releaseCallback.execute('/Documents/file.pdf', 3); | ||
|
|
||
| expect(result.isRight()).toBe(true); | ||
| }); | ||
| }); | ||
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
106 changes: 106 additions & 0 deletions
106
src/backend/features/fuse/on-release/handle-release-callback.test.ts
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,106 @@ | ||
| import { describe, it, vi } from 'vitest'; | ||
| import { call, calls } from '../../../../../tests/vitest/utils.helper'; | ||
| import { handleReleaseCallback } from './handle-release-callback'; | ||
| import { TemporalFile } from '../../../../context/storage/TemporalFiles/domain/TemporalFile'; | ||
|
|
||
| vi.mock(import('@internxt/drive-desktop-core/build/backend')); | ||
|
|
||
| function createTemporalFile(path: string): TemporalFile { | ||
| return TemporalFile.from({ | ||
| path, | ||
| size: 100, | ||
| createdAt: new Date(), | ||
| modifiedAt: new Date(), | ||
| }); | ||
| } | ||
|
|
||
| function createAuxiliaryFile(path: string): TemporalFile { | ||
| return TemporalFile.from({ | ||
| path, | ||
| size: 0, | ||
| createdAt: new Date(), | ||
| modifiedAt: new Date(), | ||
| }); | ||
| } | ||
|
|
||
| describe('handle-release-callback', () => { | ||
| const findTemporalFile = vi.fn<(path: string) => Promise<TemporalFile | undefined>>(); | ||
| const uploadTemporalFile = vi.fn<(path: string) => Promise<string>>(); | ||
| const deleteTemporalFile = vi.fn<(path: string) => Promise<void>>(); | ||
|
|
||
| it('should return right when no temporal file is found', async () => { | ||
| findTemporalFile.mockResolvedValue(undefined); | ||
|
|
||
| const result = await handleReleaseCallback({ | ||
| path: '/Documents/file.pdf', | ||
| findTemporalFile, | ||
| uploadTemporalFile, | ||
| deleteTemporalFile, | ||
| }); | ||
|
|
||
| expect(result.isRight()).toBe(true); | ||
| calls(findTemporalFile).toHaveLength(1); | ||
| calls(uploadTemporalFile).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('should skip upload for auxiliary files', async () => { | ||
| findTemporalFile.mockResolvedValue(createAuxiliaryFile('/Documents/.~lock.file.odt#')); | ||
|
|
||
| const result = await handleReleaseCallback({ | ||
| path: '/Documents/.~lock.file.odt#', | ||
| findTemporalFile, | ||
| uploadTemporalFile, | ||
| deleteTemporalFile, | ||
| }); | ||
|
|
||
| expect(result.isRight()).toBe(true); | ||
| calls(findTemporalFile).toHaveLength(1); | ||
| calls(uploadTemporalFile).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('should upload temporal file and returns right on success', async () => { | ||
| findTemporalFile.mockResolvedValue(createTemporalFile('/Documents/report.pdf')); | ||
| uploadTemporalFile.mockResolvedValue('contents-id-123'); | ||
|
|
||
| const result = await handleReleaseCallback({ | ||
| path: '/Documents/report.pdf', | ||
| findTemporalFile, | ||
| uploadTemporalFile, | ||
| deleteTemporalFile, | ||
| }); | ||
|
|
||
| expect(result.isRight()).toBe(true); | ||
| calls(findTemporalFile).toHaveLength(1); | ||
| call(uploadTemporalFile).toBe('/Documents/report.pdf'); | ||
| }); | ||
|
|
||
| it('should delete temporal file and return left when upload fails', async () => { | ||
| findTemporalFile.mockResolvedValue(createTemporalFile('/Documents/report.pdf')); | ||
| uploadTemporalFile.mockRejectedValue(new Error('Network error')); | ||
|
|
||
| const result = await handleReleaseCallback({ | ||
| path: '/Documents/report.pdf', | ||
| findTemporalFile, | ||
| uploadTemporalFile, | ||
| deleteTemporalFile, | ||
| }); | ||
|
|
||
| expect(result.isLeft()).toBe(true); | ||
| call(deleteTemporalFile).toBe('/Documents/report.pdf'); | ||
| }); | ||
|
|
||
| it('should return left when findTemporalFile throws an error', async () => { | ||
| findTemporalFile.mockRejectedValue(new Error('DB error')); | ||
|
|
||
| const result = await handleReleaseCallback({ | ||
| path: '/Documents/report.pdf', | ||
| findTemporalFile, | ||
| uploadTemporalFile, | ||
| deleteTemporalFile, | ||
| }); | ||
|
|
||
| expect(result.isLeft()).toBe(true); | ||
| calls(uploadTemporalFile).toHaveLength(0); | ||
| calls(deleteTemporalFile).toHaveLength(0); | ||
| }); | ||
| }); |
44 changes: 44 additions & 0 deletions
44
src/backend/features/fuse/on-release/handle-release-callback.ts
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,44 @@ | ||
| import { logger } from '@internxt/drive-desktop-core/build/backend'; | ||
| import { left, right, type Either } from '../../../../context/shared/domain/Either'; | ||
| import { type TemporalFile } from '../../../../context/storage/TemporalFiles/domain/TemporalFile'; | ||
| import { FuseError, FuseIOError } from '../../../../apps/drive/fuse/callbacks/FuseErrors'; | ||
|
|
||
| type Props = { | ||
| path: string; | ||
| findTemporalFile: (path: string) => Promise<TemporalFile | undefined>; | ||
| uploadTemporalFile: (path: string) => Promise<string>; | ||
| deleteTemporalFile: (path: string) => Promise<void>; | ||
| }; | ||
| export async function handleReleaseCallback({ | ||
| path, | ||
| findTemporalFile, | ||
| uploadTemporalFile, | ||
| deleteTemporalFile, | ||
| }: Props): Promise<Either<FuseError, undefined>> { | ||
| try { | ||
| const temporalFile = await findTemporalFile(path); | ||
|
|
||
| if (!temporalFile) { | ||
| logger.debug({ msg: '[Release] No temporal file found, nothing to upload', path }); | ||
| return right(undefined); | ||
| } | ||
|
|
||
| if (temporalFile.isAuxiliary()) { | ||
| logger.debug({ msg: '[Release] Auxiliary file detected, skipping upload', path }); | ||
| return right(undefined); | ||
| } | ||
|
|
||
| try { | ||
| await uploadTemporalFile(temporalFile.path.value); | ||
| logger.debug({ msg: '[Release] Temporal file uploaded', path }); | ||
| return right(undefined); | ||
| } catch (uploadError) { | ||
| logger.error({ msg: '[Release] Upload failed, deleting temporal file', error: uploadError, path }); | ||
| await deleteTemporalFile(path); | ||
| return left(new FuseIOError('Upload failed due to insufficient storage or network issues.')); | ||
| } | ||
| } catch (err: unknown) { | ||
| logger.error({ msg: '[Release] Unexpected error', error: err, path }); | ||
| return left(new FuseIOError('An unexpected error occurred during file release.')); | ||
| } | ||
| } | ||
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.
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.
the behaviour of this handler is the same as the previous code inside ReleaseCallback, the only difference is that gives more context about what is really happening and providing better naming (For example
const temporalFile = await findTemporalFile(path);instead ofconst document = await this.findDocument(path);at a first glance it is more explicit about the intention