diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/own-boost-feed-test.js | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/tools/own-boost-feed-test.js b/tools/own-boost-feed-test.js new file mode 100644 index 0000000..e5e3440 --- /dev/null +++ b/tools/own-boost-feed-test.js @@ -0,0 +1,104 @@ +// Your own boost shows in your feed. "I tried boosting this post but I don't +// see it in my feed" (Adam, 2026-11-22): the post was already in Following +// (he follows its author), and a note shows once, so his boost was folded +// away. "We dedupe other boosts right? so maybe make an exception for our +// own": my boost has its own row at the time I boosted, under "<me> +// reposted", and the post itself stays where it was. +// Run: bun tools/own-boost-feed-test.js +import puppeteer, { seedWords, skipSeedCheck } from './puppeteer.js'; +import { cacheKeyFor } from '../src/wallet.js'; +import { generateSecretKey, getPublicKey, finalizeEvent } from 'nostr-tools/pure'; +import { privateKeyFromSeedWords } from 'nostr-tools/nip06'; +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 key = () => { const sk = generateSecretKey(); return { sk, pk: getPublicKey(sk) }; }; +const PNK = key(), FILLER = key(); +const now = Math.floor(Date.now() / 1000); +const note = (who, content, ago) => finalizeEvent({ kind: 1, created_at: now - ago, tags: [], content }, who.sk); +const PAFF = note(PNK, 'Running a PAFF server on my phone pledging storage', 72000); +const POSTS = [PAFF, ...Array.from({ length: 12 }, (_, i) => note(FILLER, `filler post number ${i + 1} with words enough to tell apart ${'abcdefghijkl'[i].repeat(6)}`, 600 + i * 900))]; +const html = await buildHtml({ minify: true, pwa: false }); +const server = Bun.serve({ port: 5347, 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({ relays: 'read', executablePath: '/usr/bin/google-chrome', headless: 'new', args: ['--no-sandbox'] }); +const page = await browser.newPage(); +await page.evaluateOnNewDocument((fx) => { + const Real = window.WebSocket; + const fits = (ev, f) => (!f.kinds || f.kinds.includes(ev.kind)) && (!f.authors || f.authors.includes(ev.pubkey)) && (!f.ids || f.ids.includes(ev.id)); + const pubd = () => { try { return JSON.parse(localStorage.getItem('__pub') || '[]'); } catch { return []; } }; + window.WebSocket = function (...a) { + const ws = new Real(...a); const send = ws.send.bind(ws); + const say = (m) => ws.dispatchEvent(new MessageEvent('message', { data: JSON.stringify(m) })); + ws.send = (d) => { + try { + const m = JSON.parse(d); + if (m[0] === 'REQ') { const fs = m.slice(2); setTimeout(() => { if (window.__live) for (const ev of fx.concat(pubd()).filter((e) => fs.some((f) => fits(e, f)))) say(['EVENT', m[1], ev]); say(['EOSE', m[1]]); }, 0); return; } + if (m[0] === 'EVENT') { if (m[1].kind === 6) { const all = pubd(); if (!all.some((e) => e.id === m[1].id)) localStorage.setItem('__pub', JSON.stringify([...all, m[1]])); } setTimeout(() => say(['OK', m[1].id, true, '']), 0); return; } + } catch {} + return send(d); + }; + return ws; + }; + window.WebSocket.prototype = Real.prototype; Object.assign(window.WebSocket, Real); +}, POSTS); +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 rows = () => page.evaluate(() => [...document.querySelectorAll('.notes-feed > .row[data-key]')].map((r) => ({ paff: r.textContent.includes('Running a PAFF server'), reposted: /reposted/.test(r.querySelector('.note-reposted')?.textContent || '') }))); +try { + await page.setViewport({ width: 390, height: 844 }); + await page.goto('http://localhost:5347/', { 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 seedWords(page); + const mn = (await page.$$eval('.words .w .t', (l) => l.map((e) => e.textContent.trim()))).join(' '); + await skipSeedCheck(page); + await waitText('receive', 20000); + const base = cacheKeyFor(mn + '\n'); + await page.evaluate(([k, p, f]) => { + localStorage.setItem(k + ':follows', JSON.stringify({ tags: [['p', p], ['p', f]], c: '', at: Math.floor(Date.now() / 1000) })); + localStorage.setItem(k + ':profiles', JSON.stringify({ [p]: { name: 'pnk', t: Date.now() }, [f]: { name: 'Filler', t: Date.now() } })); + localStorage.setItem('btc-wallet-feed:' + k, 'following'); + }, [base, PNK.pk, FILLER.pk]); + await page.evaluateOnNewDocument(() => { window.__live = true; }); + await page.reload({ waitUntil: 'domcontentloaded' }); + await waitText('receive', 20000); + await page.evaluate(() => document.querySelector('.app-nav-button[aria-label="Feed"]')?.click()); + check('the followed post is in the feed', await waitText('Running a PAFF server', 30000)); + await sleep(1500); + let r = await rows(); + check('...once, below newer posts, not as a repost', r.filter((x) => x.paff).length === 1 && r.findIndex((x) => x.paff) > 3 && !r.find((x) => x.paff).reposted, JSON.stringify(r.map((x) => +x.paff))); + const tapped = await page.evaluate(() => { const row = [...document.querySelectorAll('.notes-feed > .row[data-key]')].find((n) => n.textContent.includes('Running a PAFF server')); const b = row && [...row.querySelectorAll('.note-acts button')].find((x) => /boost/i.test(x.getAttribute('aria-label') || x.title || '')); if (b) { b.click(); return true; } return false; }); + check('Boost tapped', tapped); + let top = null; + for (let i = 0; i < 40; i++) { r = await rows(); top = r[0]; if (top && top.paff && top.reposted) break; await sleep(250); } + check('the boost is at the top of my feed, under "… reposted"', !!top && top.paff && top.reposted, JSON.stringify(r.slice(0, 3))); + check('the row I tapped is still where it was', r.filter((x) => x.paff).length === 2); + check('the boost was published', await page.evaluate(() => JSON.parse(localStorage.getItem('__pub') || '[]').length === 1)); + await sleep(1500); // the feed cache is written + await page.reload({ waitUntil: 'domcontentloaded' }); + await waitText('receive', 20000); + await page.evaluate(() => document.querySelector('.app-nav-button[aria-label="Feed"]')?.click()); + await waitText('Running a PAFF server', 30000); + await sleep(4000); + r = await rows(); + const both = (r) => !!r[0] && r[0].paff && r[0].reposted && r.filter((x) => x.paff).length === 2 && r.filter((x) => x.paff && x.reposted).length === 1 && r[r.length - 1].paff && !r[r.length - 1].reposted; + check('after a reload: my boost first (the newest), the post itself still in its place', both(r), JSON.stringify(r.map((x) => [+x.paff, +x.reposted]))); + // a second device: nothing cached, the post and the boost both come from relays + await page.evaluate(() => { for (const k of Object.keys(localStorage)) if (/feed-cache|feedcache|:feed:|notes/i.test(k)) localStorage.removeItem(k); }); + await page.evaluate(async () => { if (indexedDB.databases) for (const d of await indexedDB.databases()) if (/feed|note/i.test(d.name || '')) indexedDB.deleteDatabase(d.name); }); + await page.reload({ waitUntil: 'domcontentloaded' }); + await waitText('receive', 20000); + await page.evaluate(() => document.querySelector('.app-nav-button[aria-label="Feed"]')?.click()); + await waitText('Running a PAFF server', 30000); + await sleep(4000); + r = await rows(); + check('with no cache: the same two rows', both(r), JSON.stringify(r.map((x) => [+x.paff, +x.reposted]))); + check('no page errors', errs.length === 0, errs.slice(0, 2).join(' | ')); +} finally { await browser.close(); server.stop(true); } +console.log(ok ? '\n✅ my own boost shows in my feed' : '\n❌ failed'); +process.exit(ok ? 0 : 1); |
