aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/filesink.test.ts
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-09-24 19:03:31 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-09-25 13:14:23 -0700
commit9833841101c75c3d511a64daf32e8c273d7d928f (patch)
tree543d3de0426447161a991b0475aa749c1305f08d /test/bun.js/filesink.test.ts
parent1cd67b62e9d012c02d1534accf295014469be7e6 (diff)
downloadbun-9833841101c75c3d511a64daf32e8c273d7d928f.tar.gz
bun-9833841101c75c3d511a64daf32e8c273d7d928f.tar.zst
bun-9833841101c75c3d511a64daf32e8c273d7d928f.zip
wip
Diffstat (limited to 'test/bun.js/filesink.test.ts')
-rw-r--r--test/bun.js/filesink.test.ts129
1 files changed, 129 insertions, 0 deletions
diff --git a/test/bun.js/filesink.test.ts b/test/bun.js/filesink.test.ts
new file mode 100644
index 000000000..28d51a173
--- /dev/null
+++ b/test/bun.js/filesink.test.ts
@@ -0,0 +1,129 @@
+import { ArrayBufferSink } from "bun";
+import { describe, expect, it } from "bun:test";
+
+describe("FileSink", () => {
+ const fixtures = [
+ [
+ ["abcdefghijklmnopqrstuvwxyz"],
+ new TextEncoder().encode("abcdefghijklmnopqrstuvwxyz"),
+ "abcdefghijklmnopqrstuvwxyz",
+ ],
+ [
+ ["abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"],
+ new TextEncoder().encode(
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ ),
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
+ ],
+ [
+ ["😋 Get Emoji — All Emojis to ✂️ Copy and 📋 Paste 👌"],
+ new TextEncoder().encode(
+ "😋 Get Emoji — All Emojis to ✂️ Copy and 📋 Paste 👌"
+ ),
+ "😋 Get Emoji — All Emojis to ✂️ Copy and 📋 Paste 👌",
+ ],
+ [
+ [
+ "abcdefghijklmnopqrstuvwxyz",
+ "😋 Get Emoji — All Emojis to ✂️ Copy and 📋 Paste 👌",
+ ],
+ new TextEncoder().encode(
+ "abcdefghijklmnopqrstuvwxyz" +
+ "😋 Get Emoji — All Emojis to ✂️ Copy and 📋 Paste 👌"
+ ),
+ "abcdefghijklmnopqrstuvwxyz" +
+ "😋 Get Emoji — All Emojis to ✂️ Copy and 📋 Paste 👌",
+ ],
+ [
+ [
+ "abcdefghijklmnopqrstuvwxyz",
+ "😋",
+ " Get Emoji — All Emojis",
+ " to ✂️ Copy and 📋 Paste 👌",
+ ],
+ new TextEncoder().encode(
+ "abcdefghijklmnopqrstuvwxyz" +
+ "😋 Get Emoji — All Emojis to ✂️ Copy and 📋 Paste 👌"
+ ),
+ "(rope) " +
+ "abcdefghijklmnopqrstuvwxyz" +
+ "😋 Get Emoji — All Emojis to ✂️ Copy and 📋 Paste 👌",
+ ],
+ [
+ [
+ new TextEncoder().encode("abcdefghijklmnopqrstuvwxyz"),
+ "😋",
+ " Get Emoji — All Emojis",
+ " to ✂️ Copy and 📋 Paste 👌",
+ ],
+ new TextEncoder().encode(
+ "abcdefghijklmnopqrstuvwxyz" +
+ "😋 Get Emoji — All Emojis to ✂️ Copy and 📋 Paste 👌"
+ ),
+ "(array) " +
+ "abcdefghijklmnopqrstuvwxyz" +
+ "😋 Get Emoji — All Emojis to ✂️ Copy and 📋 Paste 👌",
+ ],
+ ];
+
+ for (const [input, expected, label] of fixtures) {
+ it(`${JSON.stringify(label)}`, async () => {
+ const path = `/tmp/bun-test-${Bun.hash(label).toString(10)}.txt`;
+ try {
+ require("fs").unlinkSync(path);
+ } catch (e) {}
+
+ const sink = Bun.file(path).writer();
+ for (let i = 0; i < input.length; i++) {
+ sink.write(input[i]);
+ }
+ await sink.end();
+
+ const output = new Uint8Array(await Bun.file(path).arrayBuffer());
+ for (let i = 0; i < expected.length; i++) {
+ expect(output[i]).toBe(expected[i]);
+ }
+ expect(output.byteLength).toBe(expected.byteLength);
+ });
+
+ it(`flushing -> ${JSON.stringify(label)}`, async () => {
+ const path = `/tmp/bun-test-${Bun.hash(label).toString(10)}.txt`;
+ try {
+ require("fs").unlinkSync(path);
+ } catch (e) {}
+
+ const sink = Bun.file(path).writer();
+ for (let i = 0; i < input.length; i++) {
+ sink.write(input[i]);
+ await sink.flush();
+ }
+ await sink.end();
+
+ const output = new Uint8Array(await Bun.file(path).arrayBuffer());
+ for (let i = 0; i < expected.length; i++) {
+ expect(output[i]).toBe(expected[i]);
+ }
+ expect(output.byteLength).toBe(expected.byteLength);
+ });
+
+ it(`highWaterMark -> ${JSON.stringify(label)}`, async () => {
+ const path = `/tmp/bun-test-${Bun.hash(label).toString(10)}.txt`;
+ try {
+ require("fs").unlinkSync(path);
+ } catch (e) {}
+
+ const sink = Bun.file(path).writer({ highWaterMark: 1 });
+ for (let i = 0; i < input.length; i++) {
+ sink.write(input[i]);
+ await sink.flush();
+ }
+ await sink.end();
+
+ const output = new Uint8Array(await Bun.file(path).arrayBuffer());
+ for (let i = 0; i < expected.length; i++) {
+ expect(output[i]).toBe(expected[i]);
+ }
+ expect(output.byteLength).toBe(expected.byteLength);
+ });
+ }
+});