aboutsummaryrefslogtreecommitdiffstats
path: root/tools/unread-dot-test.js
blob: 3b43b7dabdd9fcecbf6f23149d9e105338a35ce0 (plain) (blame)
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
74
75
76
77
// The unread dot (on Messages in the bottom nav) either says someone is waiting or it doesn't — it must not
// flash on the way in. render() rebuilds the whole DOM, so any entrance
// animation on the dot replays on every repaint; an earlier build popped it
// seven times in the first seconds after a refresh, which reads as flashing.
//
// Run: bun tools/unread-dot-test.js
import puppeteer from './puppeteer.js';
import { generateMnemonic } from '@scure/bip39';
import { wordlist } from '@scure/bip39/wordlists/english';
import { buildHtml } from '../build.js';

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
let ok = true;
const html = await buildHtml({ minify: true, pwa: false });
const server = Bun.serve({ port: 5233, 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();
const click = (sel, t) => page.evaluate((s, x) => { const e = [...document.querySelectorAll(s)].find((n) => n.textContent.trim().toLowerCase().includes(x.toLowerCase())); if (e) { e.click(); return true; } return false; }, sel, 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.toLowerCase())) return true; await sleep(250); } return false; };

try {
  await page.goto('http://localhost:5233/', { waitUntil: 'domcontentloaded' });
  await page.evaluate(() => localStorage.setItem('btc-wallet-network', 'regtest'));
  await page.reload({ waitUntil: 'domcontentloaded' });
  await sleep(400);
  await click('button', 'Create a new wallet');
  await sleep(300);
  await click('button', 'Have an existing seed');
  await sleep(300);
  await page.waitForSelector('textarea');
  await page.type('textarea', generateMnemonic(wordlist));
  await click('button', 'Open wallet');
  await waitText('receive', 15000);
  await sleep(10000); // let the coinos room load so there IS unread

  // Now the refresh the report is about.
  await page.evaluate(() => {
    window.__dot = { anims: 0, appear: 0, disappear: 0, renders: 0 };
    addEventListener('animationstart', (e) => { if (e.target && e.target.classList && e.target.closest && e.target.closest('.app-nav-button')) window.__dot.anims++; }, true);
    let had = null;
    new MutationObserver(() => {
      window.__dot.renders++;
      const now = [...document.querySelectorAll('.app-nav-button.unread')].some((b) => /messages/i.test(b.getAttribute('aria-label') || ''));
      if (had !== null && now !== had) { if (now) window.__dot.appear++; else window.__dot.disappear++; }
      had = now;
    }).observe(document.getElementById('app'), { childList: true, subtree: true });
  });
  await page.reload({ waitUntil: 'domcontentloaded' });
  // re-arm after the reload wipes it
  await page.evaluate(() => {
    window.__dot = { anims: 0, appear: 0, disappear: 0, renders: 0 };
    addEventListener('animationstart', (e) => { if (e.target && e.target.classList && e.target.closest && e.target.closest('.app-nav-button')) window.__dot.anims++; }, true);
    let had = null;
    new MutationObserver(() => {
      window.__dot.renders++;
      const now = [...document.querySelectorAll('.app-nav-button.unread')].some((b) => /messages/i.test(b.getAttribute('aria-label') || ''));
      if (had !== null && now !== had) { if (now) window.__dot.appear++; else window.__dot.disappear++; }
      had = now;
    }).observe(document.body, { childList: true, subtree: true });
  });
  await sleep(12000);
  const r = await page.evaluate(() => ({ ...window.__dot, dotNow: [...document.querySelectorAll('.app-nav-button.unread')].some((b) => /messages/i.test(b.getAttribute('aria-label') || '')) }));
  console.log('over 12s after a refresh:');
  const check = (n, c, d = '') => { console.log(` ${c ? '✓' : '✗'} ${n}${d ? ' — ' + d : ''}`); if (!c) ok = false; };
  check('the dot never animates', r.anims === 0, `${r.anims} animation(s) started`);
  check('it settles once and stays', r.appear <= 1 && r.disappear === 0, `+${r.appear} / -${r.disappear}`);
  check('and the repaints it sat through were real', r.renders > 1, `${r.renders} DOM mutations`);
  check('it is showing (there is unread history)', r.dotNow);
} catch (e) {
  console.log(' ✗ run completed —', e.message);
  ok = false;
} finally {
  await browser.close();
  server.stop(true);
}
console.log(ok ? '\n✅ the dot holds still' : '\n❌ failures above');
process.exit(ok ? 0 : 1);
ctor('.app-nav-button[aria-label="Feed"]').click()); await sleep(7000); if (process.argv.includes('--slow')) { for (let i = 0; i < 40 && !(await page.evaluate(() => !!document.querySelector('.quote-card'))); i++) await sleep(250); check('a late quote first paints as a placeholder card, not a link', await page.evaluate(() => !document.body.innerText.includes('view note') && [...document.querySelectorAll('.quote-card')].some((q) => /Couldn.t find|Fetching note/.test(q.innerText)))); await sleep(SLOW_MS + 4000); } const card = await page.evaluate(() => { const q = document.querySelector('.quote-card'); if (!q) return null; return { text: q.innerText.replace(/\n+/g, ' | ').slice(0, 90), hasAvatar: !!q.querySelector('.chat-avatar'), inPost: !!q.closest('.notes-feed') }; }); check('the quoted note is shown, not linked to', !!card, card ? 'rendered' : 'no card'); check('...inside the post that quotes it', !!(card && card.inPost)); check('...with whose note it is', !!(card && card.hasAvatar), card ? card.text.slice(0, 50) : ''); const body = target.content.replace(/\s+/g, ' ').slice(0, 24); check('...and what it actually said', !!(card && card.text.replace(/\s+/g, ' ').includes(body.slice(0, 18))), body); if (!process.argv.includes('--slow')) { const far = await page.evaluate(() => { const r = [...document.querySelectorAll('.notes-feed [data-key]')].find((n) => n.innerText.includes('Quoter') && n.innerText.includes('I remember this')); return r && r.querySelector('.quote-card')?.innerText; }); check('a quote found only on a far relay still shows as its card', !!far && !/Couldn.t find|Fetching/.test(far), JSON.stringify((far || '').slice(0, 60))); } // (a quote inside a quote stays a link on purpose: one level of cards) check('no "view note" link left in the post', !(await page.evaluate(() => [...document.querySelectorAll('.notes-feed a')].some((a) => a.textContent === 'view note' && !a.closest('.quote-card'))))); // the outer post keeps its actions; the quote has none of its own const acts = await page.evaluate(() => ({ outer: document.querySelectorAll('.notes-feed > .row .note-acts').length, inside: document.querySelectorAll('.quote-card .note-acts').length, })); check('the quote is context, not a second post to act on', acts.outer >= 1 && acts.inside === 0, JSON.stringify(acts)); } finally { await browser.close(); server.stop(true); } console.log(ok ? '\n✅ quotes read as quotes' : '\n❌ failed'); process.exit(ok ? 0 : 1);