| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { stopAfterMaxProcessingTime } from "./utils"; |
| 3 | |
| 4 | describe("stopAfterMaxProcessingTime", () => { |
| 5 | beforeEach(() => { |
| 6 | vi.useFakeTimers(); |
| 7 | }); |
| 8 | |
| 9 | afterEach(() => { |
| 10 | vi.useRealTimers(); |
| 11 | }); |
| 12 | |
| 13 | async function* createMockStream(chunks: string[]): AsyncGenerator<string> { |
| 14 | for (const chunk of chunks) { |
| 15 | yield chunk; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | async function streamToString( |
| 20 | stream: AsyncGenerator<string>, |
| 21 | ): Promise<string> { |
| 22 | let result = ""; |
| 23 | for await (const chunk of stream) { |
| 24 | result += chunk; |
| 25 | } |
| 26 | return result; |
| 27 | } |
| 28 | |
| 29 | it("should yield all chunks when maxTimeMs is not reached", async () => { |
| 30 | const mockStream = createMockStream(["Hello", " world", "!"]); |
| 31 | const fullStop = vi.fn(); |
| 32 | const result = stopAfterMaxProcessingTime(mockStream, 1000, fullStop); |
| 33 | |
| 34 | const output = await streamToString(result); |
| 35 | |
| 36 | expect(output).toBe("Hello world!"); |
| 37 | expect(fullStop).not.toHaveBeenCalled(); |
| 38 | }); |
| 39 | |
| 40 | it("should stop processing after max time is reached", async () => { |
| 41 | // Mock implementation of Date.now |
| 42 | let currentTime = 0; |
| 43 | const originalDateNow = Date.now; |
| 44 | Date.now = vi.fn(() => currentTime); |
| 45 | |
| 46 | // Create a generator that we can control |
| 47 | async function* controlledGenerator(): AsyncGenerator<string> { |
| 48 | for (let i = 0; i < 100; i++) { |
| 49 | // After yielding 10 chunks, simulate time passing beyond our limit |
| 50 | if (i === 10) { |
| 51 | currentTime = 1000; // This exceeds our 500ms limit |
| 52 | } |
| 53 | yield `chunk-${i}`; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | const fullStop = vi.fn(); |
| 58 | const maxTimeMs = 500; |
| 59 | |
| 60 | const transformedGenerator = stopAfterMaxProcessingTime( |
| 61 | controlledGenerator(), |
| 62 | maxTimeMs, |
| 63 | fullStop, |
| 64 | ); |
| 65 | |
| 66 | // Consume the generator and collect outputs |
| 67 | const outputs: string[] = []; |
| 68 | for await (const chunk of transformedGenerator) { |
| 69 | outputs.push(chunk); |
| 70 | } |
| 71 | |
| 72 | // We expect: |
| 73 | // 1. Not all chunks were processed (less than 100) |
| 74 | // 2. fullStop was called |
| 75 | // 3. We processed at least the chunks before time was exceeded |
| 76 | expect(outputs.length).toBeLessThan(100); |
| 77 | expect(outputs.length).toBeGreaterThanOrEqual(10); // We should get at least the first 10 chunks |
| 78 | expect(fullStop).toHaveBeenCalled(); |
| 79 | |
| 80 | // Restore Date.now |
| 81 | Date.now = originalDateNow; |
| 82 | }); |
| 83 | |
| 84 | it("should check time only periodically based on checkInterval", async () => { |
| 85 | const chunks = Array(100).fill("x"); |
| 86 | const mockStream = createMockStream(chunks); |
| 87 | const fullStop = vi.fn(); |
| 88 | |
| 89 | // Spy on Date.now to count how many times it's called |
| 90 | const dateSpy = vi.spyOn(Date, "now"); |
| 91 | |
| 92 | // Stream should complete normally (not hitting the timeout) |
| 93 | await streamToString( |
| 94 | stopAfterMaxProcessingTime(mockStream, 10000, fullStop), |
| 95 | ); |
| 96 | |
| 97 | // The first call is to set startTime, then once every checkInterval (10) chunks |
| 98 | // So for 100 chunks, we expect startTime + ~10 checks = ~11 calls |
| 99 | // We use a range because implementation details might vary slightly |
| 100 | expect(dateSpy.mock.calls.length).toBeGreaterThanOrEqual(1); |
| 101 | expect(dateSpy.mock.calls.length).toBeLessThanOrEqual(15); |
| 102 | |
| 103 | dateSpy.mockRestore(); |
| 104 | }); |
| 105 | |
| 106 | it("should handle empty stream gracefully", async () => { |
| 107 | const mockStream = createMockStream([]); |
| 108 | const fullStop = vi.fn(); |
| 109 | const result = stopAfterMaxProcessingTime(mockStream, 1000, fullStop); |
| 110 | |
| 111 | const output = await streamToString(result); |
| 112 | |
| 113 | expect(output).toBe(""); |
| 114 | expect(fullStop).not.toHaveBeenCalled(); |
| 115 | }); |
| 116 | |
| 117 | it("should pass through all chunks if there's no timeout", async () => { |
| 118 | const chunks = Array(100).fill("test chunk"); |
| 119 | const mockStream = createMockStream(chunks); |
| 120 | const fullStop = vi.fn(); |
| 121 | |
| 122 | // Use undefined as timeout to simulate no timeout |
| 123 | const result = stopAfterMaxProcessingTime( |
| 124 | mockStream, |
| 125 | undefined as any, |
| 126 | fullStop, |
| 127 | ); |
| 128 | |
| 129 | // Process the stream |
| 130 | const processedChunks = []; |
| 131 | for await (const chunk of result) { |
| 132 | processedChunks.push(chunk); |
| 133 | } |
| 134 | |
| 135 | expect(processedChunks.length).toBe(chunks.length); |
| 136 | expect(fullStop).not.toHaveBeenCalled(); |
| 137 | }); |
| 138 | }); |