Skip to content

Commit e8298a1

Browse files
added tests for rest api encapsulation methods
1 parent 5602bd2 commit e8298a1

File tree

2 files changed

+93
-15
lines changed

2 files changed

+93
-15
lines changed

sdk/src/network-client.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ class AleoNetworkClient {
701701
async getProgramMappingPlaintext(programId: string, mappingName: string, key: string | Plaintext): Promise<Plaintext> {
702702
try {
703703
const value = await this.getProgramMappingValue(programId, mappingName, key);
704-
return Plaintext.fromString(JSON.parse(value));
704+
return Plaintext.fromString(value);
705705
} catch (error) {
706706
throw new Error(`${error}`);
707707
}
@@ -715,11 +715,11 @@ class AleoNetworkClient {
715715
* @example
716716
* const stateRoot = networkClient.getStateRoot();
717717
*/
718-
async getStateRoot(height?: number): Promise<string> {
718+
async getStateRoot(): Promise<string> {
719719
try {
720-
return await this.fetchData<string>(`/stateRoot/${height ?? "latest"}`);
720+
return await this.fetchData<string>('/stateRoot/latest');
721721
} catch (error) {
722-
throw new Error(`Error fetching ${height === undefined || height === null ? 'latest state root' : `state root at block ${height}`}: ${error}`);
722+
throw new Error(`Error fetching latest state root: ${error}`);
723723
}
724724
}
725725

@@ -812,7 +812,9 @@ class AleoNetworkClient {
812812
*/
813813
async getTransactionsByHash(hash: string): Promise<Array<ConfirmedTransactionJSON>> {
814814
try {
815-
return await this.fetchData<Array<ConfirmedTransactionJSON>>(`/block/${hash}/transactions`);
815+
const block = await this.fetchData<BlockJSON>(`/block/${hash}`);
816+
const height = block.header.metadata.height;
817+
return await this.getTransactions(height);
816818
} catch (error) {
817819
throw new Error(`Error fetching transactions for block ${hash}: ${error}`);
818820
}

sdk/tests/network-client.test.ts

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import sinon from "sinon";
22
import { expect } from "chai";
33
import {
44
Account,
5-
BlockJSON,
65
AleoNetworkClient,
76
TransactionObject,
87
InputObject,
@@ -17,6 +16,7 @@ import {
1716
Transition,
1817
TransitionObject,
1918
} from "@provablehq/sdk/%%NETWORK%%.js";
19+
import { BlockJSON } from "../src/models/blockJSON";
2020
import { beaconPrivateKeyString } from "./data/account-data";
2121

2222
async function catchError(f: () => Promise<any>): Promise<Error | null> {
@@ -33,7 +33,7 @@ async function catchError(f: () => Promise<any>): Promise<Error | null> {
3333
async function expectThrowsMessage(f: () => Promise<any>, message: string): Promise<void> {
3434
const error = await catchError(f);
3535
expect(error).not.equal(null);
36-
expect(error!.message).equal(message);
36+
expect(error!.message).contains(message);
3737
}
3838
async function expectThrows(f: () => Promise<any>): Promise<void> {
3939
const error = await catchError(f);
@@ -69,32 +69,57 @@ describe('NodeConnection', () => {
6969
});
7070

7171
describe('getBlock', () => {
72-
it.skip('should return a Block object', async () => {
72+
it('should return a Block object', async () => {
7373
const block = await connection.getBlock(1);
74-
expect((block as BlockJSON).block_hash).equal("ab1eddc3np4h6duwf5a7ht6u0x5maa08780l885j6xq0s7l88df0qrq3d72me");
74+
if (network === "testnet") {
75+
expect((block as BlockJSON).block_hash).equal("ab1eddc3np4h6duwf5a7ht6u0x5maa08780l885j6xq0s7l88df0qrq3d72me");
76+
} else {
77+
expect((block as BlockJSON).block_hash).equal("ab1yvrttjzr7d9tu2a6e8n0908amgyjddgv6mmndh75rxkwa44hcugqy3wtc2");
78+
}
7579
});
7680

7781
it('should throw an error if the request fails', async () => {
7882
await expectThrowsMessage(
7983
() => connection.getBlock(99999999),
80-
"Error fetching block.",
84+
"Error fetching block 99999999",
8185
);
8286
});
8387
});
8488

89+
describe('getBlockByHash', () => {
90+
it('should return a Block object', async () => {
91+
let hash = network === "testnet" ?
92+
"ab1eddc3np4h6duwf5a7ht6u0x5maa08780l885j6xq0s7l88df0qrq3d72me" : "ab1yvrttjzr7d9tu2a6e8n0908amgyjddgv6mmndh75rxkwa44hcugqy3wtc2";
93+
const block = await connection.getBlockByHash(hash);
94+
expect((block as BlockJSON).block_hash).equal(hash);
95+
});
96+
97+
it('should throw an error if the request fails', async () => {
98+
await expectThrowsMessage(
99+
() => connection.getBlockByHash("ahhh"),
100+
"Error fetching block ahhh",
101+
)
102+
});
103+
});
104+
85105
describe('getBlockRange', () => {
86-
it.skip('should return an array of Block objects', async () => {
106+
it('should return an array of Block objects', async () => {
87107
const blockRange = await connection.getBlockRange(1, 3);
88108
expect(Array.isArray(blockRange)).equal(true);
89-
expect((blockRange as BlockJSON[]).length).equal(3);
90-
expect(((blockRange as BlockJSON[])[0] as BlockJSON).block_hash).equal("ab1eddc3np4h6duwf5a7ht6u0x5maa08780l885j6xq0s7l88df0qrq3d72me");
91-
expect(((blockRange as BlockJSON[])[1] as BlockJSON).block_hash).equal("ab1uqmm97qk5gzhgwh6308h48aszazhfnn0xdq84lrj7e7myyrf9yyqmqdf42");
109+
expect((blockRange as BlockJSON[]).length).equal(2);
110+
if (network === "testnet") {
111+
expect(((blockRange as BlockJSON[])[0] as BlockJSON).block_hash).equal("ab1eddc3np4h6duwf5a7ht6u0x5maa08780l885j6xq0s7l88df0qrq3d72me");
112+
expect(((blockRange as BlockJSON[])[1] as BlockJSON).block_hash).equal("ab1uqmm97qk5gzhgwh6308h48aszazhfnn0xdq84lrj7e7myyrf9yyqmqdf42");
113+
} else {
114+
expect(((blockRange as BlockJSON[])[0] as BlockJSON).block_hash).equal("ab1yvrttjzr7d9tu2a6e8n0908amgyjddgv6mmndh75rxkwa44hcugqy3wtc2");
115+
expect(((blockRange as BlockJSON[])[1] as BlockJSON).block_hash).equal("ab17ddfwqlx3v4ke8h5l2lpcp5agk2flvpwgpd4l37peck7y7uvsqqsxvvnlr");
116+
}
92117
});
93118

94119
it('should throw an error if the request fails', async () => {
95120
await expectThrowsMessage(
96121
() => connection.getBlockRange(999999999, 1000000000),
97-
"Error fetching blocks between 999999999 and 1000000000.",
122+
"Error fetching blocks between 999999999 and 1000000000",
98123
);
99124
});
100125
});
@@ -141,6 +166,32 @@ describe('NodeConnection', () => {
141166
});
142167
});
143168

169+
describe('getLatestHash', () => {
170+
it('should return a string', async () => {
171+
const latestHash = await connection.getLatestHash();
172+
expect(typeof latestHash).equal('string');
173+
});
174+
175+
it('should set the X-Aleo-SDK-Version header', async () => {
176+
expect(windowFetchSpy.args).deep.equal([]);
177+
178+
await connection.getLatestBlock();
179+
180+
expect(windowFetchSpy.args).deep.equal([
181+
[
182+
"https://api.explorer.provable.com/v1/%%NETWORK%%/block/latest",
183+
{
184+
"headers": {
185+
// @TODO: Run the Jest tests on the compiled Rollup code,
186+
// so that way the version is properly replaced.
187+
"X-Aleo-SDK-Version": "%%VERSION%%"
188+
}
189+
}
190+
],
191+
]);
192+
});
193+
})
194+
144195
describe('getLatestCommittee', () => {
145196
it('should return a string', async () => {
146197
const latestCommittee = await connection.getLatestCommittee();
@@ -170,6 +221,31 @@ describe('NodeConnection', () => {
170221
});
171222
});
172223

224+
describe('getTransactionsByHash', () => {
225+
it('should return an array of Transaction objects', async () => {
226+
const hash = network === 'testnet' ?
227+
'ab1sm6kyqle2ftg4z8gegafqrjy0jwjhzu6fmy73726dgszrtxhxvfqha0eee' : 'ab19dklwl9vp63zu3hwg57wyhvmqf92fx5g8x0t6dr72py8r87pxupqfne5t9';
228+
const transactions = await connection.getTransactionsByHash(hash);
229+
expect(transactions.length).equals(4);
230+
})
231+
});
232+
233+
describe('getConfirmedTransaction', () => {
234+
it('should return a ConfirmedTransaction object', async () => {
235+
const transactionId = network === 'testnet' ?
236+
'at13rluumel7k9cnpaulhj7lwcsc8ntqn7wkv509fye848atczwxgpqqh2p43' : 'at1uushc49p5ldhdqtkuw0gvfzrednnvzd55yrxkx5uqksjj5cqecpscgavk8';
237+
const transaction = await connection.getConfirmedTransaction(transactionId);
238+
expect(transaction.transaction.id).equals(transactionId);
239+
});
240+
241+
it('should throw an error if the request fails', async () => {
242+
await expectThrowsMessage(
243+
() => connection.getConfirmedTransaction('badTransactionId'),
244+
'Error fetching confirmed transaction badTransactionId',
245+
);
246+
});
247+
})
248+
173249
describe('getProgramImports', () => {
174250
it('should return the correct program import names', async () => {
175251
const creditImports = await connection.getProgramImportNames("credits.aleo");

0 commit comments

Comments
 (0)