| 1 | import { vi } from "vitest"; |
| 2 | |
| 3 | import { createMinimalTestContext } from "../test-helpers/ui-test-context.js"; |
| 4 | |
| 5 | import { withExponentialBackoff } from "./exponentialBackoff.js"; |
| 6 | |
| 7 | describe("withExponentialBackoff", () => { |
| 8 | let context: any; |
| 9 | let abortController: AbortController; |
| 10 | |
| 11 | beforeEach(() => { |
| 12 | context = createMinimalTestContext(); |
| 13 | abortController = new AbortController(); |
| 14 | vi.clearAllMocks(); |
| 15 | }); |
| 16 | |
| 17 | afterEach(() => { |
| 18 | context.cleanup(); |
| 19 | }); |
| 20 | |
| 21 | it("should successfully yield all values from generator", async () => { |
| 22 | const mockData = ["chunk1", "chunk2", "chunk3"]; |
| 23 | const generatorFactory = vi.fn(async (retryAbortSignal: AbortSignal) => { |
| 24 | return (async function* () { |
| 25 | for (const chunk of mockData) { |
| 26 | yield chunk; |
| 27 | } |
| 28 | })(); |
| 29 | }); |
| 30 | |
| 31 | const results: string[] = []; |
| 32 | const generator = withExponentialBackoff( |
| 33 | generatorFactory, |
| 34 | abortController.signal, |
| 35 | ); |
| 36 | |
| 37 | for await (const chunk of generator) { |
| 38 | results.push(chunk); |
| 39 | } |
| 40 | |
| 41 | expect(results).toEqual(mockData); |
| 42 | expect(generatorFactory).toHaveBeenCalledTimes(1); |
| 43 | }); |
| 44 | |
| 45 | it("should retry on retryable errors during generator creation", async () => { |
| 46 | let callCount = 0; |
| 47 | const generatorFactory = vi.fn(async (retryAbortSignal: AbortSignal) => { |
| 48 | callCount++; |
| 49 | if (callCount === 1) { |
| 50 | const error = new Error("Connection reset"); |
| 51 | (error as any).code = "ECONNRESET"; |
| 52 | throw error; |
| 53 | } |
| 54 | return (async function* () { |
| 55 | yield "success"; |
| 56 | })(); |
| 57 | }); |
| 58 | |
| 59 | const results: string[] = []; |
| 60 | const generator = withExponentialBackoff( |
| 61 | generatorFactory, |
| 62 | abortController.signal, |
| 63 | { |
| 64 | maxRetries: 2, |
| 65 | initialDelay: 10, // Short delay for testing |
| 66 | }, |
| 67 | ); |
| 68 | |
| 69 | for await (const chunk of generator) { |
| 70 | results.push(chunk); |
| 71 | } |
| 72 | |
| 73 | expect(results).toEqual(["success"]); |
| 74 | expect(generatorFactory).toHaveBeenCalledTimes(2); |
| 75 | }); |
| 76 | |
| 77 | it("should not retry on non-retryable errors", async () => { |
| 78 | const generatorFactory = vi.fn(async (retryAbortSignal: AbortSignal) => { |
| 79 | const error = new Error("Bad request"); |
| 80 | (error as any).status = 400; |
| 81 | throw error; |
| 82 | }); |
| 83 | |
| 84 | const generator = withExponentialBackoff( |
| 85 | generatorFactory, |
| 86 | abortController.signal, |
| 87 | ); |
| 88 | |
| 89 | await expect(async () => { |
| 90 | for await (const chunk of generator) { |
| 91 | // Should not reach here |
| 92 | } |
| 93 | }).rejects.toThrow("Bad request"); |
| 94 | |
| 95 | expect(generatorFactory).toHaveBeenCalledTimes(1); |
| 96 | }); |
| 97 | |
| 98 | it("should handle abort signal", async () => { |
| 99 | const generatorFactory = vi.fn(async (retryAbortSignal: AbortSignal) => { |
| 100 | return (async function* () { |
| 101 | yield "should-not-yield"; |
| 102 | })(); |
| 103 | }); |
| 104 | |
| 105 | // Abort immediately |
| 106 | abortController.abort(); |
| 107 | |
| 108 | const generator = withExponentialBackoff( |
| 109 | generatorFactory, |
| 110 | abortController.signal, |
| 111 | ); |
| 112 | |
| 113 | await expect(async () => { |
| 114 | for await (const chunk of generator) { |
| 115 | // Should not reach here |
| 116 | } |
| 117 | }).rejects.toThrow("Request aborted"); |
| 118 | |
| 119 | expect(generatorFactory).toHaveBeenCalledTimes(0); |
| 120 | }); |
| 121 | }); |