From 8bc8d02d2f9c4bd1c6adaf86e8efd031c07fec18 Mon Sep 17 00:00:00 2001 From: Mian Abdullah Date: Mon, 1 Jun 2026 12:48:21 -0400 Subject: [PATCH] test(core): fix error message assertions and unskip withExponentialBackoff tests --- core/util/withExponentialBackoff.test.ts | 41 ++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/core/util/withExponentialBackoff.test.ts b/core/util/withExponentialBackoff.test.ts index 5049073c758..a989ab45ec2 100644 --- a/core/util/withExponentialBackoff.test.ts +++ b/core/util/withExponentialBackoff.test.ts @@ -6,7 +6,7 @@ import { RETRY_AFTER_HEADER, } from "./withExponentialBackoff"; -describe.skip("withExponentialBackoff", () => { +describe("withExponentialBackoff", () => { it("should return result when apiCall succeeds on first attempt", async () => { // Arrange const apiCall = jest.fn().mockResolvedValue("Success"); @@ -112,7 +112,7 @@ describe.skip("withExponentialBackoff", () => { // Act & Assert await expect( withExponentialBackoff(apiCall, maxTries, initialDelaySeconds), - ).rejects.toThrow("Failed to make API call after max tries"); + ).rejects.toThrow("Failed to make API call after"); expect(apiCall).toHaveBeenCalledTimes(maxTries); }); @@ -135,8 +135,43 @@ describe.skip("withExponentialBackoff", () => { await expect( withExponentialBackoff(apiCall, maxTries, initialDelaySeconds), - ).rejects.toThrow("Failed to make API call after max tries"); + ).rejects.toThrow("Failed to make API call after"); expect(apiCall).toHaveBeenCalledTimes(0); }); + + it("should retry when error message contains 'overloaded'", async () => { + const apiCall = jest.fn(); + const firstError = new Error("Service overloaded, please try again"); + apiCall.mockRejectedValueOnce(firstError).mockResolvedValueOnce("Success"); + + const result = await withExponentialBackoff(apiCall, 5, 0.01); + + expect(result).toBe("Success"); + expect(apiCall).toHaveBeenCalledTimes(2); + }); + + it("should retry when error message contains 'malformed json'", async () => { + const apiCall = jest.fn(); + const firstError = new Error("Received malformed JSON from upstream"); + apiCall.mockRejectedValueOnce(firstError).mockResolvedValueOnce("Success"); + + const result = await withExponentialBackoff(apiCall, 5, 0.01); + + expect(result).toBe("Success"); + expect(apiCall).toHaveBeenCalledTimes(2); + }); + + it("should retry when error message contains a 429 code in JSON body", async () => { + const apiCall = jest.fn(); + const firstError = new Error( + '{"error": {"code": 429, "message": "Too Many Requests"}}', + ); + apiCall.mockRejectedValueOnce(firstError).mockResolvedValueOnce("Success"); + + const result = await withExponentialBackoff(apiCall, 5, 0.01); + + expect(result).toBe("Success"); + expect(apiCall).toHaveBeenCalledTimes(2); + }); });