diff options
Diffstat (limited to 'tools/post-markdown-test.js')
| -rw-r--r-- | tools/post-markdown-test.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tools/post-markdown-test.js b/tools/post-markdown-test.js new file mode 100644 index 0000000..e4cf27b --- /dev/null +++ b/tools/post-markdown-test.js @@ -0,0 +1,43 @@ +// Posts render the inline markdown people type — **bold**, *italic*, +// ~~struck~~, `code` — and leave snake_case, 2*3*4 and lone stars alone. +// Run: bun tools/post-markdown-test.js +import puppeteer from './puppeteer.js'; +import { finalizeEvent, generateSecretKey } from 'nostr-tools/pure'; +import { buildHtml, buildVerifyWorker } from '../build.js'; +let ok = true; +const check = (n, c, d = '') => { console.log(` ${c ? '✓' : '✗'} ${n}${d ? ' — ' + d : ''}`); if (!c) ok = false; }; +const sk = generateSecretKey(), now = Math.floor(Date.now() / 1000); +const TEXT = "📰 **In this week's issue:**\n\nWhen the *servers* go ~~dark~~ away, run `git clone` and keep snake_case_names, 2*3*4 and a lone * star as they are. This is plain English prose for the language check."; +const note = finalizeEvent({ kind: 1, created_at: now, tags: [], content: TEXT }, sk); +const profile = finalizeEvent({ kind: 0, created_at: now, tags: [], content: JSON.stringify({ name: 'Newsletter' }) }, 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' } }); + return new Response(html, { headers: { 'Content-Type': 'text/html' } }); +} }); +const browser = await puppeteer.launch({ executablePath: '/usr/bin/google-chrome', headless: 'new', args: ['--no-sandbox'] }); +try { + const page = await browser.newPage(); + const errs = []; + page.on('pageerror', (e) => errs.push(String(e))); + await page.evaluateOnNewDocument(() => { + const Real = window.WebSocket; + 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') { setTimeout(() => ws.dispatchEvent(new MessageEvent('message', { data: JSON.stringify(['EOSE', m[1]]) })), 0); return; } } catch {} return send(d); }; return ws; }; + window.WebSocket.prototype = Real.prototype; Object.assign(window.WebSocket, Real); + }); + await page.goto(server.url.origin + '/feed'); + await page.waitForFunction(() => document.querySelector('.note-text strong'), { timeout: 15000 }).catch(() => {}); + const r = await page.evaluate(() => { + const t = document.querySelector('.note-text'); + return t ? { strong: [...t.querySelectorAll('strong')].map((x) => x.textContent), em: [...t.querySelectorAll('em')].map((x) => x.textContent), s: [...t.querySelectorAll('s')].map((x) => x.textContent), code: [...t.querySelectorAll('code')].map((x) => x.textContent), text: t.innerText } : null; + }); + check('**bold** is bold, markers gone', r && r.strong[0] === "In this week's issue:" && !r.text.includes('**'), JSON.stringify(r && r.strong)); + check('*italic*, ~~struck~~ and `code`', r && r.em[0] === 'servers' && r.s[0] === 'dark' && r.code[0] === 'git clone', JSON.stringify(r && [r.em, r.s, r.code])); + check('snake_case, 2*3*4 and a lone star stay as typed', r && r.text.includes('snake_case_names, 2*3*4 and a lone * star')); + check('no page errors', errs.length === 0, errs.slice(0, 2).join(' | ')); +} finally { await browser.close(); server.stop(true); } +console.log(ok ? '\n✅ posts render inline markdown' : '\n❌ failed'); +process.exit(ok ? 0 : 1); |
