aboutsummaryrefslogtreecommitdiffstats
path: root/tools/hats-link-test.js
blob: 411dbd33c44f141eab06a22881cec7b82ebbd329 (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
// v3.coinos.io/hats opens the hat shop. "Someone in the coinos group is
// asking where to get hats. Can we make the hat shop have a linkable url
// like v3.coinos.io/hats that I can direct him to?" (Adam, 2026-11-22).
// With a wallet the link lands in the shop; the bar says /hats while the
// shop is on screen and / again once it is left; someone with no wallet
// gets the shop after making one.
// Run: bun tools/hats-link-test.js
import puppeteer, { seedWords, skipSeedCheck } from './puppeteer.js';
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: 5348, fetch: (req) => { const p = new URL(req.url).pathname; if (p === '/verify-worker.js' || p.startsWith('/punks')) return new Response(Bun.file('dist' + p)); if (p.startsWith('/hats/')) return Response.json({ owned: [], equipped: null }); 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 click = (t) => page.evaluate((x) => { const e = [...document.querySelectorAll('button')].find((n) => n.textContent.trim().toLowerCase().includes(x)); if (e) { e.click(); return true; } return false; }, t);
const text = () => page.evaluate(() => document.body.innerText);
const waitText = async (x, ms = 25000) => { for (let i = 0; i < ms / 250; i++) { if ((await text()).toLowerCase().includes(x.toLowerCase())) return true; await sleep(250); } return false; };
const path = () => page.evaluate(() => location.pathname);
try {
  await page.setViewport({ width: 390, height: 844 });
  await page.goto('http://localhost:5348/', { waitUntil: 'domcontentloaded' });
  await page.evaluate(() => localStorage.setItem('btc-wallet-network', 'regtest'));
  // no wallet yet: the link, then a new wallet, then the shop
  await page.goto('http://localhost:5348/hats', { waitUntil: 'domcontentloaded' }); await waitText('create a new wallet', 20000); await sleep(400);
  check('with no wallet the link shows the way in, not the shop', !(await text()).includes('Hat shop'));
  await click('create a new wallet'); await sleep(600);
  await seedWords(page);
  await skipSeedCheck(page);
  check('...and the shop opens once the wallet is made', await waitText('Hat shop', 25000));
  // with a wallet: straight in
  await page.goto('http://localhost:5348/hats', { waitUntil: 'domcontentloaded' });
  check('with a wallet /hats opens the hat shop', await waitText('Hat shop', 25000));
  await sleep(600);
  check('the bar says /hats', (await path()) === '/hats', await path());
  await page.evaluate(() => [...document.querySelectorAll('button')].find((n) => /\bback\b/i.test(n.textContent))?.click()); await sleep(900);
  const t = await text();
  check('Back leaves the shop', !t.includes('Hat shop'));
  check('...and the bar is / again', (await path()) === '/', await path());
  check('no page errors', errs.length === 0, errs.slice(0, 2).join(' | '));
} finally { await browser.close(); server.stop(true); }
console.log(ok ? '\n✅ /hats opens the hat shop' : '\n❌ failed');
process.exit(ok ? 0 : 1);