aboutsummaryrefslogtreecommitdiffstats
path: root/tools/padlock-ask-test.js
blob: 763dcaebea016eec07dacfdf8f7df0304a9c6b49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// The padlock is answerable from wherever it's tapped. Feature screens (your
// profile, a chat thread) are drawn ahead of the wallet screen, and the
// "Protect this device?" card is drawn BY the wallet screen — so the tap used
// to do nothing visible until you navigated home and the card finally
// appeared there. The ask outranks feature screens now, and declining still
// leaves you exactly where you tapped.
//
// Run: bun tools/padlock-ask-test.js
import puppeteer from './puppeteer.js';
import { generateMnemonic } from '@scure/bip39';
import { wordlist } from '@scure/bip39/wordlists/english';
import { buildHtml } from '../build.js';
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
let ok = true;
const check = (n, c, d = '') => { console.log(` ${c ? '✓' : '✗'} ${n}${d ? ' — ' + d : ''}`); if (!c) ok = false; };
const html = await buildHtml({ minify: true, pwa: false });
const server = Bun.serve({ port: 5231, fetch: () => new Response(html, { headers: { 'content-type': 'text/html' } }) });
const browser = await puppeteer.launch({ executablePath: '/usr/bin/google-chrome', headless: 'new', args: ['--no-sandbox'] });
const page = await browser.newPage();
const click = (sel, t) => page.evaluate((s, x) => { const e = [...document.querySelectorAll(s)].find((n) => n.textContent.trim().toLowerCase().includes(x.toLowerCase())); if (e) { e.click(); return true; } return false; }, sel, t);
const bodyText = () => page.evaluate(() => document.body.innerText);
const waitText = async (x, ms = 25000) => { for (let i = 0; i < ms / 250; i++) { if ((await bodyText()).toLowerCase().includes(x.toLowerCase())) return true; await sleep(250); } return false; };
try {
  await page.setViewport({ width: 390, height: 844 });
  await page.goto('http://localhost:5231/', { waitUntil: 'domcontentloaded' });
  await page.evaluate(() => localStorage.setItem('btc-wallet-network', 'regtest'));
  await page.reload({ waitUntil: 'domcontentloaded' });
  await sleep(400);
  await click('button', 'create a new wallet');
  await sleep(500);
  await click('button', 'Have an existing seed');
  await sleep(400);
  await page.waitForSelector('textarea');
  await page.type('textarea', generateMnemonic(wordlist));
  await click('button', 'Open wallet');
  await waitText('receive', 20000);

  // open the balance card's settings (its gear) — the padlock lives there
  await page.evaluate(() => document.querySelector('.balance-gear')?.click());
  await sleep(900);
  check('the wallet settings page is open', /security/i.test(await bodyText()), (await bodyText()).slice(0, 60).replace(/\n/g, ' | '));

  // tap the padlock — the ask must appear without navigating anywhere
  const tapped = await page.evaluate(() => {
    const b = [...document.querySelectorAll('button')].find((e) => /lock/i.test(e.getAttribute('aria-label') || ''));
    if (!b) return false; b.click(); return true;
  });
  check('padlock is reachable from the wallet settings', tapped);
  await sleep(500);
  const text = await bodyText();
  check('the ask appears on the tap', /protect this device/i.test(text), text.split('\n').slice(0, 3).join(' | '));

  // declining lands back on the settings page, not somewhere else
  await click('button', 'not now');
  await sleep(600);
  check('declining lands back on the wallet settings', /security/i.test(await bodyText()), (await bodyText()).slice(0, 60).replace(/\n/g, ' | '));
} finally {
  await browser.close(); server.stop(true);
}
console.log(ok ? '\n✅ padlock ask is immediate' : '\n❌ failed');
process.exit(ok ? 0 : 1);