UNPKG

postchain-client

Version:

Client library for accessing a Postchain node through REST.

140 lines 7.56 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { DirectoryNodeUrlPoolException, MissingBlockchainIdentifierError, } from "../../../src/blockchainClient/errors"; import { server } from "../../../mocks/servers"; import { createNodeManager } from "../../../src/blockchainClient/nodeManager"; import { exampleTxHashString, LOCAL_POOL, mockBuffer, mockBufferBlockchainRid, mockHexStringOfThirtyTwoBytesBuffer, mockStringBlockchainRid, mockStringDirectoryChainRid, } from "../../common/mocks"; import { SystemChainException } from "../../../src/ICCF/error"; import { getTestsClient } from "../../common/setups"; import * as utils from "../../../src/blockchainClient/utils"; import * as iccf from "../../../src/ICCF/IccfProofTxMaterialBuilder"; import * as blockchainClient from "../../../src/blockchainClient/blockchainClient"; let client; describe("Blockchain client util tests", () => { const directoryNodeUrlPool = [{ url: LOCAL_POOL, whenAvailable: 0 }]; beforeAll(() => __awaiter(void 0, void 0, void 0, function* () { server.listen(); client = yield getTestsClient(); })); beforeEach(() => __awaiter(void 0, void 0, void 0, function* () { jest.clearAllMocks(); })); afterEach(() => { server.resetHandlers(); jest.restoreAllMocks(); }); describe("getNodeUrlsForBlockchainFromDirectoryChain", () => { it("returns list of node URLs", () => __awaiter(void 0, void 0, void 0, function* () { const expectedResponse = ["url1", "url2"]; const mockClient = { query: jest.fn().mockResolvedValue(expectedResponse), config: { endpointPool: [{ url: "url1" }] }, }; jest.spyOn(blockchainClient, "createClient").mockResolvedValue(mockClient); const res = yield utils.nodeDiscovery({ nodeManager: createNodeManager({ nodeUrls: directoryNodeUrlPool.map(node => node.url), }), directoryEndpointPool: directoryNodeUrlPool, blockchainRid: mockHexStringOfThirtyTwoBytesBuffer, }); expect(res).toEqual(expectedResponse); })); it("throws error if no blockchain RID or IID was provided", () => __awaiter(void 0, void 0, void 0, function* () { yield expect(utils.nodeDiscovery({ nodeManager: createNodeManager({ nodeUrls: directoryNodeUrlPool.map(node => node.url), }), directoryEndpointPool: directoryNodeUrlPool, })).rejects.toThrow(MissingBlockchainIdentifierError); })); it("throws error if directory node URL pool is missing", () => __awaiter(void 0, void 0, void 0, function* () { const emptyDirectoryNodeUrlPool = []; yield expect(utils.nodeDiscovery({ nodeManager: createNodeManager({ nodeUrls: emptyDirectoryNodeUrlPool.map(node => node.url), }), directoryEndpointPool: emptyDirectoryNodeUrlPool, blockchainRid: mockHexStringOfThirtyTwoBytesBuffer, })).rejects.toThrow(DirectoryNodeUrlPoolException); })); it("returns null if no nodes were found", () => __awaiter(void 0, void 0, void 0, function* () { const res = yield utils.nodeDiscovery({ nodeManager: createNodeManager({ nodeUrls: directoryNodeUrlPool.map(node => node.url), }), directoryEndpointPool: directoryNodeUrlPool, blockchainRid: mockHexStringOfThirtyTwoBytesBuffer, }); expect(res).toEqual(null); })); }); it("returns empty list if no nodes were found using blockchainIID", () => __awaiter(void 0, void 0, void 0, function* () { const res = yield utils.nodeDiscovery({ nodeManager: createNodeManager({ nodeUrls: directoryNodeUrlPool.map(node => node.url), }), directoryEndpointPool: directoryNodeUrlPool, blockchainIid: 0, }); expect(res).toEqual(null); })); describe("formatBlockInfoResponse", () => { const baseBlockInfo = { rid: "string", prevBlockRID: "string", header: "string", height: 323, transactions: [], witness: "string", witnesses: ["string"], timestamp: 11, }; it("formats block info", () => { const result = utils.formatBlockInfoResponse(baseBlockInfo); expect(result).not.toBeNull(); }); }); it("getAnchoringClientAndSystemChainRid returns anchoringClient and systemAnchoringChainBridString", () => __awaiter(void 0, void 0, void 0, function* () { const mockClient = { config: { endpointPool: [{ url: "http://localhost" }], blockchainRid: mockStringBlockchainRid, directoryChainRid: mockStringDirectoryChainRid, }, query: jest.fn().mockResolvedValue(mockBuffer), }; jest.spyOn(blockchainClient, "createClient").mockResolvedValue(mockClient); jest.spyOn(utils, "getSystemClient").mockResolvedValue(mockClient); jest.spyOn(iccf, "getAnchoringClient").mockResolvedValue(mockClient); jest.spyOn(utils, "getSystemAnchoringChain").mockResolvedValue(mockBufferBlockchainRid); const { anchoringClient, systemAnchoringChainBridString } = yield utils.getAnchoringClientAndSystemChainRid(mockClient); expect(anchoringClient).toEqual(mockClient); expect(systemAnchoringChainBridString).toEqual(exampleTxHashString); })); describe("getSystemAnchoringChain", () => { it("throws SystemChainException when invalid client is provided", () => __awaiter(void 0, void 0, void 0, function* () { yield expect(utils.getSystemAnchoringChain(client)).rejects.toThrow(SystemChainException); })); }); describe("getBlockchainApiUrls", () => { it("returns null when directory chain query does not find blockchain api urls for blockchain rid", () => __awaiter(void 0, void 0, void 0, function* () { const notFoundBlockchainApiUrls = yield utils.getBlockchainApiUrls(client, mockBufferBlockchainRid); expect(notFoundBlockchainApiUrls).toEqual(null); })); it("returns node URLs array", () => __awaiter(void 0, void 0, void 0, function* () { const expectedNodesUrls = ["url1", "url2"]; jest.spyOn(client, "query").mockResolvedValue(expectedNodesUrls); const foundBlockchainApiUrls = yield utils.getBlockchainApiUrls(client, mockBuffer); expect(foundBlockchainApiUrls).toEqual(expectedNodesUrls); })); }); }); //# sourceMappingURL=util.test.js.map