-
Notifications
You must be signed in to change notification settings - Fork 331
fix(web): scope file-search recents to (repo, revision) in the browse dialog #1529
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
Harsh23Kashyap
wants to merge
3
commits into
sourcebot-dev:main
Choose a base branch
from
Harsh23Kashyap:fix/file-search-recents-revision-scoped
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
3 commits
Select commit
Hold shift + click to select a range
9047d93
fix(web): scope file-search recents to (repo, revision) in the browse…
Harsh23Kashyap 3d1938d
test(web): cover the per-revision recents key in the browse file-sear…
Harsh23Kashyap 4b0eb9a
fix(web): encode the recents key with JSON to avoid boundary ambiguity
Harsh23Kashyap 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
138 changes: 138 additions & 0 deletions
138
packages/web/src/app/(app)/browse/components/fileSearchCommandDialog.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,138 @@ | ||
| import { afterEach, describe, expect, test, vi } from 'vitest'; | ||
| import { render } from '@testing-library/react'; | ||
|
|
||
| // Stub `useLocalStorage` so the test can capture the key the component | ||
| // passes and assert on it. Without this, we'd be asserting on real | ||
| // localStorage (which jsdom does provide, but the key changes per | ||
| // revision and the test only wants to verify the key format). | ||
| const capturedKeys: string[] = []; | ||
| vi.mock('usehooks-ts', () => ({ | ||
| useLocalStorage: <T,>(key: string, initial: T) => { | ||
| capturedKeys.push(key); | ||
| return [initial, vi.fn()] as [T, (v: T) => void]; | ||
| }, | ||
| })); | ||
|
|
||
| // Make the params mock swappable per-test. We use a single mutable | ||
| // holder so each `it` can set its own (repoName, revisionName) tuple | ||
| // before rendering. | ||
| const mockParams: { repoName: string; revisionName: string | undefined } = { | ||
| repoName: 'github.com/foo/bar', | ||
| revisionName: 'main', | ||
| }; | ||
| vi.mock('@/app/(app)/browse/hooks/useBrowseParams', () => ({ | ||
| useBrowseParams: () => ({ repoName: mockParams.repoName, revisionName: mockParams.revisionName }), | ||
| })); | ||
| vi.mock('@/app/(app)/browse/hooks/useBrowseState', () => ({ | ||
| useBrowseState: () => ({ state: { isFileSearchOpen: false }, updateBrowseState: vi.fn() }), | ||
| })); | ||
| vi.mock('@/app/(app)/browse/hooks/useBrowseNavigation', () => ({ | ||
| useBrowseNavigation: () => ({ navigateToPath: vi.fn() }), | ||
| })); | ||
|
|
||
| vi.mock('next/navigation', () => ({ | ||
| usePathname: () => '/browse/github.com/foo/bar@main/-/tree/src', | ||
| })); | ||
|
|
||
| vi.mock('@tanstack/react-query', () => ({ | ||
| useQuery: () => ({ data: [], isLoading: false, isError: false }), | ||
| })); | ||
|
|
||
| vi.mock('@/app/api/(client)/client', () => ({ | ||
| getFiles: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('react-hotkeys-hook', () => ({ | ||
| useHotkeys: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('@/app/(app)/browse/components/fileTreeItemIcon', () => ({ | ||
| FileTreeItemIcon: () => null, | ||
| })); | ||
|
|
||
| vi.mock('@/components/ui/command', () => ({ | ||
| Command: ({ children }: { children: React.ReactNode }) => <>{children}</>, | ||
| CommandEmpty: ({ children }: { children: React.ReactNode }) => <>{children}</>, | ||
| CommandGroup: ({ children }: { children: React.ReactNode }) => <>{children}</>, | ||
| CommandInput: () => null, | ||
| CommandItem: ({ children }: { children: React.ReactNode }) => <>{children}</>, | ||
| CommandList: ({ children }: { children: React.ReactNode }) => <>{children}</>, | ||
| })); | ||
|
|
||
| vi.mock('@/components/ui/dialog', () => ({ | ||
| Dialog: ({ children }: { children: React.ReactNode }) => <>{children}</>, | ||
| DialogContent: ({ children }: { children: React.ReactNode }) => <>{children}</>, | ||
| DialogDescription: () => null, | ||
| DialogTitle: () => null, | ||
| })); | ||
|
|
||
| const { FileSearchCommandDialog } = await import('./fileSearchCommandDialog'); | ||
|
|
||
| const renderWithParams = () => { | ||
| capturedKeys.length = 0; | ||
| render(<FileSearchCommandDialog />); | ||
| return capturedKeys[0]; | ||
| }; | ||
|
|
||
| describe('FileSearchCommandDialog recents key (issue #1387)', () => { | ||
| afterEach(() => { | ||
| // Reset the mock params to the default between tests so a | ||
| // mutation in one test doesn't leak into the next. | ||
| mockParams.repoName = 'github.com/foo/bar'; | ||
| mockParams.revisionName = 'main'; | ||
| }); | ||
|
|
||
| test('scopes the recents localStorage key to the (repo, revision) tuple', () => { | ||
| // The previous key was `recentlyOpenedFiles-${repoName}` — same | ||
| // across revisions. After the fix, the key is scoped per | ||
| // revision so switching branches doesn't carry over suggestions | ||
| // from another revision that may not exist on the current one. | ||
| mockParams.repoName = 'github.com/foo/bar'; | ||
| mockParams.revisionName = 'main'; | ||
| const key = renderWithParams(); | ||
| expect(key).toBe('recentlyOpenedFiles::["github.com/foo/bar","main"]'); | ||
| }); | ||
|
|
||
| test('uses the HEAD fallback for the "no revision in URL" case', () => { | ||
| // The file-fetch on the next line uses `revisionName ?? 'HEAD'` | ||
| // as the default; the recents key needs to agree so the user | ||
| // sees a consistent recents list for the "default branch" view. | ||
| mockParams.repoName = 'github.com/foo/bar'; | ||
| mockParams.revisionName = undefined; | ||
| const key = renderWithParams(); | ||
| expect(key).toBe('recentlyOpenedFiles::["github.com/foo/bar","HEAD"]'); | ||
| }); | ||
|
|
||
| test('produces a different key for a different revision in the same repo', () => { | ||
| // The bug was: switching from `main` to `feature/foo` showed | ||
| // recents from `main`. After the fix, the keys differ, so the | ||
| // recents are naturally scoped. This test asserts that the | ||
| // keys differ for the two revisions. | ||
| mockParams.repoName = 'github.com/foo/bar'; | ||
| mockParams.revisionName = 'main'; | ||
| const keyMain = renderWithParams(); | ||
|
|
||
| mockParams.revisionName = 'feature/foo'; | ||
| const keyFeature = renderWithParams(); | ||
|
|
||
| expect(keyMain).toBe('recentlyOpenedFiles::["github.com/foo/bar","main"]'); | ||
| expect(keyFeature).toBe('recentlyOpenedFiles::["github.com/foo/bar","feature/foo"]'); | ||
| expect(keyMain).not.toBe(keyFeature); | ||
| }); | ||
|
|
||
| test('does not collide for tuples whose naive-dash concatenation would be ambiguous', () => { | ||
| // CodeRabbit finding: a naive `<repoName>-<revisionName>` format | ||
| // collides for tuples like (`foo-bar`, `baz`) vs (`foo`, `bar-baz`). | ||
| // The JSON-encoded format is collision-free by construction, so | ||
| // this test asserts that two such tuples produce different keys. | ||
| mockParams.repoName = 'foo-bar'; | ||
| mockParams.revisionName = 'baz'; | ||
| const keyA = renderWithParams(); | ||
|
|
||
| mockParams.repoName = 'foo'; | ||
| mockParams.revisionName = 'bar-baz'; | ||
| const keyB = renderWithParams(); | ||
|
|
||
| expect(keyA).not.toBe(keyB); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.