Skip to content

Commit c26a2f4

Browse files
committed
fix: improving tests
1 parent e619651 commit c26a2f4

16 files changed

+442
-72
lines changed

.github/workflows/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ jobs:
2727
run: npm install
2828
- name: Test
2929
run: npm run test
30+
- name: Upload coverage
31+
uses: codecov/codecov-action@v1
32+
if: ${{ matrix.node-version }} == 14.x
33+
with:
34+
verbose: true

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[![codecov](https://codecov.io/gh/HathorNetwork/hathor-wallet-headless/branch/master/graph/badge.svg?token=NZ3BPUX9V7)](https://codecov.io/gh/HathorNetwork/hathor-wallet-headless)
12
# Hathor Wallet Headless
23

34
This is a headless Hathor Wallet, i.e., without a graphical user interface. It is controlled by its HTTP API.

__tests__/__fixtures__/http-fixtures.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export default {
252252
"00975897028ceb037307327c953f5e7ad4d3f42402d71bd3d11ecb63ac39f01a",
253253
"00e161a6b0bee1781ea9300680913fb76fd0fac4acab527cd9626cc1514abdc9",
254254
],
255-
height: 5,
255+
height: 12,
256256
},
257257
{
258258
tx_id:
@@ -282,7 +282,7 @@ export default {
282282
"00975897028ceb037307327c953f5e7ad4d3f42402d71bd3d11ecb63ac39f01a",
283283
"00e161a6b0bee1781ea9300680913fb76fd0fac4acab527cd9626cc1514abdc9",
284284
],
285-
height: 1,
285+
height: 13,
286286
},
287287
{
288288
tx_id:
@@ -312,7 +312,7 @@ export default {
312312
"00975897028ceb037307327c953f5e7ad4d3f42402d71bd3d11ecb63ac39f01a",
313313
"00e161a6b0bee1781ea9300680913fb76fd0fac4acab527cd9626cc1514abdc9",
314314
],
315-
height: 3,
315+
height: 14,
316316
},
317317
{
318318
tx_id:
@@ -342,7 +342,7 @@ export default {
342342
"00975897028ceb037307327c953f5e7ad4d3f42402d71bd3d11ecb63ac39f01a",
343343
"00e161a6b0bee1781ea9300680913fb76fd0fac4acab527cd9626cc1514abdc9",
344344
],
345-
height: 8,
345+
height: 15,
346346
},
347347
{
348348
tx_id:
@@ -373,7 +373,7 @@ export default {
373373
"00e161a6b0bee1781ea9300680913fb76fd0fac4acab527cd9626cc1514abdc9",
374374
],
375375
tokens: [],
376-
height: 9,
376+
height: 16,
377377
token_name: "09",
378378
token_symbol: "09",
379379
},
@@ -393,7 +393,7 @@ export default {
393393
decoded: {
394394
type: "P2PKH",
395395
address: "WewDeXWyvHP7jJTs7tjLoQfoB72LLxJQqN",
396-
timelock: null,
396+
timelock: 1675256215,
397397
},
398398
token: "00",
399399
spent_by: null,
@@ -405,7 +405,7 @@ export default {
405405
"00975897028ceb037307327c953f5e7ad4d3f42402d71bd3d11ecb63ac39f01a",
406406
"00e161a6b0bee1781ea9300680913fb76fd0fac4acab527cd9626cc1514abdc9",
407407
],
408-
height: 4,
408+
height: 17,
409409
},
410410
{
411411
tx_id:
@@ -445,7 +445,7 @@ export default {
445445
selected_as_input: false,
446446
},
447447
{
448-
value: 2,
448+
value: 100,
449449
token_data: 1,
450450
script: "dqkU/qcZVmiK7oEMzDyVX9kwfldkR8CIrA==",
451451
decoded: {
@@ -494,6 +494,7 @@ export default {
494494
token_name: "01",
495495
token_symbol: "01",
496496
tokens: [],
497+
height: 18
497498
},
498499
],
499500
},

__tests__/__fixtures__/ws-fixtures.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,79 @@ export default {
88
type: "dashboard:metrics",
99
time: 1615218985.77608,
1010
},
11+
melt: {
12+
address: "WWbt2ww4W45YLUAumnumZiyWrABYDzCTdN",
13+
history: {
14+
tx_id: "00f69d6b861fc70e6b1ed9896e0184c3e8d94b01ae1921cfc42176ce91192fb6",
15+
version: 1,
16+
weight: 8.000001,
17+
timestamp: 1615093636,
18+
is_voided: false,
19+
inputs: [
20+
{
21+
value: 2,
22+
token_data: 129,
23+
script: "dqkUVvf1nM1vemtqwnMKE6SAITXvXFqIrA==",
24+
decoded: {
25+
type: "P2PKH",
26+
address: "WewDeXWyvHP7jJTs7tjLoQfoB72LLxJQqN",
27+
timelock: null,
28+
},
29+
token:
30+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
31+
tx_id:
32+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
33+
index: 3,
34+
},
35+
{
36+
value: 100,
37+
token_data: 1,
38+
script: "dqkUX1XQjI1bgpXys7keNvHcewIVMsyIrA==",
39+
decoded: {
40+
type: "P2PKH",
41+
address: "WWbt2ww4W45YLUAumnumZiyWrABYDzCTdN",
42+
timelock: null,
43+
},
44+
token:
45+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
46+
tx_id:
47+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
48+
index: 1,
49+
},
50+
],
51+
outputs: [
52+
{
53+
value: 2,
54+
token_data: 129,
55+
script: "dqkUxwjHrGjBoPy1Zvgc8xpWNEwBe1OIrA==",
56+
decoded: {
57+
type: "P2PKH",
58+
address: "WWbt2ww4W45YLUAumnumZiyWrABYDzCTdN",
59+
timelock: null,
60+
},
61+
token:
62+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
63+
spent_by: null,
64+
},
65+
{
66+
value: 1,
67+
token_data: 0,
68+
script: "dqkUDoWv/T10Ovt/nLTx/EsBVTRRvs+IrA==",
69+
decoded: {
70+
type: "P2PKH",
71+
address: "WWbt2ww4W45YLUAumnumZiyWrABYDzCTdN",
72+
timelock: null,
73+
},
74+
token: "00",
75+
spent_by: null,
76+
},
77+
],
78+
parents: [
79+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
80+
"0000035822a76a9dee0ed2c023b5699c97d08755301aa6e6e357f72ebf98053c",
81+
],
82+
height: 6
83+
},
84+
type: "wallet:address_history",
85+
},
1186
};

__tests__/address.test.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
import { wallet as walletUtils } from "@hathor/wallet-lib";
12
import TestUtils from "./test-utils";
23

34
describe("address api", () => {
45
it("should return 200 with a valid body", async () => {
5-
const response = await TestUtils.request
6+
let response = await TestUtils.request
7+
.get("/wallet/address")
8+
.set({ "x-wallet-id": TestUtils.walletId });
9+
expect(response.status).toBe(200);
10+
expect(response.body.address).toBe(TestUtils.addresses[1]);
11+
12+
// Should return the same address for a second call
13+
response = await TestUtils.request
614
.get("/wallet/address")
715
.set({ "x-wallet-id": TestUtils.walletId });
816
expect(response.status).toBe(200);
9-
expect(response.body.address).toBe("WmtWgtk5GxdcDKwjNwmXXn74nQWTPWhKfx");
17+
expect(response.body.address).toBe(TestUtils.addresses[1]);
1018
});
1119

1220
it("should return 200 with a valid body for index = 0", async () => {
@@ -15,7 +23,7 @@ describe("address api", () => {
1523
.query({ index: 0 })
1624
.set({ "x-wallet-id": TestUtils.walletId });
1725
expect(response.status).toBe(200);
18-
expect(response.body.address).toBe("WewDeXWyvHP7jJTs7tjLoQfoB72LLxJQqN");
26+
expect(response.body.address).toBe(TestUtils.addresses[0]);
1927
});
2028

2129
it("should return 200 for a custom index (number or string)", async () => {
@@ -24,25 +32,31 @@ describe("address api", () => {
2432
.query({ index: 2 })
2533
.set({ "x-wallet-id": TestUtils.walletId });
2634
expect(response.status).toBe(200);
27-
expect(response.body.address).toBe("WPynsVhyU6nP7RSZAkqfijEutC88KgAyFc");
35+
expect(response.body.address).toBe(TestUtils.addresses[2]);
2836

2937
response = await TestUtils.request
3038
.get("/wallet/address")
3139
.query({ index: "2" })
3240
.set({ "x-wallet-id": TestUtils.walletId });
33-
expect(response.body.address).toBe("WPynsVhyU6nP7RSZAkqfijEutC88KgAyFc");
41+
expect(response.body.address).toBe(TestUtils.addresses[2]);
3442
});
3543

36-
it("should return a new address with mark_as_used", async () => {
37-
let response = await TestUtils.request
38-
.get("/wallet/address")
39-
.query({ mark_as_used: true })
40-
.set({ "x-wallet-id": TestUtils.walletId });
41-
expect(response.status).toBe(200);
44+
it("should return a new address with mark_as_used until the gapLimit is reached", async () => {
45+
const gapLimit = walletUtils.getGapLimit();
46+
for (let index = 1; index <= gapLimit; index++) {
47+
const response = await TestUtils.request
48+
.get("/wallet/address")
49+
.query({ mark_as_used: true })
50+
.set({ "x-wallet-id": TestUtils.walletId });
51+
expect(response.status).toBe(200);
52+
expect(response.body.address).toBe(TestUtils.addresses[index]);
53+
}
4254

43-
response = await TestUtils.request
55+
// Subsequent calls should return the last address as the gap limit was reached
56+
const response = await TestUtils.request
4457
.get("/wallet/address")
4558
.set({ "x-wallet-id": TestUtils.walletId });
46-
expect(response.body.address).toBe("WPynsVhyU6nP7RSZAkqfijEutC88KgAyFc");
59+
expect(response.status).toBe(200);
60+
expect(response.body.address).toBe(TestUtils.addresses[gapLimit]);
4761
});
4862
});

__tests__/addresses.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
1+
import { wallet as walletUtils } from "@hathor/wallet-lib";
12
import TestUtils from "./test-utils";
23

34
describe("addresses api", () => {
45
it("should return 200 with a valid body", async () => {
6+
const gapLimit = walletUtils.getGapLimit();
57
const response = await TestUtils.request
68
.get("/wallet/addresses")
79
.set({ "x-wallet-id": TestUtils.walletId });
810
expect(response.status).toBe(200);
9-
expect(response.body.addresses.length).toBeGreaterThan(20);
11+
expect(response.body.addresses.length).toBe(gapLimit + 1);
12+
expect(response.body.addresses).toEqual(TestUtils.addresses);
13+
});
14+
15+
it("should return all addresses for a custom gap limit", async () => {
16+
walletUtils.setGapLimit(100);
17+
18+
// restart the wallet to use the new gap limit
19+
await TestUtils.stopWallet();
20+
await TestUtils.startWallet();
21+
22+
const gapLimit = 100;
23+
const response = await TestUtils.request
24+
.get("/wallet/addresses")
25+
.set({ "x-wallet-id": TestUtils.walletId });
26+
expect(response.status).toBe(200);
27+
expect(response.body.addresses.length).toBe(gapLimit + 1);
28+
expect(response.body.addresses.slice(0, TestUtils.addresses.length)).toEqual(TestUtils.addresses);
1029
});
1130
});

__tests__/balance.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ describe("balance api", () => {
66
.get("/wallet/balance")
77
.set({ "x-wallet-id": TestUtils.walletId });
88
expect(response.status).toBe(200);
9-
expect(response.body.available).toBe(83199);
10-
expect(response.body.locked).toBe(0);
9+
expect(response.body.available).toBe(76799);
10+
expect(response.body.locked).toBe(6400);
1111
});
1212

1313
it("should return balance for custom token", async () => {

__tests__/create-token.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import TestUtils from "./test-utils";
2+
import httpFixtures from "./__fixtures__/http-fixtures";
23

34
describe("create-token api", () => {
45
it("should return 200 with a valid body", async () => {
@@ -11,6 +12,7 @@ describe("create-token api", () => {
1112
})
1213
.set({ "x-wallet-id": TestUtils.walletId });
1314
expect(response.status).toBe(200);
15+
expect(response.body.success).toBeTruthy();
1416
expect(response.body.hash).toBeDefined();
1517
});
1618

@@ -24,6 +26,7 @@ describe("create-token api", () => {
2426
})
2527
.set({ "x-wallet-id": TestUtils.walletId });
2628
expect(response.status).toBe(200);
29+
expect(response.body.success).toBeTruthy();
2730
expect(response.body.hash).toBeDefined();
2831
});
2932

@@ -44,6 +47,19 @@ describe("create-token api", () => {
4447
});
4548
});
4649

50+
it("should not create a token without the required funds to cover it", async () => {
51+
const response = await TestUtils.request
52+
.post("/wallet/create-token")
53+
.send({
54+
name: "stub_token",
55+
symbol: "03",
56+
amount: 10**9,
57+
})
58+
.set({ "x-wallet-id": TestUtils.walletId });
59+
expect(response.status).toBe(200);
60+
expect(response.body.success).toBeFalsy();
61+
});
62+
4763
it("should receive an error when trying to do concurrent create-token (lock/unlock behavior)", async () => {
4864
const promise1 = TestUtils.request
4965
.post("/wallet/create-token")
@@ -68,4 +84,20 @@ describe("create-token api", () => {
6884
expect(response2.status).toBe(200);
6985
expect(response2.body.success).toBeFalsy();
7086
});
87+
88+
// TODO: fix this test case crashing when mocking push_tx with status 400
89+
it.skip("should not create a token with symbol HTR ", async () => {
90+
TestUtils.httpMock.onPost('push_tx').replyOnce(400);
91+
TestUtils.reorderHandlers();
92+
const response = await TestUtils.request
93+
.post("/wallet/create-token")
94+
.send({
95+
name: "Hathor",
96+
symbol: "HTR",
97+
amount: 1,
98+
})
99+
.set({ "x-wallet-id": TestUtils.walletId });
100+
expect(response.status).toBe(200);
101+
expect(response.body.success).toBeFalsy();
102+
});
71103
});

0 commit comments

Comments
 (0)