diff --git a/.continue/prompts/core-util-test.prompt b/.continue/prompts/core-util-test.prompt index 19202ddee..3985432dc 100644 --- a/.continue/prompts/core-util-test.prompt +++ b/.continue/prompts/core-util-test.prompt @@ -1,7 +1,11 @@ name: Write Core Utility Test description: Generate unit tests for core utilities --- -Write jest tests for the provided code. Use best practices. Be clear and consise. +Write jest tests for the provided code. +Use jest version ^29 (e.g. jest 29.7.0) + +Do NOT write tests for core/test/util/fixtures.ts` or `core/gui/src/components/mainInput/types.d.ts`, only use them as helpers for testing other files. +Use best practices. Be clear and consise. Aim for 100% code coverage where reasonable. Multiple tests can be written, split up tests for best clarity and readability. Only use typescript, and if the file/code is not typescript, warn the user. @@ -18,4 +22,4 @@ Existing test fixtures can be found in: - import and use `ideSettingsPromise` for any IdeSettings needs // This note is for if using edit mode to encourage it to create new files -The tests are to be put in a new file with the same filepath but with a .test.ts extension rather than a .ts extension \ No newline at end of file +The tests are to be put in a new file with the same filepath but with a .test.ts extension rather than a .ts extension diff --git a/core/jest.config.js b/core/jest.config.js index b92d03e5e..f97054326 100644 --- a/core/jest.config.js +++ b/core/jest.config.js @@ -8,8 +8,8 @@ export default { moduleNameMapper: { "^(\\.{1,2}/.*)\\.js$": "$1", "^uuid$": "uuid", // https://stackoverflow.com/a/73626360 - '^@azure/(.*)$': '/node_modules/@azure/$1', - '^mssql$': '/node_modules/mssql' + "^@azure/(.*)$": "/node_modules/@azure/$1", + "^mssql$": "/node_modules/mssql", }, extensionsToTreatAsEsm: [".ts"], preset: "ts-jest/presets/default-esm", @@ -21,4 +21,5 @@ export default { }, globalSetup: "/jest.global-setup.ts", setupFilesAfterEnv: ["/jest.setup-after-env.ts"], + maxWorkers: 1 // equivalent to CLI --runInBand }; diff --git a/core/package.json b/core/package.json index 78d7ecf65..366b4bd9e 100644 --- a/core/package.json +++ b/core/package.json @@ -3,7 +3,7 @@ "version": "1.0.13", "description": "The Continue Core contains functionality that can be shared across web, VS Code, or Node.js", "scripts": { - "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand", + "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest", "tsc:check": "tsc -p ./ --noEmit", "build:npm": "tsc -p ./tsconfig.npm.json", "lint": "eslint . --ext ts", diff --git a/core/util/withExponentialBackoff.test.ts b/core/util/withExponentialBackoff.test.ts new file mode 100644 index 000000000..cfea914ed --- /dev/null +++ b/core/util/withExponentialBackoff.test.ts @@ -0,0 +1,76 @@ +// 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)); + +describe("withExponentialBackoff", () => { + it("should return result immediately if apiCall is successful", async () => { + const mockApiCall = jest.fn().mockResolvedValue("success"); + + const result = await withExponentialBackoff(mockApiCall); + expect(result).toBe("success"); + expect(mockApiCall).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 + .fn() + .mockRejectedValueOnce({ + response: { + status: 429, + headers: new Map([[RETRY_AFTER_HEADER, "1"]]), + }, + }) + .mockResolvedValue("success"); + + const startTime = Date.now(); + const result = await withExponentialBackoff(mockApiCall, 2, 1); + const endTime = Date.now(); + + expect(result).toBe("success"); + expect(endTime - startTime).toBeGreaterThanOrEqual(1000); // At least 1 second delay + expect(mockApiCall).toHaveBeenCalledTimes(2); + }); + + it("should throw error after maxRetries are exhausted", async () => { + const mockApiCall = jest.fn().mockRejectedValue(new Error("Network Error")); + + await expect(withExponentialBackoff(mockApiCall, 3, 0.001)).rejects.toThrow( + "Network Error", + ); + expect(mockApiCall).toHaveBeenCalledTimes(3); + }); + + it("should throw the error immediately if it is not rate limit reached error", async () => { + const mockApiCall = jest + .fn() + .mockRejectedValue(new Error("Unexpected Error")); + + await expect(withExponentialBackoff(mockApiCall, 3, 0.001)).rejects.toThrow( + "Unexpected Error", + ); + expect(mockApiCall).toHaveBeenCalledTimes(1); + }); +}); diff --git a/core/util/withExponentialBackoff.ts b/core/util/withExponentialBackoff.ts index efe9d95c1..84b301b26 100644 --- a/core/util/withExponentialBackoff.ts +++ b/core/util/withExponentialBackoff.ts @@ -2,7 +2,7 @@ interface APIError extends Error { response?: Response; } -const RETRY_AFTER_HEADER = "Retry-After"; +export const RETRY_AFTER_HEADER = "Retry-After"; const withExponentialBackoff = async ( apiCall: () => Promise, @@ -18,8 +18,12 @@ const withExponentialBackoff = async ( (error as APIError).response?.status === 429 && attempt < maxRetries - 1 ) { - const retryAfter = (error as APIError).response?.headers.get(RETRY_AFTER_HEADER); - const delay = retryAfter ? parseInt(retryAfter, 10) : initialDelaySeconds * 2 ** attempt; + const retryAfter = (error as APIError).response?.headers.get( + RETRY_AFTER_HEADER, + ); + const delay = retryAfter + ? parseInt(retryAfter, 10) + : initialDelaySeconds * 2 ** attempt; console.log( `Hit rate limit. Retrying in ${delay} seconds (attempt ${ attempt + 1