Skip to content

Commit

Permalink
test(kmd): add tests for empty and null password handling (#319)
Browse files Browse the repository at this point in the history
Add test coverage for KmdWallet's `getPassword` method to verify:
- Empty string passwords are properly cached and reused
- Cancelled prompts (null) fall back to empty string
- Password prompt is only shown once when cached
  • Loading branch information
drichar authored Nov 28, 2024
1 parent 4c2f5a8 commit 55ce7ad
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/use-wallet/src/__tests__/wallets/kmd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,34 @@ describe('KmdWallet', () => {
})
})
})

describe('getPassword', () => {
it('should return empty string password when set', async () => {
// Mock prompt to return empty string
global.prompt = vi.fn().mockReturnValue('')

// First call to connect will set the empty password
mockKmd.listKeys.mockResolvedValueOnce({ addresses: [account1.address] })
await wallet.connect()

// Second call to connect should reuse the empty password
mockKmd.listKeys.mockResolvedValueOnce({ addresses: [account1.address] })
await wallet.connect()

// Prompt should only be called once
expect(global.prompt).toHaveBeenCalledTimes(1)
expect(mockKmd.initWalletHandle).toHaveBeenCalledWith(mockWallet.id, '')
})

it('should handle null from cancelled prompt', async () => {
// Mock prompt to return null (user cancelled)
global.prompt = vi.fn().mockReturnValue(null)

mockKmd.listKeys.mockResolvedValueOnce({ addresses: [account1.address] })
await wallet.connect()

expect(global.prompt).toHaveBeenCalledTimes(1)
expect(mockKmd.initWalletHandle).toHaveBeenCalledWith(mockWallet.id, '')
})
})
})

0 comments on commit 55ce7ad

Please sign in to comment.