|
1 | 1 |
|
2 | | -import { Readable, Transform } from 'stream'; |
| 2 | +import { Readable, Transform } from 'node:stream'; |
3 | 3 |
|
4 | | -import Aline from '.' |
| 4 | +import { Aline, AlineWeb, concat, lastIndexOf } from '.' |
| 5 | + |
| 6 | +describe('concat two Uint8Array', () => { |
| 7 | + test('regular two array', () => { |
| 8 | + const left = new Uint8Array([1, 2, 3]); |
| 9 | + const right = new Uint8Array([4, 5, 6]); |
| 10 | + |
| 11 | + const combined = Array.from(concat(left, right)); |
| 12 | + |
| 13 | + expect(combined).toEqual([1, 2, 3, 4, 5, 6]); |
| 14 | + }); |
| 15 | + |
| 16 | + test('left array is empty', () => { |
| 17 | + const left = new Uint8Array(); |
| 18 | + const right = new Uint8Array([4, 5, 6]); |
| 19 | + |
| 20 | + const combined = Array.from(concat(left, right)); |
| 21 | + |
| 22 | + expect(combined).toEqual([4, 5, 6]); |
| 23 | + }); |
| 24 | + |
| 25 | + test('right array is empty', () => { |
| 26 | + const left = new Uint8Array([1, 2, 3]); |
| 27 | + const right = new Uint8Array(); |
| 28 | + |
| 29 | + const combined = Array.from(concat(left, right)); |
| 30 | + |
| 31 | + expect(combined).toEqual([1, 2, 3]); |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +describe('lastIndexOf Uint8Array in Uint8Array', () => { |
| 38 | + test('regular two array', () => { |
| 39 | + const left = new Uint8Array([1, 2, 3, 4, 5, 6]); |
| 40 | + |
| 41 | + expect(lastIndexOf(left, new Uint8Array([1]))).toBe(0); |
| 42 | + |
| 43 | + expect(lastIndexOf(left, new Uint8Array([1, 2, 3]))).toBe(0); |
| 44 | + |
| 45 | + expect(lastIndexOf(left, new Uint8Array([6]))).toBe(5); |
| 46 | + |
| 47 | + expect(lastIndexOf(left, new Uint8Array([6, 7, 8]))).toBe(-1); |
| 48 | + |
| 49 | + expect(lastIndexOf(left, new Uint8Array([3, 4, 5]))).toBe(2); |
| 50 | + |
| 51 | + expect(lastIndexOf(left, new Uint8Array([3, 4, 5, 6]))).toBe(2); |
| 52 | + |
| 53 | + expect(lastIndexOf(left, new Uint8Array([3, 4, 5, 7]))).toBe(-1); |
| 54 | + |
| 55 | + expect(lastIndexOf(left, new Uint8Array([3, 4]))).toBe(2); |
| 56 | + |
| 57 | + expect(lastIndexOf(left, new Uint8Array([3]))).toBe(2); |
| 58 | + |
| 59 | + expect(lastIndexOf(left, new Uint8Array([3, 5]))).toBe(-1); |
| 60 | + |
| 61 | + expect(lastIndexOf(left, new Uint8Array([7]))).toBe(-1); |
| 62 | + |
| 63 | + expect(lastIndexOf(left, new Uint8Array([4, 5, 6, 7]))).toBe(-1); |
| 64 | + }); |
| 65 | + |
| 66 | + test('empty array', () => { |
| 67 | + const fill = new Uint8Array([1, 2, 3]); |
| 68 | + const empty = new Uint8Array(); |
| 69 | + |
| 70 | + expect(lastIndexOf(fill, empty)).toBe(-1); |
| 71 | + |
| 72 | + expect(lastIndexOf(empty, fill)).toBe(-1); |
| 73 | + |
| 74 | + expect(lastIndexOf(empty, empty)).toBe(-1); |
| 75 | + }); |
| 76 | +}); |
5 | 77 |
|
6 | 78 |
|
7 | 79 | function combineData<T extends Transform>(stream: T) { |
@@ -62,5 +134,50 @@ const fixture: Array<FixtureItem> = [ |
62 | 134 | ['\nfoo\n']] |
63 | 135 | ]; |
64 | 136 |
|
65 | | -fixture.forEach(([name, generate, target]) => test(name, async() => expect(await combineData(Readable.from(generate()).pipe(new Aline()))).toEqual(target))); |
| 137 | +describe('node transform', () => { |
| 138 | + fixture.forEach(([name, generate, target]) => test(name, async() => { |
| 139 | + expect(await combineData(Readable.from(generate()).pipe(new Aline()))).toEqual(target) |
| 140 | + })); |
| 141 | +}); |
| 142 | + |
| 143 | + |
| 144 | +function readableStreamFrom<T>(iterable: AsyncIterable<T>) { |
| 145 | + return new ReadableStream<T>({ |
| 146 | + async start(controller): Promise<void> { |
| 147 | + for await (const chunk of iterable) { |
| 148 | + controller.enqueue(chunk); |
| 149 | + } |
| 150 | + |
| 151 | + controller.close(); |
| 152 | + } |
| 153 | + }); |
| 154 | +} |
| 155 | + |
| 156 | +async function arrayFromAsync<T>(readable: ReadableStream<T>): Promise<Array<T>> { |
| 157 | + const chunks: Array<T> = []; |
| 158 | + |
| 159 | + const reader = readable.getReader(); |
| 160 | + |
| 161 | + while (true) { |
| 162 | + const { done, value } = await reader.read(); |
| 163 | + if (done) { |
| 164 | + break; |
| 165 | + } |
| 166 | + chunks.push(value); |
| 167 | + } |
| 168 | + |
| 169 | + reader.releaseLock(); |
| 170 | + |
| 171 | + return chunks; |
| 172 | +} |
| 173 | + |
| 174 | +describe('web transform stream', () => { |
| 175 | + |
| 176 | + fixture.forEach(([name, generate, target]) => test(name, async() => { |
| 177 | + expect(await arrayFromAsync(readableStreamFrom(generate()) |
| 178 | + .pipeThrough(new TextEncoderStream()) |
| 179 | + .pipeThrough(new TransformStream(new AlineWeb())) |
| 180 | + .pipeThrough(new TextDecoderStream()))).toEqual(target); |
| 181 | + })); |
| 182 | +}); |
66 | 183 |
|
0 commit comments