// Closing a full-screen picture leaves the feed where it was. The scroll // memory that puts a reader back on the row they left when Back closes a // thread or profile used to fire on every history pop — and a remembered // place goes stale whenever its page is left another way (the Feed button // from a thread) — so a photo's × jumped the feed to wherever an earlier // thread had been opened from, or to the top. Back out of a thread still // lands on the row that opened it. // Run: bun tools/lightbox-scroll-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 = Bun.spawnSync(['ffmpeg', '-loglevel', 'error', '-f', 'lavfi', '-i', 'testsrc=size=400x300', '-frames:v', '1', '-f', 'image2', '-c:v', 'png', 'pipe:1']).stdout; const SK = generateSecretKey(), AUTHOR = getPublicKey(SK); const now = Math.floor(Date.now() / 1000); const post = (content, ago) => finalizeEvent({ kind: 1, created_at: now - ago, tags: [], content }, SK); const html = await buildHtml({ minify: true, pwa: false }); const server = Bun.serve({ port: 0, fetch: (req) => { const path = new URL(req.url).pathname; if (path.startsWith('/punks')) return new Response(Bun.file('dist' + path)); if (path === '/pic.png') return new Response(PNG, { headers: { 'content-type': 'image/png' } }); return new Response(html, { headers: { 'content-type': 'text/html' } }); } }); const PIC = server.url.origin + '/pic.png'; // a long feed: text posts to scroll through, a picture every so often const seeded = []; for (let i = 0; i < 40; i++) seeded.push(post((i % 5 === 2 ? 'picture post ' + i + ' ' + PIC : 'text post ' + i + ' ' + 'lorem ipsum dolor sit amet. '.repeat(6)), 100 + i * 60)); 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))); 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, arg, ms = 10000) => { for (let i = 0; i < ms / 200; i++) { if (await page.evaluate(fn, arg)) return true; await sleep(200); } return false; }; const scrollY = () => page.evaluate(() => window.scrollY); // scroll a post into the middle of the screen; returns the page offset there const showPost = async (word) => page.evaluate((w) => { const r = [...document.querySelectorAll('.notes-feed [data-key]')].find((n) => n.textContent.includes(w)); r.scrollIntoView({ block: 'center' }); return window.scrollY; }, word); const clickPicture = async () => { const at = await page.evaluate(() => { const i = [...document.querySelectorAll('.notes-feed img.note-img')].map((x) => x.getBoundingClientRect()).find((r) => r.top > 0 && r.bottom < innerHeight); return i && { x: i.x + i.width / 2, y: i.y + i.height / 2 }; }); if (!at) return false; await page.mouse.click(at.x, at.y); return true; }; 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); 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 + ':feedNotes', JSON.stringify(ns)); localStorage.setItem(k + ':profiles', JSON.stringify({ [pk]: { name: 'Long Poster', t: Date.now() } })); localStorage.setItem('btc-wallet-feed:' + k, 'following'); }, [base, AUTHOR, seeded]); await page.reload({ waitUntil: 'domcontentloaded' }); await waitText('receive', 20000); await page.evaluate(() => document.querySelector('.app-nav-button[aria-label="Feed"]').click()); check('the feed is up', await waitFor(() => document.querySelectorAll('.notes-feed img.note-img').length >= 2, null, 20000)); console.log('\n[a stale remembered place: a thread left by the Feed button]'); const y1 = await showPost('text post 3'); await sleep(300); await page.evaluate(() => { const r = [...document.querySelectorAll('.notes-feed [data-key]')].find((n) => n.textContent.includes('text post 3')); r.click(); }); await sleep(800); check('the thread opened', await page.evaluate(() => !document.querySelector('.notes-feed'))); await page.evaluate(() => document.querySelector('.app-nav-button[aria-label="Feed"]').click()); await sleep(800); check('the Feed button brings the feed back (no history pop)', await waitFor(() => !!document.querySelector('.notes-feed'), null, 5000)); console.log('\n[a picture, opened and closed]'); const y2 = await showPost('picture post 17'); await sleep(400); check('the reader is somewhere else now', Math.abs(y2 - y1) > 400, `${y1} → ${y2}`); check('a picture opens full screen', await clickPicture() && await waitFor(() => !!document.querySelector('.lightbox, .media-pager'), null, 4000)); const under = await scrollY(); await page.keyboard.press('Escape'); await sleep(900); check('the picture closed', await page.evaluate(() => !document.querySelector('.lightbox, .media-pager'))); const y3 = await scrollY(); check('the feed is where it was, not jumped to the stale place or the top', Math.abs(y3 - y2) <= 2, `before ${y2}, under ${under}, after ${y3}, stale ${y1}`); console.log('\n[Back out of a thread still lands on its row]'); const y4 = await showPost('text post 23'); await sleep(300); await page.evaluate(() => { const r = [...document.querySelectorAll('.notes-feed [data-key]')].find((n) => n.textContent.includes('text post 23')); r.click(); }); await sleep(800); check('the thread opened at its top', await page.evaluate(() => !document.querySelector('.notes-feed') && window.scrollY === 0)); await page.evaluate(() => history.back()); await sleep(1200); const y5 = await scrollY(); check('Back puts the reader on the row they left', Math.abs(y5 - y4) <= 2, `${y4} → ${y5}`); check('no page errors', errs.length === 0, errs.slice(0, 2).join(' | ')); } finally { await browser.close(); server.stop(true); } console.log(ok ? '\n✅ closing a picture leaves the feed where it was' : '\n❌ failed'); process.exit(ok ? 0 : 1);