From 65a3897f8712742f915924b9ef62fa6eea36ddff Mon Sep 17 00:00:00 2001 From: d98762625 Date: Mon, 29 Apr 2019 17:09:01 +0100 Subject: [PATCH] add dish translation tests for node --- src/core/Dish.mjs | 2 +- src/core/Utils.mjs | 3 +- src/core/dishTranslationTypes/DishFile.mjs | 1 - src/core/dishTranslationTypes/DishHTML.mjs | 12 +- .../dishTranslationTypes/DishListFile.mjs | 5 + tests/node/tests/NodeDish.mjs | 143 +++++++++++++++++- tests/node/tests/nodeApi.mjs | 5 +- 7 files changed, 147 insertions(+), 24 deletions(-) diff --git a/src/core/Dish.mjs b/src/core/Dish.mjs index e5ba9aec..cc4dfa18 100755 --- a/src/core/Dish.mjs +++ b/src/core/Dish.mjs @@ -208,7 +208,7 @@ class Dish { valid() { switch (this.type) { case Dish.BYTE_ARRAY: - if (!(this.value instanceof Array)) { + if (!(this.value instanceof Uint8Array) && !(this.value instanceof Array)) { return false; } diff --git a/src/core/Utils.mjs b/src/core/Utils.mjs index 34f80034..a7286d09 100755 --- a/src/core/Utils.mjs +++ b/src/core/Utils.mjs @@ -1039,8 +1039,7 @@ class Utils { if (!Utils.isNode()) { throw new TypeError("Browser environment cannot support readFileSync"); } - // Resist using node's Buffer.buffer here - this yields a 8192 length byteArray - // regardless of the length of the buffer. + const arrayBuffer = Uint8Array.from(file.data); return arrayBuffer.buffer; } diff --git a/src/core/dishTranslationTypes/DishFile.mjs b/src/core/dishTranslationTypes/DishFile.mjs index 5bc61022..151b19b6 100644 --- a/src/core/dishTranslationTypes/DishFile.mjs +++ b/src/core/dishTranslationTypes/DishFile.mjs @@ -19,7 +19,6 @@ class DishFile extends DishTranslationType { static toArrayBuffer() { DishFile.checkForValue(this.value); if (Utils.isNode()) { - // TODO this.value = Utils.readFileSync(this.value); } else { return new Promise((resolve, reject) => { diff --git a/src/core/dishTranslationTypes/DishHTML.mjs b/src/core/dishTranslationTypes/DishHTML.mjs index 893c46a0..369357ed 100644 --- a/src/core/dishTranslationTypes/DishHTML.mjs +++ b/src/core/dishTranslationTypes/DishHTML.mjs @@ -4,14 +4,13 @@ * @license Apache-2.0 */ -import DishTranslationType from "./DishTranslationType"; -import Utils from "../Utils"; import DishString from "./DishString"; +import Utils from "../Utils"; /** * Translation methods for HTML Dishes */ -class DishHTML extends DishTranslationType { +class DishHTML extends DishString { /** * convert the given value to a ArrayBuffer @@ -22,13 +21,6 @@ class DishHTML extends DishTranslationType { this.value = this.value ? Utils.strToArrayBuffer(Utils.unescapeHtml(Utils.stripHtmlTags(this.value, true))) : new ArrayBuffer; } - /** - * convert the given value from a ArrayBuffer - * @param {boolean} notUTF8 - */ - static fromArrayBuffer(notUTF8) { - DishString.fromByteArray(this.value, notUTF8); - } } export default DishHTML; diff --git a/src/core/dishTranslationTypes/DishListFile.mjs b/src/core/dishTranslationTypes/DishListFile.mjs index 77848c4a..3fc2d5d2 100644 --- a/src/core/dishTranslationTypes/DishListFile.mjs +++ b/src/core/dishTranslationTypes/DishListFile.mjs @@ -5,6 +5,7 @@ */ import DishTranslationType from "./DishTranslationType"; +import Utils from "../Utils.mjs"; /** * Translation methods for ListFile Dishes @@ -16,6 +17,10 @@ class DishListFile extends DishTranslationType { */ static toArrayBuffer() { DishListFile.checkForValue(this.value); + + if (Utils.isNode()) { + this.value = this.value.map(file => Uint8Array.from(file.data)); + } this.value = DishListFile.concatenateTypedArrays(...this.value).buffer; } diff --git a/tests/node/tests/NodeDish.mjs b/tests/node/tests/NodeDish.mjs index e4261ecd..67ac9e9a 100644 --- a/tests/node/tests/NodeDish.mjs +++ b/tests/node/tests/NodeDish.mjs @@ -1,9 +1,13 @@ 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"; +import BigNumber from "bignumber.js"; + +import { Dish, toBase32, SHA3 } from "../../../src/node/index"; +import File from "../../../src/node/File"; +import TestRegister from "../../lib/TestRegister"; + TestRegister.addApiTests([ it("Composable Dish: Should have top level Dish object", () => { assert.ok(Dish); @@ -61,8 +65,135 @@ TestRegister.addApiTests([ assert.strictEqual(result.toString(), "493e8136b759370a415ef2cf2f7a69690441ff86592aba082bc2e2e0"); }), - // it("Dish translation: ArrayBuffer to ArrayBuffer", () => { - // const dish = new Dish(); - - // }), + it("Dish translation: ArrayBuffer to ArrayBuffer", () => { + const dish = new Dish(new ArrayBuffer(10), 4); + dish.get("array buffer"); + assert.strictEqual(dish.value.byteLength, 10); + assert.strictEqual(dish.type, 4); + }), + + it("Dish translation: ArrayBuffer and String", () => { + const dish = new Dish("some string", 1); + dish.get("array buffer"); + + assert.strictEqual(dish.type, 4); + assert.deepStrictEqual(dish.value, new ArrayBuffer(11)); + assert.deepEqual(dish.value.byteLength, 11); + + dish.get("string"); + assert.strictEqual(dish.type, 1); + assert.strictEqual(dish.value, "some string"); + }), + + it("Dish translation: ArrayBuffer and number", () => { + const dish = new Dish(100, 2); + dish.get(4); + + assert.strictEqual(dish.type, 4); + assert.deepStrictEqual(dish.value, new ArrayBuffer(10)); + assert.strictEqual(dish.value.byteLength, 3); + + // Check the data in ArrayBuffer represents 100 as a string. + const view = new DataView(dish.value, 0); + assert.strictEqual(String.fromCharCode(view.getUint8(0), view.getUint8(1), view.getUint8(2)), "100"); + + dish.get("number"); + assert.strictEqual(dish.type, 2); + assert.strictEqual(dish.value, 100); + }), + + it("Dish translation: ArrayBuffer and byte array", () => { + const dish = new Dish(new Uint8Array([1, 2, 3]), 0); + dish.get(4); + + // Check intermediate value + const check = new Uint8Array(dish.value); + assert.deepEqual(check, [1, 2, 3]); + + // Check converts back OK + dish.get(0); + assert.deepEqual(dish.value, new Uint8Array([1, 2, 3])); + }), + + it("Dish translation: ArrayBuffer and HTML", () => { + const html = ` + + + + + + Click here + + +`.replace(/\n|\s{4}/g, ""); //remove newlines, tabs + + const dish = new Dish(html, Dish.HTML); + dish.get(4); + + dish.get(3); + assert.strictEqual(dish.value, "Click here"); + }), + + it("Dish translation: ArrayBuffer and BigNumber", () => { + const number = BigNumber(4001); + const dish = new Dish(number, Dish.BIG_NUMBER); + + dish.get(Dish.ARRAY_BUFFER); + assert.deepStrictEqual(dish.value, new ArrayBuffer(10)); + assert.strictEqual(dish.value.byteLength, 4); + + // Check the data in ArrayBuffer represents 4001 as a string. + const view = new DataView(dish.value, 0); + assert.strictEqual(String.fromCharCode(view.getUint8(0), view.getUint8(1), view.getUint8(2), view.getUint8(3)), "4001"); + + dish.get(5); + assert.deepStrictEqual(dish.value, number); + }), + + it("Dish translation: ArrayBuffer and JSON", () => { + const jsonString = "{\"a\": 123455, \"b\": { \"aa\": [1,2,3]}}"; + const dish = new Dish(JSON.parse(jsonString), Dish.JSON); + + dish.get(Dish.ARRAY_BUFFER); + dish.get(Dish.JSON); + + assert.deepStrictEqual(dish.value, JSON.parse(jsonString)); + }), + + it("Dish translation: ArrayBuffer and File", () => { + const file = new File("abcd", "unknown"); + const dish = new Dish(file, Dish.FILE); + + dish.get(Dish.ARRAY_BUFFER); + assert.deepStrictEqual(dish.value, new ArrayBuffer(10)); + assert.strictEqual(dish.value.byteLength, 4); + + // Check the data in ArrayBuffer represents "abcd" + const view = new DataView(dish.value, 0); + assert.strictEqual(String.fromCharCode(view.getUint8(0), view.getUint8(1), view.getUint8(2), view.getUint8(3)), "abcd"); + + dish.get(Dish.FILE); + + assert.deepStrictEqual(dish.value.data, file.data); + assert.strictEqual(dish.value.name, file.name); + assert.strictEqual(dish.value.type, file.type); + // Do not test lastModified + }), + + it("Dish translation: ArrayBuffer and ListFile", () => { + const file1 = new File("abcde", "unknown"); + const file2 = new File("fghijk", "unknown"); + + const dish = new Dish([file1, file2], Dish.LIST_FILE); + + dish.get(Dish.ARRAY_BUFFER); + assert.deepStrictEqual(dish.value, new ArrayBuffer(10)); + assert.strictEqual(dish.value.byteLength, 11); + + dish.get(Dish.LIST_FILE); + const dataArray = new Uint8Array(dish.value[0].data); + // cant store chars in a Uint8Array, so make it a normal one. + const actual = Array.prototype.slice.call(dataArray).map(c => String.fromCharCode(c)).join(""); + assert.strictEqual(actual, "abcdefghijk"); + }), ]); diff --git a/tests/node/tests/nodeApi.mjs b/tests/node/tests/nodeApi.mjs index bcb61415..6233459a 100644 --- a/tests/node/tests/nodeApi.mjs +++ b/tests/node/tests/nodeApi.mjs @@ -15,9 +15,8 @@ import it from "../assertionHandler"; import chef from "../../../src/node/index"; import OperationError from "../../../src/core/errors/OperationError"; import NodeDish from "../../../src/node/NodeDish"; -import fs from "fs"; -import { toBase32, Dish, SHA3 } from "../../../src/node/index"; +import { toBase32} from "../../../src/node/index"; import TestRegister from "../../lib/TestRegister"; TestRegister.addApiTests([ @@ -325,8 +324,6 @@ TestRegister.addApiTests([ assert.strictEqual(result.toString(), "begin_something_anananaaaaak_da_aaak_da_aaaaananaaaaaaan_da_aaaaaaanan_da_aaak_end_something"); }), - - it("Excluded operations: throw a sensible error when you try and call one", () => { try { chef.fork();