postchain-client
Version:
Client library for accessing a Postchain node through REST.
166 lines • 6.54 kB
JavaScript
import _ from "lodash";
import { encodeValue, decodeValue, encodeValueGtx, decodeValueGtx, createTypedArg, parseValue, } from "../../../src/gtx/serialization";
import BN from "bn.js";
import { mixedArray, nestedArray, nestedObject, simpleObject, stringArray, } from "../../common/serializationtestobjects";
function buildDefaultRawGtx(args) {
const operation = buildRawGtxOp("opname", args);
return buildRawGtx(operation);
}
function buildRawGtxOp(opname, args) {
return [opname, args];
}
function buildRawGtx(rawGtxOp, brid = Buffer.from(""), signers = [], signatures = []) {
const tx = [brid, [rawGtxOp], signers];
return [tx, signatures];
}
function testEncodeDecode(arg) {
const encoded = encodeValue(arg);
const decoded = decodeValue(encoded);
expect(_.isEqual(arg, decoded)).toBe(true);
}
function testEncodeDecodeGtx(arg) {
const encoded = encodeValueGtx(arg);
const decoded = decodeValueGtx(encoded);
expect(_.isEqual(arg, decoded)).toBe(true);
}
const gtvValues = [
{ typeName: "null", args: [null] },
{ typeName: "bytearray", args: [Buffer.from("test buffer")] },
{ typeName: "string", args: ["test string"] },
{ typeName: "integer", args: [1] },
{ typeName: "dict", args: [{ name: "test", value: "test string" }] },
{
typeName: "array",
args: [buildDefaultRawGtx([1]), buildDefaultRawGtx([1])],
},
{ typeName: "bigInteger", args: [BigInt(9007199254740991)] },
{ typeName: "empty", args: [] },
];
const bigIntValue = "18446744073709551616";
const negativeBigIntValue = "-18446744073709551616";
describe("Serialization", function () {
gtvValues.forEach(({ typeName, args }) => {
it(`encodes and decodes a GTX object with arguments of type ${typeName}`, () => {
const rawGtx = buildDefaultRawGtx(args);
testEncodeDecodeGtx(rawGtx);
});
});
gtvValues.forEach(({ typeName, args }) => {
it(`encodes and decodes an input of type ${typeName}`, () => {
testEncodeDecode(args);
});
});
it("should throw error when decoding GTV value as GTX", () => {
expect(() => decodeValueGtx(encodeValue("test string"))).toThrow("Unexpected type of value: test string, expected decoded value to be of type RawGtx");
});
it("should throw an error when decoding unsupported value", () => {
try {
testEncodeDecode(null);
}
catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error.message).toEqual("Invalid GTV data. Choice not matched at: (shallow)");
}
});
});
describe("Create typed args tests", () => {
it("null", () => {
expect(createTypedArg(null)).toEqual({ type: "null", value: null });
});
it("Buffer", () => {
expect(createTypedArg(Buffer.from("hello"))).toEqual({
type: "byteArray",
value: Buffer.from("hello"),
});
});
it("Boolean true", () => {
expect(createTypedArg(true)).toEqual({ type: "integer", value: 1 });
});
it("Boolean false", () => {
expect(createTypedArg(false)).toEqual({ type: "integer", value: 0 });
});
it("Empty String", () => {
expect(createTypedArg("")).toEqual({ type: "string", value: "" });
});
it("Escaped String", () => {
expect(createTypedArg('"Name"')).toEqual({ type: "string", value: '"Name"' });
});
it("Positive Integer", () => {
expect(createTypedArg(1)).toEqual({ type: "integer", value: new BN(1) });
});
it("Max Integer", () => {
expect(createTypedArg(Number.MAX_SAFE_INTEGER)).toEqual({
type: "integer",
value: new BN(Number.MAX_SAFE_INTEGER),
});
});
it("Min Integer", () => {
expect(createTypedArg(Number.MIN_SAFE_INTEGER)).toEqual({
type: "integer",
value: new BN(Number.MIN_SAFE_INTEGER),
});
});
it("Negative Integer", () => {
expect(createTypedArg(-1)).toEqual({ type: "integer", value: new BN(-1) });
});
it("Float throws error", () => {
expect(() => createTypedArg(1.1)).toThrow("Failed to encode 1.1: Error: User error: Only integers are supported");
});
it("Binary", () => {
expect(createTypedArg(0b11)).toEqual({ type: "integer", value: new BN(3) });
});
it("Hex", () => {
expect(createTypedArg(0xa)).toEqual({ type: "integer", value: new BN(10) });
});
it("Octal", () => {
expect(createTypedArg(0o4)).toEqual({ type: "integer", value: new BN(4) });
});
it("test BigInt beyond JS number limit", () => {
expect(() => createTypedArg(Number(bigIntValue))).toThrow(`Failed to encode 18446744073709552000: Error: Assertion failed`);
});
it("BigInt", () => {
expect(createTypedArg(BigInt(bigIntValue))).toEqual({
type: "bigInteger",
value: new BN(bigIntValue),
});
});
it("Negative BigInt", () => {
expect(createTypedArg(BigInt(negativeBigIntValue))).toEqual({
type: "bigInteger",
value: new BN(negativeBigIntValue),
});
});
it("Empty array", () => {
expect(createTypedArg([])).toEqual({ type: "array", value: [] });
});
it("String array", () => {
expect(createTypedArg(["foo", "bar"])).toEqual(stringArray);
});
it("Mixed type array", () => {
expect(createTypedArg(["foo", 1])).toEqual(mixedArray);
});
it("Nested array", () => {
expect(createTypedArg([["foo"], ["bar"]])).toEqual(nestedArray);
});
it("Object", () => {
expect(createTypedArg(["queryname", { arg: "foo" }])).toEqual(simpleObject);
});
it("Nested Object", () => {
expect(createTypedArg(["queryname", { arg: { arg1: "foo" } }])).toEqual(nestedObject);
});
});
describe("ParseValue", () => {
it("null", () => {
expect(parseValue({ type: "null", value: null })).toEqual(null);
});
it("Buffer", () => {
expect(parseValue({ type: "byteArray", value: Buffer.from("hello") })).toEqual(Buffer.from("hello"));
});
it("BigInt", () => {
expect(parseValue({ type: "bigInteger", value: new BN(bigIntValue) })).toEqual(BigInt(bigIntValue));
});
it("Missing type throws error", () => {
expect(() => parseValue({ type: "missing", value: [] })).toThrow('Cannot parse typedArg {"type":"missing","value":[]}. Unknown type missing');
});
});
//# sourceMappingURL=serialization.test.js.map