-
Notifications
You must be signed in to change notification settings - Fork 269
refactor(code-index): extract workspace manager registry #1595
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
WebMad
wants to merge
5
commits into
Zoo-Code-Org:main
Choose a base branch
from
WebMad:refactor/1594-code-index-manager-registry
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
61cefd5
refactor(code-index): extract workspace manager registry (#1594)
WebMad 51c0545
test(code-index): provide URI API in task mock
WebMad 0715555
fix(code-index): own registry cleanup and isolate disposal failures
WebMad 8ae217c
test(code-index): assert registry cleanup failure diagnostic
WebMad b583717
test(code-index): align registry mock with undefined contract
WebMad 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
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
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
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
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
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
147 changes: 147 additions & 0 deletions
147
src/services/code-index/__tests__/manager-registry.spec.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,147 @@ | ||
| import * as vscode from "vscode" | ||
| import { makeExtensionContext, makeTextEditor, makeUri } from "../../../test-utils/vscode" | ||
| import { CodeIndexManager } from "../manager" | ||
| import { CodeIndexManagerRegistry } from "../manager-registry" | ||
|
|
||
| vi.mock("vscode", () => ({ | ||
| window: { activeTextEditor: undefined }, | ||
| workspace: { workspaceFolders: undefined, getWorkspaceFolder: vi.fn() }, | ||
| Uri: { file: vi.fn() }, | ||
| })) | ||
|
|
||
| vi.mock("../manager", () => ({ | ||
| CodeIndexManager: vi.fn().mockImplementation(function () { | ||
| return { dispose: vi.fn() } | ||
| }), | ||
| })) | ||
|
|
||
| describe("CodeIndexManagerRegistry", () => { | ||
| let context: vscode.ExtensionContext | ||
| const first: vscode.WorkspaceFolder = { uri: makeUri("/first"), name: "first", index: 0 } | ||
| const second: vscode.WorkspaceFolder = { | ||
| uri: makeUri("/second", { scheme: "vscode-remote", authority: "ssh-remote+host" }), | ||
| name: "second", | ||
| index: 1, | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| context = makeExtensionContext() | ||
| vi.mocked(vscode.Uri.file).mockImplementation((value) => makeUri(value)) | ||
| Object.defineProperty(vscode.window, "activeTextEditor", { value: undefined, configurable: true }) | ||
| Object.defineProperty(vscode.workspace, "workspaceFolders", { | ||
| value: [first, second], | ||
| configurable: true, | ||
| }) | ||
| vi.mocked(vscode.workspace.getWorkspaceFolder).mockReturnValue(undefined) | ||
| }) | ||
|
|
||
| afterEach(() => CodeIndexManagerRegistry.disposeAll()) | ||
|
|
||
| it("returns no manager without a workspace or explicit path", () => { | ||
| Object.defineProperty(vscode.workspace, "workspaceFolders", { value: undefined }) | ||
| expect(CodeIndexManagerRegistry.getInstance(context)).toBeUndefined() | ||
| expect(CodeIndexManager).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("defaults to the first workspace and reuses its manager", () => { | ||
| const manager = CodeIndexManagerRegistry.getInstance(context) | ||
| expect(CodeIndexManagerRegistry.getInstance(context, first.uri.fsPath)).toBe(manager) | ||
| expect(CodeIndexManager).toHaveBeenCalledExactlyOnceWith(first.uri.fsPath, first.uri, context) | ||
| }) | ||
|
|
||
| it("uses the active editor workspace and preserves its remote URI", () => { | ||
| const editor = makeTextEditor() | ||
| Object.defineProperty(vscode.window, "activeTextEditor", { value: editor }) | ||
| vi.mocked(vscode.workspace.getWorkspaceFolder).mockReturnValue(second) | ||
| CodeIndexManagerRegistry.getInstance(context) | ||
| expect(vscode.workspace.getWorkspaceFolder).toHaveBeenCalledWith(editor.document.uri) | ||
| expect(CodeIndexManager).toHaveBeenCalledWith(second.uri.fsPath, second.uri, context) | ||
| }) | ||
|
|
||
| it("falls back to the first workspace when the active editor is outside it", () => { | ||
| Object.defineProperty(vscode.window, "activeTextEditor", { value: makeTextEditor() }) | ||
| CodeIndexManagerRegistry.getInstance(context) | ||
| expect(CodeIndexManager).toHaveBeenCalledWith(first.uri.fsPath, first.uri, context) | ||
| }) | ||
|
|
||
| it("prefers an explicit workspace over the active editor", () => { | ||
| Object.defineProperty(vscode.window, "activeTextEditor", { value: makeTextEditor() }) | ||
| vi.mocked(vscode.workspace.getWorkspaceFolder).mockReturnValue(first) | ||
| CodeIndexManagerRegistry.getInstance(context, second.uri.fsPath) | ||
| expect(CodeIndexManager).toHaveBeenCalledWith(second.uri.fsPath, second.uri, context) | ||
| expect(vscode.workspace.getWorkspaceFolder).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("creates a file URI for an explicit path outside workspace folders", () => { | ||
| Object.defineProperty(vscode.workspace, "workspaceFolders", { value: undefined }) | ||
| const uri = makeUri("/outside") | ||
| vi.mocked(vscode.Uri.file).mockReturnValue(uri) | ||
| CodeIndexManagerRegistry.getInstance(context, "/outside") | ||
| expect(vscode.Uri.file).toHaveBeenCalledWith("/outside") | ||
| expect(CodeIndexManager).toHaveBeenCalledWith("/outside", uri, context) | ||
| }) | ||
|
|
||
| it("creates distinct managers for different workspaces", () => { | ||
| const a = CodeIndexManagerRegistry.getInstance(context, first.uri.fsPath)! | ||
| const b = CodeIndexManagerRegistry.getInstance(context, second.uri.fsPath)! | ||
| expect(a).not.toBe(b) | ||
| }) | ||
|
|
||
| it("lists all registered managers", () => { | ||
| const a = CodeIndexManagerRegistry.getInstance(context, first.uri.fsPath)! | ||
| const b = CodeIndexManagerRegistry.getInstance(context, second.uri.fsPath)! | ||
| expect(CodeIndexManagerRegistry.getAllInstances()).toEqual([a, b]) | ||
| }) | ||
|
|
||
| it("disposes every registered manager", () => { | ||
| const a = CodeIndexManagerRegistry.getInstance(context, first.uri.fsPath)! | ||
| const b = CodeIndexManagerRegistry.getInstance(context, second.uri.fsPath)! | ||
| CodeIndexManagerRegistry.disposeAll() | ||
| expect(a.dispose).toHaveBeenCalledTimes(1) | ||
| expect(b.dispose).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it("removes all managers from the registry on disposal", () => { | ||
| CodeIndexManagerRegistry.getInstance(context, first.uri.fsPath) | ||
| CodeIndexManagerRegistry.getInstance(context, second.uri.fsPath) | ||
| CodeIndexManagerRegistry.disposeAll() | ||
| expect(CodeIndexManagerRegistry.getAllInstances()).toEqual([]) | ||
| }) | ||
|
|
||
| it("does not dispose managers again when cleanup is repeated", () => { | ||
| const manager = CodeIndexManagerRegistry.getInstance(context, first.uri.fsPath)! | ||
| CodeIndexManagerRegistry.disposeAll() | ||
| CodeIndexManagerRegistry.disposeAll() | ||
| expect(manager.dispose).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it("creates a new manager for the same workspace after disposal", () => { | ||
| const manager = CodeIndexManagerRegistry.getInstance(context, first.uri.fsPath)! | ||
| CodeIndexManagerRegistry.disposeAll() | ||
| expect(CodeIndexManagerRegistry.getInstance(context, first.uri.fsPath)).not.toBe(manager) | ||
| }) | ||
|
|
||
| it("attempts every disposal and rethrows the first error", () => { | ||
| const a = CodeIndexManagerRegistry.getInstance(context, first.uri.fsPath)! | ||
| const b = CodeIndexManagerRegistry.getInstance(context, second.uri.fsPath)! | ||
| const firstError = new Error("first cleanup failed") | ||
| vi.mocked(a.dispose).mockImplementation(() => { | ||
| throw firstError | ||
| }) | ||
| vi.mocked(b.dispose).mockImplementation(() => { | ||
| throw new Error("second cleanup failed") | ||
| }) | ||
| expect(() => CodeIndexManagerRegistry.disposeAll()).toThrow(firstError) | ||
| expect(b.dispose).toHaveBeenCalledTimes(1) | ||
| expect(CodeIndexManagerRegistry.getAllInstances()).toEqual([]) | ||
| }) | ||
|
|
||
| it("clears the registry before disposal callbacks run", () => { | ||
| const manager = CodeIndexManagerRegistry.getInstance(context, first.uri.fsPath)! | ||
| vi.mocked(manager.dispose).mockImplementation(() => { | ||
| expect(CodeIndexManagerRegistry.getAllInstances()).toEqual([]) | ||
| }) | ||
| CodeIndexManagerRegistry.disposeAll() | ||
| }) | ||
| }) |
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.