Skip to content

Commit 784bb1f

Browse files
committed
Review comments 1
1 parent ac418d1 commit 784bb1f

File tree

3 files changed

+38
-44
lines changed

3 files changed

+38
-44
lines changed

src/__mocks__/testHelpers.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,27 @@ export class MockConfigurationProvider {
4141
*/
4242
private setupVSCodeMock(): void {
4343
vi.mocked(vscode.workspace.getConfiguration).mockImplementation(
44-
(section?: string) =>
45-
({
44+
(section?: string) => {
45+
// Create a snapshot of the current config when getConfiguration is called
46+
const snapshot = new Map(this.config);
47+
const getFullKey = (part: string) =>
48+
section ? `${section}.${part}` : part;
49+
50+
return {
4651
get: vi.fn((key: string, defaultValue?: unknown) => {
47-
const fullKey = section ? `${section}.${key}` : key;
48-
const value = this.config.get(fullKey);
52+
const value = snapshot.get(getFullKey(key));
4953
return value !== undefined ? value : defaultValue;
5054
}),
5155
has: vi.fn((key: string) => {
52-
const fullKey = section ? `${section}.${key}` : key;
53-
return this.config.has(fullKey);
56+
return snapshot.has(getFullKey(key));
5457
}),
5558
inspect: vi.fn(),
56-
update: vi.fn(),
57-
}) as unknown as vscode.WorkspaceConfiguration,
59+
update: vi.fn((key: string, value: unknown) => {
60+
this.config.set(getFullKey(key), value);
61+
return Promise.resolve();
62+
}),
63+
};
64+
},
5865
);
5966
}
6067
}
@@ -139,21 +146,10 @@ export class MockUserInteraction {
139146
}
140147

141148
/**
142-
* Set a response for a specific message or set a default response
149+
* Set a response for a specific message
143150
*/
144-
setResponse(response: string | undefined): void;
145-
setResponse(message: string, response: string | undefined): void;
146-
setResponse(
147-
messageOrResponse: string | undefined,
148-
response?: string | undefined,
149-
): void {
150-
if (response === undefined && messageOrResponse !== undefined) {
151-
// Single argument - set default response
152-
this.responses.set("default", messageOrResponse);
153-
} else if (messageOrResponse !== undefined) {
154-
// Two arguments - set specific response
155-
this.responses.set(messageOrResponse, response);
156-
}
151+
setResponse(message: string, response: string | undefined): void {
152+
this.responses.set(message, response);
157153
}
158154

159155
/**
@@ -182,7 +178,7 @@ export class MockUserInteraction {
182178
*/
183179
private setupVSCodeMock(): void {
184180
const getResponse = (message: string): string | undefined => {
185-
return this.responses.get(message) ?? this.responses.get("default");
181+
return this.responses.get(message);
186182
};
187183

188184
vi.mocked(vscode.window.showErrorMessage).mockImplementation(

src/core/binaryManager.test.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,10 @@ describe("BinaryManager", () => {
256256

257257
it("handles 404 platform not supported", async () => {
258258
withHttpResponse(404);
259-
mockUI.setResponse("Open an Issue");
259+
mockUI.setResponse(
260+
"Coder isn't supported for your platform. Please open an issue, we'd love to support it!",
261+
"Open an Issue",
262+
);
260263
await expect(manager.fetchBinary(mockApi, "test")).rejects.toThrow(
261264
"Platform not supported",
262265
);
@@ -271,7 +274,10 @@ describe("BinaryManager", () => {
271274

272275
it("handles server errors", async () => {
273276
withHttpResponse(500);
274-
mockUI.setResponse("Open an Issue");
277+
mockUI.setResponse(
278+
"Failed to download binary. Please open an issue.",
279+
"Open an Issue",
280+
);
275281
await expect(manager.fetchBinary(mockApi, "test")).rejects.toThrow(
276282
"Failed to download binary",
277283
);
@@ -352,16 +358,10 @@ describe("BinaryManager", () => {
352358
it("tries fallback signature on 404", async () => {
353359
withSuccessfulDownload();
354360
withSignatureResponses([404, 200]);
355-
mockUI.setResponse("Download signature");
361+
mockUI.setResponse("Signature not found", "Download signature");
356362
const result = await manager.fetchBinary(mockApi, "test");
357363
expect(result).toBe(BINARY_PATH);
358-
expect(vscode.window.showWarningMessage).toHaveBeenCalledWith(
359-
"Signature not found",
360-
expect.any(Object),
361-
expect.any(String),
362-
expect.any(String),
363-
);
364-
// First download and when verfiying twice (404 then 200)
364+
// First download and then verfiying twice (404 then 200)
365365
expect(mockAxios.get).toHaveBeenCalledTimes(3);
366366
});
367367

@@ -371,7 +371,7 @@ describe("BinaryManager", () => {
371371
vi.mocked(pgp.verifySignature).mockRejectedValueOnce(
372372
createVerificationError("Invalid signature"),
373373
);
374-
mockUI.setResponse("Run anyway");
374+
mockUI.setResponse("Signature does not match", "Run anyway");
375375
const result = await manager.fetchBinary(mockApi, "test");
376376
expect(result).toBe(BINARY_PATH);
377377
expect(mockLogger.info).toHaveBeenCalledWith(
@@ -385,7 +385,7 @@ describe("BinaryManager", () => {
385385
vi.mocked(pgp.verifySignature).mockRejectedValueOnce(
386386
createVerificationError("Invalid signature"),
387387
);
388-
mockUI.setResponse(undefined);
388+
mockUI.setResponse("Signature does not match", undefined);
389389
await expect(manager.fetchBinary(mockApi, "test")).rejects.toThrow(
390390
"Signature verification aborted",
391391
);
@@ -405,7 +405,7 @@ describe("BinaryManager", () => {
405405
it("allows skipping verification on 404", async () => {
406406
withSuccessfulDownload();
407407
withHttpResponse(404);
408-
mockUI.setResponse("Run without verification");
408+
mockUI.setResponse("Signature not found", "Run without verification");
409409
const result = await manager.fetchBinary(mockApi, "test");
410410
expect(result).toBe(BINARY_PATH);
411411
expect(pgp.verifySignature).not.toHaveBeenCalled();
@@ -417,21 +417,19 @@ describe("BinaryManager", () => {
417417
it("handles signature download failure", async () => {
418418
withSuccessfulDownload();
419419
withHttpResponse(500);
420-
mockUI.setResponse("Run without verification");
421-
const result = await manager.fetchBinary(mockApi, "test");
422-
expect(result).toBe(BINARY_PATH);
423-
expect(vscode.window.showWarningMessage).toHaveBeenCalledWith(
420+
mockUI.setResponse(
424421
"Failed to download signature",
425-
expect.any(Object),
426-
"Download signature", // from the next source
427422
"Run without verification",
428423
);
424+
const result = await manager.fetchBinary(mockApi, "test");
425+
expect(result).toBe(BINARY_PATH);
426+
// TODO test that a file was run?
429427
});
430428

431429
it("aborts when user declines missing signature", async () => {
432430
withSuccessfulDownload();
433431
withHttpResponse(404);
434-
mockUI.setResponse(undefined); // User cancels
432+
mockUI.setResponse("Signature not found", undefined); // User cancels
435433
await expect(manager.fetchBinary(mockApi, "test")).rejects.toThrow(
436434
"Signature download aborted",
437435
);
@@ -602,6 +600,7 @@ describe("BinaryManager", () => {
602600
pgp.VerificationErrorCode.Invalid,
603601
msg,
604602
);
603+
vi.mocked(error.summary).mockReturnValue("Signature does not match");
605604
return error;
606605
}
607606
});

src/error.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ describe("Certificate errors", () => {
1919
(process.versions.electron || process.env.ELECTRON_RUN_AS_NODE) &&
2020
!process.env.VSCODE_PID; // Running from the test explorer in VS Code
2121

22-
// TODO: Remove the vscode mock once we revert the testing framework.
2322
beforeAll(() => {
2423
vi.mock("vscode", () => {
2524
return {};

0 commit comments

Comments
 (0)