| 1 | import { jest } from "@jest/globals"; |
| 2 | import { GeneratorReuseManager } from "./GeneratorReuseManager"; |
| 3 | |
| 4 | function createMockGenerator( |
| 5 | data: string[], |
| 6 | delay: number = 0, |
| 7 | ): (abortSignal: AbortSignal) => AsyncGenerator<string> { |
| 8 | const mockGenerator = async function* () { |
| 9 | for (const chunk of data) { |
| 10 | yield chunk; |
| 11 | |
| 12 | if (delay > 0) { |
| 13 | await new Promise((resolve) => setTimeout(resolve, delay)); |
| 14 | } |
| 15 | } |
| 16 | }; |
| 17 | const newGenerator = jest |
| 18 | .fn<() => AsyncGenerator<string>>() |
| 19 | .mockReturnValue(mockGenerator()); |
| 20 | |
| 21 | return newGenerator; |
| 22 | } |
| 23 | |
| 24 | describe("GeneratorReuseManager", () => { |
| 25 | let reuseManager: GeneratorReuseManager; |
| 26 | let onErrorMock: jest.Mock; |
| 27 | |
| 28 | beforeEach(() => { |
| 29 | onErrorMock = jest.fn(); |
| 30 | reuseManager = new GeneratorReuseManager(onErrorMock); |
| 31 | }); |
| 32 | |
| 33 | afterEach(() => { |
| 34 | jest.clearAllMocks(); |
| 35 | }); |
| 36 | |
| 37 | test("creates new generator when there is no current generator", async () => { |
| 38 | const data = ["hello ", "world"]; |
| 39 | const newGenerator = createMockGenerator(data); |
| 40 | |
| 41 | const prefix = ""; |
| 42 | |
| 43 | const generator = reuseManager.getGenerator(prefix, newGenerator, true); |
| 44 | |
| 45 | const output: string[] = []; |
| 46 | for await (const chunk of generator) { |
| 47 | output.push(chunk); |
| 48 | } |
| 49 | |
| 50 | expect(output).toEqual(data); |
| 51 | expect(newGenerator).toHaveBeenCalledTimes(1); |
| 52 | }); |
| 53 | |
| 54 | test("reuses generator when prefix matches pending completion", async () => { |
| 55 | const newGenerator = createMockGenerator(["llo ", "world"]); |
| 56 | |
| 57 | // First call with initial prefix |
| 58 | const prefix1 = "he"; |
| 59 | const generator1 = reuseManager.getGenerator(prefix1, newGenerator, true); |
| 60 | |
| 61 | const output1: string[] = []; |
| 62 | for await (const chunk of generator1) { |
| 63 | output1.push(chunk); |
| 64 | } |
| 65 | |
| 66 | expect(output1).toEqual(["llo ", "world"]); |
| 67 | |
| 68 | // Second call with extended prefix that matches pending completion |
| 69 | const prefix2 = "hello "; |
| 70 | const generator2 = reuseManager.getGenerator(prefix2, newGenerator, true); |
| 71 | |
| 72 | const output2: string[] = []; |
| 73 | for await (const chunk of generator2) { |
| 74 | output2.push(chunk); |
| 75 | } |
| 76 | |
| 77 | expect(output2).toEqual(["world"]); |
| 78 | |
| 79 | // Ensure generator was reused (newGenerator should be called only once) |
| 80 | expect(newGenerator).toHaveBeenCalledTimes(1); |
| 81 | }); |
| 82 | |
| 83 | test("creates new generator when prefix does not match pending completion", async () => { |
| 84 | const data = ["goodbye ", "world"]; |
| 85 | const newGenerator = createMockGenerator(data); |
| 86 | |
| 87 | // Initial generator with different prefix |
| 88 | reuseManager.pendingGeneratorPrefix = "hello "; |
| 89 | reuseManager.pendingCompletion = "world"; |
| 90 | |
| 91 | const prefix = "good"; |
| 92 | const generator = reuseManager.getGenerator(prefix, newGenerator, true); |
| 93 | |
| 94 | const output: string[] = []; |
| 95 | for await (const chunk of generator) { |
| 96 | output.push(chunk); |
| 97 | } |
| 98 | |
| 99 | expect(output).toEqual(data); |
| 100 | // Ensure a new generator was created |
| 101 | expect(newGenerator).toHaveBeenCalledTimes(1); |
| 102 | }); |
| 103 | |
| 104 | test("handles multiline=false by stopping at newline", async () => { |
| 105 | const data = ["first line\n", "second line"]; |
| 106 | const newGenerator = createMockGenerator(data); |
| 107 | |
| 108 | const prefix = ""; |
| 109 | const generator = reuseManager.getGenerator(prefix, newGenerator, false); |
| 110 | |
| 111 | const output: string[] = []; |
| 112 | for await (const chunk of generator) { |
| 113 | output.push(chunk); |
| 114 | } |
| 115 | |
| 116 | expect(output).toEqual(["first line"]); |
| 117 | // Ensure it stops after the first newline |
| 118 | }); |
| 119 | |
| 120 | test("handles multiline=true by not stopping at newline", async () => { |
| 121 | const data = ["first line\n", "second line"]; |
| 122 | const newGenerator = createMockGenerator(data); |
| 123 | |
| 124 | const prefix = ""; |
| 125 | const generator = reuseManager.getGenerator(prefix, newGenerator, true); |
| 126 | |
| 127 | const output: string[] = []; |
| 128 | for await (const chunk of generator) { |
| 129 | output.push(chunk); |
| 130 | } |
| 131 | |
| 132 | expect(output).toEqual(data); |
| 133 | }); |
| 134 | |
| 135 | test("cancels previous generator when creating a new one", async () => { |
| 136 | const data1 = ["data from generator 1", "not generated"]; |
| 137 | const data2 = ["data from generator 2"]; |
| 138 | |
| 139 | const newGenerator1 = createMockGenerator(data1, 1000); // Delay so we have the chance to cancel it |
| 140 | const newGenerator2 = createMockGenerator(data2); |
| 141 | |
| 142 | const prefix1 = "prefix1"; |
| 143 | const prefix2 = "prefix2"; |
| 144 | |
| 145 | // First generator |
| 146 | const generator1 = reuseManager.getGenerator(prefix1, newGenerator1, true); |
| 147 | const output1: string[] = []; |
| 148 | for await (const chunk of generator1) { |
| 149 | output1.push(chunk); |
| 150 | // Simulate the generator being canceled before completing |
| 151 | reuseManager.currentGenerator?.cancel(); |
| 152 | } |
| 153 | |
| 154 | expect(output1.length).toEqual(1); |
| 155 | expect(output1[0]).toEqual(data1[0]); |
| 156 | |
| 157 | // Second generator |
| 158 | const generator2 = reuseManager.getGenerator(prefix2, newGenerator2, true); |
| 159 | const output2: string[] = []; |
| 160 | for await (const chunk of generator2) { |
| 161 | output2.push(chunk); |
| 162 | } |
| 163 | |
| 164 | expect(output2).toEqual(data2); |
| 165 | }); |
| 166 | |
| 167 | test("calls onError when generator throws an error", async () => { |
| 168 | const error = new Error("Generator error"); |
| 169 | const mockGenerator = async function* () { |
| 170 | throw error; |
| 171 | }; |
| 172 | const newGenerator = jest |
| 173 | .fn<() => AsyncGenerator<string>>() |
| 174 | .mockReturnValue(mockGenerator()); |
| 175 | |
| 176 | const prefix = ""; |
| 177 | const generator = reuseManager.getGenerator(prefix, newGenerator, true); |
| 178 | |
| 179 | const output: string[] = []; |
| 180 | await expect(async () => { |
| 181 | for await (const chunk of generator) { |
| 182 | output.push(chunk); |
| 183 | } |
| 184 | }).not.toThrow(); // getGenerator handles errors internally |
| 185 | |
| 186 | expect(onErrorMock).toHaveBeenCalledWith(error); |
| 187 | expect(output).toEqual([]); |
| 188 | }); |
| 189 | |
| 190 | test("handles backspacing by creating new generator when prefix is shorter", async () => { |
| 191 | const data = ["hello world"]; |
| 192 | const newGenerator1 = createMockGenerator(data); |
| 193 | const newGenerator2 = createMockGenerator(data); |
| 194 | |
| 195 | // First prefix |
| 196 | const prefix1 = "hello world"; |
| 197 | const generator1 = reuseManager.getGenerator(prefix1, newGenerator1, true); |
| 198 | const output1: string[] = []; |
| 199 | for await (const chunk of generator1) { |
| 200 | output1.push(chunk); |
| 201 | } |
| 202 | |
| 203 | // Simulate backspace (prefix is shorter) |
| 204 | const prefix2 = "hello worl"; |
| 205 | const generator2 = reuseManager.getGenerator(prefix2, newGenerator2, true); |
| 206 | const output2: string[] = []; |
| 207 | for await (const chunk of generator2) { |
| 208 | output2.push(chunk); |
| 209 | } |
| 210 | |
| 211 | // Ensure a new generator was created |
| 212 | expect(newGenerator1).toHaveBeenCalledTimes(1); |
| 213 | expect(newGenerator2).toHaveBeenCalledTimes(1); |
| 214 | }); |
| 215 | }); |