// Reposts in a feed: someone you follow reposting a note (NIP-18 kind 6, // the note embedded) shows the note under " reposted" — Amethyst // showed Zapstore's repost of LWB's post, we showed nothing. A repost whose // copy doesn't check out is left out, and a note already in the feed isn't // shown twice. // Run: bun tools/feed-repost-test.js import puppeteer 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 ZAPSTORE = key(), LWB = key(), OTHER = 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 repost = (who, ev, ago, content = JSON.stringify(ev)) => finalizeEvent({ kind: 6, created_at: now - ago, tags: [['e', ev.id, '', 'mention'], ['p', ev.pubkey]], content }, who.sk); const TANKFUL = note(LWB, 'Tankful is a vehicle logbook that stays on your phone', 900); const FORGED = { ...note(OTHER, 'the real words', 950) }; const forgedCopy = JSON.stringify({ ...FORGED, content: 'words swapped after signing' }); const OWN = note(ZAPSTORE, 'zapstore says hello itself', 300); const POSTS = [ OWN, repost(ZAPSTORE, TANKFUL, 200), repost(ZAPSTORE, FORGED, 250, forgedCopy), repost(ZAPSTORE, OWN, 100), // its own note, already in the feed ]; const html = await buildHtml({ minify: true, pwa: false }); const server = Bun.serve({ port: 5316, 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)); window.WebSocket = function (...a) { const ws = new Real(...a); const send = ws.send.bind(ws); 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(window.__extra || []).filter((e) => fs.some((f) => fits(e, f)))) ws.dispatchEvent(new MessageEvent('message', { data: JSON.stringify(['EVENT', m[1], ev]) })); ws.dispatchEvent(new MessageEvent('message', { data: JSON.stringify(['EOSE', m[1]]) })); }, 0); return; } if (m[0] === 'EVENT') 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; }; try { await page.setViewport({ width: 390, height: 844 }); await page.goto('http://localhost:5316/', { 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); const base = cacheKeyFor(mn + '\n'); await page.evaluate(([k, z, l, o]) => { localStorage.setItem(k + ':follows', JSON.stringify({ tags: [['p', z]], c: '', at: Math.floor(Date.now() / 1000) })); localStorage.setItem(k + ':profiles', JSON.stringify({ [z]: { name: 'Zapstore', t: Date.now() }, [l]: { name: 'LWB', t: Date.now() }, [o]: { name: 'Other', t: Date.now() } })); localStorage.setItem('btc-wallet-feed:' + k, 'following'); }, [base, ZAPSTORE.pk, LWB.pk, OTHER.pk]); // and a post of my own, which I don't follow myself const MINE = finalizeEvent({ kind: 1, created_at: now - 50, tags: [], content: 'my own latest post' }, privateKeyFromSeedWords(mn)); await page.evaluateOnNewDocument((e) => { window.__live = true; window.__extra = [e]; }, MINE); await page.reload({ waitUntil: 'domcontentloaded' }); await waitText('receive', 20000); await page.evaluate(() => document.querySelector('.app-nav-button[aria-label="Feed"]')?.click()); check('the reposted note shows', await waitText('Tankful is a vehicle logbook', 30000)); const row = await page.evaluate(() => { const r = [...document.querySelectorAll('[data-zap-post]')].find((n) => n.textContent.includes('Tankful')); return r && { text: r.innerText, post: r.getAttribute('data-zap-post') }; }); check('...under "Zapstore reposted", as LWB\'s post', !!row && /Zapstore reposted/.test(row.text) && /LWB/.test(row.text) && row.post === TANKFUL.id, JSON.stringify(row)); await sleep(1500); const t = await text(); check('a repost whose copy was tampered with is left out', !t.includes('words swapped') && !t.includes('the real words')); check('a note already in the feed isn\'t shown again for its repost', (t.match(/zapstore says hello itself/g) || []).length === 1); await page.evaluate(() => [...document.querySelectorAll('[data-zap-post]')].find((n) => n.textContent.includes('Tankful')).querySelector('.note-text, .note-content').click()); check('a tap opens the reposted note\'s thread', await page.evaluate(async () => { for (let i = 0; i < 20; i++) { if (location.pathname.includes('nevent') || document.querySelector('.thread-reply-send, .thread-view')) return true; await new Promise((r) => setTimeout(r, 200)); } return false; })); await page.evaluate(() => history.back()); await sleep(800); check('my own post is in my Following feed', await waitText('my own latest post', 10000)); check('no page errors', errs.length === 0, errs.slice(0, 2).join(' | ')); } finally { await browser.close(); server.stop(true); } console.log(ok ? '\n✅ reposts show in the feed' : '\n❌ failed'); process.exit(ok ? 0 : 1);