Elffuss BraidHub

kikocis/continue public

3a1e5cc69df2 · 3.6 KiBRawPermalink
1// Generated by continue
2
3import {
4 withExponentialBackoff,
5 APIError,
6 RETRY_AFTER_HEADER,
7} from "./withExponentialBackoff";
8
9describe.skip("withExponentialBackoff", () => {
10 it("should return result when apiCall succeeds on first attempt", async () => {
11 // Arrange
12 const apiCall = jest.fn().mockResolvedValue("Success");
13
14 // Act
15 const result = await withExponentialBackoff(apiCall);
16
17 // Assert
18 expect(result).toBe("Success");
19 expect(apiCall).toHaveBeenCalledTimes(1);
20 });
21
22 it("should throw error when apiCall fails with non-429 error", async () => {
23 // Arrange
24 const error = new Error("Some error");
25 const apiCall = jest.fn().mockRejectedValue(error);
26
27 // Act & Assert
28 await expect(withExponentialBackoff(apiCall)).rejects.toThrow("Some error");
29 expect(apiCall).toHaveBeenCalledTimes(1);
30 });
31
32 it("should retry when apiCall fails with 429 and no Retry-After header", async () => {
33 // Arrange
34 const apiCall = jest.fn();
35
36 const firstError: APIError = new Error("Rate limit");
37 firstError.response = {
38 status: 429,
39 headers: {
40 get: () => null, // No Retry-After header
41 },
42 } as unknown as Response;
43
44 // First call fails with 429, second call succeeds
45 apiCall.mockRejectedValueOnce(firstError).mockResolvedValueOnce("Success");
46
47 // Act
48 const result = await withExponentialBackoff(
49 apiCall,
50 5,
51 0.01, // initialDelaySeconds
52 );
53
54 // Assert
55 expect(result).toBe("Success");
56 expect(apiCall).toHaveBeenCalledTimes(2);
57 });
58
59 it("should retry when apiCall fails with 429 and Retry-After header", async () => {
60 // Arrange
61 const apiCall = jest.fn();
62
63 const firstError: APIError = new Error("Rate limit");
64 firstError.response = {
65 status: 429,
66 headers: {
67 get: (headerName: string) => {
68 if (headerName === RETRY_AFTER_HEADER) {
69 return "0.02";
70 }
71 return null;
72 },
73 },
74 } as unknown as Response;
75
76 // First call fails with 429, second call succeeds
77 apiCall.mockRejectedValueOnce(firstError).mockResolvedValueOnce("Success");
78
79 jest.useFakeTimers();
80
81 const promise = withExponentialBackoff(apiCall, 5, 0.01);
82
83 // Advance timers to cover the delay
84 await jest.advanceTimersByTimeAsync(20);
85
86 const result = await promise;
87
88 // Assert
89 expect(result).toBe("Success");
90 expect(apiCall).toHaveBeenCalledTimes(2);
91
92 jest.useRealTimers();
93 });
94
95 it("should throw error after maxTries reached", async () => {
96 // Arrange
97 const apiCall = jest.fn();
98
99 const error: APIError = new Error("Rate limit");
100 error.response = {
101 status: 429,
102 headers: {
103 get: () => null,
104 },
105 } as unknown as Response;
106
107 apiCall.mockRejectedValue(error);
108
109 const maxTries = 3;
110 const initialDelaySeconds = 0.01;
111
112 // Act & Assert
113 await expect(
114 withExponentialBackoff(apiCall, maxTries, initialDelaySeconds),
115 ).rejects.toThrow("Failed to make API call after max tries");
116
117 expect(apiCall).toHaveBeenCalledTimes(maxTries);
118 });
119
120 it("should not call if maxTries is 0", async () => {
121 const apiCall = jest.fn();
122
123 const error: APIError = new Error("Rate limit");
124 error.response = {
125 status: 429,
126 headers: {
127 get: () => null,
128 },
129 } as unknown as Response;
130
131 apiCall.mockRejectedValue(error);
132
133 const maxTries = 0;
134 const initialDelaySeconds = 0.01;
135
136 await expect(
137 withExponentialBackoff(apiCall, maxTries, initialDelaySeconds),
138 ).rejects.toThrow("Failed to make API call after max tries");
139
140 expect(apiCall).toHaveBeenCalledTimes(0);
141 });
142});