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