|
| 1 | +import { vi } from "vitest"; |
| 2 | +import * as vscode from "vscode"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Mock configuration provider that integrates with the vscode workspace configuration mock. |
| 6 | + * Use this to set configuration values that will be returned by vscode.workspace.getConfiguration(). |
| 7 | + */ |
| 8 | +export class MockConfigurationProvider { |
| 9 | + private config = new Map<string, unknown>(); |
| 10 | + |
| 11 | + /** |
| 12 | + * Set a configuration value that will be returned by vscode.workspace.getConfiguration().get() |
| 13 | + */ |
| 14 | + set(key: string, value: unknown): void { |
| 15 | + this.config.set(key, value); |
| 16 | + this.setupVSCodeMock(); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Get a configuration value (for testing purposes) |
| 21 | + */ |
| 22 | + get<T>(key: string): T | undefined; |
| 23 | + get<T>(key: string, defaultValue: T): T; |
| 24 | + get<T>(key: string, defaultValue?: T): T | undefined { |
| 25 | + const value = this.config.get(key); |
| 26 | + return value !== undefined ? (value as T) : defaultValue; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Clear all configuration values |
| 31 | + */ |
| 32 | + clear(): void { |
| 33 | + this.config.clear(); |
| 34 | + this.setupVSCodeMock(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Setup the vscode.workspace.getConfiguration mock to return our values |
| 39 | + */ |
| 40 | + setupVSCodeMock(): void { |
| 41 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue({ |
| 42 | + get: vi.fn((key: string, defaultValue?: unknown) => { |
| 43 | + const value = this.config.get(key); |
| 44 | + return value !== undefined ? value : defaultValue; |
| 45 | + }), |
| 46 | + has: vi.fn((key: string) => this.config.has(key)), |
| 47 | + inspect: vi.fn(), |
| 48 | + update: vi.fn(), |
| 49 | + } as unknown as vscode.WorkspaceConfiguration); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Mock progress reporter that integrates with vscode.window.withProgress. |
| 55 | + * Use this to control progress reporting behavior and cancellation in tests. |
| 56 | + */ |
| 57 | +export class MockProgressReporter { |
| 58 | + private shouldCancel = false; |
| 59 | + private progressReports: Array<{ message?: string; increment?: number }> = []; |
| 60 | + |
| 61 | + /** |
| 62 | + * Set whether the progress should be cancelled |
| 63 | + */ |
| 64 | + setCancellation(cancel: boolean): void { |
| 65 | + this.shouldCancel = cancel; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get all progress reports that were made |
| 70 | + */ |
| 71 | + getProgressReports(): Array<{ message?: string; increment?: number }> { |
| 72 | + return [...this.progressReports]; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Clear all progress reports |
| 77 | + */ |
| 78 | + clearProgressReports(): void { |
| 79 | + this.progressReports = []; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Setup the vscode.window.withProgress mock |
| 84 | + */ |
| 85 | + setupVSCodeMock(): void { |
| 86 | + vi.mocked(vscode.window.withProgress).mockImplementation( |
| 87 | + async <T>( |
| 88 | + _options: vscode.ProgressOptions, |
| 89 | + task: ( |
| 90 | + progress: vscode.Progress<{ message?: string; increment?: number }>, |
| 91 | + token: vscode.CancellationToken, |
| 92 | + ) => Thenable<T>, |
| 93 | + ): Promise<T> => { |
| 94 | + const progress = { |
| 95 | + report: vi.fn((value: { message?: string; increment?: number }) => { |
| 96 | + this.progressReports.push(value); |
| 97 | + }), |
| 98 | + }; |
| 99 | + |
| 100 | + const cancellationToken: vscode.CancellationToken = { |
| 101 | + isCancellationRequested: this.shouldCancel, |
| 102 | + onCancellationRequested: vi.fn((listener: (x: unknown) => void) => { |
| 103 | + if (this.shouldCancel) { |
| 104 | + setTimeout(listener, 0); |
| 105 | + } |
| 106 | + return { dispose: vi.fn() }; |
| 107 | + }), |
| 108 | + }; |
| 109 | + |
| 110 | + return task(progress, cancellationToken); |
| 111 | + }, |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Mock user interaction that integrates with vscode.window message dialogs. |
| 118 | + * Use this to control user responses in tests. |
| 119 | + */ |
| 120 | +export class MockUserInteraction { |
| 121 | + private responses = new Map<string, string | undefined>(); |
| 122 | + private externalUrls: string[] = []; |
| 123 | + |
| 124 | + /** |
| 125 | + * Set a response for a specific message or set a default response |
| 126 | + */ |
| 127 | + setResponse(response: string | undefined): void; |
| 128 | + setResponse(message: string, response: string | undefined): void; |
| 129 | + setResponse( |
| 130 | + messageOrResponse: string | undefined, |
| 131 | + response?: string | undefined, |
| 132 | + ): void { |
| 133 | + if (response === undefined && messageOrResponse !== undefined) { |
| 134 | + // Single argument - set default response |
| 135 | + this.responses.set("default", messageOrResponse); |
| 136 | + } else if (messageOrResponse !== undefined) { |
| 137 | + // Two arguments - set specific response |
| 138 | + this.responses.set(messageOrResponse, response); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Get all URLs that were opened externally |
| 144 | + */ |
| 145 | + getExternalUrls(): string[] { |
| 146 | + return [...this.externalUrls]; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Clear all external URLs |
| 151 | + */ |
| 152 | + clearExternalUrls(): void { |
| 153 | + this.externalUrls = []; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Clear all responses |
| 158 | + */ |
| 159 | + clearResponses(): void { |
| 160 | + this.responses.clear(); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Setup the vscode.window message dialog mocks |
| 165 | + */ |
| 166 | + setupVSCodeMock(): void { |
| 167 | + const getResponse = (message: string): string | undefined => { |
| 168 | + return this.responses.get(message) ?? this.responses.get("default"); |
| 169 | + }; |
| 170 | + |
| 171 | + vi.mocked(vscode.window.showErrorMessage).mockImplementation( |
| 172 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 173 | + (message: string): Thenable<any> => { |
| 174 | + const response = getResponse(message); |
| 175 | + return Promise.resolve(response); |
| 176 | + }, |
| 177 | + ); |
| 178 | + |
| 179 | + vi.mocked(vscode.window.showWarningMessage).mockImplementation( |
| 180 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 181 | + (message: string): Thenable<any> => { |
| 182 | + const response = getResponse(message); |
| 183 | + return Promise.resolve(response); |
| 184 | + }, |
| 185 | + ); |
| 186 | + |
| 187 | + vi.mocked(vscode.window.showInformationMessage).mockImplementation( |
| 188 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 189 | + (message: string): Thenable<any> => { |
| 190 | + const response = getResponse(message); |
| 191 | + return Promise.resolve(response); |
| 192 | + }, |
| 193 | + ); |
| 194 | + |
| 195 | + vi.mocked(vscode.env.openExternal).mockImplementation( |
| 196 | + (target: vscode.Uri): Promise<boolean> => { |
| 197 | + this.externalUrls.push(target.toString()); |
| 198 | + return Promise.resolve(true); |
| 199 | + }, |
| 200 | + ); |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +/** |
| 205 | + * Helper function to setup all VS Code mocks for testing. |
| 206 | + * Call this in your test setup to initialize all the mock integrations. |
| 207 | + */ |
| 208 | +export function setupVSCodeMocks(): { |
| 209 | + mockConfig: MockConfigurationProvider; |
| 210 | + mockProgress: MockProgressReporter; |
| 211 | + mockUI: MockUserInteraction; |
| 212 | +} { |
| 213 | + const mockConfig = new MockConfigurationProvider(); |
| 214 | + const mockProgress = new MockProgressReporter(); |
| 215 | + const mockUI = new MockUserInteraction(); |
| 216 | + |
| 217 | + // Setup all the VS Code API mocks |
| 218 | + mockConfig.setupVSCodeMock(); |
| 219 | + mockProgress.setupVSCodeMock(); |
| 220 | + mockUI.setupVSCodeMock(); |
| 221 | + |
| 222 | + return { mockConfig, mockProgress, mockUI }; |
| 223 | +} |
0 commit comments