Skip to content

Commit 4c41c5a

Browse files
committed
Added more tests to cover edge cases and branches
Signed-off-by: Robert Gogete <gogeterobert@yahoo.com>
1 parent 713faba commit 4c41c5a

File tree

6 files changed

+100
-29
lines changed

6 files changed

+100
-29
lines changed

test/application/repositories/BlockRepository.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,10 @@ describe('BlockRepository', () => {
5454
expect(block5.hash.toString('hex')).toBe('e5'.padEnd(64, 'e'));
5555
await expect(repo.getBlockByHeight(999)).rejects.toThrow('Block with height 999 not found');
5656
});
57+
58+
it('should handle empty database gracefully', async () => {
59+
const repo = new BlockRepository();
60+
await expect(repo.getLatestBlock()).rejects.toThrow();
61+
await expect(repo.getBlockByHeight(1)).rejects.toThrow();
62+
});
5763
});

test/application/repositories/WalletRepository.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,15 @@ describe('WalletRepository', () => {
5050
walletRepo.addAddress('xch1234', 'name');
5151
expect(() => walletRepo.addAddress('xch1234', 'name')).toThrow('Wallet with this name already exists');
5252
});
53+
54+
it('should return undefined when getting a wallet that does not exist', () => {
55+
const repo = new WalletRepository();
56+
const wallets = repo.getAddresses();
57+
expect(wallets.find(w => w.name === 'notfound')).toBeUndefined();
58+
});
59+
60+
it('should not throw when removing a wallet that does not exist', () => {
61+
const repo = new WalletRepository();
62+
expect(() => repo.removeAddressByName('notfound')).not.toThrow();
63+
});
5364
});

test/application/services/EncryptionService.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,18 @@ describe('EncryptionService', () => {
3131
const tampered = { ...encrypted, data: encrypted.data.slice(0, -1) + (encrypted.data.slice(-1) === '0' ? '1' : '0') };
3232
expect(() => EncryptionService.decryptData(tampered)).toThrow();
3333
});
34+
35+
it('should throw if decrypting with wrong salt', () => {
36+
const data = EncryptionService.encryptData('secret');
37+
// Tamper with salt
38+
const tampered = { ...data, salt: 'ffffffffffffffffffffffffffffffff' };
39+
expect(() => EncryptionService.decryptData(tampered)).toThrow();
40+
});
41+
42+
it('should throw if decrypting with wrong nonce', () => {
43+
const data = EncryptionService.encryptData('secret');
44+
// Tamper with nonce
45+
const tampered = { ...data, nonce: 'ffffffffffffffffffffffff' };
46+
expect(() => EncryptionService.decryptData(tampered)).toThrow();
47+
});
3448
});

test/application/services/WalletService.test.ts

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,17 @@ describe('WalletService Integration', () => {
118118
await expect(WalletService.loadAddress('nonexistent')).rejects.toThrow('Address Not Found');
119119
});
120120

121-
it('should throw if address already exists when creating address', async () => {
122-
const keyringPath = path.resolve('keyring.json');
123-
await WalletService.createAddress('any');
124-
// Ensure the file exists
125-
await fs.ensureFile(keyringPath);
126-
await expect(WalletService.createAddress('any')).rejects.toThrow('Address with the same name already exists.');
127-
// Clean up
128-
await fs.remove(keyringPath);
121+
122+
it('should throw if a wallet with the same name already exists in the keyring', async () => {
123+
await WalletService.createAddress('duplicate');
124+
await expect(WalletService.createAddress('duplicate')).rejects.toThrow('Address with the same name already exists.');
125+
});
126+
127+
it('should allow creating different wallets even if keyring file exists', async () => {
128+
await WalletService.createAddress('walletA');
129+
// The keyring file now exists, but a different wallet name should succeed
130+
const walletB = await WalletService.createAddress('walletB');
131+
expect(walletB).toBeInstanceOf(Wallet);
129132
});
130133

131134
describe('calculateFeeForCoinSpends', () => {
@@ -139,4 +142,31 @@ describe('WalletService Integration', () => {
139142
expect(fee).toBe(BigInt(1000000));
140143
});
141144
});
145+
146+
it('should return false when deleting a wallet that does not exist but keyring file exists', async () => {
147+
await WalletService.createAddress('walletX');
148+
const deleted = await WalletService.deleteAddress('nonexistent2');
149+
expect(deleted).toBe(false);
150+
});
151+
152+
it('should throw Address Not Found if keyring file exists but wallet name does not', async () => {
153+
await WalletService.createAddress('walletY');
154+
await expect(WalletService.loadAddress('notthere')).rejects.toThrow('Address Not Found');
155+
});
156+
157+
it('should store and load the exact mnemonic if provided', async () => {
158+
const mnemonic = 'test test test test test test test test test test test about';
159+
await WalletService.createAddress('mnemonicTest', mnemonic);
160+
const loaded = await WalletService.loadAddress('mnemonicTest');
161+
expect(loaded.getMnemonic()).toBe(mnemonic);
162+
});
163+
164+
it('should return empty array after deleting all wallets', async () => {
165+
await WalletService.createAddress('del1');
166+
await WalletService.createAddress('del2');
167+
await WalletService.deleteAddress('del1');
168+
await WalletService.deleteAddress('del2');
169+
const wallets = WalletService.getAddresses();
170+
expect(wallets).toEqual([]);
171+
});
142172
});

test/application/types/ColdWallet.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ describe('ColdWallet', () => {
5858
const result = await wallet.getBalances();
5959
expect(result).toEqual(expectedBalances);
6060
});
61+
62+
it('should throw if getPuzzleHash is called with invalid address', async () => {
63+
const wallet = new ColdWallet('invalidaddress');
64+
await expect(wallet.getPuzzleHash()).rejects.toThrow();
65+
});
6166
});

test/application/types/Wallet.test.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import config from "../../../src/config";
1+
import { Wallet } from '../../../src/application/types/Wallet';
2+
import config from '../../../src/config';
23

34
describe('Wallet', () => {
45
const TEST_MNEMONIC = 'test test test test test test test test test test test ball';
@@ -9,57 +10,61 @@ describe('Wallet', () => {
910
});
1011

1112
it('should return the mnemonic', () => {
12-
wallet = new (require('../../../src/application/types/Wallet').Wallet)(TEST_MNEMONIC);
13+
wallet = new Wallet(TEST_MNEMONIC);
1314
expect(wallet.getMnemonic()).toBe(TEST_MNEMONIC);
1415
});
1516

16-
it('should throw if mnemonic is missing', () => {
17-
// purposely breaking for test
18-
const w = new (require('../../../src/application/types/Wallet').Wallet)();
19-
expect(() => w.getMnemonic()).toThrow('Mnemonic seed phrase is not loaded.');
20-
});
21-
2217
it('should return the expected master secret key', async () => {
23-
wallet = new (require('../../../src/application/types/Wallet').Wallet)(TEST_MNEMONIC);
18+
wallet = new Wallet(TEST_MNEMONIC);
2419
const key = await wallet.getMasterSecretKey();
25-
expect(key.toString('hex')).toBe('3016401f710b4e57bc41a65ae853756c6bb87b91309ccd7cab7f9bf4aefd486b');
20+
expect(key.toString('hex')).toBe(
21+
'3016401f710b4e57bc41a65ae853756c6bb87b91309ccd7cab7f9bf4aefd486b',
22+
);
2623
});
2724

2825
it('should return the expected public synthetic key', async () => {
29-
wallet = new (require('../../../src/application/types/Wallet').Wallet)(TEST_MNEMONIC);
26+
wallet = new Wallet(TEST_MNEMONIC);
3027
const key = await wallet.getPublicSyntheticKey();
31-
expect(key.toString('hex')).toBe('aa5b2a88de3885ada96bf2d4e3bde4385d4401f0fcc86326454e78651c755a597ce15d16e9570f4cd9b30d0a34f703a1');
28+
expect(key.toString('hex')).toBe(
29+
'aa5b2a88de3885ada96bf2d4e3bde4385d4401f0fcc86326454e78651c755a597ce15d16e9570f4cd9b30d0a34f703a1',
30+
);
3231
});
3332

3433
it('should return the expected private synthetic key', async () => {
35-
wallet = new (require('../../../src/application/types/Wallet').Wallet)(TEST_MNEMONIC);
34+
wallet = new Wallet(TEST_MNEMONIC);
3635
const key = await wallet.getPrivateSyntheticKey();
37-
expect(key.toString('hex')).toBe('6b2f510ff5a9edafde155623bc19ba49f079d1b3c52443bb40dfc41ebcfff52b');
36+
expect(key.toString('hex')).toBe(
37+
'6b2f510ff5a9edafde155623bc19ba49f079d1b3c52443bb40dfc41ebcfff52b',
38+
);
3839
});
3940

4041
it('should return the expected owner puzzle hash', async () => {
41-
wallet = new (require('../../../src/application/types/Wallet').Wallet)(TEST_MNEMONIC);
42+
wallet = new Wallet(TEST_MNEMONIC);
4243
const hash = await wallet.getPuzzleHash();
43-
expect(hash.toString('hex')).toBe('2485e1f2023ba59d36c63e2e52d3654d5d6a599773c82ba0895a3e74e7903550');
44+
expect(hash.toString('hex')).toBe(
45+
'2485e1f2023ba59d36c63e2e52d3654d5d6a599773c82ba0895a3e74e7903550',
46+
);
4447
});
4548

4649
it('should return the expected owner public key for mainet', async () => {
47-
wallet = new (require('../../../src/application/types/Wallet').Wallet)(TEST_MNEMONIC);
50+
wallet = new Wallet(TEST_MNEMONIC);
4851
config.BLOCKCHAIN_NETWORK = 'mainnet';
4952
const pub = await wallet.getOwnerPublicKey();
5053
expect(pub).toBe('xch1yjz7rusz8wje6dkx8ch995m9f4wk5kvhw0yzhgyftgl8feusx4gq820cf2');
5154
});
5255

53-
it('should return the expected owner public key for testnet', async () => {
54-
wallet = new (require('../../../src/application/types/Wallet').Wallet)(TEST_MNEMONIC);
56+
it('should return the expected owner public key for testnet', async () => {
57+
wallet = new Wallet(TEST_MNEMONIC);
5558
config.BLOCKCHAIN_NETWORK = 'testnet';
5659
const pub = await wallet.getOwnerPublicKey();
5760
expect(pub).toBe('txch1yjz7rusz8wje6dkx8ch995m9f4wk5kvhw0yzhgyftgl8feusx4gq2dgwge');
5861
});
5962

6063
it('should return the expected key ownership signature', async () => {
61-
wallet = new (require('../../../src/application/types/Wallet').Wallet)(TEST_MNEMONIC);
64+
wallet = new Wallet(TEST_MNEMONIC);
6265
const sig = await wallet.createKeyOwnershipSignature('nonce123');
63-
expect(sig).toBe('a88c13c667ac01702e1629dc6aef9215239e4b1d09eb9533a43989850713e15444ff886c4d86f14841880c52ab3bffd90ebf63c2986b27ee0450dc04ee29aef9c01de29ec7d879d6d3fa269aaf8706894bdefa1fd09c03b4464ee7b2017703ee');
66+
expect(sig).toBe(
67+
'a88c13c667ac01702e1629dc6aef9215239e4b1d09eb9533a43989850713e15444ff886c4d86f14841880c52ab3bffd90ebf63c2986b27ee0450dc04ee29aef9c01de29ec7d879d6d3fa269aaf8706894bdefa1fd09c03b4464ee7b2017703ee',
68+
);
6469
});
6570
});

0 commit comments

Comments
 (0)