aboutsummaryrefslogtreecommitdiffstats
path: root/tools/zap-locked-test.js
blob: 3077c1fe68c422eefe294f2f4ddd17de3117f958 (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
// A locked wallet (the watch-only view of a sealed vault, with an identity
// signed in over it) can't pay: tapping ⚡ on a post used to do nothing at
// all — the button pressed, the cursor an arrow. Now it says the wallet is
// locked and opens the Accounts page, remembering the feed for the way back.
// Run: bun tools/zap-locked-test.js
import puppeteer from './puppeteer.js';
import { generateMnemonic } from '@scure/bip39';
import { wordlist } from '@scure/bip39/wordlists/english';
import { accountXpubFor, cacheKeyFor } from '../src/wallet.js';
import { generateSecretKey, getPublicKey, finalizeEvent } from 'nostr-tools/pure';
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 xpub = accountXpubFor({ mnemonic: generateMnemonic(wordlist) });
const SK = generateSecretKey(), AUTHOR = getPublicKey(SK);
const now = Math.floor(Date.now() / 1000);
const seeded = [finalizeEvent({ kind: 1, created_at: now - 100, tags: [], content: 'a sunset worth a zap' }, SK)];

const html = await buildHtml({ minify: true, pwa: false });
const server = Bun.serve({ port: 0, fetch: (req) => {
  const path = new URL(req.url).pathname;
  if (path.startsWith('/punks')) return new Response(Bun.file('dist' + path));
  return 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 errs = [];
page.on('pageerror', (e) => errs.push(String(e)));
const waitFor = async (fn, arg, ms = 10000) => { for (let i = 0; i < ms / 200; i++) { if (await page.evaluate(fn, arg)) return true; await sleep(200); } return false; };
try {
  await page.setViewport({ width: 390, height: 844 });
  await page.goto(server.url.href, { waitUntil: 'domcontentloaded' });
  const base = cacheKeyFor(xpub);
  await page.evaluate(([xpub, k, pk, ns]) => {
    // a saved wallet, locked: only its xpub and its nostr identity on this device
    localStorage.setItem('btc-wallet-watch', JSON.stringify([{ id: 'locked-watch', label: 'Account 1', xpub, network: 'mainnet', nostrPk: pk }]));
    localStorage.setItem('btc-wallet-lockpw', '1');
    localStorage.setItem(k + ':follows', JSON.stringify({ tags: [['p', pk]], c: '', at: Math.floor(Date.now() / 1000) }));
    localStorage.setItem(k + ':feedNotes', JSON.stringify(ns));
    localStorage.setItem(k + ':profiles', JSON.stringify({ [pk]: { name: 'Sunset Poster', t: Date.now() } }));
    localStorage.setItem('btc-wallet-feed:' + k, 'following');
  }, [xpub, base, AUTHOR, seeded]);
  await page.reload({ waitUntil: 'domcontentloaded' });
  await page.waitForSelector('.header-avatar', { timeout: 20000 });
  await page.evaluate(() => document.querySelector('.app-nav-button[aria-label="Feed"]').click());
  check('the post is in the feed', await waitFor(() => !!document.querySelector('.notes-feed .note-zap'), null, 20000));
  const before = await page.evaluate(() => { const b = document.querySelector('.notes-feed .note-zap'); return { disabled: b.getAttribute('aria-disabled'), cursor: getComputedStyle(b).cursor }; });
  check('the ⚡ button is live (not aria-disabled, a pointer cursor)', before.disabled !== 'true' && before.cursor === 'pointer', JSON.stringify(before));
  await page.evaluate(() => document.querySelector('.notes-feed .note-zap').click());
  check('the tap says the wallet is locked', await waitFor(() => /locked, so it can.t zap/i.test(document.body.innerText), null, 4000));
  check('…and opens the Accounts page', await waitFor(() => !document.querySelector('.notes-feed') && /add account/i.test(document.body.innerText), null, 4000),
    (await page.evaluate(() => document.body.innerText.slice(0, 160))).replace(/\n+/g, ' | '));
  check('the feed is remembered for the way back', await page.evaluate(() => /^(\/feed|feed:)/.test(sessionStorage.getItem('coinos-after-signin') || '')));
  check('no page errors', errs.length === 0, errs.slice(0, 2).join(' | '));
} finally { await browser.close(); server.stop(true); }
console.log(ok ? '\n✅ a locked wallet\'s ⚡ says so and opens the Accounts page' : '\n❌ failed');
process.exit(ok ? 0 : 1);