// @mentions: "@" and a few letters in the post composer open a list of // people — the ones you follow first, then the search — and picking one puts // nostr:npub… in the text; the post goes out with their p tag. // Run: bun tools/mention-ac-test.js import puppeteer from './puppeteer.js'; import { cacheKeyFor } from '../src/wallet.js'; import { generateSecretKey, getPublicKey } from 'nostr-tools/pure'; import { npubEncode } from 'nostr-tools/nip19'; 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 FRIEND = getPublicKey(generateSecretKey()), STRANGER = getPublicKey(generateSecretKey()); const html = await buildHtml({ minify: true, pwa: false }); const server = Bun.serve({ port: 0, 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(); await page.evaluateOnNewDocument((stranger) => { window.__published = []; // the registrar's people search knows one stranger const realFetch = window.fetch; window.fetch = (url, opts) => { if (/\/search\?q=/.test(String(url))) { const q = new URL(String(url)).searchParams.get('q'); const results = 'satoshi nakamoto'.startsWith(q) || q.startsWith('sat') ? [{ pubkey: stranger, name: 'Satoshi Stranger', address: 'satoshi@coinos.io' }] : []; return Promise.resolve(new Response(JSON.stringify({ results }), { headers: { 'content-type': 'application/json' } })); } return realFetch(url, opts); }; const Real = window.WebSocket; window.WebSocket = function (...a) { const ws = new Real(...a); const send = ws.send.bind(ws); ws.send = (data) => { try { const m = JSON.parse(data); if (m[0] === 'REQ') { setTimeout(() => ws.dispatchEvent(new MessageEvent('message', { data: JSON.stringify(['EOSE', m[1]]) })), 0); return; } if (m[0] === 'EVENT') { window.__published.push(m[1]); setTimeout(() => ws.dispatchEvent(new MessageEvent('message', { data: JSON.stringify(['OK', m[1].id, true, '']) })), 0); return; } } catch {} return send(data); }; return ws; }; window.WebSocket.prototype = Real.prototype; Object.assign(window.WebSocket, Real); }, STRANGER); 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 waitText = async (x, ms = 25000) => { for (let i = 0; i < ms / 250; i++) { if ((await page.evaluate(() => document.body.innerText)).toLowerCase().includes(x)) return true; await sleep(250); } return false; }; const waitFor = async (fn, ms = 8000) => { for (let i = 0; i < ms / 200; i++) { if (await page.evaluate(fn)) return true; await sleep(200); } return false; }; const list = () => page.evaluate(() => [...document.querySelectorAll('.mention-ac .chat-name')].map((n) => n.textContent)); const value = () => page.evaluate(() => document.querySelector('.post-input').value); try { await page.setViewport({ width: 390, height: 844 }); await page.goto(server.url.href, { waitUntil: 'domcontentloaded' }); await page.evaluate(() => localStorage.setItem('btc-wallet-network', 'regtest')); await page.reload({ waitUntil: 'domcontentloaded' }); await sleep(400); await click('create a new wallet'); await sleep(600); await page.waitForSelector('.words .w .t'); const mn = (await page.$$eval('.words .w .t', (l) => l.map((e) => e.textContent.trim()))).join(' '); await click('skip verification'); await waitText('receive', 20000); await sleep(3000); // a new wallet's starter follows land first; ours go over them const base = cacheKeyFor(mn + '\n'); await page.evaluate(([k, pk]) => { localStorage.setItem(k + ':follows', JSON.stringify({ tags: [['p', pk]], c: '', at: Math.floor(Date.now() / 1000) })); localStorage.setItem(k + ':profiles', JSON.stringify({ [pk]: { name: 'Globetrotter', t: Date.now() } })); localStorage.setItem('btc-wallet-feed:' + k, 'following'); }, [base, FRIEND]); await page.reload({ waitUntil: 'domcontentloaded' }); await waitText('receive', 20000); await page.evaluate(() => document.querySelector('.app-nav-button[aria-label="Feed"]').click()); await sleep(1500); const opened = await page.evaluate(() => { const b = [...document.querySelectorAll('button')].find((n) => (n.getAttribute('aria-label') || n.textContent).trim().toLowerCase() === 'new post'); if (b) { b.click(); return true; } return false; }); check('the composer opens', opened && !!(await page.waitForSelector('.post-input', { timeout: 5000 }).catch(() => null))); await page.click('.post-input'); console.log('\n[someone you follow]'); await page.keyboard.type('hello @glo'); check('"@glo" lists the person you follow', await waitFor(() => [...document.querySelectorAll('.mention-ac .chat-name')].some((n) => n.textContent === 'Globetrotter')), JSON.stringify(await list())); await page.keyboard.press('Enter'); await sleep(300); check('Enter puts nostr:npub… in its place, list gone', (await value()) === 'hello nostr:' + npubEncode(FRIEND) + ' ' && !(await list()).length, JSON.stringify(await value())); console.log('\n[someone found by the search]'); await page.keyboard.type('and @sato'); check('"@sato" lists the search\'s hit', await waitFor(() => [...document.querySelectorAll('.mention-ac .chat-name')].some((n) => n.textContent === 'Satoshi Stranger')), JSON.stringify(await list())); await page.evaluate(() => [...document.querySelectorAll('.mention-ac .item')].find((n) => /Satoshi Stranger/.test(n.textContent)).click()); await sleep(300); check('a tap puts them in too', (await value()).includes('and nostr:' + npubEncode(STRANGER) + ' '), JSON.stringify(await value())); await page.keyboard.type('@nobody'); await sleep(300); await page.keyboard.press('Escape'); await sleep(200); check('Escape closes the list', !(await list()).length); await page.keyboard.press('Backspace'); // on a line of its own (Chrome wraps lines in