diff options
Diffstat (limited to 'tools/feed-editor-relays-test.js')
| -rw-r--r-- | tools/feed-editor-relays-test.js | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/tools/feed-editor-relays-test.js b/tools/feed-editor-relays-test.js index 7e3edb5..5a808a7 100644 --- a/tools/feed-editor-relays-test.js +++ b/tools/feed-editor-relays-test.js @@ -10,16 +10,21 @@ 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 html = await buildHtml({ minify: true, pwa: false }); -const server = Bun.serve({ port: 0, fetch: () => new Response(html, { headers: { 'content-type': 'text/html' } }) }); +// nos.lol itself is a stand-in served here (an empty relay: EOSE to every +// REQ), so the test doesn't hang on whether the real one answers today +const server = Bun.serve({ port: 0, + fetch: (req, srv) => (new URL(req.url).pathname === '/relay' && srv.upgrade(req) ? undefined : new Response(html, { headers: { 'content-type': 'text/html' } })), + websocket: { message: (ws, data) => { try { const m = JSON.parse(data); if (m[0] === 'REQ') ws.send(JSON.stringify(['EOSE', m[1]])); } catch {} } } }); +const RELAY = 'ws://localhost:' + server.port + '/relay'; const browser = await puppeteer.launch({ executablePath: process.env.CHROME || '/usr/bin/google-chrome', headless: 'new', args: ['--no-sandbox'] }); const page = await browser.newPage(); const errs = []; page.on('pageerror', (e) => errs.push(String(e))); // record what the page asks which relay, without letting it publish anything -await page.evaluateOnNewDocument(() => { +await page.evaluateOnNewDocument((stand) => { window.__reqs = []; const Real = window.WebSocket; window.WebSocket = function (url, ...a) { - const ws = new Real(url, ...a); + const ws = new Real(/^wss:\/\/nos\.lol\/?$/.test(String(url)) ? stand : url, ...a); const send = ws.send.bind(ws); ws.send = (data) => { try { const m = JSON.parse(data); if (m[0] === 'REQ') window.__reqs.push({ url: String(url), filters: m.slice(2) }); if (m[0] === 'EVENT') return; } catch {} @@ -28,7 +33,7 @@ await page.evaluateOnNewDocument(() => { return ws; }; window.WebSocket.prototype = Real.prototype; Object.assign(window.WebSocket, Real); -}); +}, RELAY); 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 text = () => page.evaluate(() => document.body.innerText); const waitFor = async (fn, ms = 15000) => { for (let i = 0; i < ms / 200; i++) { if (await page.evaluate(fn)) return true; await sleep(200); } return false; }; |
