Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions tests/TrezorWalletProvider.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,69 @@
// tests/TrezorWalletProvider.test.js
const { TrezorWalletProvider } = require('../src/services/wallet/TrezorWalletProvider');

// Mock the Trezor Connect library so we don't need a physical device for tests
jest.mock('@trezor/connect-web', () => ({
__esModule: true,
default: {
init: jest.fn().mockResolvedValue(),
getPublicKey: jest.fn(),
signTransaction: jest.fn(),
}
}));
const TrezorConnect = require('@trezor/connect-web').default;

describe('TrezorWalletProvider', () => {
let provider;

beforeEach(() => {
provider = new TrezorWalletProvider();
// Clear mocks before each test so responses don't leak across tests
jest.clearAllMocks();
});

test('should have correct id and name', () => {
expect(provider.id).toBe('trezor');
expect(provider.name).toBe('Trezor');
});

test('connect should be defined', () => {
test('connect should be defined and handle successful connection', async () => {
// Keeping your original structural check
expect(provider.connect).toBeDefined();

// Simulate the user successfully plugging in and unlocking the Trezor
TrezorConnect.getPublicKey.mockResolvedValue({
success: true,
payload: { publicKey: '0xabc123', path: "m/44'/60'/0'/0/0" }
});

// Actively assert that connecting does not throw an error
await expect(provider.connect()).resolves.not.toThrow();
});

test('signTransaction should be defined', () => {
expect(provider.signTransaction).toBeDefined();
test('connect should throw error if device connection fails', async () => {
// Simulate a failure state (e.g., user unplugs device or denies access)
TrezorConnect.getPublicKey.mockResolvedValue({
success: false,
payload: { error: 'Device not found or user cancelled' }
});

// Actively assert that the provider surfaces the error
await expect(provider.connect()).rejects.toThrow();
});

// Add more tests
});
test('signTransaction should be defined and process signatures', async () => {
// Keeping your original structural check
expect(provider.signTransaction).toBeDefined();

// Simulate a successful hardware signature
TrezorConnect.signTransaction.mockResolvedValue({
success: true,
payload: { signatures: ['0xsignaturehash'] }
});

const mockTx = { to: '0x123', amount: '100' };

// Actively assert that signing resolves correctly
await expect(provider.signTransaction(mockTx)).resolves.toBeDefined();
});
});