From ea6d09389a86ef02f0ba3edff4f8d302ab057195 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Wed, 4 Dec 2024 13:46:04 -0800 Subject: [PATCH] jest utils p2 --- core/jest.config.js | 1 + core/jest.setup-after-env.ts | 2 + core/package-lock.json | 8 +-- core/package.json | 2 +- core/util/withExponentialBackoff.test.ts | 89 +++++++++++------------- extensions/vscode/package-lock.json | 2 +- gui/package-lock.json | 2 +- 7 files changed, 52 insertions(+), 54 deletions(-) diff --git a/core/jest.config.js b/core/jest.config.js index f97054326..d58b0b510 100644 --- a/core/jest.config.js +++ b/core/jest.config.js @@ -19,6 +19,7 @@ export default { __dirname: path.dirname(fileURLToPath(import.meta.url)), __filename: path.resolve(fileURLToPath(import.meta.url)), }, + globalSetup: "/jest.global-setup.ts", setupFilesAfterEnv: ["/jest.setup-after-env.ts"], maxWorkers: 1 // equivalent to CLI --runInBand diff --git a/core/jest.setup-after-env.ts b/core/jest.setup-after-env.ts index b5fbacc11..d92f171dd 100644 --- a/core/jest.setup-after-env.ts +++ b/core/jest.setup-after-env.ts @@ -1,4 +1,5 @@ // https://github.com/mswjs/msw/issues/1576#issuecomment-1482643055 + import { TextDecoder, TextEncoder } from "util"; import { jest } from "@jest/globals"; @@ -10,6 +11,7 @@ if (process.env.DEBUG === "jest") { const globalThis = global as any; +globalThis.jest = jest; globalThis.fetch = fetch; globalThis.Request = Request; globalThis.Response = Response; diff --git a/core/package-lock.json b/core/package-lock.json index 58762590f..3a3f77c43 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -27,7 +27,7 @@ "comment-json": "^4.2.3", "dbinfoz": "^0.14.0", "diff": "^7.0.0", - "dotenv": "^16.4.5", + "dotenv": "^16.4.7", "fastest-levenshtein": "^1.0.16", "follow-redirects": "^1.15.5", "google-auth-library": "^9.14.2", @@ -7882,9 +7882,9 @@ } }, "node_modules/dotenv": { - "version": "16.4.5", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", - "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "version": "16.4.7", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.7.tgz", + "integrity": "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==", "engines": { "node": ">=12" }, diff --git a/core/package.json b/core/package.json index 366b4bd9e..8aa6f9f9c 100644 --- a/core/package.json +++ b/core/package.json @@ -58,7 +58,7 @@ "comment-json": "^4.2.3", "dbinfoz": "^0.14.0", "diff": "^7.0.0", - "dotenv": "^16.4.5", + "dotenv": "^16.4.7", "fastest-levenshtein": "^1.0.16", "follow-redirects": "^1.15.5", "google-auth-library": "^9.14.2", diff --git a/core/util/withExponentialBackoff.test.ts b/core/util/withExponentialBackoff.test.ts index cfea914ed..55bdb6184 100644 --- a/core/util/withExponentialBackoff.test.ts +++ b/core/util/withExponentialBackoff.test.ts @@ -1,41 +1,35 @@ -// core/util/withExponentialBackoff.test.ts - import { withExponentialBackoff, RETRY_AFTER_HEADER, } from "./withExponentialBackoff"; - -// Helper function to simulate delay -const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); +const MAX_RETRIES = 4; +const INITIAL_DELAY_SECONDS = 0.01; describe("withExponentialBackoff", () => { - it("should return result immediately if apiCall is successful", async () => { - const mockApiCall = jest.fn().mockResolvedValue("success"); + // beforeEach(() => { + // jest.useFakeTimers(); + // jest.clearAllMocks(); + // }); + + // afterEach(() => { + // jest.useRealTimers(); + // }); + + it("should return result on first successful API call", async () => { + const apiCall = jest.fn().mockResolvedValue("success"); + + const result = await withExponentialBackoff( + apiCall, + MAX_RETRIES, + INITIAL_DELAY_SECONDS, + ); - const result = await withExponentialBackoff(mockApiCall); expect(result).toBe("success"); - expect(mockApiCall).toHaveBeenCalledTimes(1); + expect(apiCall).toHaveBeenCalledTimes(1); }); - it("should retry on API error 429 and eventually succeed", async () => { - // Mock an API function that returns HTTP 429 twice then succeeds - const mockApiCall = jest - .fn() - .mockRejectedValueOnce({ - response: { status: 429, headers: new Map() }, - }) - .mockRejectedValueOnce({ - response: { status: 429, headers: new Map() }, - }) - .mockResolvedValue("success"); - - const result = await withExponentialBackoff(mockApiCall, 3, 0.001); // Use a short delay for testing - expect(result).toBe("success"); - expect(mockApiCall).toHaveBeenCalledTimes(3); - }); - - it("should respect Retry-After header value for delay", async () => { - const mockApiCall = jest + it("should retry on API rate limit and succeed", async () => { + const apiCall = jest .fn() .mockRejectedValueOnce({ response: { @@ -45,32 +39,33 @@ describe("withExponentialBackoff", () => { }) .mockResolvedValue("success"); - const startTime = Date.now(); - const result = await withExponentialBackoff(mockApiCall, 2, 1); - const endTime = Date.now(); + const result = await withExponentialBackoff( + apiCall, + MAX_RETRIES, + INITIAL_DELAY_SECONDS, + ); expect(result).toBe("success"); - expect(endTime - startTime).toBeGreaterThanOrEqual(1000); // At least 1 second delay - expect(mockApiCall).toHaveBeenCalledTimes(2); + expect(apiCall).toHaveBeenCalledTimes(2); }); - it("should throw error after maxRetries are exhausted", async () => { - const mockApiCall = jest.fn().mockRejectedValue(new Error("Network Error")); + it("should stop retrying after maxRetries and throw error", async () => { + const apiCall = jest.fn().mockRejectedValue({ + response: { status: 429, headers: new Map([[RETRY_AFTER_HEADER, "1"]]) }, + }); - await expect(withExponentialBackoff(mockApiCall, 3, 0.001)).rejects.toThrow( - "Network Error", - ); - expect(mockApiCall).toHaveBeenCalledTimes(3); + await expect( + withExponentialBackoff(apiCall, MAX_RETRIES, INITIAL_DELAY_SECONDS), + ).rejects.toThrow("Failed to make API call after multiple retries"); + expect(apiCall).toHaveBeenCalledTimes(MAX_RETRIES); }); - it("should throw the error immediately if it is not rate limit reached error", async () => { - const mockApiCall = jest - .fn() - .mockRejectedValue(new Error("Unexpected Error")); + it("should throw on non-rate limit errors", async () => { + const apiCall = jest.fn().mockRejectedValue(new Error("Server Error")); - await expect(withExponentialBackoff(mockApiCall, 3, 0.001)).rejects.toThrow( - "Unexpected Error", - ); - expect(mockApiCall).toHaveBeenCalledTimes(1); + await expect( + withExponentialBackoff(apiCall, MAX_RETRIES, INITIAL_DELAY_SECONDS), + ).rejects.toThrow("Server Error"); + expect(apiCall).toHaveBeenCalledTimes(1); }); }); diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 9ac0c6f53..20f94f30a 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -112,7 +112,7 @@ "comment-json": "^4.2.3", "dbinfoz": "^0.14.0", "diff": "^7.0.0", - "dotenv": "^16.4.5", + "dotenv": "^16.4.7", "fastest-levenshtein": "^1.0.16", "follow-redirects": "^1.15.5", "google-auth-library": "^9.14.2", diff --git a/gui/package-lock.json b/gui/package-lock.json index 91410e6df..af3d5cafa 100644 --- a/gui/package-lock.json +++ b/gui/package-lock.json @@ -121,7 +121,7 @@ "comment-json": "^4.2.3", "dbinfoz": "^0.14.0", "diff": "^7.0.0", - "dotenv": "^16.4.5", + "dotenv": "^16.4.7", "fastest-levenshtein": "^1.0.16", "follow-redirects": "^1.15.5", "google-auth-library": "^9.14.2",