generate_wallet.test.ts
1 import { generateWallet } from '../utils/wallet'; 2 import { Encoding, MemoryAccount } from '@aeternity/aepp-sdk'; 3 import { describe, it, expect, beforeEach, spyOn, jest } from 'bun:test'; 4 import { isResultOk } from '../result'; 5 6 function mockAccount(address: string): MemoryAccount { 7 const mockAcct = new MemoryAccount('sk_2imh3ryxmi86jxz46Jb2NVwYWUndUep8qxQoFxQHoW6TWr63wq'); 8 const mockAddress: Encoding.AccountAddress = `ak_${address}` as Encoding.AccountAddress 9 Object.defineProperty(mockAcct, 'address', { get: () => mockAddress }); 10 return mockAcct; 11 } 12 13 describe('generateWallet', () => { 14 beforeEach(() => { 15 jest.clearAllMocks(); 16 }); 17 it('should generate an address with the correct prefix', async () => { 18 19 const prefix = 'test'; 20 21 const mockAcct = mockAccount(`${prefix}_validaddress`); 22 23 spyOn(MemoryAccount, 'generate').mockReturnValue( 24 mockAcct 25 ); 26 27 const walletResponse = await generateWallet(prefix, 10); 28 if (isResultOk(walletResponse)) { 29 const wallet = walletResponse.value; 30 expect(wallet.address).toEqual(mockAcct.address); 31 expect(wallet.secretKey).toEqual(mockAcct.secretKey); 32 } 33 34 }); 35 36 it('should return undefined if max attempts are exceeded', async () => { 37 const prefix = 'test'; 38 let attemptCount = 0; 39 40 spyOn(MemoryAccount, 'generate').mockImplementation(() => { 41 attemptCount++; 42 return mockAccount(`not_${attemptCount}_${prefix}`); 43 }); 44 45 const wallet = await generateWallet(prefix, 5); 46 expect(isResultOk(wallet)).toBe(false); 47 expect(attemptCount).toBe(5); 48 }); 49 50 it('should eventually generate an address with prefix "0"', async () => { 51 const walletResponse = await generateWallet("d", 500); 52 53 if (isResultOk(walletResponse)) { 54 55 const wallet = walletResponse.value; 56 expect(wallet).toBeDefined(); 57 expect(wallet?.address).toStartWith("ak_0"); 58 } 59 }, 10000); // Increased timeout for this test 60 });