|
| 1 | +import { AsyncIteratorClass } from './iterator' |
| 2 | +import { asyncIteratorToStream, streamToAsyncIteratorClass } from './stream' |
| 3 | + |
| 4 | +describe('streamToAsyncIteratorClass', () => { |
| 5 | + it('should convert a ReadableStream to AsyncIteratorClass', async () => { |
| 6 | + const values = [1, 2, 3, 4, 5] |
| 7 | + const stream = new ReadableStream<number>({ |
| 8 | + start(controller) { |
| 9 | + values.forEach(value => controller.enqueue(value)) |
| 10 | + controller.close() |
| 11 | + }, |
| 12 | + }) |
| 13 | + |
| 14 | + const iterator = streamToAsyncIteratorClass(stream) |
| 15 | + expect(iterator).toBeInstanceOf(AsyncIteratorClass) |
| 16 | + |
| 17 | + const results: number[] = [] |
| 18 | + for await (const value of iterator) { |
| 19 | + results.push(value) |
| 20 | + } |
| 21 | + |
| 22 | + expect(results).toEqual(values) |
| 23 | + }) |
| 24 | + |
| 25 | + it('should handle empty stream', async () => { |
| 26 | + const stream = new ReadableStream<number>({ |
| 27 | + start(controller) { |
| 28 | + controller.close() |
| 29 | + }, |
| 30 | + }) |
| 31 | + |
| 32 | + const iterator = streamToAsyncIteratorClass(stream) |
| 33 | + const results: number[] = [] |
| 34 | + |
| 35 | + for await (const value of iterator) { |
| 36 | + results.push(value) |
| 37 | + } |
| 38 | + |
| 39 | + expect(results).toEqual([]) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should handle stream errors', async () => { |
| 43 | + const error = new Error('Stream error') |
| 44 | + const stream = new ReadableStream<number>({ |
| 45 | + start(controller) { |
| 46 | + controller.error(error) |
| 47 | + }, |
| 48 | + }) |
| 49 | + |
| 50 | + const iterator = streamToAsyncIteratorClass(stream) |
| 51 | + |
| 52 | + try { |
| 53 | + for await (const value of iterator) { |
| 54 | + // Should not reach here |
| 55 | + } |
| 56 | + expect.fail('Should have thrown an error') |
| 57 | + } |
| 58 | + catch (err) { |
| 59 | + expect(err).toBe(error) |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + it('should properly cleanup when iterator is returned early', async () => { |
| 64 | + let cleanupCalled = false |
| 65 | + const stream = new ReadableStream<number>({ |
| 66 | + start(controller) { |
| 67 | + for (let i = 1; i <= 10; i++) { |
| 68 | + controller.enqueue(i) |
| 69 | + } |
| 70 | + controller.close() |
| 71 | + }, |
| 72 | + cancel() { |
| 73 | + cleanupCalled = true |
| 74 | + }, |
| 75 | + }) |
| 76 | + |
| 77 | + const iterator = streamToAsyncIteratorClass(stream) |
| 78 | + |
| 79 | + await iterator.next() |
| 80 | + await iterator.return() |
| 81 | + |
| 82 | + expect(cleanupCalled).toBe(true) |
| 83 | + }) |
| 84 | +}) |
| 85 | + |
| 86 | +describe('asyncIteratorToStream', () => { |
| 87 | + it('should convert an AsyncIterator to ReadableStream', async () => { |
| 88 | + async function* generator() { |
| 89 | + yield 1 |
| 90 | + yield 2 |
| 91 | + yield 3 |
| 92 | + } |
| 93 | + |
| 94 | + const asyncIterator = generator() |
| 95 | + const stream = asyncIteratorToStream(asyncIterator) |
| 96 | + |
| 97 | + expect(stream).toBeInstanceOf(ReadableStream) |
| 98 | + |
| 99 | + const reader = stream.getReader() |
| 100 | + const results: number[] = [] |
| 101 | + |
| 102 | + let result = await reader.read() |
| 103 | + while (!result.done) { |
| 104 | + results.push(result.value) |
| 105 | + result = await reader.read() |
| 106 | + } |
| 107 | + |
| 108 | + expect(results).toEqual([1, 2, 3]) |
| 109 | + }) |
| 110 | + |
| 111 | + it('should handle empty async iterator', async () => { |
| 112 | + async function* emptyGenerator() { |
| 113 | + // Empty generator |
| 114 | + } |
| 115 | + |
| 116 | + const asyncIterator = emptyGenerator() |
| 117 | + const stream = asyncIteratorToStream(asyncIterator) |
| 118 | + |
| 119 | + const reader = stream.getReader() |
| 120 | + const result = await reader.read() |
| 121 | + |
| 122 | + expect(result.done).toBe(true) |
| 123 | + expect(result.value).toBeUndefined() |
| 124 | + }) |
| 125 | + |
| 126 | + it('should handle async iterator errors', async () => { |
| 127 | + const error = new Error('Iterator error') |
| 128 | + async function* errorGenerator() { |
| 129 | + yield 1 |
| 130 | + throw error |
| 131 | + } |
| 132 | + |
| 133 | + const asyncIterator = errorGenerator() |
| 134 | + const stream = asyncIteratorToStream(asyncIterator) |
| 135 | + |
| 136 | + const reader = stream.getReader() |
| 137 | + |
| 138 | + // First read should succeed |
| 139 | + const firstResult = await reader.read() |
| 140 | + expect(firstResult.done).toBe(false) |
| 141 | + expect(firstResult.value).toBe(1) |
| 142 | + |
| 143 | + // Second read should throw the error |
| 144 | + await expect(reader.read()).rejects.toThrow(error) |
| 145 | + }) |
| 146 | + |
| 147 | + it('should call iterator.return when stream is cancelled', async () => { |
| 148 | + let cleanupCalled = false |
| 149 | + |
| 150 | + const stream = asyncIteratorToStream(async function* () { |
| 151 | + try { |
| 152 | + yield 1 |
| 153 | + yield 2 |
| 154 | + await new Promise(resolve => setTimeout(resolve, 100)) // Simulate async operation |
| 155 | + } |
| 156 | + finally { |
| 157 | + cleanupCalled = true |
| 158 | + } |
| 159 | + }()) |
| 160 | + |
| 161 | + const reader = stream.getReader() |
| 162 | + await reader.read() |
| 163 | + await reader.cancel() |
| 164 | + |
| 165 | + expect(cleanupCalled).toBe(true) |
| 166 | + }) |
| 167 | +}) |
| 168 | + |
| 169 | +it('streamToAsyncIteratorClass + asyncIteratorToStream', async () => { |
| 170 | + const stream = new ReadableStream<number>({ |
| 171 | + start(controller) { |
| 172 | + controller.enqueue(1) |
| 173 | + controller.enqueue(2) |
| 174 | + controller.enqueue(3) |
| 175 | + controller.close() |
| 176 | + }, |
| 177 | + }) |
| 178 | + |
| 179 | + const iterator = streamToAsyncIteratorClass(stream) |
| 180 | + const newStream = asyncIteratorToStream(iterator) |
| 181 | + const newIterator = streamToAsyncIteratorClass(newStream) |
| 182 | + |
| 183 | + const results: number[] = [] |
| 184 | + for await (const value of newIterator) { |
| 185 | + results.push(value) |
| 186 | + } |
| 187 | + |
| 188 | + expect(results).toEqual([1, 2, 3]) |
| 189 | +}) |
0 commit comments