|
1 | 1 | import * as vscode from "vscode" |
2 | | -import { makeUri, makeTextDocument, makeTextEditor, makeExtensionContext } from "../../../test-utils/vscode" |
| 2 | +import { |
| 3 | + makeUri, |
| 4 | + makeTextDocument, |
| 5 | + makeTextEditor, |
| 6 | + makeExtensionContext, |
| 7 | + makeWorkspaceConfiguration, |
| 8 | +} from "../../../test-utils/vscode" |
3 | 9 | import { CodeIndexManager } from "../manager" |
4 | 10 | import { CodeIndexStateManager } from "../state-manager" |
5 | 11 | import { CodeIndexStatusManager, type CodeIndexStatus } from "../code-index-status-manager" |
6 | 12 |
|
| 13 | +// Reload the real workspace resolver against this suite's VS Code mock, |
| 14 | +// rather than the VS Code instance cached by the global test setup. |
| 15 | +vi.hoisted(() => vi.resetModules()) |
| 16 | + |
7 | 17 | vi.mock("../../../core/webview/ClineProvider", () => ({ ClineProvider: { getAllInstances: vi.fn(() => []) } })) |
8 | 18 | vi.mock("../manager") |
9 | 19 | vi.mock("../state-manager") |
10 | 20 |
|
11 | 21 | vi.mock("vscode", () => ({ |
12 | 22 | window: { onDidChangeActiveTextEditor: vi.fn(), activeTextEditor: undefined }, |
13 | | - workspace: { workspaceFolders: undefined, getWorkspaceFolder: vi.fn() }, |
| 23 | + workspace: { workspaceFolders: undefined, getWorkspaceFolder: vi.fn(), getConfiguration: vi.fn() }, |
14 | 24 | })) |
15 | 25 |
|
16 | 26 | function makeSource(workspacePath: string) { |
@@ -52,6 +62,7 @@ describe("CodeIndexStatusManager", () => { |
52 | 62 |
|
53 | 63 | beforeEach(() => { |
54 | 64 | vi.clearAllMocks() |
| 65 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(makeWorkspaceConfiguration()) |
55 | 66 | Object.defineProperty(vscode.window, "activeTextEditor", { configurable: true, value: undefined }) |
56 | 67 | Object.defineProperty(vscode.workspace, "workspaceFolders", { |
57 | 68 | configurable: true, |
@@ -89,6 +100,48 @@ describe("CodeIndexStatusManager", () => { |
89 | 100 | manager.dispose() |
90 | 101 | }) |
91 | 102 |
|
| 103 | + it.each([ |
| 104 | + ["firstFolder", "/first"], |
| 105 | + ["activeEditor", "/second"], |
| 106 | + ])("publishes status for the configured %s workspace", (strategy, expectedPath) => { |
| 107 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue( |
| 108 | + makeWorkspaceConfiguration({ "workspace.rootResolution": strategy }), |
| 109 | + ) |
| 110 | + const firstFolder = { uri: makeUri("/first"), name: "first", index: 0 } |
| 111 | + const secondFolder = { uri: makeUri("/second"), name: "second", index: 1 } |
| 112 | + Object.defineProperty(vscode.workspace, "workspaceFolders", { |
| 113 | + configurable: true, |
| 114 | + value: [firstFolder, secondFolder], |
| 115 | + }) |
| 116 | + Object.defineProperty(vscode.window, "activeTextEditor", { |
| 117 | + configurable: true, |
| 118 | + value: makeTextEditor({ document: makeTextDocument({ uri: makeUri("/second/file.ts") }) }), |
| 119 | + }) |
| 120 | + vi.mocked(vscode.workspace.getWorkspaceFolder).mockReturnValue(secondFolder) |
| 121 | + const first = makeSource("/first") |
| 122 | + const second = makeSource("/second") |
| 123 | + const resolve = vi.fn((path: string) => (path === "/first" ? first : second)) |
| 124 | + const publish = vi.fn() |
| 125 | + const manager = new CodeIndexStatusManager(resolve) |
| 126 | + manager["publishStatus"] = publish |
| 127 | + try { |
| 128 | + manager.init() |
| 129 | + expect(resolve).toHaveBeenLastCalledWith(expectedPath) |
| 130 | + const selected = expectedPath === "/first" ? first : second |
| 131 | + const other = selected === first ? second : first |
| 132 | + expect(publish).toHaveBeenCalledExactlyOnceWith(selected.status) |
| 133 | + expect(other.onProgressUpdate).not.toHaveBeenCalled() |
| 134 | + editorChanged() |
| 135 | + selected.status.message = "Updated" |
| 136 | + selected.emit() |
| 137 | + expect(publish).toHaveBeenLastCalledWith(selected.status) |
| 138 | + expect(publish).toHaveBeenCalledTimes(2) |
| 139 | + expect(selected.onProgressUpdate).toHaveBeenCalledTimes(1) |
| 140 | + } finally { |
| 141 | + manager.dispose() |
| 142 | + } |
| 143 | + }) |
| 144 | + |
92 | 145 | it.each([undefined, []])("does not request a manager without a workspace: %s", (folders) => { |
93 | 146 | Object.defineProperty(vscode.workspace, "workspaceFolders", { configurable: true, value: folders }) |
94 | 147 | const resolve = vi.fn() |
|
0 commit comments