notes
All repositories: gitoria
7.6 KB
// Confirmation in the creator's existing Chrome; one owned tab, closed in finally.import { spawn } from 'node:child_process';import { mkdirSync, rmSync, writeFileSync, existsSync } from 'node:fs';import { resolve, join } from 'node:path';import { fileURLToPath } from 'node:url';import { launchBrowser, sleep } from './cdp.mjs';import { startIdent } from './identkit.mjs';const app = resolve(fileURLToPath(new URL('..', import.meta.url)));const identDir = process.env.NOTES_GATE_IDENT_DIR || [resolve(app, '../ident.worldapi.org'), '/media/STORAGE/projects/WorldAPI/ident.worldapi.org'].find(d => existsSync(join(d, 'project.hl')));const scratch = join(app, '.scratch/proof');const port = Number(process.env.NOTES_GATE_PORT || 8800);const identPort = Number(process.env.NOTES_GATE_IDENT_PORT || 8801);const base = `http://loreana.tailfd3886.ts.net:${port}`;const api = `http://127.0.0.1:${port}`;process.env.HL_CHROME_ARGS = `--host-resolver-rules=MAP *.notes.test 127.0.0.1, MAP notes.test 127.0.0.1|--unsafely-treat-insecure-origin-as-secure=${base}`;rmSync(scratch, { recursive: true, force: true });mkdirSync(scratch, { recursive: true });let ident, server, browser, log = '', failures = 0, passes = 0;const check = (name, ok, detail) => { console.log((ok ? 'ok ' : 'FAIL ') + name + ' ' + JSON.stringify(detail)); ok ? passes++ : failures++; };try {ident = await startIdent({ identDir, workDir: join(scratch, 'ident'), port: identPort });const alice = await ident.signIn('[email protected]');const registered = await ident.registerApp(alice, 'PR7 regressions', [base]);server = spawn(join(app, 'bin/hybriel'), ['project.hl'], {cwd: app, env: { ...process.env, NOTES_PORT: String(port), NOTES_STORAGE: join(scratch, 'data'), NOTES_SESSIONS: join(scratch, 'sessions') + '/', NOTES_WATCH: '0', NOTES_PUBLIC_URL: base, IDENT_URL: `http://ident.notes.test:${identPort}`, IDENT_EXCHANGE_URL: ident.base, IDENT_API_KEY: registered.key, IDENT_API_SECRET: registered.secret },stdio: ['ignore', 'pipe', 'pipe'],});server.stdout.on('data', d => log += d); server.stderr.on('data', d => log += d);let up = false;for (const end = Date.now() + 180000; Date.now() < end && !up;) {try { up = (await fetch(api + '/')).ok; } catch {}if (!up) await sleep(250);}if (!up) throw new Error('server did not start: ' + log);const code = await ident.selectorCode(alice, registered, base);const login = await fetch(api + '/login/callback?ident_code=' + encodeURIComponent(code), { redirect: 'manual' });const cookie = login.headers.get('set-cookie')?.split(';')[0];if (login.status !== 302 || !cookie) throw new Error('login failed');const save = async (text, fields = {}) => {const res = await fetch(api + '/api/notes', { method: 'POST', headers: { cookie, 'content-type': 'application/json' }, body: JSON.stringify({ op: 'save', id: '', local: '', text, base: 0, at: Date.now(), ...fields }) });const value = await res.json();if (res.status !== 200 || !value.id) throw new Error('save failed: ' + JSON.stringify(value));return value;};writeFileSync(join(scratch, 'server.html'), await (await fetch(api + '/', {headers:{cookie}})).text())console.log('Processes', {ident:ident.proc.pid, server:server.pid})const original = await save('Original note', { at: Date.now() - 10000 });const other = await save('Other note');process.env.HL_CDP_URL = 'http://[::1]:9223'browser = await launchBrowser({ debugPortRange: [8803, 8810] });const page = await browser.newPage();await page.send('Emulation.setDeviceMetricsOverride', { width: 1280, height: 900, deviceScaleFactor: 1, mobile: false });await page.send('Network.enable');await page.send('Network.setCookie', { name: 'notessid', value: cookie.slice('notessid='.length), url: base });const ready = async () => page.waitFor('window.__hl?.socket?.readyState === 1 && !window.__hl.navigating', { timeout: 120000 });const standing = async (path) => page.waitFor(`location.pathname === ${JSON.stringify(path)} && !window.__hl.navigating && window.__hl.page.instance.sel === ${JSON.stringify(path === '/' ? '' : path.slice(6))}`, { timeout: 30000 });const text = () => page.evaluate(`document.querySelector('#notetext input[name=text]')?.value`);const replaceText = async (value) => {await page.waitFor(`!!document.querySelector('#notetext md-editor article > *')`, { timeout: 30000 });if (!await page.evaluate("!!document.querySelector('#notetext textarea')?.getBoundingClientRect().width")) await page.click('#notetext button.source-switch');await page.waitFor(`document.querySelector('#notetext textarea').getBoundingClientRect().width > 0`);await page.click('#notetext textarea');await page.send('Input.dispatchKeyEvent', { type: 'rawKeyDown', key: 'a', code: 'KeyA', windowsVirtualKeyCode: 65, modifiers: 2 });await page.send('Input.dispatchKeyEvent', { type: 'keyUp', key: 'a', code: 'KeyA', windowsVirtualKeyCode: 65, modifiers: 2 });await page.send('Input.insertText', { text: value });};const shot = async (name) => { const r = await page.send('Page.captureScreenshot', { format: 'png', captureBeyondViewport: true }); writeFileSync(join(scratch, name + '.png'), Buffer.from(r.data, 'base64')); };await page.captureWs()await page.goto(base + '/note/' + original.id); await ready()for (const [width, height] of [[390,844],[800,1280],[1280,800],[1280,900]]) {await page.send('Emulation.setDeviceMetricsOverride', {width,height,deviceScaleFactor:1,mobile:width<600})await replaceText('Typed at ' + width + 'x' + height)await page.waitFor('window.__hl.page.instance.edits.length === 0', {timeout:30000})check('typed text saved at ' + width + 'x' + height, await text() === 'Typed at ' + width + 'x' + height)await shot('proof-' + width + 'x' + height)}delete process.env.HL_CDP_URLconst second = await launchBrowser({debugPortRange:[8803,8810]})try {const tab = await second.newPage()await tab.send('Network.enable')await tab.send('Network.setCookie', {name:'notessid',value:cookie.slice('notessid='.length),url:base})await tab.goto(base + '/note/' + original.id, {timeout:120000})await tab.waitFor('window.__hl?.socket?.readyState === 1',{timeout:120000})await tab.click('#notetext button.source-switch')await tab.click('#notetext textarea')await tab.send('Input.dispatchKeyEvent',{type:'rawKeyDown',key:'a',code:'KeyA',windowsVirtualKeyCode:65,modifiers:2})await tab.send('Input.dispatchKeyEvent',{type:'keyUp',key:'a',code:'KeyA',windowsVirtualKeyCode:65,modifiers:2})await tab.send('Input.insertText',{text:'Pushed from a second tab'})await page.waitFor("document.querySelector('#notetext input[name=text]').value === 'Pushed from a second tab'",{timeout:30000})check('second tab edit arrives by push',true)} finally { await second.close() }server.kill('SIGSTOP')try {await page.waitFor("document.querySelector('#syncstate').textContent.includes('Offline')", {timeout:90000})await shot('proof-offline')check('server stop shows offline note',true)} finally { server.kill('SIGCONT') }await page.waitFor("document.querySelector('#syncstate').textContent === ''",{timeout:120000})check('server recovery clears offline note',true)} catch (e) { failures++; console.log('FAIL crashed ' + e.stack + '\n' + log.slice(-2000)); }finally {if (browser) { for (const p of browser.pages) await p.close(); browser.conn.close(); }if (server) { server.kill('SIGTERM'); await new Promise(r => { if (server.exitCode !== null || server.signalCode !== null) return r(); server.once('exit', r); }); }if (ident) await ident.stop();console.log(`${passes} passed, ${failures} failed`);process.exitCode = failures ? 1 : 0;}
Branches
Latest commits
- 8342cd28Merge branch 'notes9-no-poll' into mainAtlas
- 1f0457a5|||PR make provisional rejected-draft setup deterministicAtlas
- 996768bf|||PR WIP sequence note creation before provisional-id adoptionAtlas
- c49ce093|||PR WIP remove notes polling and use framework reachabilityAtlas
- c028ac61Merge branch 'notes-pr7-kunibert' into mainAtlas
- 9cc92bdfKeep conflict and history fixes while awaiting queued save supportAtlas
- a5c27cb6Allow real edits back to earlier text and honor configured identAtlas
- 15c46ced|||PR Preserve note edits, conflict bases and visible historyAtlas
- 4c6e0bf6Merge branch 'notes13-day61' into mainAtlas
- eb51864f|||PR Back/Forward via navigate, server drafts, Hybriel day61Atlas
- 3537cf7eMerge branch 'vendor-day59' into mainAtlas
- c13d188b|||PR Vendor Hybriel day59Atlas
- 24c95433Merge branch 'notes17-ghost-note' into mainAtlas
- ae1075b2Preserve rejected drafts during note ID adoptionAtlas
- 383cce23|||PR Prevent saved local edits from restoring deleted notesAtlas
- 99ca1cf6Merge branch 'vendor-day56' into mainAtlas
- 649cef37|||PR Vendor Hybriel day56Atlas
- aadd0abcMerge branch 'hl-standard-2' into mainAtlas
- 52ffac9f|||PR Keyboard and reload checks for the notes gate (HL-STANDARD F21, F5)Atlas
- 617ee8c0Merge branch 'hl-standard' into mainAtlas