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
71
72
73
|
// A feed row waits up to 8 s for its pictures, then freezes as it is. A
// picture still downloading then (a 3.5 MB PNG on a phone) used to be frozen
// as a bare link — the post looked broken until opened. Now it keeps loading
// inside a fixed box (nothing moves when it lands); a picture that refused to
// load is still a link.
// Run: bun tools/slow-image-test.js
import puppeteer from './puppeteer.js';
import { cacheKeyFor } from '../src/wallet.js';
import { generateSecretKey, getPublicKey, finalizeEvent } from 'nostr-tools/pure';
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 PNG = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==', 'base64');
const AUTHOR = (() => { const sk = generateSecretKey(); return { sk, pk: getPublicKey(sk) }; })();
const now = Math.floor(Date.now() / 1000);
const html = await buildHtml({ minify: true, pwa: false });
const server = Bun.serve({ port: 5313, idleTimeout: 60, async 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 === '/slow.png') { await sleep(11000); return new Response(PNG, { headers: { 'content-type': 'image/png', 'access-control-allow-origin': '*' } }); }
if (p === '/gone.png') return new Response('no', { status: 404 });
return new Response(html, { headers: { 'content-type': 'text/html' } });
} });
const POSTS = [
finalizeEvent({ kind: 1, created_at: now - 100, tags: [], content: 'slow picture http://localhost:5313/slow.png' }, AUTHOR.sk),
finalizeEvent({ kind: 1, created_at: now - 200, tags: [], content: 'dead picture http://localhost:5313/gone.png' }, AUTHOR.sk),
];
const browser = await puppeteer.launch({ relays: 'read', executablePath: '/usr/bin/google-chrome', headless: 'new', args: ['--no-sandbox'] });
const page = await browser.newPage();
// the posts arrive from a relay, as live ones do (cached rows skip the gate)
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.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 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 row = (m) => `[...document.querySelectorAll('[data-zap-post]')].find((n) => n.textContent.includes(${JSON.stringify(m)}))`;
try {
await page.setViewport({ width: 390, height: 844 });
await page.goto('http://localhost:5313/', { 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, pk, ns]) => {
localStorage.setItem(k + ':follows', JSON.stringify({ tags: [['p', pk]], c: '', at: Math.floor(Date.now() / 1000) }));
localStorage.setItem(k + ':profiles', JSON.stringify({ [pk]: { name: 'Slowpoke', t: Date.now() } }));
localStorage.setItem('btc-wallet-feed:' + k, 'following');
}, [base, AUTHOR.pk, POSTS]);
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('both posts show', await waitText('slow picture', 30000) && await waitText('dead picture', 5000));
check('the slow picture is a picture box, not a link', await page.evaluate((r) => { const n = eval(r); return !!n && !!n.querySelector('img.note-img') && ![...n.querySelectorAll('a')].some((a) => a.href.endsWith('/slow.png')); }, row('slow picture')),
await page.evaluate((r) => eval(r)?.innerHTML.slice(0, 300), row('slow picture')));
let loaded = false;
for (let i = 0; i < 60 && !loaded; i++) { loaded = await page.evaluate((r) => { const i = eval(r)?.querySelector('img.note-img'); return !!i && i.complete && i.naturalWidth > 0; }, row('slow picture')); if (!loaded) await sleep(250); }
check('...and it lands once downloaded', loaded);
check('a picture that refused to load is a link', await page.evaluate((r) => { const n = eval(r); return !!n && [...n.querySelectorAll('a')].some((a) => a.href.endsWith('/gone.png')) && !n.querySelector('img.note-img'); }, row('dead picture')));
check('no page errors', errs.length === 0, errs.slice(0, 2).join(' | '));
} finally { await browser.close(); server.stop(true); }
console.log(ok ? '\n✅ slow pictures keep loading in place' : '\n❌ failed');
process.exit(ok ? 0 : 1);
|