Files
edentext/tests/smoke/run.mjs
T
Steffen Becker 026f6b6d83 test: headless-Chromium smoke test for the UI layer
tests/smoke/run.mjs boots the production build in Playwright Chromium and
checks what jsdom cannot: the bundle boots, typing and the bold shortcut
reach ProseMirror, autosave survives a reload, a corpus ODT imports and
paginates; any uncaught page error fails the run. Wired as npm run
test:smoke and a CI job (build + chromium install), playwright-core is
now a devDependency.
2026-08-15 21:58:47 +02:00

82 lines
3.7 KiB
JavaScript

// Browser smoke test: boots the production build in headless Chromium and checks the
// paths jsdom cannot — the bundle boots, typing works, autosave survives a reload,
// a corpus document imports and paginates. Fails on any uncaught page error.
import { spawn, execSync } from 'node:child_process';
import { existsSync } from 'node:fs';
import { join, resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { chromium } from 'playwright-core';
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '../..');
const PORT = +(process.env.SMOKE_PORT ?? 4180);
const MOD = process.platform === 'darwin' ? 'Meta' : 'Control';
const failures = [];
const check = (cond, label) => {
console.log(`${cond ? '✓' : '✗'} ${label}`);
if (!cond) failures.push(label);
};
async function previewServer() {
const up = await fetch(`http://localhost:${PORT}/`).then(() => true).catch(() => false);
if (up) return null;
if (!existsSync(join(ROOT, 'dist/index.html'))) {
console.log('no dist/, building…');
execSync('npm run build', { cwd: ROOT, stdio: 'inherit' });
}
const proc = spawn('npm', ['run', 'preview', '--', '--port', String(PORT), '--strictPort'],
{ cwd: ROOT, stdio: 'ignore', detached: true });
for (let i = 0; i < 60; i++) {
await new Promise((r) => setTimeout(r, 500));
if (await fetch(`http://localhost:${PORT}/`).then(() => true).catch(() => false)) return proc;
}
throw new Error('preview server did not start');
}
const server = await previewServer();
const browser = await chromium.launch({ executablePath: chromium.executablePath(), args: ['--no-sandbox'] });
const page = await browser.newPage({ viewport: { width: 1400, height: 1000 } });
const pageErrors = [];
page.on('pageerror', (err) => pageErrors.push(String(err)));
page.on('dialog', (d) => d.accept());
try {
// Boot on a clean profile.
await page.goto(`http://localhost:${PORT}/`, { waitUntil: 'load' });
await page.evaluate(() => localStorage.clear());
await page.reload({ waitUntil: 'load' });
await page.waitForSelector('.tiptap', { timeout: 15_000 });
check(true, 'app boots, editor mounts');
// Typing and a keyboard command reach ProseMirror.
await page.click('.tiptap');
await page.keyboard.type('Hello smoke ');
await page.keyboard.press(`${MOD}+b`);
await page.keyboard.type('bold');
const strong = await page.evaluate(() => document.querySelector('.tiptap strong')?.textContent);
check(strong === 'bold', 'typing + bold shortcut render');
// Autosave (1s debounce) persists the document across a reload.
await page.waitForFunction(() => (localStorage.getItem('edentext-doc') ?? '').includes('Hello smoke'),
null, { timeout: 10_000 });
await page.reload({ waitUntil: 'load' });
await page.waitForSelector('.tiptap', { timeout: 15_000 });
await page.waitForFunction(() => document.querySelector('.tiptap')?.textContent.includes('Hello smoke'),
null, { timeout: 10_000 });
check(true, 'autosave survives a reload');
// A corpus document imports, renders its table and reports a page count.
await page.setInputFiles('input.file-input', join(ROOT, 'tests/corpus/04-table.odt'));
await page.waitForFunction(() => document.querySelector('.tiptap table td')?.textContent.trim(),
null, { timeout: 30_000 });
const pages = await page.evaluate(() => document.querySelector('.statusbar')?.textContent ?? '');
check(/\d/.test(pages), 'ODT import renders table + statusbar page count');
} catch (err) {
check(false, `smoke run threw: ${err.message ?? err}`);
} finally {
check(pageErrors.length === 0, pageErrors.length ? `no uncaught page errors — got: ${pageErrors.join(' | ')}` : 'no uncaught page errors');
await browser.close();
if (server) process.kill(-server.pid);
}
process.exit(failures.length ? 1 : 0);