aboutsummaryrefslogtreecommitdiffstats
path: root/tools/comments-test.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/comments-test.js')
-rw-r--r--tools/comments-test.js23
1 files changed, 21 insertions, 2 deletions
diff --git a/tools/comments-test.js b/tools/comments-test.js
index 01d47d5..6801f44 100644
--- a/tools/comments-test.js
+++ b/tools/comments-test.js
@@ -25,12 +25,31 @@ const html = await buildHtml({ minify: true, pwa: false });
const server = Bun.serve({ port: 5283, 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();
-await page.evaluateOnNewDocument(() => {
+// The article and its comments, fetched above, are also handed to the app
+// on every relay socket that asks for them: the app reaching the same public
+// relays as this script (one of them can be refusing this IP) is not what's
+// under test.
+await page.evaluateOnNewDocument((known) => {
window.__sent = [];
window.__all = [];
+ const matches = (f, e) => {
+ if (f.ids && !f.ids.includes(e.id)) return false;
+ if (f.kinds && !f.kinds.includes(e.kind)) return false;
+ if (f.authors && !f.authors.includes(e.pubkey)) return false;
+ for (const [k, vals] of Object.entries(f)) {
+ if (k[0] !== '#' || !Array.isArray(vals)) continue;
+ if (!e.tags.some((t) => t[0] === k.slice(1) && vals.includes(t[1]))) return false;
+ }
+ return !!(f.ids || Object.keys(f).some((k) => k[0] === '#'));
+ };
const send = WebSocket.prototype.send;
WebSocket.prototype.send = function (d) {
window.__all.push(this.url + ' ' + String(d).slice(0, 60));
+ if (typeof d === 'string' && d.startsWith('["REQ"')) {
+ const [, sub, ...filters] = JSON.parse(d);
+ const hits = known.filter((e) => filters.some((f) => matches(f, e)));
+ if (hits.length) setTimeout(() => { for (const e of hits) this.dispatchEvent(new MessageEvent('message', { data: JSON.stringify(['EVENT', sub, e]) })); }, 30);
+ }
if (typeof d === 'string' && d.startsWith('["EVENT"')) { // every event: nothing leaves the test
const ev = JSON.parse(d)[1];
window.__sent.push(ev);
@@ -40,7 +59,7 @@ await page.evaluateOnNewDocument(() => {
}
return send.call(this, d);
};
-});
+}, [root, ...comments]);
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, ms = 15000) => { for (let i = 0; i < ms / 200; i++) { if (await page.evaluate(fn)) return true; await sleep(200); } return false; };