|
| 1 | +import { StreamingBlobPayloadOutputTypes } from "@smithy/types"; |
| 2 | +import { sdkStreamMixin } from "@smithy/util-stream"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | + |
| 5 | +import { joinStreams } from "./join-streams.browser"; |
| 6 | + |
| 7 | +describe("join-streams tests", () => { |
| 8 | + const createReadableStreamWithContent = (content: Uint8Array) => |
| 9 | + new ReadableStream({ |
| 10 | + start(controller) { |
| 11 | + controller.enqueue(content); |
| 12 | + controller.close(); |
| 13 | + }, |
| 14 | + }); |
| 15 | + |
| 16 | + const createEmptyReadableStream = () => |
| 17 | + new ReadableStream({ |
| 18 | + start(controller) { |
| 19 | + controller.close(); |
| 20 | + }, |
| 21 | + }); |
| 22 | + |
| 23 | + const consumeReadableStream = async (stream: any): Promise<Uint8Array[]> => { |
| 24 | + const reader = stream.getReader(); |
| 25 | + const chunks: Uint8Array[] = []; |
| 26 | + |
| 27 | + while (true) { |
| 28 | + const { done, value } = await reader.read(); |
| 29 | + if (done) break; |
| 30 | + if (value) chunks.push(value); |
| 31 | + } |
| 32 | + |
| 33 | + return chunks; |
| 34 | + }; |
| 35 | + |
| 36 | + const testCases = [ |
| 37 | + { |
| 38 | + name: "ReadableStream", |
| 39 | + createStream: () => new ReadableStream(), |
| 40 | + createWithContent: createReadableStreamWithContent, |
| 41 | + createEmpty: createEmptyReadableStream, |
| 42 | + consume: consumeReadableStream, |
| 43 | + isInstance: (stream: any) => typeof stream.getReader === "function", |
| 44 | + }, |
| 45 | + ]; |
| 46 | + |
| 47 | + testCases.forEach(({ name, createStream, createWithContent, createEmpty, consume, isInstance }) => { |
| 48 | + describe(`joinStreams() with ${name}`, () => { |
| 49 | + it("should return single stream when only one stream is provided", async () => { |
| 50 | + const stream = createStream(); |
| 51 | + const mixedStream = sdkStreamMixin(stream); |
| 52 | + const result = await joinStreams([Promise.resolve(mixedStream as StreamingBlobPayloadOutputTypes)]); |
| 53 | + |
| 54 | + expect(result).toBeDefined(); |
| 55 | + expect(result).not.toBe(stream); |
| 56 | + expect(isInstance(result)).toBe(true); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should join multiple streams into a single stream", async () => { |
| 60 | + const contents = [ |
| 61 | + new Uint8Array([67, 104, 117, 110, 107, 32, 49]), // "Chunk 1" |
| 62 | + new Uint8Array([67, 104, 117, 110, 107, 32, 50]), // "Chunk 2" |
| 63 | + new Uint8Array([67, 104, 117, 110, 107, 32, 51]), // "Chunk 3" |
| 64 | + ]; |
| 65 | + |
| 66 | + const streams = contents.map((content) => |
| 67 | + Promise.resolve(sdkStreamMixin(createWithContent(content)) as StreamingBlobPayloadOutputTypes) |
| 68 | + ); |
| 69 | + |
| 70 | + const joinedStream = await joinStreams(streams); |
| 71 | + |
| 72 | + const chunks = await consume(joinedStream); |
| 73 | + |
| 74 | + expect(chunks.length).toBe(contents.length); |
| 75 | + chunks.forEach((chunk, i) => { |
| 76 | + expect(chunk).toEqual(contents[i]); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should handle consecutive calls of joining multiple streams into a single stream", async () => { |
| 81 | + for (let i = 0; i <= 3; i++) { |
| 82 | + const contents = [ |
| 83 | + new Uint8Array([67, 104, 117, 110, 107, 32, 49]), // "Chunk 1" |
| 84 | + new Uint8Array([67, 104, 117, 110, 107, 32, 50]), // "Chunk 2" |
| 85 | + new Uint8Array([67, 104, 117, 110, 107, 32, 51]), // "Chunk 3" |
| 86 | + ]; |
| 87 | + |
| 88 | + const streams = contents.map((content) => |
| 89 | + Promise.resolve(sdkStreamMixin(createWithContent(content)) as StreamingBlobPayloadOutputTypes) |
| 90 | + ); |
| 91 | + |
| 92 | + const joinedStream = await joinStreams(streams); |
| 93 | + |
| 94 | + const chunks = await consume(joinedStream); |
| 95 | + |
| 96 | + expect(chunks.length).toBe(contents.length); |
| 97 | + chunks.forEach((chunk, i) => { |
| 98 | + expect(chunk).toEqual(contents[i]); |
| 99 | + }); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + it("should handle streams with no data", async () => { |
| 104 | + const streams = [ |
| 105 | + Promise.resolve(sdkStreamMixin(createEmpty()) as StreamingBlobPayloadOutputTypes), |
| 106 | + Promise.resolve(sdkStreamMixin(createEmpty()) as StreamingBlobPayloadOutputTypes), |
| 107 | + ]; |
| 108 | + |
| 109 | + const joinedStream = await joinStreams(streams); |
| 110 | + |
| 111 | + const chunks = await consume(joinedStream); |
| 112 | + |
| 113 | + expect(chunks.length).toBe(0); |
| 114 | + }); |
| 115 | + |
| 116 | + it("should report progress via eventListeners", async () => { |
| 117 | + const streams = [ |
| 118 | + Promise.resolve( |
| 119 | + sdkStreamMixin(createWithContent(new Uint8Array([100, 97, 116, 97]))) as StreamingBlobPayloadOutputTypes |
| 120 | + ), // "data" |
| 121 | + Promise.resolve( |
| 122 | + sdkStreamMixin(createWithContent(new Uint8Array([109, 111, 114, 101]))) as StreamingBlobPayloadOutputTypes |
| 123 | + ), // "more" |
| 124 | + ]; |
| 125 | + |
| 126 | + const onBytesSpy = vi.fn(); |
| 127 | + const onCompletionSpy = vi.fn(); |
| 128 | + |
| 129 | + const joinedStream = await joinStreams(streams, { |
| 130 | + onBytes: onBytesSpy, |
| 131 | + onCompletion: onCompletionSpy, |
| 132 | + }); |
| 133 | + |
| 134 | + await consume(joinedStream); |
| 135 | + |
| 136 | + expect(onBytesSpy).toHaveBeenCalled(); |
| 137 | + expect(onCompletionSpy).toHaveBeenCalledWith(expect.any(Number), 1); |
| 138 | + }); |
| 139 | + |
| 140 | + it("should throw error for unsupported stream types", async () => { |
| 141 | + const blob = new Blob(["test"]); |
| 142 | + await expect( |
| 143 | + joinStreams([Promise.resolve(blob as unknown as StreamingBlobPayloadOutputTypes)]) |
| 144 | + ).rejects.toThrow("Unsupported Stream Type"); |
| 145 | + }); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments