From 0bd82d6f585b03b752aec018f1418901cdb0bd8f Mon Sep 17 00:00:00 2001 From: Doug Richar Date: Thu, 28 Nov 2024 02:09:29 -0500 Subject: [PATCH] test(kmd): add tests for empty and null password handling 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 --- .../src/__tests__/wallets/kmd.test.ts | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/packages/use-wallet/src/__tests__/wallets/kmd.test.ts b/packages/use-wallet/src/__tests__/wallets/kmd.test.ts index 4550d27e..b73b4578 100644 --- a/packages/use-wallet/src/__tests__/wallets/kmd.test.ts +++ b/packages/use-wallet/src/__tests__/wallets/kmd.test.ts @@ -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, '') + }) + }) })