gitoria
All repositories: gitoria
5.2 KB
// tests/firefox.mjs — a REAL Firefox for the gate (mission 033: the creator uses Firefox and saw full page loads).// Headless Firefox driven over WebDriver BiDi (Firefox's own protocol, `--remote-debugging-port`, ws …/session), no// driver or npm package. Host names are mapped to 127.0.0.1 with the pref `network.dns.localDomains` (exact names).// Clicks are REAL pointer input (input.performActions), not element.click().// const ff = await launchFirefox({ port: 8730, hosts: ['gitoria.test', 'alpha.gitoria.test'] });// await ff.goto(url); await ff.click('#navcode'); await ff.waitFor('expr'); await ff.evaluate('expr'); await ff.close();import { spawn } from 'node:child_process';import { mkdtempSync, writeFileSync, rmSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';const sleep = (ms) => new Promise(r => setTimeout(r, ms));export async function launchFirefox({ port, hosts = [], bin = process.env.HL_FIREFOX || '/usr/bin/firefox' }) {const profile = mkdtempSync(join(tmpdir(), 'gitoria-ff-'));writeFileSync(join(profile, 'user.js'), [`user_pref("network.dns.localDomains", ${JSON.stringify(hosts.join(','))});`,'user_pref("browser.shell.checkDefaultBrowser", false);','user_pref("datareporting.policy.dataSubmissionEnabled", false);','user_pref("toolkit.telemetry.reportingpolicy.firstRun", false);','user_pref("browser.aboutwelcome.enabled", false);','user_pref("network.proxy.type", 0);',].join('\n') + '\n');const proc = spawn(bin, ['--headless', '--no-remote', '--profile', profile, `--remote-debugging-port=${port}`, 'about:blank'], { stdio: ['ignore', 'pipe', 'pipe'] });let out = '';proc.stdout.on('data', d => out += d); proc.stderr.on('data', d => out += d);let ws = null;for (let i = 0; i < 80 && !ws; i++) {await sleep(250);if (!/WebDriver BiDi listening/.test(out)) continue;ws = await new Promise((res) => { const w = new WebSocket(`ws://127.0.0.1:${port}/session`); w.onopen = () => res(w); w.onerror = () => res(null); });}if (!ws) { try { proc.kill('SIGKILL'); } catch {} rmSync(profile, { recursive: true, force: true }); throw new Error('firefox did not open BiDi\n' + out.slice(-800)); }let next = 0; const pending = new Map();const errors = [];ws.onmessage = (ev) => {const m = JSON.parse(ev.data);if (m.id != null && pending.has(m.id)) { const p = pending.get(m.id); pending.delete(m.id); m.type === 'error' ? p.rej(new Error(m.error + ': ' + m.message)) : p.res(m.result); return; }if (m.method === 'log.entryAdded' && m.params && m.params.level === 'error') errors.push(m.params.text || '');};const send = (method, params = {}) => new Promise((res, rej) => { const id = ++next; pending.set(id, { res, rej }); ws.send(JSON.stringify({ id, method, params })); });await send('session.new', { capabilities: {} });await send('session.subscribe', { events: ['log.entryAdded'] });const tree = await send('browsingContext.getTree', {});const context = tree.contexts[0].context;const evaluate = async (expression) => {const r = await send('script.evaluate', { expression, target: { context }, awaitPromise: true, resultOwnership: 'none' });if (r.type === 'exception') throw new Error('evaluate: ' + (r.exceptionDetails && r.exceptionDetails.text));// primitives only (string, number, boolean, null): an expression that wants to hand back more returns JSON.stringify(…)const v = r.result; return v && 'value' in v && typeof v.value !== 'object' ? v.value : null;};const waitFor = async (expression, { timeout = 15000, label = expression } = {}) => {const end = Date.now() + timeout;while (Date.now() < end) { try { const v = await evaluate(expression); if (v) return v; } catch {} await sleep(100); }throw new Error('firefox waitFor timed out: ' + label);};return {context, errors,evaluate,waitFor,goto: (url) => send('browsingContext.navigate', { context, url, wait: 'complete' }),setCookie: (name, value, domain) => send('storage.setCookie', { cookie: { name, value: { type: 'string', value }, domain, path: '/' } }),async click(selector, { timeout = 15000 } = {}) {const box = await waitFor(`(() => { const el = document.querySelector(${JSON.stringify(selector)}); if (!el) return null; el.scrollIntoView({ block: 'center' }); const b = el.getBoundingClientRect(); return b.width ? JSON.stringify({ x: Math.round(b.left + b.width / 2), y: Math.round(b.top + b.height / 2) }) : null; })()`, { timeout, label: 'click ' + selector });const { x, y } = JSON.parse(box);await send('input.performActions', { context, actions: [{ type: 'pointer', id: 'mouse', parameters: { pointerType: 'mouse' }, actions: [{ type: 'pointerMove', x, y }, { type: 'pointerDown', button: 0 }, { type: 'pointerUp', button: 0 }] }] });},async close() {try { await send('session.end', {}); } catch {}try { ws.close(); } catch {}try { proc.kill('SIGTERM'); } catch {}await new Promise(r => { if (proc.exitCode !== null || proc.signalCode !== null) return r(); proc.once('exit', r); setTimeout(() => { try { proc.kill('SIGKILL'); } catch {} r(); }, 4000); });rmSync(profile, { recursive: true, force: true });},};}
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- ce08920dMerge branch 'w52c-header-phone' into mainGitoria
- 0bab9e46|||PR header: ident status wraps on phones instead of clippingmre
- e7af3eceMerge branch 'w52b-approval-followup' into mainGitoria
- c01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mre
- 841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria
- 80307ae2|||PR pulls page cache: live PR numbers, complete scans onlymre
- 69141f65|||PR pulls page: cache the git part per repo by branch tipsmre
- 155861f6Merge branch 'w52-person-approval' into mainGitoria
- b5435de8|||PR person approvals: second Kunibert round (#52)mre
- 3a8b2fb8|||PR person approvals: Kunibert review fixes (#52)mre
- 2a89d8d0|||PR person approvals: Approve button for people; merge needs the required checks or a listed person's approval (#52)mre
- 83f0c953Report person approval verificationmre
- 3ae94b86Record browser failures and verify owned tab cleanupmre
- 54eec687Gate person approvals and merge alternativesmre
- 12b08829Add person approvals for pull request headsmre
- 55c84779Merge branch 'gp-gate-ports' into mainGitoria
- 57af42bbREADME: point to deploy.sh for the port listmre
- d63375d2README: gate commands with explicit portsmre
- feeaa701|||PR gates: explicit ports only, deploy.sh passes themmre
- 6cc1f943Merge branch 'records-1112' into mainGitoria