aboutsummaryrefslogtreecommitdiffstats
path: root/tools/hats-santa-test.js
blob: a9966e1c0953dd8d4b0fd82175d306c08de8ebf8 (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
62
63
64
65
66
67
68
69
70
// A free Santa hat for everyone, through December. "Let's add a free Santa
// hat to the hat shop for everyone. Available only through the month of
// December." (Adam, 2026-12-01). The registrar says which hats are free right
// now (/hats/<pk> → free: ['santa']); the shop lists the Santa hat first with
// a Free button that takes it (POST /hats/free, no invoice) and puts it on.
// Out of season (free: []) the Santa hat isn't listed unless you own it.
// Run: bun tools/hats-santa-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: 5349, fetch: (req) => { const p = new URL(req.url).pathname; if (p === '/verify-worker.js' || p.startsWith('/punks')) return new Response(Bun.file('dist' + p)); 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)));
// the registrar, mocked
let season = true, rec = { owned: [], equipped: null }, taken = null;
await page.setRequestInterception(true);
const cors = { 'access-control-allow-origin': '*', 'access-control-allow-headers': 'content-type,authorization', 'access-control-allow-methods': 'GET,POST,OPTIONS' };
page.on('request', (r) => {
  const u = new URL(r.url());
  if (u.hostname !== 'names.coinos.io' || !u.pathname.startsWith('/hats')) return r.continue();
  const send = (b) => r.respond({ status: 200, contentType: 'application/json', headers: cors, body: JSON.stringify(b) });
  if (r.method() === 'OPTIONS') return send({});
  if (u.pathname === '/hats/free' && r.method() === 'POST') {
    taken = { body: JSON.parse(r.postData() || '{}'), auth: r.headers().authorization || '' };
    rec = { owned: ['santa'], equipped: 'santa' };
    return send(rec);
  }
  if (/^\/hats\/[0-9a-f]{64}$/.test(u.pathname)) return send({ ...rec, prices: { beanie: 21, trucker: 2100 }, free: season ? ['santa'] : [] });
  if (u.pathname === '/hats/worn') return send({ hats: {} });
  return send({ hats: {} });
});
const click = (t) => page.evaluate((x) => { const e = [...document.querySelectorAll('button')].find((n) => n.textContent.trim().toLowerCase() === 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()).includes(x)) return true; await sleep(250); } return false; };
try {
  await page.setViewport({ width: 390, height: 844 });
  await page.goto('http://localhost:5349/', { waitUntil: 'domcontentloaded' });
  await page.evaluate(() => localStorage.setItem('btc-wallet-network', 'regtest'));
  await page.goto('http://localhost:5349/', { waitUntil: 'domcontentloaded' });
  await waitText('Create a new wallet', 20000); await sleep(400);
  await page.evaluate(() => [...document.querySelectorAll('button')].find((n) => /create a new wallet/i.test(n.textContent))?.click()); await sleep(600);
  await seedWords(page); await skipSeedCheck(page); await sleep(1500);
  await page.goto('http://localhost:5349/hats', { waitUntil: 'domcontentloaded' });
  check('in December the shop lists the Santa hat', await waitText('Santa hat'));
  const t1 = await text();
  check('...first, ahead of the Toque', t1.indexOf('Santa hat') < t1.indexOf('Toque'));
  check('...saying it is free through December', t1.includes('Free for everyone through December'));
  check('...with a Free button', await click('free'));
  check('taking it puts it on', await waitText('Wearing ✓', 10000));
  check('...through POST /hats/free for the santa hat, NIP-98 signed', taken && taken.body.hat === 'santa' && /^Nostr /.test(taken.auth), JSON.stringify(taken?.body));
  check('...and says it is yours', (await text()).includes('Santa hat is yours'));
  // out of season, a head without one sees no Santa hat at all
  season = false; rec = { owned: [], equipped: null };
  await page.goto('http://localhost:5349/hats', { waitUntil: 'domcontentloaded' });
  await waitText('Toque');
  check('out of season the Santa hat is not listed', !(await text()).includes('Santa hat'));
  // ...but one already taken stays in the rack
  rec = { owned: ['santa'], equipped: null };
  await page.goto('http://localhost:5349/hats', { waitUntil: 'domcontentloaded' });
  await waitText('Toque');
  check('...unless you took one: it stays in your rack', (await text()).includes('Santa hat') && (await text()).includes('In your hat rack'));
  check('no page errors', errs.length === 0, errs.slice(0, 2).join(' | '));
} finally { await browser.close(); server.stop(true); }
console.log(ok ? '\n✅ free Santa hat' : '\n❌ failed');
process.exit(ok ? 0 : 1);