diff --git a/packages/tendermint-rpc/src/client.spec.ts b/packages/tendermint-rpc/src/client.spec.ts index c7466c95..da9eff42 100644 --- a/packages/tendermint-rpc/src/client.spec.ts +++ b/packages/tendermint-rpc/src/client.spec.ts @@ -14,8 +14,12 @@ import * as responses from "./responses"; import { HttpClient, RpcClient, WebsocketClient } from "./rpcclients"; import { TxBytes } from "./types"; +function tendermintEnabled(): boolean { + return !!process.env.TENDERMINT_ENABLED; +} + function pendingWithoutTendermint(): void { - if (!process.env.TENDERMINT_ENABLED) { + if (!tendermintEnabled()) { pending("Set TENDERMINT_ENABLED to enable tendermint-based tests"); } } @@ -124,118 +128,142 @@ function defaultTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor): void { client.disconnect(); }); - it("can call status", async () => { - pendingWithoutTendermint(); - const client = new Client(rpcFactory(), adaptor); + describe("status", () => { + it("works", async () => { + pendingWithoutTendermint(); + const client = new Client(rpcFactory(), adaptor); - const status = await client.status(); - expect(status.nodeInfo.other.size).toBeGreaterThanOrEqual(2); - expect(status.nodeInfo.other.get("tx_index")).toEqual("on"); - expect(status.validatorInfo.pubkey).toBeTruthy(); - expect(status.validatorInfo.votingPower).toBeGreaterThan(0); - expect(status.syncInfo.catchingUp).toEqual(false); - expect(status.syncInfo.latestBlockHeight).toBeGreaterThanOrEqual(1); + const status = await client.status(); + expect(status.nodeInfo.other.size).toBeGreaterThanOrEqual(2); + expect(status.nodeInfo.other.get("tx_index")).toEqual("on"); + expect(status.validatorInfo.pubkey).toBeTruthy(); + expect(status.validatorInfo.votingPower).toBeGreaterThan(0); + expect(status.syncInfo.catchingUp).toEqual(false); + expect(status.syncInfo.latestBlockHeight).toBeGreaterThanOrEqual(1); - client.disconnect(); + client.disconnect(); + }); }); - it("can query a tx properly", async () => { - pendingWithoutTendermint(); - const client = new Client(rpcFactory(), adaptor); + describe("tx", () => { + it("can query a tx properly", async () => { + pendingWithoutTendermint(); + const client = new Client(rpcFactory(), adaptor); - const find = randomString(); - const me = randomString(); - const tx = buildKvTx(find, me); - - const txRes = await client.broadcastTxCommit({ tx: tx }); - expect(responses.broadcastTxCommitSuccess(txRes)).toEqual(true); - expect(txRes.height).toBeTruthy(); - const height: number = txRes.height || 0; // || 0 for type system - expect(txRes.hash.length).not.toEqual(0); - const hash = txRes.hash; - - await tendermintSearchIndexUpdated(); - - // find by hash - does it match? - const r = await client.tx({ hash: hash, prove: true }); - // both values come from rpc, so same type (Buffer/Uint8Array) - expect(r.hash).toEqual(hash); - // force the type when comparing to locally generated value - expect(r.tx).toEqual(tx); - expect(r.height).toEqual(height); - expect(r.proof).toBeTruthy(); - - // txSearch - you must enable the indexer when running - // tendermint, else you get empty results - const query = buildQuery({ tags: [{ key: "app.key", value: find }] }); - - const s = await client.txSearch({ query: query, page: 1, per_page: 30 }); - // should find the tx - expect(s.totalCount).toEqual(1); - // should return same info as querying directly, - // except without the proof - expect(s.txs[0]).toEqual({ ...r, proof: undefined }); - - // ensure txSearchAll works as well - const sall = await client.txSearchAll({ query: query }); - // should find the tx - expect(sall.totalCount).toEqual(1); - // should return same info as querying directly, - // except without the proof - expect(sall.txs[0]).toEqual({ ...r, proof: undefined }); - - // and let's query the block itself to see this transaction - const block = await client.block(height); - expect(block.block.txs.length).toEqual(1); - expect(block.block.txs[0]).toEqual(tx); - - client.disconnect(); - }); - - it("can paginate over txSearch results", async () => { - pendingWithoutTendermint(); - const client = new Client(rpcFactory(), adaptor); - - const find = randomString(); - const query = buildQuery({ tags: [{ key: "app.key", value: find }] }); - - async function sendTx(): Promise { + const find = randomString(); const me = randomString(); const tx = buildKvTx(find, me); const txRes = await client.broadcastTxCommit({ tx: tx }); expect(responses.broadcastTxCommitSuccess(txRes)).toEqual(true); expect(txRes.height).toBeTruthy(); + const height: number = txRes.height || 0; // || 0 for type system expect(txRes.hash.length).not.toEqual(0); - } + const hash = txRes.hash; - // send 3 txs - await sendTx(); - await sendTx(); - await sendTx(); + await tendermintSearchIndexUpdated(); - await tendermintSearchIndexUpdated(); + // find by hash - does it match? + const r = await client.tx({ hash: hash, prove: true }); + // both values come from rpc, so same type (Buffer/Uint8Array) + expect(r.hash).toEqual(hash); + // force the type when comparing to locally generated value + expect(r.tx).toEqual(tx); + expect(r.height).toEqual(height); + expect(r.proof).toBeTruthy(); - // expect one page of results - const s1 = await client.txSearch({ query: query, page: 1, per_page: 2 }); - expect(s1.totalCount).toEqual(3); - expect(s1.txs.length).toEqual(2); + // txSearch - you must enable the indexer when running + // tendermint, else you get empty results + const query = buildQuery({ tags: [{ key: "app.key", value: find }] }); - // second page - const s2 = await client.txSearch({ query: query, page: 2, per_page: 2 }); - expect(s2.totalCount).toEqual(3); - expect(s2.txs.length).toEqual(1); + const s = await client.txSearch({ query: query, page: 1, per_page: 30 }); + // should find the tx + expect(s.totalCount).toEqual(1); + // should return same info as querying directly, + // except without the proof + expect(s.txs[0]).toEqual({ ...r, proof: undefined }); - // and all together now - const sall = await client.txSearchAll({ query: query, per_page: 2 }); - expect(sall.totalCount).toEqual(3); - expect(sall.txs.length).toEqual(3); - // make sure there are in order from lowest to highest height - const [tx1, tx2, tx3] = sall.txs; - expect(tx2.height).toEqual(tx1.height + 1); - expect(tx3.height).toEqual(tx2.height + 1); + // ensure txSearchAll works as well + const sall = await client.txSearchAll({ query: query }); + // should find the tx + expect(sall.totalCount).toEqual(1); + // should return same info as querying directly, + // except without the proof + expect(sall.txs[0]).toEqual({ ...r, proof: undefined }); - client.disconnect(); + // and let's query the block itself to see this transaction + const block = await client.block(height); + expect(block.block.txs.length).toEqual(1); + expect(block.block.txs[0]).toEqual(tx); + + client.disconnect(); + }); + }); + + describe("txSearch", () => { + const key = randomString(); + + beforeAll(async () => { + if (tendermintEnabled()) { + const client = new Client(rpcFactory(), adaptor); + + // eslint-disable-next-line no-inner-declarations + async function sendTx(): Promise { + const me = randomString(); + const tx = buildKvTx(key, me); + + const txRes = await client.broadcastTxCommit({ tx: tx }); + expect(responses.broadcastTxCommitSuccess(txRes)).toEqual(true); + expect(txRes.height).toBeTruthy(); + expect(txRes.hash.length).not.toEqual(0); + } + + // send 3 txs + await sendTx(); + await sendTx(); + await sendTx(); + + client.disconnect(); + + await tendermintSearchIndexUpdated(); + } + }); + + it("can paginate over txSearch results", async () => { + pendingWithoutTendermint(); + const client = new Client(rpcFactory(), adaptor); + + const query = buildQuery({ tags: [{ key: "app.key", value: key }] }); + + // expect one page of results + const s1 = await client.txSearch({ query: query, page: 1, per_page: 2 }); + expect(s1.totalCount).toEqual(3); + expect(s1.txs.length).toEqual(2); + + // second page + const s2 = await client.txSearch({ query: query, page: 2, per_page: 2 }); + expect(s2.totalCount).toEqual(3); + expect(s2.txs.length).toEqual(1); + + client.disconnect(); + }); + + it("can get all search results in one call", async () => { + pendingWithoutTendermint(); + const client = new Client(rpcFactory(), adaptor); + + const query = buildQuery({ tags: [{ key: "app.key", value: key }] }); + + const sall = await client.txSearchAll({ query: query, per_page: 2 }); + expect(sall.totalCount).toEqual(3); + expect(sall.txs.length).toEqual(3); + // make sure there are in order from lowest to highest height + const [tx1, tx2, tx3] = sall.txs; + expect(tx2.height).toEqual(tx1.height + 1); + expect(tx3.height).toEqual(tx2.height + 1); + + client.disconnect(); + }); }); }