#!/usr/bin/env node // End to end: the desktop app (mollusc dev, so Vite and the sidecar too), // against the mock cloud, driven by drive.mjs; then what reached the cloud is // checked. Needs mollusc on the PATH and a display; not part of `npm test`. import { spawn } from 'node:child_process'; import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join, dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { startCloudServer } from '../helpers/cloud-server.js'; import { PageEditor } from '../../lib/index.js'; const here = dirname(fileURLToPath(import.meta.url)); const root = resolve(here, '..', '..'); // A one-page PDF with a line of text, offsets and all. function tinyPdf () { const objects = [ '<< /Type /Catalog /Pages 2 0 R >>', '<< /Type /Pages /Kids [3 0 R] /Count 1 >>', '<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>', null, '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>', ]; const stream = 'BT /F1 24 Tf 72 700 Td (Hello Notable) Tj ET'; objects[3] = `<< /Length ${stream.length} >>\nstream\n${stream}\nendstream`; let out = '%PDF-1.4\n'; const offsets = []; objects.forEach((body, i) => { offsets.push(out.length); out += `${i + 1} 0 obj\n${body}\nendobj\n`; }); const xref = out.length; out += `xref\n0 ${objects.length + 1}\n0000000000 65535 f \n`; for (const o of offsets) out += `${String(o).padStart(10, '0')} 00000 n \n`; out += `trailer\n<< /Root 1 0 R /Size ${objects.length + 1} >>\nstartxref\n${xref}\n%%EOF\n`; return out; } const cloud = await startCloudServer(); const dir = mkdtempSync(join(tmpdir(), 'notable-e2e-')); const pdfPath = join(dir, 'Hello.pdf'); writeFileSync(pdfPath, tinyPdf()); const env = { ...process.env, MOLLUSC_SMOKE: '1', MOLLUSC_SMOKE_WAIT: '3000', MOLLUSC_SMOKE_SCRIPT: join(here, 'drive.mjs'), NOTABLE_CONFIG: join(dir, 'config', 'cloud.json'), NOTABLE_CACHE: join(dir, 'cache'), NOTABLE_CLOUD_HOST: cloud.url, NOTABLE_E2E_CODE: cloud.code, NOTABLE_E2E_PDF: pdfPath, }; console.log(`e2e: mock cloud at ${cloud.url}, scratch in ${dir}`); const child = spawn('mollusc', ['dev', '.'], { cwd: root, env, stdio: ['ignore', 'pipe', 'pipe'] }); let out = ''; child.stdout.on('data', (d) => { out += d; process.stdout.write(d); }); child.stderr.on('data', (d) => { out += d; process.stderr.write(d); }); const code = await new Promise((res) => child.on('exit', res)); let failed = code !== 0; const problems = []; try { const m = out.match(/smoke: .*script: (\{.*\})/); const report = m ? JSON.parse(m[1]) : null; if (!report) problems.push('no report from the drive script'); else { if (report.pairedAs !== 'robin@example.com') problems.push(`paired as ${report.pairedAs}`); if (!(report.inkElements > 0)) problems.push('no ink drawn'); if (report.saveState !== 'Saved') problems.push(`save state ${report.saveState}: ${report.error}`); if (report.pages !== 2) problems.push(`${report.pages} pages after adding one`); if (report.pdfPages !== 1) problems.push(`${report.pdfPages} PDF pages`); if (!(report.pdfSaved > 100)) problems.push('pdf.js did not save the document'); if (report.pdfError) problems.push(`pdf editor: ${report.pdfError}`); } // What the cloud holds now: a page with a stroke, its thumbnail, the PDF. const blobs = [...cloud.state.blobs.values()]; const pages = blobs.filter((b) => /\.rm$/.test(b.name)).map((b) => PageEditor.parse(b.data)); if (!pages.some((p) => p.strokes.length > 0)) problems.push('no page on the cloud holds a stroke'); if (!blobs.some((b) => /\.thumbnails\/.*\.png$/.test(b.name))) problems.push('no thumbnail on the cloud'); if (!blobs.some((b) => /\.pdf$/.test(b.name))) problems.push('no PDF on the cloud'); const withStroke = pages.find((p) => p.strokes.length > 0); if (withStroke) { const authors = [...withStroke.authors.values()]; const line = withStroke.strokes[0].line; console.log(`e2e: stroke of ${line.points.length} points, tool ${line.tool}, by ${authors.join(', ')}`); } } catch (err) { problems.push(err.message); } await cloud.close(); rmSync(dir, { recursive: true, force: true }); if (problems.length) { failed = true; for (const p of problems) console.error(`e2e: ${p}`); } console.log(failed ? 'e2e: FAILED' : 'e2e: ok'); process.exit(failed ? 1 : 0);