UNPKG

postchain-client

Version:

Client library for accessing a Postchain node through REST.

109 lines 5.54 kB
import { checkGtvType, ensureBuffer, ensureString, matchRellErrorString, removeDuplicateSigners, toBuffer, toQueryObjectGTV, } from "../../src/formatter"; import { mockHexStringOfThirtyTwoBytesBuffer, mockSignerPrivKey, mockThirtyTwoBytesBuffer, } from "../common/mocks"; const gtvValues = [ { typeName: "null", value: null }, { typeName: "bytearray", value: Buffer.from("test buffer") }, { typeName: "string", value: "test string" }, { typeName: "integer", value: 1 }, { typeName: "dict", value: { name: "test", value: "test string" } }, { typeName: "array", value: ["test string", [1, "test string 2"]], }, { typeName: "emptyArray", value: [] }, { typeName: "bigInteger", value: BigInt(9007199254740991) }, ]; describe("Formatter tests", () => { describe("Buffer tests", () => { it("converts hex to Buffer", () => { const pubKey = mockHexStringOfThirtyTwoBytesBuffer; const bufferedPubKey = toBuffer(pubKey); expect(bufferedPubKey.toString("hex")).toBe(pubKey); }); }); describe("Ensure Buffer", () => { it("returns Buffer when input is buffer", () => { const pubKey = mockThirtyTwoBytesBuffer; const bufferPubKey = ensureBuffer(pubKey); expect(bufferPubKey).toBeInstanceOf(Buffer); }); it("returns Buffer when input is string", () => { const pubKey = mockHexStringOfThirtyTwoBytesBuffer; const bufferPubKey = ensureBuffer(pubKey); expect(bufferPubKey).toBeInstanceOf(Buffer); }); }); describe("Ensure String", () => { it("returns string when input is string", () => { const formattedStringValue = ensureString(mockHexStringOfThirtyTwoBytesBuffer); expect(typeof formattedStringValue).toBe("string"); }); it("returns string when input is Buffer", () => { const formattedStringValue = ensureString(mockThirtyTwoBytesBuffer); expect(typeof formattedStringValue).toBe("string"); }); }); describe("Filter signers", () => { const signerPrivKeyB = Buffer.alloc(32, "b"); it("removes duplicate entries of signers", () => { const signers = [mockSignerPrivKey, mockSignerPrivKey, signerPrivKeyB]; const filteredSigners = removeDuplicateSigners(signers); expect(filteredSigners).toStrictEqual([mockSignerPrivKey, signerPrivKeyB]); }); }); describe("Check GTV type", () => { gtvValues.forEach(({ typeName, value }) => { it(`expect value to be of GTV type ${typeName}`, () => { expect(checkGtvType(value)).toBe(true); }); }); it(`expect boolean to not be of GTV type`, () => { const nonGtvValue = true; expect(checkGtvType(nonGtvValue)).toBe(false); }); }); describe("Format QueryObject", () => { it("formats empty query", () => { const object = { name: "aQueryName" }; const result = toQueryObjectGTV(object); expect(result).toStrictEqual(["aQueryName", {}]); }); it("formats one arg query", () => { const object = { name: "aQueryName", args: { argName: 1 } }; const result = toQueryObjectGTV(object); expect(result).toStrictEqual(["aQueryName", { argName: 1 }]); }); it("formats two arg query", () => { const object = { name: "aQueryName", args: { argName: 1, argName2: 2 }, }; const result = toQueryObjectGTV(object); expect(result).toStrictEqual(["aQueryName", { argName: 1, argName2: 2 }]); }); }); describe("Rell error matching function", () => { it("returns operation, rellLine, and shortReason when string is a match", () => { const errorString = "[bc-rid=8E:DFB4, chain-id=0] Operation 'news_feed_ch5.news_feed:make_post' failed: Expected at least two operations, make sure that you included auth"; const result = matchRellErrorString(errorString); expect(result.operation).toBe("news_feed_ch5.news_feed:make_post"); expect(result.rellLine).toBe("bc-rid=8E:DFB4, chain-id=0"); expect(result.shortReason).toBe("Expected at least two operations, make sure that you included auth"); }); it("returns operation, rellLine, and shortReason when string is a match (different string)", () => { const errorString = "[bc-rindslhferuhb] Operation 'news_hciduhcidhws_feed:make_post' failed: Expec.rvblcs(dhs) sure that you included auth"; const result = matchRellErrorString(errorString); expect(result.operation).toBe("news_hciduhcidhws_feed:make_post"); expect(result.rellLine).toBe("bc-rindslhferuhb"); expect(result.shortReason).toBe("Expec.rvblcs(dhs) sure that you included auth"); }); it("returns undefined when string is not a match", () => { const errorString = "Operation 'news_feed_ch5.news_feed:make_post' failed: Expected at least two operations, make sure that you included auth"; const result = matchRellErrorString(errorString); expect(result.operation).toBeUndefined(); expect(result.rellLine).toBeUndefined(); expect(result.shortReason).toBeUndefined(); }); }); }); //# sourceMappingURL=formatter.test.js.map