-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(ts-client): prune out-of-scope batch resources #3085
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
OldFriendWenjianjian
wants to merge
1
commit into
spacedriveapp:main
Choose a base branch
from
OldFriendWenjianjian:fix/normalized-cache-mixed-batch
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.
+219
−18
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
162 changes: 162 additions & 0 deletions
162
packages/ts-client/tests/useNormalizedQuery.mixed-batch.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,162 @@ | ||
| import {describe, expect, test} from 'bun:test'; | ||
| import {QueryClient} from '@tanstack/react-query'; | ||
| import { | ||
| filterBatchResources, | ||
| updateBatchResources, | ||
| type UseNormalizedQueryOptions | ||
| } from '../src/hooks/useNormalizedQuery'; | ||
|
|
||
| const queryKey = ['query:files.directory_listing', 'library-id', {}]; | ||
|
|
||
| function file(id: string, path: string, name = id) { | ||
| return { | ||
| id, | ||
| name, | ||
| sd_path: { | ||
| Physical: { | ||
| device_slug: 'device', | ||
| path | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| function options(path: string): UseNormalizedQueryOptions<any> { | ||
| return { | ||
| query: 'files.directory_listing', | ||
| resourceType: 'file', | ||
| pathScope: { | ||
| Physical: { | ||
| device_slug: 'device', | ||
| path | ||
| } | ||
| }, | ||
| includeDescendants: false | ||
| }; | ||
| } | ||
|
|
||
| function updateCache( | ||
| initialFiles: ReturnType<typeof file>[], | ||
| resources: ReturnType<typeof file>[], | ||
| pathScope: string | ||
| ) { | ||
| const queryClient = new QueryClient(); | ||
| queryClient.setQueryData(queryKey, { | ||
| files: initialFiles, | ||
| total_count: initialFiles.length, | ||
| has_more: false | ||
| }); | ||
|
|
||
| updateBatchResources( | ||
| resources, | ||
| null, | ||
| options(pathScope), | ||
| queryKey, | ||
| queryClient | ||
| ); | ||
|
|
||
| return queryClient.getQueryData(queryKey) as { | ||
| files: ReturnType<typeof file>[]; | ||
| total_count: number; | ||
| has_more: boolean; | ||
| }; | ||
| } | ||
|
|
||
| describe('updateBatchResources scoped batches', () => { | ||
| test('removes out-of-scope IDs and merges in-scope resources in one mixed batch', () => { | ||
| const result = updateCache( | ||
| [ | ||
| file('moved-out', 'C:\\Users\\Test\\Current\\moved.txt'), | ||
| file( | ||
| 'staying', | ||
| 'C:\\Users\\Test\\Current\\staying.txt', | ||
| 'old-name' | ||
| ), | ||
| file('untouched', 'C:\\Users\\Test\\Current\\untouched.txt') | ||
| ], | ||
| [ | ||
| file('moved-out', 'C:/Users/Test/Other/moved.txt'), | ||
| file( | ||
| 'staying', | ||
| 'c:/users/test/current/staying.txt', | ||
| 'new-name' | ||
| ), | ||
| file('moved-in', 'C:/USERS/TEST/CURRENT/moved-in.txt') | ||
| ], | ||
| 'C:\\Users\\Test\\Current\\' | ||
| ); | ||
|
|
||
| expect(result.files.map(({id}) => id)).toEqual([ | ||
| 'staying', | ||
| 'untouched', | ||
| 'moved-in' | ||
| ]); | ||
| expect(result.files.find(({id}) => id === 'staying')?.name).toBe( | ||
| 'new-name' | ||
| ); | ||
| }); | ||
|
|
||
| test('applies the same mixed-batch update to direct array caches', () => { | ||
| const queryClient = new QueryClient(); | ||
| queryClient.setQueryData(queryKey, [ | ||
| file('moved-out', '/current/moved.txt'), | ||
| file('staying', '/current/staying.txt', 'old-name') | ||
| ]); | ||
|
|
||
| updateBatchResources( | ||
| [ | ||
| file('moved-out', '/other/moved.txt'), | ||
| file('staying', '/current/staying.txt', 'new-name') | ||
| ], | ||
| null, | ||
| options('/current'), | ||
| queryKey, | ||
| queryClient | ||
| ); | ||
|
|
||
| expect(queryClient.getQueryData(queryKey)).toEqual([ | ||
| file('staying', '/current/staying.txt', 'new-name') | ||
| ]); | ||
| }); | ||
|
|
||
| test('removes every cached resource when the entire batch moves out of scope', () => { | ||
| const result = updateCache( | ||
| [ | ||
| file('first', '/current/first.txt'), | ||
| file('second', '/current/second.txt') | ||
| ], | ||
| [ | ||
| file('first', '/other/first.txt'), | ||
| file('second', '/other/second.txt') | ||
| ], | ||
| '/current' | ||
| ); | ||
|
|
||
| expect(result.files).toEqual([]); | ||
| }); | ||
|
|
||
| test('merges every resource when the entire batch remains in scope', () => { | ||
| const result = updateCache( | ||
| [file('existing', '/current/existing.txt', 'old-name')], | ||
| [ | ||
| file('existing', '/current/existing.txt', 'new-name'), | ||
| file('new', '/current/new.txt') | ||
| ], | ||
| '/current/' | ||
| ); | ||
|
|
||
| expect(result.files.map(({id}) => id)).toEqual(['existing', 'new']); | ||
| expect(result.files[0]?.name).toBe('new-name'); | ||
| }); | ||
|
|
||
| test('keeps POSIX path matching case-sensitive', () => { | ||
| const resources = [ | ||
| file('matching', '/Users/Test/Current/file.txt'), | ||
| file('different-case', '/users/test/current/file.txt') | ||
| ]; | ||
|
|
||
| expect( | ||
| filterBatchResources(resources, options('/Users/Test/Current')) | ||
| ).toEqual([resources[0]]); | ||
| }); | ||
| }); |
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.