-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(connect): add tests for TrezorConnect.cancel
- Loading branch information
Showing
3 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import { SetupEmu } from '@trezor/trezor-user-env-link'; | ||
import TrezorConnect from '../../../src'; | ||
|
||
const { getController, setup, initTrezorConnect } = global.Trezor; | ||
|
||
const getAddress = (showOnTrezor: boolean) => { | ||
return TrezorConnect.getAddress({ | ||
path: "m/44'/1'/0'/0/0", | ||
showOnTrezor, | ||
}); | ||
}; | ||
|
||
const passphraseHandler = (value: string) => () => { | ||
TrezorConnect.uiResponse({ | ||
type: 'ui-receive_passphrase', | ||
payload: { | ||
passphraseOnDevice: false, | ||
value, | ||
}, | ||
}); | ||
TrezorConnect.removeAllListeners('ui-request_passphrase'); | ||
}; | ||
|
||
const addressHandler = () => () => { | ||
TrezorConnect.uiResponse({ | ||
type: 'ui-receive_confirmation', | ||
payload: true, | ||
}); | ||
TrezorConnect.removeAllListeners('ui-request_confirmation'); | ||
}; | ||
|
||
const assertGetAddressWorks = async () => { | ||
// validate that further communication is possible without any glitch | ||
TrezorConnect.on('ui-request_passphrase', passphraseHandler('a')); | ||
TrezorConnect.on('ui-request_confirmation', addressHandler()); | ||
const getAddressResponse = await getAddress(false); | ||
expect(getAddressResponse).toMatchObject({ | ||
payload: { address: 'ms1TJk4b4s7aisyL3jfrkCqwznttWwiS4r' }, | ||
}); | ||
}; | ||
|
||
describe('TrezorConnect.cancel', () => { | ||
const controller = getController(); | ||
|
||
const setupTest = async ({ setupParams }: { setupParams: SetupEmu; bridgeVersion: string }) => { | ||
await setup(controller, setupParams); | ||
await TrezorConnect.dispose(); | ||
await initTrezorConnect(controller, { debug: false }); | ||
}; | ||
|
||
afterAll(async () => { | ||
controller.dispose(); | ||
await TrezorConnect.dispose(); | ||
}); | ||
|
||
[ | ||
'2.0.27', | ||
'2.0.30', | ||
// todo: add support in trezor-user-env-link | ||
// '3.0.0' | ||
].forEach(bridgeVersion => { | ||
describe(`Bridge ${bridgeVersion}`, () => { | ||
TrezorConnect.removeAllListeners(); | ||
// the goal is to run this test couple of times to uncover possible race conditions/flakiness | ||
test(`GetAddress - ButtonRequest_Address - Cancel `, async () => { | ||
await setupTest({ | ||
setupParams: { | ||
mnemonic: 'mnemonic_all', | ||
}, | ||
bridgeVersion, | ||
}); | ||
|
||
const getAddressCall = getAddress(true); | ||
await new Promise(resolve => { | ||
TrezorConnect.on('button', event => { | ||
if (event.code === 'ButtonRequest_Address') { | ||
resolve(undefined); | ||
} | ||
}); | ||
}); | ||
|
||
TrezorConnect.cancel('my custom message'); | ||
|
||
const response = await getAddressCall; | ||
|
||
expect(response).toMatchObject({ | ||
success: false, | ||
payload: { | ||
error: 'my custom message', | ||
code: 'Method_Cancel', | ||
}, | ||
}); | ||
|
||
// TODO: this sometimes fails with, probably a race condition | ||
// success: false, | ||
// payload: { | ||
// error: 'Initialize failed: Unexpected message, code: Failure_UnexpectedMessage', | ||
// code: 'Device_InitializeFailed' | ||
// } | ||
// await assertGetAddressWorks(); | ||
}); | ||
|
||
// todo: this doesn't seem to work | ||
// await getAddressCal does not resolve | ||
test.skip('Synchronous Cancel', async () => { | ||
await setupTest({ | ||
setupParams: { | ||
mnemonic: 'mnemonic_all', | ||
}, | ||
bridgeVersion, | ||
}); | ||
|
||
const getAddressCall = getAddress(true); | ||
|
||
TrezorConnect.cancel(); | ||
|
||
const response = await getAddressCall; | ||
|
||
// This looks like a bug. there was showOnTrezor: true but we bypassed it by sending cancel? | ||
expect(response).toMatchObject({ | ||
success: true, | ||
payload: { | ||
address: 'mvbu1Gdy8SUjTenqerxUaZyYjmveZvt33q', | ||
}, | ||
}); | ||
}); | ||
|
||
test('Passphrase request - Cancel', async () => { | ||
await setupTest({ | ||
setupParams: { | ||
mnemonic: 'mnemonic_all', | ||
passphrase_protection: true, | ||
}, | ||
bridgeVersion, | ||
}); | ||
|
||
const getAddressCall = getAddress(true); | ||
await new Promise(resolve => { | ||
TrezorConnect.on('UI_EVENT', event => { | ||
if (event.type === 'ui-request_passphrase') { | ||
resolve(undefined); | ||
} | ||
}); | ||
}); | ||
TrezorConnect.cancel(); | ||
|
||
const response = await getAddressCall; | ||
|
||
expect(response.success).toEqual(false); | ||
|
||
await assertGetAddressWorks(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters