-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathtestHelpers.ts
More file actions
836 lines (747 loc) · 20.9 KB
/
testHelpers.ts
File metadata and controls
836 lines (747 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
import axios, {
AxiosError,
AxiosHeaders,
type AxiosAdapter,
type AxiosResponse,
type InternalAxiosRequestConfig,
} from "axios";
import { vi } from "vitest";
import * as vscode from "vscode";
import type { Experiment, User } from "coder/site/src/api/typesGenerated";
import type { IncomingMessage } from "node:http";
import type { CoderApi } from "@/api/coderApi";
import type { CliCredentialManager } from "@/core/cliCredentialManager";
import type { Logger } from "@/logging/logger";
/**
* Mock configuration provider that integrates with the vscode workspace configuration mock.
* Use this to set configuration values that will be returned by vscode.workspace.getConfiguration().
*/
export class MockConfigurationProvider {
private readonly config = new Map<string, unknown>();
constructor() {
this.setupVSCodeMock();
}
/**
* Set a configuration value that will be returned by vscode.workspace.getConfiguration().get()
* Automatically fires onDidChangeConfiguration event (emulating VS Code behavior).
*/
set(key: string, value: unknown): void {
this.config.set(key, value);
this.fireConfigChangeEvent(key);
}
/**
* Get a configuration value (for testing purposes)
*/
get<T>(key: string): T | undefined;
get<T>(key: string, defaultValue: T): T;
get<T>(key: string, defaultValue?: T): T | undefined {
const value = this.config.get(key);
return value === undefined ? defaultValue : (value as T);
}
/**
* Clear all configuration values
*/
clear(): void {
this.config.clear();
}
/**
* Fire configuration change event for a specific setting.
*/
private fireConfigChangeEvent(setting: string): void {
const fireEvent = (
vscode.workspace as typeof vscode.workspace & {
__fireDidChangeConfiguration: (
e: vscode.ConfigurationChangeEvent,
) => void;
}
).__fireDidChangeConfiguration;
fireEvent({
affectsConfiguration: (section: string) => section === setting,
});
}
/**
* Setup the vscode.workspace.getConfiguration mock to return our values
*/
private setupVSCodeMock(): void {
vi.mocked(vscode.workspace.getConfiguration).mockImplementation(
(section?: string) => {
// Create a snapshot of the current config when getConfiguration is called
const snapshot = new Map(this.config);
const getFullKey = (part: string) =>
section ? `${section}.${part}` : part;
return {
get: vi.fn((key: string, defaultValue?: unknown) => {
const value = snapshot.get(getFullKey(key));
return value === undefined ? defaultValue : value;
}),
has: vi.fn((key: string) => {
return snapshot.has(getFullKey(key));
}),
inspect: vi.fn(),
update: vi.fn((key: string, value: unknown) => {
this.set(getFullKey(key), value);
return Promise.resolve();
}),
};
},
);
}
}
/**
* Mock progress reporter that integrates with vscode.window.withProgress.
* Use this to control progress reporting behavior and cancellation in tests.
*/
export class MockProgressReporter {
private shouldCancel = false;
private progressReports: Array<{ message?: string; increment?: number }> = [];
constructor() {
this.setupVSCodeMock();
}
/**
* Set whether the progress should be cancelled
*/
setCancellation(cancel: boolean): void {
this.shouldCancel = cancel;
}
/**
* Get all progress reports that were made
*/
getProgressReports(): Array<{ message?: string; increment?: number }> {
return [...this.progressReports];
}
/**
* Clear all progress reports
*/
clearProgressReports(): void {
this.progressReports = [];
}
/**
* Setup the vscode.window.withProgress mock
*/
private setupVSCodeMock(): void {
vi.mocked(vscode.window.withProgress).mockImplementation(
async <T>(
_options: vscode.ProgressOptions,
task: (
progress: vscode.Progress<{ message?: string; increment?: number }>,
token: vscode.CancellationToken,
) => Thenable<T>,
): Promise<T> => {
const progress = {
report: vi.fn((value: { message?: string; increment?: number }) => {
this.progressReports.push(value);
}),
};
const cancellationToken: vscode.CancellationToken = {
isCancellationRequested: this.shouldCancel,
onCancellationRequested: vi.fn((listener: (x: unknown) => void) => {
if (this.shouldCancel) {
setTimeout(listener, 0);
}
return { dispose: vi.fn() };
}),
};
return task(progress, cancellationToken);
},
);
}
}
/**
* Mock user interaction that integrates with vscode.window message dialogs and input boxes.
* Use this to control user responses in tests.
*/
export class MockUserInteraction {
private readonly responses = new Map<string, string | undefined>();
private inputBoxValue: string | undefined;
private inputBoxValidateInput: ((value: string) => Promise<void>) | undefined;
private externalUrls: string[] = [];
constructor() {
this.setupVSCodeMock();
}
/**
* Set a response for a specific message dialog
*/
setResponse(message: string, response: string | undefined): void {
this.responses.set(message, response);
}
/**
* Set the value to return from showInputBox.
* Pass undefined to simulate user cancelling.
*/
setInputBoxValue(value: string | undefined): void {
this.inputBoxValue = value;
}
/**
* Set a custom validateInput handler for showInputBox.
* This allows tests to simulate the validation callback behavior.
*/
setInputBoxValidateInput(fn: (value: string) => Promise<void>): void {
this.inputBoxValidateInput = fn;
}
/**
* Get all URLs that were opened externally
*/
getExternalUrls(): string[] {
return [...this.externalUrls];
}
/**
* Clear all external URLs
*/
clearExternalUrls(): void {
this.externalUrls = [];
}
/**
* Clear all responses and input box values
*/
clear(): void {
this.responses.clear();
this.inputBoxValue = undefined;
this.inputBoxValidateInput = undefined;
this.externalUrls = [];
}
/**
* Setup the vscode.window message dialog mocks
*/
private setupVSCodeMock(): void {
const getResponse = (message: string): string | undefined => {
return this.responses.get(message);
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleMessage = (message: string): Thenable<any> => {
const response = getResponse(message);
return Promise.resolve(response);
};
vi.mocked(vscode.window.showErrorMessage).mockImplementation(handleMessage);
vi.mocked(vscode.window.showWarningMessage).mockImplementation(
handleMessage,
);
vi.mocked(vscode.window.showInformationMessage).mockImplementation(
handleMessage,
);
vi.mocked(vscode.env.openExternal).mockImplementation(
(target: vscode.Uri): Promise<boolean> => {
this.externalUrls.push(target.toString());
return Promise.resolve(true);
},
);
vi.mocked(vscode.window.showInputBox).mockImplementation(
async (options?: vscode.InputBoxOptions) => {
const value = this.inputBoxValue;
if (value === undefined) {
return undefined; // User cancelled
}
if (options?.validateInput) {
const validationResult = await options.validateInput(value);
if (validationResult) {
// Validation failed - in real VS Code this would show error
// For tests, we can use the custom handler or return undefined
if (this.inputBoxValidateInput) {
await this.inputBoxValidateInput(value);
}
return undefined;
}
} else if (this.inputBoxValidateInput) {
// Run custom validation handler even without options.validateInput
await this.inputBoxValidateInput(value);
}
return value;
},
);
}
}
// Simple in-memory implementation of Memento
export class InMemoryMemento implements vscode.Memento {
private readonly storage = new Map<string, unknown>();
get<T>(key: string): T | undefined;
get<T>(key: string, defaultValue: T): T;
get<T>(key: string, defaultValue?: T): T | undefined {
return this.storage.has(key) ? (this.storage.get(key) as T) : defaultValue;
}
async update(key: string, value: unknown): Promise<void> {
if (value === undefined) {
this.storage.delete(key);
} else {
this.storage.set(key, value);
}
return Promise.resolve();
}
keys(): readonly string[] {
return Array.from(this.storage.keys());
}
}
// Simple in-memory implementation of SecretStorage
export class InMemorySecretStorage implements vscode.SecretStorage {
private readonly secrets = new Map<string, string>();
private isCorrupted = false;
private readonly listeners: Array<
(e: vscode.SecretStorageChangeEvent) => void
> = [];
onDidChange: vscode.Event<vscode.SecretStorageChangeEvent> = (listener) => {
this.listeners.push(listener);
return {
dispose: () => {
const index = this.listeners.indexOf(listener);
if (index > -1) {
this.listeners.splice(index, 1);
}
},
};
};
async get(key: string): Promise<string | undefined> {
if (this.isCorrupted) {
return Promise.reject(new Error("Storage corrupted"));
}
return this.secrets.get(key);
}
async store(key: string, value: string): Promise<void> {
if (this.isCorrupted) {
return Promise.reject(new Error("Storage corrupted"));
}
const oldValue = this.secrets.get(key);
this.secrets.set(key, value);
if (oldValue !== value) {
this.fireChangeEvent(key);
}
}
async delete(key: string): Promise<void> {
if (this.isCorrupted) {
return Promise.reject(new Error("Storage corrupted"));
}
const hadKey = this.secrets.has(key);
this.secrets.delete(key);
if (hadKey) {
this.fireChangeEvent(key);
}
}
keys(): Promise<string[]> {
if (this.isCorrupted) {
return Promise.reject(new Error("Storage corrupted"));
}
return Promise.resolve(Array.from(this.secrets.keys()));
}
corruptStorage(): void {
this.isCorrupted = true;
}
private fireChangeEvent(key: string): void {
const event: vscode.SecretStorageChangeEvent = { key };
this.listeners.forEach((listener) => listener(event));
}
}
export function createMockCliCredentialManager(): CliCredentialManager {
return {
storeToken: vi.fn().mockResolvedValue(undefined),
readToken: vi.fn().mockResolvedValue(undefined),
deleteToken: vi.fn().mockResolvedValue(undefined),
} as unknown as CliCredentialManager;
}
export function createMockLogger(): Logger {
return {
trace: vi.fn(),
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
show: vi.fn(),
};
}
export function createMockStream(
content: string,
options: {
chunkSize?: number;
delay?: number;
// If defined will throw an error instead of closing normally
error?: NodeJS.ErrnoException;
} = {},
): IncomingMessage {
const { chunkSize = 8, delay = 1, error } = options;
const buffer = Buffer.from(content);
let position = 0;
let closeCallback: ((...args: unknown[]) => void) | null = null;
let errorCallback: ((error: Error) => void) | null = null;
return {
on: vi.fn((event: string, callback: (...args: unknown[]) => void) => {
if (event === "data") {
const sendChunk = () => {
if (position < buffer.length) {
const chunk = buffer.subarray(
position,
Math.min(position + chunkSize, buffer.length),
);
position += chunkSize;
callback(chunk);
if (position < buffer.length) {
setTimeout(sendChunk, delay);
} else {
setImmediate(() => {
if (error && errorCallback) {
errorCallback(error);
} else if (closeCallback) {
closeCallback();
}
});
}
}
};
setTimeout(sendChunk, delay);
} else if (event === "error") {
errorCallback = callback;
} else if (event === "close") {
closeCallback = callback;
}
}),
destroy: vi.fn(),
} as unknown as IncomingMessage;
}
/**
* Mock status bar that integrates with vscode.window.createStatusBarItem.
* Use this to inspect status bar state in tests.
*/
export class MockStatusBar {
text = "";
tooltip: string | vscode.MarkdownString = "";
backgroundColor: vscode.ThemeColor | undefined;
color: string | vscode.ThemeColor | undefined;
command: string | vscode.Command | undefined;
accessibilityInformation: vscode.AccessibilityInformation | undefined;
name: string | undefined;
priority: number | undefined;
alignment: vscode.StatusBarAlignment = vscode.StatusBarAlignment.Left;
readonly show = vi.fn();
readonly hide = vi.fn();
readonly dispose = vi.fn();
constructor() {
this.setupVSCodeMock();
}
/**
* Reset all status bar state
*/
reset(): void {
this.text = "";
this.tooltip = "";
this.backgroundColor = undefined;
this.color = undefined;
this.command = undefined;
this.show.mockClear();
this.hide.mockClear();
this.dispose.mockClear();
}
/**
* Setup the vscode.window.createStatusBarItem mock
*/
private setupVSCodeMock(): void {
vi.mocked(vscode.window.createStatusBarItem).mockReturnValue(
this as unknown as vscode.StatusBarItem,
);
}
}
/**
* Mock CoderApi for testing. Tracks method calls and allows controlling responses.
*/
export class MockCoderApi implements Pick<
CoderApi,
| "setHost"
| "setSessionToken"
| "setCredentials"
| "getHost"
| "getAuthenticatedUser"
| "dispose"
| "getExperiments"
> {
private _host: string | undefined;
private _token: string | undefined;
private _disposed = false;
private authenticatedUser: User | Error | undefined;
readonly setHost = vi.fn((host: string | undefined) => {
this._host = host;
});
readonly setSessionToken = vi.fn((token: string) => {
this._token = token;
});
readonly setCredentials = vi.fn(
(host: string | undefined, token: string | undefined) => {
this._host = host;
this._token = token;
},
);
readonly getHost = vi.fn(() => this._host);
readonly getAuthenticatedUser = vi.fn((): Promise<User> => {
if (this.authenticatedUser instanceof Error) {
return Promise.reject(this.authenticatedUser);
}
if (!this.authenticatedUser) {
return Promise.reject(new Error("Not authenticated"));
}
return Promise.resolve(this.authenticatedUser);
});
readonly dispose = vi.fn(() => {
this._disposed = true;
});
readonly getExperiments = vi.fn(
(): Promise<Experiment[]> => Promise.resolve([]),
);
/**
* Get current host (for assertions)
*/
get host(): string | undefined {
return this._host;
}
/**
* Get current token (for assertions)
*/
get token(): string | undefined {
return this._token;
}
/**
* Check if dispose was called (for assertions)
*/
get disposed(): boolean {
return this._disposed;
}
/**
* Set the authenticated user that will be returned by getAuthenticatedUser.
* Pass an Error to make getAuthenticatedUser reject.
*/
setAuthenticatedUserResponse(user: User | Error | undefined): void {
this.authenticatedUser = user;
}
}
/**
* Mock OAuthSessionManager for testing.
* Provides no-op implementations of all public methods.
*/
export class MockOAuthSessionManager {
readonly setDeployment = vi.fn().mockResolvedValue(undefined);
readonly clearDeployment = vi.fn();
readonly login = vi.fn().mockResolvedValue({ access_token: "test-token" });
readonly handleCallback = vi.fn().mockResolvedValue(undefined);
readonly refreshToken = vi
.fn()
.mockResolvedValue({ access_token: "test-token" });
readonly revokeRefreshToken = vi.fn().mockResolvedValue(undefined);
readonly isLoggedInWithOAuth = vi.fn().mockResolvedValue(false);
readonly clearOAuthState = vi.fn().mockResolvedValue(undefined);
readonly dispose = vi.fn();
}
export class MockOAuthInterceptor {
readonly setDeployment = vi.fn().mockResolvedValue(undefined);
readonly clearDeployment = vi.fn();
readonly dispose = vi.fn();
}
/**
* Create a mock User for testing.
*/
export function createMockUser(overrides: Partial<User> = {}): User {
return {
id: "user-123",
username: "testuser",
email: "test@example.com",
name: "Test User",
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
last_seen_at: new Date().toISOString(),
status: "active",
organization_ids: [],
roles: [],
avatar_url: "",
login_type: "password",
theme_preference: "",
...overrides,
};
}
/**
* Creates an AxiosError for testing.
*/
export function createAxiosError(
status: number,
message: string,
config: Record<string, unknown> = {},
): AxiosError {
const error = new AxiosError(
message,
"ERR_BAD_REQUEST",
undefined,
undefined,
{
status,
statusText: message,
headers: {},
config: { headers: new AxiosHeaders() },
data: {},
},
);
error.config = { headers: new AxiosHeaders(), ...config };
return error;
}
type MockAdapterFn = ReturnType<typeof vi.fn<AxiosAdapter>>;
const AXIOS_MOCK_SETUP_EXAMPLE = `
vi.mock("axios", async () => {
const actual = await vi.importActual<typeof import("axios")>("axios");
const mockAdapter = vi.fn();
return {
...actual,
default: {
...actual.default,
create: vi.fn((config) =>
actual.default.create({ ...config, adapter: mockAdapter }),
),
__mockAdapter: mockAdapter,
},
};
});`;
/**
* Gets the mock axios adapter from the mocked axios module.
* The axios module must be mocked with __mockAdapter exposed.
*
* @throws Error if axios mock is not set up correctly, with instructions on how to fix it
*/
export function getAxiosMockAdapter(): MockAdapterFn {
const axiosWithMock = axios as typeof axios & {
__mockAdapter?: MockAdapterFn;
};
const mockAdapter = axiosWithMock.__mockAdapter;
if (!mockAdapter) {
throw new Error(
"Axios mock adapter not found. Make sure to mock axios with __mockAdapter:\n" +
AXIOS_MOCK_SETUP_EXAMPLE,
);
}
return mockAdapter;
}
/**
* Sets up mock routes for the axios adapter.
*
* Route values can be:
* - Any data: Returns 200 OK with that data
* - Error instance: Rejects with that error
*
* If no route matches, rejects with a 404 AxiosError.
*
* @example
* ```ts
* setupAxiosMockRoutes(mockAdapter, {
* "/.well-known/oauth": metadata, // Returns 200 with metadata
* "/oauth2/register": new Error("Registration failed"), // Throws error
* "/api/v2/users/me": user, // Returns 200 with user
* });
* ```
*/
export function setupAxiosMockRoutes(
mockAdapter: MockAdapterFn,
routes: Record<string, unknown>,
): void {
mockAdapter.mockImplementation(
async (
config: InternalAxiosRequestConfig,
): Promise<AxiosResponse<unknown>> => {
for (const [pattern, value] of Object.entries(routes)) {
if (config.url?.includes(pattern)) {
if (value instanceof Error) {
throw value;
}
const data =
typeof value === "function" ? await value(config) : value;
return {
data,
status: 200,
statusText: "OK",
headers: new AxiosHeaders(),
config,
};
}
}
const error = new AxiosError(
`Request failed with status code 404`,
"ERR_BAD_REQUEST",
undefined,
undefined,
{
status: 404,
statusText: "Not Found",
headers: new AxiosHeaders(),
config,
data: {
message: "Not found",
detail: `No route matched: ${config.url}`,
},
},
);
throw error;
},
);
}
/**
* A mock vscode.Progress implementation that tracks all reported progress.
* Use this when testing code that accepts a Progress parameter directly.
*/
export class MockProgress<
T = { message?: string; increment?: number },
> implements vscode.Progress<T> {
private readonly reports: T[] = [];
readonly report = vi.fn((value: T) => {
this.reports.push(value);
});
/**
* Get all progress reports that have been made.
*/
getReports(): readonly T[] {
return this.reports;
}
/**
* Clear all recorded reports.
*/
clear(): void {
this.reports.length = 0;
this.report.mockClear();
}
}
/**
* A mock vscode.CancellationToken that can be programmatically cancelled.
* Use this when testing code that accepts a CancellationToken parameter directly.
*/
export class MockCancellationToken implements vscode.CancellationToken {
private _isCancellationRequested: boolean;
private readonly listeners: Array<(e: unknown) => void> = [];
constructor(initialCancelled = false) {
this._isCancellationRequested = initialCancelled;
}
get isCancellationRequested(): boolean {
return this._isCancellationRequested;
}
onCancellationRequested: vscode.Event<unknown> = (
listener: (e: unknown) => void,
) => {
this.listeners.push(listener);
// If already cancelled, fire immediately (async to match VS Code behavior)
if (this._isCancellationRequested) {
setTimeout(() => listener(undefined), 0);
}
return {
dispose: () => {
const index = this.listeners.indexOf(listener);
if (index > -1) {
this.listeners.splice(index, 1);
}
},
};
};
/**
* Trigger cancellation. This will:
* - Set isCancellationRequested to true
* - Fire all registered cancellation listeners
*/
cancel(): void {
if (this._isCancellationRequested) {
return; // Already cancelled
}
this._isCancellationRequested = true;
for (const listener of this.listeners) {
listener(undefined);
}
}
/**
* Reset to uncancelled state. Useful for reusing the token across tests.
*/
reset(): void {
this._isCancellationRequested = false;
}
}