|
| 1 | +import { makeExtensionContext, makeUri } from "../../../test-utils/vscode" |
| 2 | +import { CodeIndexManager } from "../manager" |
| 3 | +import { CodeIndexWorkspaceScope } from "../code-index-workspace-scope" |
| 4 | + |
| 5 | +vi.mock("../manager", () => ({ |
| 6 | + CodeIndexManager: vi.fn().mockImplementation(function () { |
| 7 | + return { initialize: vi.fn(), dispose: vi.fn() } |
| 8 | + }), |
| 9 | +})) |
| 10 | + |
| 11 | +describe("CodeIndexWorkspaceScope", () => { |
| 12 | + beforeEach(() => vi.clearAllMocks()) |
| 13 | + |
| 14 | + it("guards generic values and preserves defined falsy values", () => { |
| 15 | + const scope = new CodeIndexWorkspaceScope("/workspace", makeUri("/workspace"), makeExtensionContext()) |
| 16 | + expect(() => scope["ensureInitialized"](42)).toThrow("Code index workspace scope is not initialized") |
| 17 | + scope.init() |
| 18 | + expect(() => scope["ensureInitialized"](undefined)).toThrow("Code index workspace scope is not initialized") |
| 19 | + expect(scope["ensureInitialized"](false)).toBe(false) |
| 20 | + expect(scope["ensureInitialized"](0)).toBe(0) |
| 21 | + expect(scope["ensureInitialized"]("")).toBe("") |
| 22 | + }) |
| 23 | + |
| 24 | + it("creates its manager only on init without starting configuration initialization", () => { |
| 25 | + const context = makeExtensionContext() |
| 26 | + const uri = makeUri("/workspace") |
| 27 | + const scope = new CodeIndexWorkspaceScope(uri.fsPath, uri, context) |
| 28 | + |
| 29 | + expect(CodeIndexManager).not.toHaveBeenCalled() |
| 30 | + expect(() => scope.codeIndexManager).toThrow("Code index workspace scope is not initialized") |
| 31 | + expect(scope.init()).toBeUndefined() |
| 32 | + const manager = scope.codeIndexManager |
| 33 | + |
| 34 | + expect(CodeIndexManager).toHaveBeenCalledExactlyOnceWith(uri.fsPath, uri, context) |
| 35 | + expect(scope.codeIndexManager).toBe(manager) |
| 36 | + expect(() => scope.init()).toThrow("Code index workspace scope is already initialized") |
| 37 | + expect(scope.codeIndexManager).toBe(manager) |
| 38 | + expect(CodeIndexManager).toHaveBeenCalledTimes(1) |
| 39 | + expect(manager.initialize).not.toHaveBeenCalled() |
| 40 | + }) |
| 41 | + |
| 42 | + it("synchronously disposes its manager", () => { |
| 43 | + const scope = new CodeIndexWorkspaceScope("/workspace", makeUri("/workspace"), makeExtensionContext()) |
| 44 | + scope.init() |
| 45 | + const manager = scope.codeIndexManager |
| 46 | + |
| 47 | + expect(scope.dispose()).toBeUndefined() |
| 48 | + expect(() => scope.codeIndexManager).toThrow("Code index workspace scope is not initialized") |
| 49 | + scope.dispose() |
| 50 | + expect(manager.dispose).toHaveBeenCalledExactlyOnceWith() |
| 51 | + }) |
| 52 | + |
| 53 | + it("can dispose before dependency creation without constructing a manager", () => { |
| 54 | + const scope = new CodeIndexWorkspaceScope("/workspace", makeUri("/workspace"), makeExtensionContext()) |
| 55 | + scope.dispose() |
| 56 | + expect(() => scope.codeIndexManager).toThrow("Code index workspace scope is not initialized") |
| 57 | + expect(CodeIndexManager).not.toHaveBeenCalled() |
| 58 | + }) |
| 59 | + |
| 60 | + it("creates a fresh manager when initialized after disposal", () => { |
| 61 | + const scope = new CodeIndexWorkspaceScope("/workspace", makeUri("/workspace"), makeExtensionContext()) |
| 62 | + scope.init() |
| 63 | + const previous = scope.codeIndexManager |
| 64 | + scope.dispose() |
| 65 | + scope.init() |
| 66 | + const current = scope.codeIndexManager |
| 67 | + expect(current).not.toBe(previous) |
| 68 | + expect(scope.codeIndexManager).toBe(current) |
| 69 | + expect(CodeIndexManager).toHaveBeenCalledTimes(2) |
| 70 | + expect(current.dispose).not.toHaveBeenCalled() |
| 71 | + }) |
| 72 | + |
| 73 | + it("clears its reference even when manager disposal throws", () => { |
| 74 | + const scope = new CodeIndexWorkspaceScope("/workspace", makeUri("/workspace"), makeExtensionContext()) |
| 75 | + scope.init() |
| 76 | + const manager = scope.codeIndexManager |
| 77 | + const error = new Error("disposal failed") |
| 78 | + vi.mocked(manager.dispose).mockImplementationOnce(() => { |
| 79 | + throw error |
| 80 | + }) |
| 81 | + expect(() => scope.dispose()).toThrow(error) |
| 82 | + expect(() => scope.codeIndexManager).toThrow("Code index workspace scope is not initialized") |
| 83 | + scope.dispose() |
| 84 | + expect(manager.dispose).toHaveBeenCalledTimes(1) |
| 85 | + }) |
| 86 | + |
| 87 | + it("remains uninitialized when construction fails and permits retry", () => { |
| 88 | + const scope = new CodeIndexWorkspaceScope("/workspace", makeUri("/workspace"), makeExtensionContext()) |
| 89 | + const error = new Error("construction failed") |
| 90 | + vi.mocked(CodeIndexManager).mockImplementationOnce(function () { |
| 91 | + throw error |
| 92 | + }) |
| 93 | + |
| 94 | + expect(() => scope.init()).toThrow(error) |
| 95 | + expect(() => scope.codeIndexManager).toThrow("Code index workspace scope is not initialized") |
| 96 | + expect(scope.init()).toBeUndefined() |
| 97 | + expect(scope.codeIndexManager).toBe(vi.mocked(CodeIndexManager).mock.results[1].value) |
| 98 | + expect(CodeIndexManager).toHaveBeenCalledTimes(2) |
| 99 | + }) |
| 100 | +}) |
0 commit comments