Skip to content

Commit

Permalink
fix: refactor tests to use throw
Browse files Browse the repository at this point in the history
  • Loading branch information
limpbrains committed Nov 25, 2024
1 parent eb00760 commit b5dfd39
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 152 deletions.
68 changes: 17 additions & 51 deletions tests/boost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ describe('Boost', async function () {

// balance is 0
const r = await wallet.getNextAvailableAddress();
if (r.isErr()) {
throw r.error;
}
if (r.isErr()) throw r.error;
const a1 = r.value.addressIndex.address;
await rpc.sendToAddress(a1, '1');
await rpc.generateToAddress(1, await rpc.getNewAddress());
Expand All @@ -103,9 +101,7 @@ describe('Boost', async function () {
amount: 99999743,
satsPerByte: 1
});
if (s1.isErr()) {
throw s1.error;
}
if (s1.isErr()) throw s1.error;
await wallet.refreshWallet({});
expect(wallet.data.balance).to.be.below(256);
const b2 = wallet.canBoost(s1.value);
Expand All @@ -120,9 +116,7 @@ describe('Boost', async function () {
address: 'bcrt1q6rz28mcfaxtmd6v789l9rrlrusdprr9pz3cppk',
amount: 10000000
});
if (s2.isErr()) {
throw s2.error;
}
if (s2.isErr()) throw s2.error;
await rpc.generateToAddress(1, await rpc.getNewAddress()); // confirm tx
await wallet.refreshWallet({});
const b3 = wallet.canBoost(s2.value);
Expand All @@ -131,9 +125,7 @@ describe('Boost', async function () {

it('Should generate CPFP for send transaction', async () => {
const r = await wallet.getNextAvailableAddress();
if (r.isErr()) {
throw r.error;
}
if (r.isErr()) throw r.error;
const a1 = r.value.addressIndex.address;
await rpc.sendToAddress(a1, '1');
await rpc.generateToAddress(1, await rpc.getNewAddress());
Expand All @@ -148,18 +140,14 @@ describe('Boost', async function () {
satsPerByte: 1,
rbf: false
});
if (s1.isErr()) {
throw s1.error;
}
if (s1.isErr()) throw s1.error;
const oldTxId = s1.value;
await wallet.refreshWallet({});
const b1 = wallet.canBoost(oldTxId);
expect(b1).to.deep.equal({ canBoost: true, rbf: false, cpfp: true });

const setup = await wallet.transaction.setupCpfp({ txid: oldTxId });
if (setup.isErr()) {
throw setup.error;
}
if (setup.isErr()) throw setup.error;
expect(setup.value.inputs.length).to.equal(1);
expect(setup.value.outputs.length).to.equal(1);
expect(setup.value.boostType).to.equal(EBoostType.cpfp);
Expand All @@ -169,9 +157,7 @@ describe('Boost', async function () {
expect(setup.value.satsPerByte).to.be.above(6);

const createRes = await wallet.transaction.createTransaction();
if (createRes.isErr()) {
throw createRes.error;
}
if (createRes.isErr()) throw createRes.error;
const newTxId = createRes.value.id;
wallet.electrum.broadcastTransaction({ rawTx: createRes.value.hex });

Expand All @@ -181,9 +167,7 @@ describe('Boost', async function () {
type: EBoostType.cpfp,
fee: setup.value.fee
});
if (addBoost.isErr()) {
throw addBoost.error;
}
if (addBoost.isErr()) throw addBoost.error;
const boosted = wallet.getBoostedTransactions();
expect(boosted).to.deep.equal({
[oldTxId]: {
Expand All @@ -205,18 +189,14 @@ describe('Boost', async function () {

it('Should generate RBF for send transaction', async () => {
const r = await wallet.getNextAvailableAddress();
if (r.isErr()) {
throw r.error;
}
if (r.isErr()) throw r.error;
const a1 = r.value.addressIndex.address;
await rpc.sendToAddress(a1, '0.0001'); // 10000 sats
await rpc.generateToAddress(1, await rpc.getNewAddress());

await waitForElectrum();
const r1 = await wallet.refreshWallet({});
if (r1.isErr()) {
throw r1.error;
}
if (r1.isErr()) throw r1.error;
expect(wallet.data.balance).to.equal(10000);

// create and send original transaction
Expand All @@ -226,45 +206,33 @@ describe('Boost', async function () {
satsPerByte: 1,
rbf: true
});
if (s1.isErr()) {
throw s1.error;
}
if (s1.isErr()) throw s1.error;
const oldTxId = s1.value;
const r2 = await wallet.refreshWallet({});
if (r2.isErr()) {
throw r2.error;
}
if (r2.isErr()) throw r2.error;
const b1 = wallet.canBoost(oldTxId);
expect(b1).to.deep.equal({ canBoost: true, rbf: true, cpfp: true });

// replace original transaction using RBF
const setup = await wallet.transaction.setupRbf({ txid: oldTxId });
if (setup.isErr()) {
throw setup.error;
}
if (setup.isErr()) throw setup.error;
expect(setup.value.boostType).to.equal(EBoostType.rbf);
expect(setup.value.minFee).to.be.above(1);
const createRes = await wallet.transaction.createTransaction();
if (createRes.isErr()) {
throw createRes.error;
}
if (createRes.isErr()) throw createRes.error;
const newTxId = createRes.value.id;
const broadcastResp = await wallet.electrum.broadcastTransaction({
rawTx: createRes.value.hex
});
if (broadcastResp.isErr()) {
throw broadcastResp.error;
}
if (broadcastResp.isErr()) throw broadcastResp.error;

const addBoost = await wallet.addBoostedTransaction({
oldTxId,
newTxId,
type: EBoostType.rbf,
fee: setup.value.fee
});
if (addBoost.isErr()) {
throw addBoost.error;
}
if (addBoost.isErr()) throw addBoost.error;
const boosted = wallet.getBoostedTransactions();
expect(boosted).to.deep.equal({
[oldTxId]: {
Expand All @@ -276,9 +244,7 @@ describe('Boost', async function () {
});

const r3 = await wallet.refreshWallet({});
if (r3.isErr()) {
throw r3.error;
}
if (r3.isErr()) throw r3.error;

expect(Object.keys(wallet.transactions).length).to.equal(2);
expect(wallet.transactions).not.to.have.property(oldTxId);
Expand Down
36 changes: 12 additions & 24 deletions tests/derivation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ describe('Derivation Methods', () => {
addressType: EAddressType.p2wpkh,
network: EAvailableNetworks.mainnet
});
expect(pathRes.isErr()).to.equal(false);
if (pathRes.isErr()) return;
if (pathRes.isErr()) throw pathRes.error;
expect(pathRes.value.purpose).to.equal('84');
expect(pathRes.value.coinType).to.equal('0');
expect(pathRes.value.account).to.equal('0');
Expand All @@ -29,8 +28,7 @@ describe('Derivation Methods', () => {
addressType: EAddressType.p2sh,
network: EAvailableNetworks.mainnet
});
expect(pathRes.isErr()).to.equal(false);
if (pathRes.isErr()) return;
if (pathRes.isErr()) throw pathRes.error;
expect(pathRes.value.purpose).to.equal('49');
expect(pathRes.value.coinType).to.equal('0');
expect(pathRes.value.account).to.equal('0');
Expand All @@ -42,8 +40,7 @@ describe('Derivation Methods', () => {
addressType: EAddressType.p2pkh,
network: EAvailableNetworks.mainnet
});
expect(pathRes.isErr()).to.equal(false);
if (pathRes.isErr()) return;
if (pathRes.isErr()) throw pathRes.error;
expect(pathRes.value.purpose).to.equal('44');
expect(pathRes.value.coinType).to.equal('0');
expect(pathRes.value.account).to.equal('0');
Expand All @@ -58,8 +55,7 @@ describe('Derivation Methods', () => {
index: 0,
network: EAvailableNetworks.mainnet
});
expect(pathRes.isErr()).to.equal(false);
if (pathRes.isErr()) return;
if (pathRes.isErr()) throw pathRes.error;
expect(pathRes.value).to.equal("m/84'/0'/0'/1/0");
});
it('Should return a valid testnet p2sh derivation string at index 40', () => {
Expand All @@ -69,8 +65,7 @@ describe('Derivation Methods', () => {
index: '40',
network: EAvailableNetworks.testnet
});
expect(pathRes.isErr()).to.equal(false);
if (pathRes.isErr()) return;
if (pathRes.isErr()) throw pathRes.error;
expect(pathRes.value).to.equal("m/49'/1'/0'/0/40");
});
it('Should return a valid mainnet p2pkh derivation string at index 8', () => {
Expand All @@ -80,8 +75,7 @@ describe('Derivation Methods', () => {
index: 8,
network: EAvailableNetworks.mainnet
});
expect(pathRes.isErr()).to.equal(false);
if (pathRes.isErr()) return;
if (pathRes.isErr()) throw pathRes.error;
expect(pathRes.value).to.equal("m/44'/0'/0'/0/8");
});

Expand All @@ -90,8 +84,7 @@ describe('Derivation Methods', () => {
path: "m/84'/0'/0'/1/0",
network: EAvailableNetworks.mainnet
});
expect(pathRes.isErr()).to.equal(false);
if (pathRes.isErr()) return;
if (pathRes.isErr()) throw pathRes.error;
expect(pathRes.value.purpose).to.equal('84');
expect(pathRes.value.coinType).to.equal('0');
expect(pathRes.value.account).to.equal('0');
Expand All @@ -103,8 +96,7 @@ describe('Derivation Methods', () => {
path: "m/49'/1'/0'/0/40",
network: EAvailableNetworks.testnet
});
expect(pathRes.isErr()).to.equal(false);
if (pathRes.isErr()) return;
if (pathRes.isErr()) throw pathRes.error;
expect(pathRes.value.purpose).to.equal('49');
expect(pathRes.value.coinType).to.equal('1');
expect(pathRes.value.account).to.equal('0');
Expand All @@ -116,8 +108,7 @@ describe('Derivation Methods', () => {
path: "m/44'/0'/0'/0/8",
network: EAvailableNetworks.mainnet
});
expect(pathRes.isErr()).to.equal(false);
if (pathRes.isErr()) return;
if (pathRes.isErr()) throw pathRes.error;
expect(pathRes.value.purpose).to.equal('44');
expect(pathRes.value.coinType).to.equal('0');
expect(pathRes.value.account).to.equal('0');
Expand All @@ -127,20 +118,17 @@ describe('Derivation Methods', () => {

it('Should return a valid address type from p2wpkh path', () => {
const pathRes = getAddressTypeFromPath("m/84'/0'/0'/0/0");
expect(pathRes.isErr()).to.equal(false);
if (pathRes.isErr()) return;
if (pathRes.isErr()) throw pathRes.error;
expect(pathRes.value).to.equal(EAddressType.p2wpkh);
});
it('Should return a valid address type from p2sh path', () => {
const pathRes = getAddressTypeFromPath("m/49'/0'/0'/0/0");
expect(pathRes.isErr()).to.equal(false);
if (pathRes.isErr()) return;
if (pathRes.isErr()) throw pathRes.error;
expect(pathRes.value).to.equal(EAddressType.p2sh);
});
it('Should return a valid address type from p2pkh path', () => {
const pathRes = getAddressTypeFromPath("m/44'/0'/0'/0/0");
expect(pathRes.isErr()).to.equal(false);
if (pathRes.isErr()) return;
if (pathRes.isErr()) throw pathRes.error;
expect(pathRes.value).to.equal(EAddressType.p2pkh);
});
});
18 changes: 5 additions & 13 deletions tests/electrum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ before(async function () {
tls
}
});
if (res.isErr()) {
console.log('error: ', res.error.message);
//throw new Error(res.error.message);
return;
}
if (res.isErr()) throw res.error;
wallet = res.value;
await wallet.refreshWallet({});
});
Expand All @@ -40,8 +36,7 @@ describe('Electrum Methods', async function (): Promise<void> {
this.timeout(testTimeout);
it('connectToElectrum: Should connect to a random Electrum server', async () => {
const connectResponse = await wallet.connectToElectrum();
expect(connectResponse.isErr()).to.equal(false);
if (connectResponse.isErr()) return;
if (connectResponse.isErr()) throw connectResponse.error;
expect(connectResponse.value).to.equal('Connected to Electrum server.');
});

Expand All @@ -54,24 +49,21 @@ describe('Electrum Methods', async function (): Promise<void> {
const addressBalance = await wallet.getAddressBalance(
'tb1qyvc8r7338383xjshqsz38mfn2eql879nhrf8y0'
);
expect(addressBalance.isErr()).to.equal(false);
if (addressBalance.isErr()) return;
if (addressBalance.isErr()) throw addressBalance.error;
expect(addressBalance.value.confirmed).to.equal(20000);
});

it('getNextAvailableAddress: Should return the next available address/change index and the last used address/change index', async () => {
const r = await wallet.getNextAvailableAddress();
expect(r.isErr()).to.equal(false);
if (r.isErr()) return;
if (r.isErr()) throw r.error;
expect(r.value).to.deep.equal(
EXPECTED_SHARED_RESULTS.getNextAvailableAddress
);
});

it("Should return available UTXO's", async () => {
const getUtxosRes: Result<IGetUtxosResponse> = await wallet.getUtxos({});
expect(getUtxosRes.isErr()).to.equal(false);
if (getUtxosRes.isErr()) return;
if (getUtxosRes.isErr()) throw getUtxosRes.error;
expect(Array.isArray(getUtxosRes.value.utxos)).to.equal(true);
expect(getUtxosRes.value.utxos.length).to.equal(3);
expect(getUtxosRes.value.balance).to.equal(5855);
Expand Down
21 changes: 6 additions & 15 deletions tests/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ before(async function () {
tls
}
});
if (res.isErr()) {
console.log('error: ', res.error.message);
//throw new Error(res.error.message);
return;
}
if (res.isErr()) throw res.error;
wallet = res.value;
});

Expand Down Expand Up @@ -100,44 +96,39 @@ describe('Storage Test', async function (): Promise<void> {

it('Should generate a bech32 receiving address at index 0 via its path', async () => {
const address = await wallet.getAddressByPath({ path: "m/84'/1'/0'/0/0" });
expect(address.isErr()).to.equal(false);
if (address.isErr()) return;
if (address.isErr()) throw address.error;
expect(address.value.address).to.equal(
'tb1qmja98kkd540qtesjqdanfg0ywags845vehfg66'
);
});

it('Should generate a segwit change address at index 1 via its path', async () => {
const address = await wallet.getAddressByPath({ path: "m/49'/1'/0'/1/1" });
expect(address.isErr()).to.equal(false);
if (address.isErr()) return;
if (address.isErr()) throw address.error;
expect(address.value.address).to.equal(
'2NDRG1ZGhWMGGNW7Mp58BKcyBs4Hyat8Law'
);
});

it('Should generate a testnet legacy receiving address at index 5 via its path', async () => {
const address = await wallet.getAddressByPath({ path: "m/44'/1'/0'/0/5" });
expect(address.isErr()).to.equal(false);
if (address.isErr()) return;
if (address.isErr()) throw address.error;
expect(address.value.address).to.equal(
'mohdq3fadTtT4uSH4oNr4F1Dp3YM4pR3VF'
);
});

it('getNextAvailableAddress: Should return the next available address/change index and the last used address/change index', async () => {
const r = await wallet.getNextAvailableAddress();
expect(r.isErr()).to.equal(false);
if (r.isErr()) return;
if (r.isErr()) throw r.error;
expect(r.value).to.deep.equal(
EXPECTED_SHARED_RESULTS.getNextAvailableAddress
);
});

it("Should return available UTXO's", async () => {
const getUtxosRes: Result<IGetUtxosResponse> = await wallet.getUtxos({});
expect(getUtxosRes.isErr()).to.equal(false);
if (getUtxosRes.isErr()) return;
if (getUtxosRes.isErr()) throw getUtxosRes.error;
expect(Array.isArray(getUtxosRes.value.utxos)).to.equal(true);
expect(getUtxosRes.value.utxos.length).to.equal(3);
expect(getUtxosRes.value.balance).to.equal(5855);
Expand Down
Loading

0 comments on commit b5dfd39

Please sign in to comment.