diff --git a/tests/node/index.mjs b/tests/node/index.mjs index 468d15a0..2e7f70b1 100644 --- a/tests/node/index.mjs +++ b/tests/node/index.mjs @@ -30,6 +30,7 @@ import TestRegister from "../lib/TestRegister"; import "./tests/nodeApi"; import "./tests/operations"; import "./tests/File"; +import "./tests/NodeDish"; const testStatus = { allTestsPassing: true, diff --git a/tests/node/tests/NodeDish.mjs b/tests/node/tests/NodeDish.mjs new file mode 100644 index 00000000..e4261ecd --- /dev/null +++ b/tests/node/tests/NodeDish.mjs @@ -0,0 +1,68 @@ +import assert from "assert"; +import it from "../assertionHandler"; +import TestRegister from "../../lib/TestRegister"; +import { Dish, toBase32, SHA3 } from "../../../src/node/index"; +import fs from "fs"; + +TestRegister.addApiTests([ + it("Composable Dish: Should have top level Dish object", () => { + assert.ok(Dish); + }), + + it("Composable Dish: Should construct empty dish object", () => { + const dish = new Dish(); + assert.strictEqual(dish.value.byteLength, new ArrayBuffer(0).byteLength); + assert.strictEqual(dish.type, 4); + }), + + it("Composable Dish: constructed dish should have apply prototype functions", () => { + const dish = new Dish(); + assert.ok(dish.apply); + assert.throws(() => dish.someInvalidFunction()); + }), + + it("Composable Dish: composed function returns another dish", () => { + const result = new Dish("some input").apply(toBase32); + assert.ok(result instanceof Dish); + }), + + + it("Composable dish: infers type from input if needed", () => { + const dish = new Dish("string input"); + assert.strictEqual(dish.type, 1); + + const numberDish = new Dish(333); + assert.strictEqual(numberDish.type, 2); + + const arrayBufferDish = new Dish(Buffer.from("some buffer input").buffer); + assert.strictEqual(arrayBufferDish.type, 4); + + const byteArrayDish = new Dish(Buffer.from("some buffer input")); + assert.strictEqual(byteArrayDish.type, 0); + + const JSONDish = new Dish({key: "value"}); + assert.strictEqual(JSONDish.type, 6); + }), + + it("Composable dish: Buffer type dishes should be converted to strings", () => { + fs.writeFileSync("test.txt", "abc"); + const dish = new Dish(fs.readFileSync("test.txt")); + assert.strictEqual(dish.type, 0); + fs.unlinkSync("test.txt"); + }), + + it("Composable Dish: apply should allow set of arguments for operation", () => { + const result = new Dish("input").apply(SHA3, {size: "256"}); + assert.strictEqual(result.toString(), "7640cc9b7e3662b2250a43d1757e318bb29fb4860276ac4373b67b1650d6d3e3"); + }), + + it("Composable Dish: apply functions can be chained", () => { + const result = new Dish("input").apply(toBase32).apply(SHA3, {size: "224"}); + assert.strictEqual(result.toString(), "493e8136b759370a415ef2cf2f7a69690441ff86592aba082bc2e2e0"); + }), + + // it("Dish translation: ArrayBuffer to ArrayBuffer", () => { + // const dish = new Dish(); + + // }), +]); diff --git a/tests/node/tests/nodeApi.mjs b/tests/node/tests/nodeApi.mjs index 1a21c39d..bcb61415 100644 --- a/tests/node/tests/nodeApi.mjs +++ b/tests/node/tests/nodeApi.mjs @@ -325,61 +325,7 @@ TestRegister.addApiTests([ assert.strictEqual(result.toString(), "begin_something_anananaaaaak_da_aaak_da_aaaaananaaaaaaan_da_aaaaaaanan_da_aaak_end_something"); }), - it("Composable Dish: Should have top level Dish object", () => { - assert.ok(Dish); - }), - it("Composable Dish: Should construct empty dish object", () => { - const dish = new Dish(); - assert.strictEqual(dish.value.byteLength, new ArrayBuffer(0).byteLength); - assert.strictEqual(dish.type, 4); - }), - - it("Composable Dish: constructed dish should have apply prototype functions", () => { - const dish = new Dish(); - assert.ok(dish.apply); - assert.throws(() => dish.someInvalidFunction()); - }), - - it("Composable Dish: composed function returns another dish", () => { - const result = new Dish("some input").apply(toBase32); - assert.ok(result instanceof NodeDish); - }), - - - it("Composable dish: infers type from input if needed", () => { - const dish = new Dish("string input"); - assert.strictEqual(dish.type, 1); - - const numberDish = new Dish(333); - assert.strictEqual(numberDish.type, 2); - - const arrayBufferDish = new Dish(Buffer.from("some buffer input").buffer); - assert.strictEqual(arrayBufferDish.type, 4); - - const byteArrayDish = new Dish(Buffer.from("some buffer input")); - assert.strictEqual(byteArrayDish.type, 0); - - const JSONDish = new Dish({key: "value"}); - assert.strictEqual(JSONDish.type, 6); - }), - - it("Composable dish: Buffer type dishes should be converted to strings", () => { - fs.writeFileSync("test.txt", "abc"); - const dish = new Dish(fs.readFileSync("test.txt")); - assert.strictEqual(dish.type, 0); - fs.unlinkSync("test.txt"); - }), - - it("Composable Dish: apply should allow set of arguments for operation", () => { - const result = new Dish("input").apply(SHA3, {size: "256"}); - assert.strictEqual(result.toString(), "7640cc9b7e3662b2250a43d1757e318bb29fb4860276ac4373b67b1650d6d3e3"); - }), - - it("Composable Dish: apply functions can be chained", () => { - const result = new Dish("input").apply(toBase32).apply(SHA3, {size: "224"}); - assert.strictEqual(result.toString(), "493e8136b759370a415ef2cf2f7a69690441ff86592aba082bc2e2e0"); - }), it("Excluded operations: throw a sensible error when you try and call one", () => { try { @@ -428,6 +374,5 @@ TestRegister.addApiTests([ // First element (subheading) removed assert.equal(chef.convertDistance.args.inputUnits.options[0], "Nanometres (nm)"); assert.equal(chef.defangURL.args.process.options[1], "Only full URLs"); - }) - + }), ]);