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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
// A touch on a feed clip that has been playing (its own controls up) opens
// the pager. Chrome on Android keeps such a tap for the player's controls —
// the page gets pointer events but no click — so the tap is read from the
// pointer events; here that is played out by swallowing the click. A touch
// on the control bar or the paused clip's play button stays the player's, a
// drag is a scroll, and where the click does come it isn't taken twice.
// Run: bun tools/video-touch-tap-test.js
import puppeteer from './puppeteer.js';
import { cacheKeyFor } from '../src/wallet.js';
import { generateSecretKey, getPublicKey, finalizeEvent } from 'nostr-tools/pure';
import { buildHtml } from '../build.js';
import { existsSync } from 'node:fs';
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 SK = generateSecretKey(); const AUTHOR = getPublicKey(SK);
const now = Math.floor(Date.now() / 1000);
const CLIP = '/tmp/coinos-touch-clip.webm';
if (!existsSync(CLIP)) Bun.spawnSync(['ffmpeg', '-y', '-loglevel', 'error', '-f', 'lavfi', '-i', 'testsrc=size=320x180:rate=10', '-t', '30', '-c:v', 'libvpx', '-b:v', '100k', CLIP]);
const POSTS = [finalizeEvent({ kind: 1, created_at: now - 100, tags: [], content: 'watch this http://localhost:5315/clip.webm' }, SK)];
const html = await buildHtml({ minify: true, pwa: false });
const server = Bun.serve({ port: 5315, fetch: (req) => {
const path = new URL(req.url).pathname;
if (path === '/clip.webm') return new Response(Bun.file(CLIP), { headers: { 'content-type': 'video/webm' } });
if (path.startsWith('/punks') || path === '/verify-worker.js') return new Response(Bun.file('dist' + path));
return new Response(html, { headers: { 'content-type': 'text/html' } });
} });
const browser = await puppeteer.launch({ executablePath: '/usr/bin/google-chrome', headless: 'new', args: ['--no-sandbox', '--autoplay-policy=no-user-gesture-required'] });
const page = await browser.newPage();
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; } if (m[0] === 'EVENT') return; } catch {} return send(d); }; return ws; };
window.WebSocket.prototype = Real.prototype; Object.assign(window.WebSocket, Real);
});
const errs = [];
page.on('pageerror', (e) => errs.push(String(e)));
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 st = () => page.evaluate(() => { const v = document.querySelector('.thread-card video.note-video') || document.querySelector('video.note-video'); return { fs: document.fullscreenElement === v, paused: v.paused, controls: v.controls, thread: !document.querySelector('video.note-video')?.isConnected }; });
// tap the clip at a fraction of its box (or of the screen, full screen)
const tap = async (fx, fy) => {
const fs = await page.evaluate(() => !!document.fullscreenElement);
const r = fs ? { x: 0, y: 0, width: await page.evaluate(() => innerWidth), height: await page.evaluate(() => innerHeight) }
: await page.evaluate(() => { const b = (document.querySelector('.thread-card video.note-video') || document.querySelector('video.note-video')).getBoundingClientRect(); return { x: b.x, y: b.y, width: b.width, height: b.height }; });
await page.mouse.click(r.x + r.width * fx, r.y + r.height * fy);
await sleep(700);
};
try {
await page.setViewport({ width: 390, height: 844, hasTouch: true });
await page.goto('http://localhost:5315/', { waitUntil: 'domcontentloaded' });
await page.evaluate(() => localStorage.setItem('btc-wallet-network', 'regtest'));
await page.reload({ waitUntil: 'domcontentloaded' }); await sleep(400);
await click('create a new wallet'); await sleep(600);
await page.waitForSelector('.words .w .t');
const mn = (await page.$$eval('.words .w .t', (l) => l.map((e) => e.textContent.trim()))).join(' ');
await click('skip verification');
await waitText('receive', 20000);
const base = cacheKeyFor(mn + '\n');
await page.evaluate(([k, pk, ns]) => {
localStorage.setItem(k + ':follows', JSON.stringify({ tags: [['p', pk]], c: '', at: Math.floor(Date.now() / 1000) }));
localStorage.setItem(k + ':feedNotes', JSON.stringify(ns));
localStorage.setItem(k + ':profiles', JSON.stringify({ [pk]: { name: 'Clipper', t: Date.now() } }));
localStorage.setItem('btc-wallet-feed:' + k, 'following');
}, [base, AUTHOR, POSTS]);
await page.reload({ waitUntil: 'domcontentloaded' });
await waitText('receive', 20000);
await page.evaluate(() => document.querySelector('.app-nav-button[aria-label="Feed"]')?.click());
await page.waitForSelector('video.note-video', { timeout: 20000 });
await page.evaluate(() => document.querySelector('video.note-video').scrollIntoView({ block: 'center' }));
const vid = () => page.evaluate(() => { const v = document.querySelector('.notes-feed video.note-video'); return v ? { paused: v.paused, controls: v.controls } : null; });
for (let i = 0; i < 40 && (await vid())?.paused; i++) await sleep(250);
check('the clip plays in the feed, its controls up', (await vid())?.controls === true && !(await vid()).paused, JSON.stringify(await vid()));
const box = () => page.evaluate(() => { const b = document.querySelector('.notes-feed video.note-video').getBoundingClientRect(); return { x: b.x, y: b.y, w: b.width, h: b.height }; });
const touch = async (fx, fy) => { const b = await box(); await page.touchscreen.tap(b.x + b.w * fx, b.y + b.h * fy); await sleep(700); };
const state = () => page.evaluate(() => ({ pagers: document.querySelectorAll('.media-pager').length, thread: !!document.querySelector('.thread-card') }));
const close = async () => { await page.keyboard.press('Escape'); await sleep(800); };
console.log('\n[the phone keeps the click: the tap still opens the pager]');
await page.evaluate(() => { window.__swallow = true; document.addEventListener('click', (e) => { if (window.__swallow && e.target.tagName === 'VIDEO' && e.target.controls) { e.stopImmediatePropagation(); window.__swallowed = (window.__swallowed || 0) + 1; } }, true); });
await touch(0.2, 0.3);
let s = await state();
check('a touch on the picture opens the pager', s.pagers === 1, JSON.stringify(s));
check('…and not the thread', !s.thread);
await close();
check('Escape closes it', (await state()).pagers === 0);
console.log('\n[the player\'s own controls stay its own]');
await page.evaluate(() => document.querySelector('.notes-feed video.note-video').pause()); await sleep(300);
await touch(0.5, 0.5);
check('a touch on the paused clip\'s play button: no pager', (await state()).pagers === 0);
await touch(0.3, 0.97);
check('a touch on the control bar: no pager', (await state()).pagers === 0);
console.log('\n[a drag is a scroll]');
const b = await box();
await page.touchscreen.touchStart(b.x + b.w * 0.3, b.y + b.h * 0.4);
await page.touchscreen.touchMove(b.x + b.w * 0.3, b.y + b.h * 0.4 - 60);
await page.touchscreen.touchEnd();
await sleep(700);
check('a drag across the clip opens nothing', (await state()).pagers === 0);
console.log('\n[where the click does come, the tap is taken once]');
await page.evaluate(() => { window.__swallow = false; });
await page.evaluate(() => document.querySelector('.notes-feed video.note-video').scrollIntoView({ block: 'center' })); await sleep(300);
const hist = await page.evaluate(() => history.length);
await touch(0.2, 0.3);
s = await state();
check('the touch opens one pager', s.pagers === 1, JSON.stringify(s));
check('…the click after it doesn\'t open the thread', !s.thread);
check('…or a second place in history', (await page.evaluate(() => history.length)) - hist <= 1);
await sleep(1200);
check('…or land on the pager (pausing the clip, raising the details)', await page.evaluate(() => {
const p = document.querySelector('.media-pager'); const sc = p && p.querySelector('.mp-scroll');
const cur = sc && [...sc.children].find((x) => Math.abs(x.offsetTop - sc.scrollTop) < 4);
const v = cur && cur.querySelector('video.mp-media');
return !!p && !p.classList.contains('show') && !!v && !v.paused;
}));
await close();
check('no page errors', errs.length === 0, errs.slice(0, 2).join(' | '));
} finally { await browser.close(); server.stop(true); }
console.log(ok ? '\n✅ a touch on a playing clip opens the pager' : '\n❌ failed');
process.exit(ok ? 0 : 1);
|