aboutsummaryrefslogtreecommitdiffstats
path: root/tools/bare-link-test.js
blob: 71161affe59a1fe0d994df4f7c5e3c51867da95f (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
// A web address written without https:// ("saturdaybitcoin.com/rally/1?…")
// is a link, as Amethyst shows it; file names, emails and nip05s stay text.
// Run: bun tools/bare-link-test.js
import assert from 'node:assert/strict';
import puppeteer from './puppeteer.js';
import { finalizeEvent, generateSecretKey } from 'nostr-tools/pure';
import { buildHtml, buildVerifyWorker } from '../build.js';

const sk = generateSecretKey(), now = Math.floor(Date.now() / 1000);
const note = finalizeEvent({ kind: 1, created_at: now, tags: [], content: 'Rally No. 1 is set.\n\nJoin the rally: saturdaybitcoin.com/rally/1?utm_source=nostr&utm_campaign=rally1-announce\n\nOr see example.org. Built with node.js, mail [email protected], I am [email protected].' }, sk);
const profile = finalizeEvent({ kind: 0, created_at: now, tags: [], content: JSON.stringify({ name: 'Saturday Bitcoin' }) }, sk);
const html = await buildHtml({ minify: true, pwa: false });
const worker = await buildVerifyWorker({ minify: true });
const server = Bun.serve({ port: 0, fetch(req) {
  const path = new URL(req.url).pathname;
  if (path === '/api/feed') return Response.json({ version: 1, generatedAt: Date.now(), popUntil: now - 100, notes: [note], profiles: [profile] });
  if (path === '/verify-worker.js') return new Response(worker, { headers: { 'Content-Type': 'text/javascript' } });
  if (path.startsWith('/punks')) return new Response(null, { status: 404 });
  return new Response(html, { headers: { 'Content-Type': 'text/html' } });
} });
const browser = await puppeteer.launch({ executablePath: '/usr/bin/google-chrome', headless: 'new', args: ['--no-sandbox'] });
const check = (name, ok, detail = '') => { assert(ok, name + (detail ? ' — ' + detail : '')); console.log(' ✓ ' + name); };
try {
  const page = await browser.newPage();
  const errors = [];
  page.on('pageerror', (e) => errors.push(e.message));
  await page.setRequestInterception(true);
  page.on('request', (r) => (r.url().startsWith(server.url.origin) || r.url().startsWith('data:') ? r.continue() : r.abort()));
  await page.goto(server.url.origin + '/feed');
  await page.waitForFunction(() => document.body.innerText.includes('Rally No. 1'), { timeout: 8000 });
  const links = await page.$$eval('.notes-feed .note-content a, .notes-feed a', (as) => [...new Set(as)].map((a) => ({ text: a.textContent, href: a.getAttribute('href') })).filter((a) => /^https:/.test(a.href || '')));
  const rally = links.find((a) => /saturdaybitcoin/.test(a.href));
  check('the bare address is a link', !!rally, JSON.stringify(links));
  check('...to its https address', rally.href === 'https://saturdaybitcoin.com/rally/1?utm_source=nostr&utm_campaign=rally1-announce', rally.href);
  check('...shown as written', /^saturdaybitcoin\.com\/rally/.test(rally.text), rally.text);
  const ex = links.find((a) => /example\.org/.test(a.href));
  check('a bare domain ending a sentence is a link without the full stop', ex && ex.href === 'https://example.org' && ex.text === 'example.org', JSON.stringify(ex));
  check('a file name, an email and a nip05 stay text', !links.some((a) => /node\.js|gmail|coinos/.test(a.href)), JSON.stringify(links));
  check('the text around them is intact', (await page.evaluate(() => document.querySelector('.notes-feed').innerText)).includes('example.org. Built with node.js, mail [email protected], I am [email protected].'));
  check('no page errors', !errors.length, errors.join(' | '));
  console.log('\n✅ bare web addresses are links');
} finally {
  await browser.close();
  server.stop(true);
}