diff options
author | 2022-06-03 04:43:38 -0700 | |
---|---|---|
committer | 2022-06-03 04:43:38 -0700 | |
commit | 102553dca6a090533725416cc384d36a49a9ce1d (patch) | |
tree | e68fbe29e817f04519e1991e0561ed8aac92029a | |
parent | 549e5bbbcdbfec70b1649b1400b2bb59b4791974 (diff) | |
download | bun-102553dca6a090533725416cc384d36a49a9ce1d.tar.gz bun-102553dca6a090533725416cc384d36a49a9ce1d.tar.zst bun-102553dca6a090533725416cc384d36a49a9ce1d.zip |
Update streams.test.js
-rw-r--r-- | integration/bunjs-only-snippets/streams.test.js | 56 |
1 files changed, 49 insertions, 7 deletions
diff --git a/integration/bunjs-only-snippets/streams.test.js b/integration/bunjs-only-snippets/streams.test.js index f43413997..549fbc533 100644 --- a/integration/bunjs-only-snippets/streams.test.js +++ b/integration/bunjs-only-snippets/streams.test.js @@ -1,5 +1,6 @@ import { file, gc } from "bun"; import { expect, it } from "bun:test"; +import { writeFileSync } from "node:fs"; it("exists globally", () => { expect(typeof ReadableStream).toBe("function"); @@ -33,16 +34,17 @@ it("ReadableStream", async () => { it("ReadableStream for Blob", async () => { var blob = new Blob(["abdefgh", "ijklmnop"]); + expect(await blob.text()).toBe("abdefghijklmnop"); var stream = blob.stream(); const chunks = []; var reader = stream.getReader(); while (true) { const chunk = await reader.read(); if (chunk.done) break; - chunks.push(chunk.value); + chunks.push(new TextDecoder().decode(chunk.value)); } - expect(chunks.map((a) => a.join("")).join("")).toBe( - Buffer.from("abdefghijklmnop").join("") + expect(chunks.join("")).toBe( + new TextDecoder().decode(Buffer.from("abdefghijklmnop")) ); }); @@ -52,17 +54,57 @@ it("ReadableStream for File", async () => { const chunks = []; var reader = stream.getReader(); stream = undefined; - while (true) { const chunk = await reader.read(); gc(true); if (chunk.done) break; chunks.push(chunk.value); + expect(chunk.value.byteLength <= 24).toBe(true); gc(true); } reader = undefined; - expect(chunks.map((a) => a.join("")).join("")).toBe( - new Uint8Array(await blob.arrayBuffer()).join("") - ); + const output = new Uint8Array(await blob.arrayBuffer()).join(""); + const input = chunks.map((a) => a.join("")).join(""); + expect(output).toBe(input); gc(true); }); + +it("ReadableStream for File errors", async () => { + try { + var blob = file(import.meta.dir + "/fetch.js.txt.notfound"); + blob.stream().getReader(); + throw new Error("should not reach here"); + } catch (e) { + expect(e.code).toBe("ENOENT"); + expect(e.syscall).toBe("open"); + } +}); + +it("ReadableStream for empty blob closes immediately", async () => { + var blob = new Blob([]); + var stream = blob.stream(); + const chunks = []; + var reader = stream.getReader(); + while (true) { + const chunk = await reader.read(); + if (chunk.done) break; + chunks.push(chunk.value); + } + + expect(chunks.length).toBe(0); +}); + +it("ReadableStream for empty file closes immediately", async () => { + writeFileSync("/tmp/bun-empty-file-123456", ""); + var blob = file("/tmp/bun-empty-file-123456"); + var stream = blob.stream(); + const chunks = []; + var reader = stream.getReader(); + while (true) { + const chunk = await reader.read(); + if (chunk.done) break; + chunks.push(chunk.value); + } + + expect(chunks.length).toBe(0); +}); |