console.log(`%cPASS `, 'color:green', name)
}
-export async function click (text, fn) {
- if (isNode) return await fn()
- return new Promise((resolve, reject) => {
- const button = document.createElement('button')
- const hourglass = document.createTextNode('⏳')
- button.innerText = text
- button.onclick = async () => {
- button.disabled = true
- button.innerText = 'Waiting for device...'
- button.after(hourglass)
- try {
- const result = await fn()
- resolve(result)
- } catch (err) {
- reject(err)
- } finally {
- hourglass.remove()
- button.remove()
+/**
+ *
+ * @param {string} text
+ * @param {AsyncFunction} fn
+ * @param {object} [opt]
+ * @param {number} [opt.timeout]
+ * @returns {Promise<any>}
+ */
+export async function click (text, fn, opt) {
+ async function wait (timeout) {
+ if (typeof timeout === 'number' && timeout > 0) {
+ if (timeout > 300) {
+ throw new RangeError('Click test should not wait more than 5 minutes', { cause: timeout })
}
+ console.log(`Click done, waiting ${timeout} seconds to reset transient user activation timer...`)
+ return await new Promise(r => setTimeout(r, timeout * 1000))
}
- document.body.appendChild(button)
- window?.scrollTo(0, document.body.scrollHeight)
- })
+ if (isNode) {
+ await wait(opt?.timeout)
+ return await fn()
+ }
+ const { timeout } = opt
+ return new Promise((resolve, reject) => {
+ const button = document.createElement('button')
+ const hourglass = document.createTextNode('⏳')
+ button.innerText = text
+ button.onclick = async () => {
+ button.disabled = true
+ button.innerText = 'Waiting for device...'
+ button.after(hourglass)
+ try {
+ const result = await fn()
+ resolve(result)
+ } catch (err) {
+ reject(err)
+ } finally {
+ hourglass.remove()
+ button.remove()
+ await wait(opt?.timeout)
+ }
+ }
+ document.body.appendChild(button)
+ window?.scrollTo(0, document.body.scrollHeight)
+ })
+ }
}
export function stats (times) {
\r
await assert.resolves(click(\r
'Sign and autogen PoW when processing',\r
- async () => block.sign(OPEN_BLOCK.key)\r
+ async () => block.sign(OPEN_BLOCK.key),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
\r
assert.nullish(block.work)\r
const result = await assert.rejects(block.process(rpc))\r
\r
await assert.resolves(click(\r
'Sign with BLAKE2b',\r
- async () => wallet.sign(1, block)\r
+ async () => wallet.sign(1, block),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
assert.ok(await block.verify(BLAKE2B_PUBLIC_1))\r
\r
await assert.resolves(wallet.destroy())\r
\r
await assert.resolves(click(\r
'Sign with BIP-44',\r
- async () => wallet.sign(0, block)\r
+ async () => wallet.sign(0, block),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
assert.ok(await block.verify(PUBLIC_0))\r
\r
await assert.resolves(wallet.destroy())\r
\r
await assert.resolves(click(\r
'Sign with Exodus',\r
- async () => wallet.sign(0, block)\r
+ async () => wallet.sign(0, block),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
assert.ok(await block.verify(EXODUS.PUBLIC_0))\r
\r
await assert.resolves(wallet.destroy())\r
\r
await assert.rejects(click(\r
'Fail to sign while locked',\r
- async () => wallet.sign(0, block)\r
+ async () => wallet.sign(0, block),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
assert.ok(block.signature === undefined)\r
\r
await wallet.destroy()\r
\r
await assert.resolves(click(\r
'Sign open block with key',\r
- async () => block.sign(OPEN_BLOCK.key)\r
+ async () => block.sign(OPEN_BLOCK.key),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
assert.equal(block.hash, OPEN_BLOCK.hash)\r
assert.equal(block.signature, OPEN_BLOCK.signature)\r
})\r
\r
await assert.resolves(click(\r
'Sign receive block with key',\r
- async () => block.sign(RECEIVE_BLOCK.key)\r
+ async () => block.sign(RECEIVE_BLOCK.key),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
\r
assert.equal(block.hash, RECEIVE_BLOCK.hash)\r
assert.equal(block.signature, RECEIVE_BLOCK.signature)\r
\r
await assert.resolves(click(\r
'Sign receive block without work',\r
- async () => block.sign(RECEIVE_BLOCK.key)\r
+ async () => block.sign(RECEIVE_BLOCK.key),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
\r
assert.equal(block.hash, RECEIVE_BLOCK.hash)\r
assert.equal(block.signature, RECEIVE_BLOCK.signature)\r
\r
await assert.resolves(click(\r
'Sign Ledger-derived block using BIP-44 wallet',\r
- async () => wallet.sign(0, block)\r
+ async () => wallet.sign(0, block),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
\r
assert.equal(block.signature, LEDGER_NANOS.OPEN_BLOCK.signature)\r
assert.ok(await block.verify(account.publicKey))\r
\r
await assert.resolves(click(\r
'Sign send block with key',\r
- async () => block.sign(SEND_BLOCK.key)\r
+ async () => block.sign(SEND_BLOCK.key),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
\r
assert.equal(block.hash, SEND_BLOCK.hash)\r
assert.equal(block.signature, SEND_BLOCK.signature)\r
\r
await assert.resolves(click(\r
'Sign send block without work',\r
- async () => block.sign(SEND_BLOCK.key)\r
+ async () => block.sign(SEND_BLOCK.key),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
\r
assert.equal(block.hash, SEND_BLOCK.hash)\r
assert.equal(block.signature, SEND_BLOCK.signature)\r
\r
await assert.resolves(click(\r
'Sign change block with key',\r
- async () => block.sign('781186FB9EF17DB6E3D1056550D9FAE5D5BBADA6A6BC370E4CBB938B1DC71DA3') // Did not find a private key at nano docs for this address\r
+ async () => block.sign('781186FB9EF17DB6E3D1056550D9FAE5D5BBADA6A6BC370E4CBB938B1DC71DA3'), // Did not find a private key at nano docs for this address\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
\r
assert.equal(block.signature?.toUpperCase(), 'A3C3C66D6519CBC0A198E56855942DEACC6EF741021A1B11279269ADC587DE1DA53CD478B8A47553231104CF24D742E1BB852B0546B87038C19BAE20F9082B0D')\r
assert.equal(block.work, work)\r
\r
await assert.resolves(click(\r
'Sign change block without work',\r
- async () => block.sign(PRIVATE_0)\r
+ async () => block.sign(PRIVATE_0),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
\r
assert.equal(block.signature?.toUpperCase(), '2BD2F905E74B5BEE3E2277CED1D1E3F7535E5286B6E22F7B08A814AA9E5C4E1FEA69B61D60B435ADC2CE756E6EE5F5BE7EC691FE87E024A0B22A3D980CA5B305')\r
assert.nullish(block.work)\r
await assert.rejects(click(\r
'fail to sign without args',\r
//@ts-expect-error\r
- async () => block.sign()\r
+ async () => block.sign(),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
assert.nullish(block.signature)\r
\r
await assert.rejects(click(\r
'fail to sign with null arg',\r
//@ts-expect-error\r
- async () => block.sign(null)\r
+ async () => block.sign(null),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
assert.nullish(block.signature)\r
\r
await assert.rejects(click(\r
'fail to sign with invalid string length',\r
- async () => block.sign('1')\r
+ async () => block.sign('1'),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
assert.nullish(block.signature)\r
\r
await assert.rejects(click(\r
'fail to sign with invalid string characters',\r
- async () => block.sign('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')\r
+ async () => block.sign('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'),\r
+ { timeout: 6 }\r
))\r
- console.log('Click done, waiting 6 seconds to reset transient user activation timer...')\r
- await new Promise(r => setTimeout(r, 6000))\r
assert.nullish(block.signature)\r
})\r
})\r