Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119// Drives the running app from Electron's main process (mollusc's// MOLLUSC_SMOKE_SCRIPT): pairs with the mock cloud, makes a notebook, draws a// stroke with a synthetic pen, saves, adds a page, uploads a PDF and opens it// in the PDF editor. What it returns is printed by the smoke run; run.mjs// then checks what reached the cloud.const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
export default async function drive ({ win }) { const js = (code) => win.webContents.executeJavaScript(`(async () => { ${code} })()`, true); // mollusc gives the whole drive 20 s, so every wait is short const waitFor = async (expr, what, ms = 8000) => { const deadline = Date.now() + ms; while (Date.now() < deadline) { if (await js(`return Boolean(${expr});`)) return; await sleep(100); } throw new Error(`timed out waiting for ${what}`); }; const report = {}; const code = process.env.NOTABLE_E2E_CODE; const pdfPath = process.env.NOTABLE_E2E_PDF; // NOTABLE_E2E_SHOTS names a directory to drop screenshots into, for a look const shots = process.env.NOTABLE_E2E_SHOTS; const shot = async (name) => { if (!shots) return; const { writeFileSync, mkdirSync } = await import('node:fs'); const { join } = await import('node:path'); await sleep(300); mkdirSync(shots, { recursive: true }); writeFileSync(join(shots, `${name}.png`), (await win.webContents.capturePage()).toPNG()); };
await waitFor(`document.querySelector('nb-login input.code')`, 'the login screen'); await shot('1-login'); await js(` const input = document.querySelector('nb-login input.code'); input.value = ${JSON.stringify(code)}; input.dispatchEvent(new Event('input', { bubbles: true })); await new Promise((r) => setTimeout(r, 50)); document.querySelector('nb-login form').requestSubmit(); `); await waitFor(`document.querySelector('nb-library')`, 'the library after pairing'); report.pairedAs = await js(`return document.querySelector('notable-app').status.user.email;`);
// A notebook, through the same prompt the user gets. await js(` const app = document.querySelector('notable-app'); void app.newNotebook(); await new Promise((r) => setTimeout(r, 150)); const form = document.querySelector('dialog.prompt form'); form.elements.namedItem('value').value = 'Smoke notebook'; form.requestSubmit(); `); await waitFor(`document.querySelector('nb-notebook svg.page')`, 'the notebook editor'); report.template = await js(`return document.querySelector('nb-notebook select.template').value;`);
// A stroke from a synthetic pen. await js(` const svg = document.querySelector('nb-notebook svg.page'); const r = svg.getBoundingClientRect(); const ev = (type, fx, fy) => svg.dispatchEvent(new PointerEvent(type, { bubbles: true, cancelable: true, pointerId: 7, pointerType: 'pen', isPrimary: true, button: type === 'pointerdown' ? 0 : -1, buttons: type === 'pointerup' ? 0 : 1, pressure: type === 'pointerup' ? 0 : 0.6, clientX: r.left + r.width * fx, clientY: r.top + r.height * fy, })); ev('pointerdown', 0.3, 0.3); for (let i = 1; i <= 24; i++) ev('pointermove', 0.3 + i * 0.015, 0.3 + Math.sin(i / 3) * 0.04); ev('pointerup', 0.66, 0.3); `); await waitFor(`document.querySelector('nb-notebook').dirty === true`, 'the stroke to register'); report.inkElements = await js(`return document.querySelector('nb-notebook g.ink').childElementCount;`); await js(`await document.querySelector('nb-notebook').save();`); await waitFor(`document.querySelector('nb-notebook').dirty === false && !document.querySelector('nb-notebook').saving`, 'the save'); await shot('2-notebook'); report.saveState = await js(`return document.querySelector('nb-notebook .save-state').textContent.trim();`); report.error = await js(`return document.querySelector('nb-notebook').error;`);
await js(`await document.querySelector('nb-notebook').addPage();`); report.pages = await js(`return document.querySelector('nb-notebook').info.pages.length;`); report.pageIndex = await js(`return document.querySelector('nb-notebook').pageIndex;`); report.pageError = await js(`return document.querySelector('nb-notebook').error;`);
await js(`await document.querySelector('notable-app').closeDoc();`); await waitFor(`document.querySelector('nb-library .card.doc')`, 'the library with the notebook'); report.cards = await js(`return document.querySelectorAll('nb-library .card.doc').length;`); await shot('3-library');
// A PDF, uploaded through the sidecar the way the page does it, then opened. await js(` const s = window.mollusc.env; const res = await fetch(s.sidecarOrigin + '/upload', { method: 'POST', headers: { 'content-type': 'application/json', authorization: 'Bearer ' + s.token }, body: JSON.stringify({ paths: [${JSON.stringify(pdfPath)}], parent: '' }), }); if (!res.ok) throw new Error('upload: ' + res.status + ' ' + (await res.text())); const app = document.querySelector('notable-app'); await app.refresh(true); const doc = app.docs.find((d) => d.fileType === 'pdf'); if (!doc) throw new Error('the PDF is not in the library'); await app.openDoc(doc); `); report.pdfArea = await js(`const w = document.querySelector('nb-pdf .pdf-wrap'); return w ? [w.clientWidth, w.clientHeight] : null;`); await waitFor(`document.querySelector('nb-pdf .pdfViewer .page canvas')`, `the PDF to render (viewer area ${JSON.stringify(report.pdfArea)})`, 10000); report.pdfPages = await js(`return document.querySelector('nb-pdf').pageCount;`); await js(`document.querySelector('nb-pdf').setMode('ink');`); await waitFor(`document.querySelector('nb-pdf').mode === 'ink' && document.querySelector('nb-pdf .annotationEditorLayer')`, 'the ink editor layer'); await shot('4-pdf'); report.pdfSaved = await js(` const el = document.querySelector('nb-pdf'); const bytes = await el.pdfDocument.saveDocument(); return bytes.length; `); report.pdfError = await js(`return document.querySelector('nb-pdf').error;`); await js(`await document.querySelector('notable-app').closeDoc();`); report.title = await js(`return document.title;`); return report;}