From c35b8ab0f0a52f7b358d90ccae8064b7206971bf Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Fri, 23 Oct 2026 04:48:51 +0300 Subject: [PATCH 1/4] feat: h5p-verify --preview, a package in the real player in your browser The check says whether a package starts. Whether it is right, a person judges by playing it, the same way for every content type. - --preview checks nothing. It serves the package and a page of its own from the verifier's server, with the same player, runtime and library sources as the check, and opens the default browser on it. --no-open prints the address instead. - The package is served with Range, as a static host serves it, so the element reads it range-http: the libraries in a few requests, media by the ranges asked for. With a 32 MB stored video added beside the demo's clip, 1.58 MB of 36.1 MB reached ready, and one range of that video cost a kilobyte. - It ends ten seconds after its last tab closes, or ten minutes after starting if no tab opens. It takes port 4645 when that is free, so every preview shares one origin's worker and caches. - The check shares the server: it now takes the caller's routes and a port, and a folder given as the package is refused by name. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 29 +++- docs/compatibility.md | 3 +- docs/verify.md | 9 + packages/verify/README.md | 29 ++++ packages/verify/bin/h5p-verify.mjs | 44 +++++ packages/verify/lib/preview.html | 60 +++++++ packages/verify/lib/preview.mjs | 227 ++++++++++++++++++++++++++ packages/verify/lib/verify.mjs | 161 ++++++++++++------ packages/verify/tests/preview.test.ts | 173 ++++++++++++++++++++ 9 files changed, 681 insertions(+), 54 deletions(-) create mode 100644 packages/verify/lib/preview.html create mode 100644 packages/verify/lib/preview.mjs create mode 100644 packages/verify/tests/preview.test.ts diff --git a/AGENTS.md b/AGENTS.md index 47b0d8ce..0b2014a9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -155,8 +155,11 @@ packages/normalize/ @missing-elements/h5p-normalize — the package rewrit packages/verify/ @missing-elements/h5p-verify — plays a package headless and reports whether it works bin/h5p-verify.mjs the command lib/verify.mjs the static server, the browser run and the report; lib/page.html the page it drives + lib/preview.mjs `--preview`: the package in the person's own browser, served by range + until its last tab has closed; lib/preview.html its page tests/ node tests that run the real thing against the demo's committed content; - package-types.ts as for the normalizer + package-types.ts as for the normalizer; preview.test.ts the preview's + ranges, a browser playing from it, its ending and its command line types/ generated, not in git: the declarations, `VerifyReport` among them packages/cmi5/ @missing-elements/h5p-cmi5 — cmi5 for the element, published as a library @@ -1701,6 +1704,30 @@ driver along and the normalizer should not. content was checked as the site will play it. Writing the test found the demo's own quiz, built in March, older than the pack by a patch or two in four libraries, H5P.Video 1.6.68 against 1.6.79 among them. +- **`--preview` is the person's half of the check** (2026-10-23, #69). The check says whether a + package starts. Whether it is right, a person judges by playing it, the same way for every content + type; an answer key or scripted screenshots would be one more thing to write per type. So + `--preview` checks nothing. It serves the package and a page of its own from the verifier's + server, with the same player, runtime and library sources, and opens the default browser on + it. Three choices in it: + - **The package is served with `Range`, as a static host serves it**, not handed over as a + picked file, which a page cannot get without a click. That means `206`, `Content-Range`, and an + `ETag` from the file's size and time, so a file written again is a new package to the element. + The element reads it `range-http`: the libraries in the warm's few requests, media by the + ranges asked for. Measured on 2026-10-23 with a 32 MB stored video added beside the demo's + clip: of 36.1 MB, 1.58 MB in 9 requests reached `ready`, and a range of that video cost a + kilobyte. Every request was ranged. In Chrome, Firefox and WebKit the clip played. + - **It ends by itself.** The page holds an event stream open. The server ends ten seconds after + the last stream closes, which a reload reconnects within, or ten minutes after starting if + none ever opened. The page stops reconnecting once its server has gone, so a stale tab cannot + keep a later preview on the same port alive. + - **It takes port 4645 when that port is free.** Every preview then shares one origin's worker + and caches, instead of leaving a new origin's behind for each run. When another preview or + another program holds the port, it takes any free one. + + The check's own options are refused with it, not ignored. The browser opener is `open`, + `cmd /c start` or `xdg-open`, and the address is printed either way; it has not been tried on + Windows. Not built, deliberately: steps that click through the content and check the xAPI it sends, and a content digest for reviewing what was generated. A check of library versions against the hub's diff --git a/docs/compatibility.md b/docs/compatibility.md index 6ebfcba4..72e4ef81 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -108,7 +108,8 @@ long as the player streams the result at least as well. ### The verifier, `@missing-elements/h5p-verify` -The command's options, its exit codes — 0 plays, 1 does not, 2 could not run — `report.json`, to +The command's options, its exit codes — 0 plays, 1 does not, 2 could not run; with `--preview`, +0 when the preview ends and 2 when it could not start — `report.json`, to which fields may be added, and `verifyPackage()` and `summarize()`, with their types: `VerifyReport` is `report.json`'s shape, and gains fields the same way. The verifier is a step in other people's pipelines, so a check that turns a pass into a fail comes in a minor, never a patch; a new diff --git a/docs/verify.md b/docs/verify.md index e032a09a..0aebde30 100644 --- a/docs/verify.md +++ b/docs/verify.md @@ -16,3 +16,12 @@ With `--libraries pack`, or a copy of the pack's `parts/`, it plays a library th at the pack's newer version, as a site with the pack will, and says which. It is meant as the last step of a pipeline that generates or rewrites packages; the skill in `skills/h5p-verify/` tells an agent when to run it and how to read the result. + +The check cannot say whether the content is right; a person decides that by playing it. +`--preview` checks nothing and opens the package in the real player in the person's own +browser, served from their computer with `Range`, as a static host serves it. It works for any +content type, and a large video starts at once. It ends a few seconds after its last tab closes: + +```bash +npx @missing-elements/h5p-verify@latest course.h5p --preview +``` diff --git a/packages/verify/README.md b/packages/verify/README.md index 3d92fed4..6e999ba2 100644 --- a/packages/verify/README.md +++ b/packages/verify/README.md @@ -52,10 +52,39 @@ A package built against a library newer than the pack is refused, with what to d come from the pack on jsDelivr unless `--libraries` names another; name a copy on disk, such as `node_modules/@missing-elements/h5p-libraries/parts`, to run the check offline. +## Preview + +The check says whether a package starts. Whether it is right — the answers, a scenario's paths, +what happens at a video's timestamps — is for a person to judge, by playing it. `--preview` +checks nothing and opens the package in the real player in your browser, served from your +computer: + +```bash +npx @missing-elements/h5p-verify@latest course.h5p --preview +``` + +``` +Previewing course.h5p at http://127.0.0.1:4645/ +The package stays on this computer. The preview ends when its tab is closed, or with Ctrl+C. +``` + +It plays any content type the player plays, with the same player, runtime and library sources +as the check, so `--untrusted`, `--libraries` and `--no-refresh-libraries` mean the same. + +The package is served with `Range`, as a static host serves it. The libraries take a few +requests, and a video is read by the ranges the player asks for, so a large one starts at once +and nothing is read whole or extracted to disk. + +The preview ends a few seconds after its last tab closes, so an agent can start it in the +background and leave it. `--no-open` prints the address instead of opening your default browser. +Exit code 0 when it ends, 2 when it could not start. + ## Options | | | |---|---| +| `--preview` | check nothing: open the package in the real player in your browser, served from your computer, until its tab is closed | +| `--no-open` | with `--preview`, print the address instead of opening a browser | | `--untrusted` | play it as a site plays a package it did not make, and report what the content check removed | | `--libraries ` | where libraries come from, tried in order: `pack` (the library pack), a bundle's URL, a bundle on disk, or a copy of the pack's `parts/` folder, which plays as `pack`; the verifier serves what is on disk itself. With the pack, a library the package carries plays at the pack's newer version, as on a site with the pack, and the summary lists each under `refreshed from the pack` | | `--no-refresh-libraries` | play the libraries the package carries even where the pack has newer ones | diff --git a/packages/verify/bin/h5p-verify.mjs b/packages/verify/bin/h5p-verify.mjs index 96e2a240..59bebaa0 100755 --- a/packages/verify/bin/h5p-verify.mjs +++ b/packages/verify/bin/h5p-verify.mjs @@ -1,6 +1,7 @@ #!/usr/bin/env node import { parseArgs } from 'node:util' import { basename, dirname, join, resolve } from 'node:path' +import { openBrowser, previewPackage } from '../lib/preview.mjs' import { VerifyError, summarize, verifyPackage } from '../lib/verify.mjs' const HELP = `Usage: h5p-verify [options] @@ -8,7 +9,14 @@ const HELP = `Usage: h5p-verify [options] Plays the package in a headless browser through h5p-offline-player and reports whether it works. Exit code 0: it plays (warnings possible). 1: it does not. 2: could not run. +With --preview it checks nothing: it opens the package in the real player in your browser, +served from this computer, until its tab is closed. Exit code 0 when the preview ends, 2 when +it could not start. + Options: + --preview open the package in your browser instead of checking it; it takes + --untrusted, --libraries and --no-refresh-libraries + --no-open with --preview, print the address instead of opening a browser --untrusted play it as a site plays a package it did not make: none of its own code runs, its libraries come only from --libraries (pack by default), and its content is checked against their semantics. What @@ -34,6 +42,8 @@ try { args = parseArgs({ allowPositionals: true, options: { + preview: { type: 'boolean' }, + 'no-open': { type: 'boolean' }, libraries: { type: 'string' }, untrusted: { type: 'boolean' }, 'no-refresh-libraries': { type: 'boolean' }, @@ -58,6 +68,40 @@ if (args.values.help || !file) { process.exit(args.values.help ? 0 : 2) } +if (args.values['no-open'] && !args.values.preview) { + console.error('--no-open goes with --preview') + process.exit(2) +} + +if (args.values.preview) { + // What only the check reads is refused rather than ignored, so a mistaken call says so. + const unsupported = ['out', 'browser', 'ready', 'settle', 'json', 'quiet'].filter((name) => args.values[name] !== undefined) + if (unsupported.length) { + console.error(`--preview does not take ${unsupported.map((name) => `--${name}`).join(', ')}`) + process.exit(2) + } + let preview + try { + preview = await previewPackage({ + file, + libraries: args.values.libraries, + untrusted: args.values.untrusted, + refreshLibraries: !args.values['no-refresh-libraries'] + }) + } catch (error) { + console.error(error instanceof VerifyError ? error.message : error) + process.exit(2) + } + const address = `${preview.url}/` + console.log(`Previewing ${basename(file)} at ${address}`) + if (args.values['no-open'] || !(await openBrowser(address))) console.log('Open that address in a browser.') + console.log('The package stays on this computer. The preview ends when its tab is closed, or with Ctrl+C.') + for (const signal of ['SIGINT', 'SIGTERM']) process.once(signal, () => preview.close()) + const ended = await preview.closed + console.log(ended === 'unopened' ? 'No tab opened the preview in time; it ended.' : 'The preview ended.') + process.exit(0) +} + const seconds = (value, fallback) => { if (value === undefined) return fallback const number = Number(value) diff --git a/packages/verify/lib/preview.html b/packages/verify/lib/preview.html new file mode 100644 index 00000000..8343272a --- /dev/null +++ b/packages/verify/lib/preview.html @@ -0,0 +1,60 @@ + + + + + + Preview + + + + +
+

Preview

+ Opening… +
+
+ + + + diff --git a/packages/verify/lib/preview.mjs b/packages/verify/lib/preview.mjs new file mode 100644 index 00000000..5f2a176e --- /dev/null +++ b/packages/verify/lib/preview.mjs @@ -0,0 +1,227 @@ +import { spawn } from 'node:child_process' +import { createReadStream } from 'node:fs' +import { readFile, stat } from 'node:fs/promises' +import { basename, join } from 'node:path' +import { pipeline } from 'node:stream/promises' +import { fileURLToPath } from 'node:url' +import { librarySources, packageFile, playerDist, serve } from './verify.mjs' + +/** @import { IncomingMessage, ServerResponse } from 'node:http' */ + +/** + * `--preview`: the package in the real player, in the person's own browser, served from this + * computer. The check says whether a package starts; whether it is right — the answers, a + * scenario's paths, what happens at a video's timestamps — a person judges by playing it, and + * that is the same for every content type. The package is served as a static host with `Range` + * serves it, so the libraries take a few requests and media the ranges the player asks for: a + * large video starts at once, and nothing is read whole or extracted to disk. It runs until its + * last tab has gone. + */ + +const here = fileURLToPath(new URL('.', import.meta.url)) + +/** + * The port a preview takes when it is free: every preview on one origin, so the browser keeps one + * copy of the player's worker and caches rather than a new one for each run. + */ +const PORT = 4645 +/** Where the page finds the package, under its own file name. */ +const ARCHIVE_ROUTE = '/__package/' +/** The page's open connection, by which the preview knows a tab still shows it. */ +const PRESENCE_ROUTE = '/__preview/events' +/** How often an open tab is written to, so that one the browser dropped without a word is noticed. */ +const HEARTBEAT_MS = 15_000 + +/** + * @typedef {object} PreviewOptions + * @property {string} file the `.h5p` to show + * @property {string} [libraries] where libraries come from, as for the check + * @property {boolean} [untrusted] play it as a site that did not make it plays it + * @property {boolean} [refreshLibraries] with the pack, play its newer versions of the libraries the + * package carries; default true + * @property {number} [port] the port to take; default 4645, and any free one when that is taken + * @property {number} [openWait] ms to wait for a tab before stopping; default ten minutes + * @property {number} [closeGrace] ms the preview outlives its last tab, so that a reload does not end + * it; default 10 s + * @property {(answer: { range: string | null, bytes: number }) => void} [onArchiveRequest] each answer + * for the package: the `Range` asked for, and how many bytes answered it + */ + +/** + * Serves the package and its page until the preview is stopped or stops by itself: `closed` + * settles with `closed` once its last tab has gone, `unopened` when no tab came within `openWait`, + * and `stopped` after `close()`. + * @internal + * @param {PreviewOptions} options + * @returns {Promise<{ url: string, closed: Promise<'closed' | 'unopened' | 'stopped'>, close: () => void }>} + */ +export async function previewPackage(options) { + const { path: file } = await packageFile(options.file) + const sources = librarySources(options.libraries) + const name = basename(file) + const settings = { + name, + src: ARCHIVE_ROUTE + encodeURIComponent(name), + libraries: sources.attribute, + packBase: sources.packBase, + refreshLibraries: options.refreshLibraries !== false, + untrusted: options.untrusted === true + } + // `<` as an escape, so that no file name can end the script block it is written into. + const page = (await readFile(join(here, 'preview.html'), 'utf8')).replace('{{settings}}', () => JSON.stringify(settings).replaceAll('<', '\\u003c')) + const openWait = options.openWait ?? 10 * 60_000 + const closeGrace = options.closeGrace ?? 10_000 + + /** @type {Set} */ + const tabs = new Set() + let seen = false + let stopped = false + /** @type {ReturnType | undefined} */ + let timer + /** @type {(reason: 'closed' | 'unopened' | 'stopped') => void} */ + let settle = () => {} + /** @type {Promise<'closed' | 'unopened' | 'stopped'>} */ + const closed = new Promise((resolveClosed) => { + settle = resolveClosed + }) + + const server = await serve( + await playerDist(), + sources, + async (path, request, response) => { + if (path === '/') { + response.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store' }).end(page) + return true + } + if (path === ARCHIVE_ROUTE + name) { + await sendArchive(file, request, response, options.onArchiveRequest) + return true + } + if (path === PRESENCE_ROUTE) { + response.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-store' }) + response.write(': open\n\n') + tabs.add(response) + watch() + request.on('close', () => { + tabs.delete(response) + watch() + }) + return true + } + return false + }, + options.port ?? PORT + ) + + const heartbeat = setInterval(() => { + for (const tab of tabs) tab.write(': still here\n\n') + }, HEARTBEAT_MS) + + /** @param {'closed' | 'unopened' | 'stopped'} reason */ + function stop(reason) { + if (stopped) return + stopped = true + clearTimeout(timer) + clearInterval(heartbeat) + for (const tab of tabs) tab.end() + server.close() + settle(reason) + } + + // Waits for the first tab; once tabs have come and every one has gone, outlives them by the grace. + function watch() { + clearTimeout(timer) + if (tabs.size > 0) seen = true + else if (!stopped) timer = setTimeout(() => stop(seen ? 'closed' : 'unopened'), seen ? closeGrace : openWait) + } + + watch() + return { url: server.url, closed, close: () => stop('stopped') } +} + +/** + * The package as a static host with `Range` serves it: `206` with the bytes asked for, `200` whole + * otherwise, streamed from disk. Its validator is the file's size and time, so the package written + * again at the same path is a new package to the element rather than its old caches. + * @param {string} file + * @param {IncomingMessage} request + * @param {ServerResponse} response + * @param {PreviewOptions['onArchiveRequest']} report + */ +async function sendArchive(file, request, response, report) { + const { size, mtimeMs } = await stat(file) + const headers = { + 'Content-Type': 'application/zip', + 'Accept-Ranges': 'bytes', + 'Cache-Control': 'no-store', + ETag: `"${size.toString(16)}-${Math.trunc(mtimeMs).toString(16)}"`, + 'Last-Modified': new Date(mtimeMs).toUTCString() + } + const asked = request.headers.range ?? null + const range = byteRange(asked, size) + if (range === false) { + response.writeHead(416, { ...headers, 'Content-Range': `bytes */${size}` }).end() + report?.({ range: asked, bytes: 0 }) + return + } + const { start, end } = range ?? { start: 0, end: size - 1 } + const length = end - start + 1 + response.writeHead(range ? 206 : 200, { + ...headers, + 'Content-Length': length, + ...(range ? { 'Content-Range': `bytes ${start}-${end}/${size}` } : {}) + }) + const body = request.method !== 'HEAD' && length > 0 + report?.({ range: asked, bytes: body ? length : 0 }) + if (!body) { + response.end() + return + } + // A media element drops a range request whenever it seeks; the read ends with it. + await pipeline(createReadStream(file, { start, end }), response).catch(() => {}) +} + +/** + * The one range a `Range` header asks for, read as a static host reads it: `null` when it asks + * for none, for one this does not parse, or for several, each answered with the whole file; `false` + * when it starts past the end, which is a `416`. + * @internal + * @param {string | null} header + * @param {number} size + * @returns {{ start: number, end: number } | null | false} + */ +export function byteRange(header, size) { + const match = /^bytes=(\d*)-(\d*)$/.exec(header?.trim() ?? '') + if (!match || (!match[1] && !match[2])) return null + if (!match[1]) { + // The last n bytes. + const suffix = Number(match[2]) + return suffix > 0 && size > 0 ? { start: Math.max(0, size - suffix), end: size - 1 } : false + } + const start = Number(match[1]) + // A last byte before the first is no range at all; one past the end is the end. + if (match[2] && Number(match[2]) < start) return null + if (start >= size) return false + return { start, end: match[2] ? Math.min(Number(match[2]), size - 1) : size - 1 } +} + +/** + * Opens `url` in the default browser. False when no opener could be started, `xdg-open` missing on + * a server for one; the command prints the address either way. + * @internal + * @param {string} url + * @returns {Promise} + */ +export function openBrowser(url) { + const windows = process.platform === 'win32' + /** @type {[string, string[]]} */ + const [command, args] = process.platform === 'darwin' ? ['open', [url]] : windows ? ['cmd', ['/c', 'start', '""', url]] : ['xdg-open', [url]] + return new Promise((resolveOpened) => { + const child = spawn(command, args, { stdio: 'ignore', detached: true, windowsHide: true, windowsVerbatimArguments: windows }) + child.once('error', () => resolveOpened(false)) + child.once('spawn', () => { + child.unref() + resolveOpened(true) + }) + }) +} diff --git a/packages/verify/lib/verify.mjs b/packages/verify/lib/verify.mjs index eeb37c9a..784cdc88 100644 --- a/packages/verify/lib/verify.mjs +++ b/packages/verify/lib/verify.mjs @@ -7,6 +7,7 @@ import { fileURLToPath } from 'node:url' import { chromium, errors as playwrightErrors } from 'playwright-core' /** @import { H5PPlayerElement, PlayerErrorDetail, PlayerState, RefreshedLibrary } from '@missing-elements/h5p-offline-player' */ +/** @import { IncomingMessage, ServerResponse } from 'node:http' */ /** * Plays a package the way a learner would — the real element, the real Service Worker, a real @@ -143,21 +144,17 @@ export async function verifyPackage(options) { * @typedef {{ started: number | null, states: { state: PlayerState, at: number }[], errors: (ReportedError & { at: number })[], xapi: (string | null)[] }} PageReport * @typedef {Window & typeof globalThis & { __verify: PageReport, __csp?: BlockedLoad[], H5P?: { instances?: unknown[] }, H5PIntegration?: { contents?: Record } }} PageWindow */ - const file = resolve(options.file) - let size - try { - size = (await stat(file)).size - } catch (error) { - // Only a missing file is "no such file"; a file that cannot be read says why. - const code = error && typeof error === 'object' && 'code' in error ? error.code : undefined - throw new VerifyError(code === 'ENOENT' ? `No such file: ${file}` : `Cannot read ${file}: ${error instanceof Error ? error.message : String(error)}`) - } + const { path: file, size } = await packageFile(options.file) const readyTimeout = options.readyTimeout ?? 60_000 const settle = options.settle ?? 3_000 const untrusted = options.untrusted === true const sources = librarySources(options.libraries) - const server = await serve(await playerDist(), sources.files, sources.packDir) + const server = await serve(await playerDist(), sources, async (path, _request, response) => { + if (path !== '/') return false + await sendFile(join(here, 'page.html'), response) + return true + }) const browser = await launch(options.browser) try { const context = await browser.newContext({ viewport: { width: 1100, height: 800 } }) @@ -204,7 +201,7 @@ export async function verifyPackage(options) { const query = new URLSearchParams() if (sources.attribute) query.set('libraries', sources.attribute) - if (sources.packDir) query.set('pack-base', PACK_ROUTE) + if (sources.packBase) query.set('pack-base', sources.packBase) if (options.refreshLibraries === false) query.set('refresh-libraries', 'off') if (untrusted) query.set('untrusted', '') await page.goto(`${server.url}/${query.size ? `?${query}` : ''}`) @@ -355,21 +352,44 @@ export async function verifyPackage(options) { export class VerifyError extends Error {} +/** + * The package's absolute path and size. A missing file, a folder, or a file that cannot be read + * says which, before anything starts. + * @internal + * @param {string} file + * @returns {Promise<{ path: string, size: number }>} + */ +export async function packageFile(file) { + const path = resolve(file) + let found + try { + found = await stat(path) + } catch (error) { + // Only a missing file is "no such file"; a file that cannot be read says why. + const code = error && typeof error === 'object' && 'code' in error ? error.code : undefined + throw new VerifyError(code === 'ENOENT' ? `No such file: ${path}` : `Cannot read ${path}: ${error instanceof Error ? error.message : String(error)}`) + } + if (!found.isFile()) throw new VerifyError(`Not a file: ${path}`) + return { path, size: found.size } +} + /** Where the verifier serves a copy of the pack named on disk, which the element's `pack-base` names. */ const PACK_ROUTE = '/__pack/' /** * `libraries` as the element reads it, with each bundle on disk given a route on the verifier's - * own server, and a folder on disk — a copy of the pack's `parts/` — served as the pack: `pack` - * and URLs pass as they are. A path that names nothing is an error before anything starts. + * own server, and a folder on disk — a copy of the pack's `parts/` — served as the pack, which + * `packBase` then names: `pack` and URLs pass as they are. A path that names nothing is an error + * before anything starts. + * @internal * @param {string | undefined} value */ -function librarySources(value) { +export function librarySources(value) { /** @type {Map} */ const files = new Map() /** @type {string | null} */ let packDir = null - if (!value) return { attribute: null, files, packDir } + if (!value) return { attribute: null, files, packDir, packBase: null } const tokens = value.split(/\s+/).filter(Boolean).map((token) => { if (token === 'pack' || /^https?:\/\//i.test(token)) return token const path = resolve(token) @@ -384,7 +404,7 @@ function librarySources(value) { files.set(route, path) return route }) - return { attribute: [...new Set(tokens)].join(' '), files, packDir } + return { attribute: [...new Set(tokens)].join(' '), files, packDir, packBase: packDir ? PACK_ROUTE : null } } /** @@ -547,8 +567,9 @@ function dedupe(messages) { * The player's `dist/` and the runtime's, from the installed packages; the workspace needs both * built first. The runtime is a package of its own (`@missing-elements/h5p-runtime`, GPL) and is * served at `/frame-assets/`, which is where the element looks beside its own script. + * @internal */ -async function playerDist() { +export async function playerDist() { const element = require.resolve('@missing-elements/h5p-offline-player') const dist = dirname(element) if (!existsSync(join(dist, 'h5p-sw.js'))) { @@ -562,58 +583,94 @@ async function playerDist() { } /** - * A static server for the player, the runtime, the test page and any library bundle named on - * disk, on a free localhost port. + * A server on localhost for the player, the runtime and any library bundle named on disk. `own` + * answers first, for the paths its caller adds — the check's page; a preview's page, package and + * presence — and says whether it did. + * @internal * @param {{ dist: string, runtime: string }} roots - * @param {Map} bundles routes to bundle files - * @param {string | null} packDir a copy of the pack's `parts/`, served under `PACK_ROUTE` + * @param {{ files: Map, packDir: string | null }} sources bundles on disk by route, and a + * copy of the pack's `parts/`, served under `PACK_ROUTE` + * @param {(path: string, request: IncomingMessage, response: ServerResponse) => boolean | Promise} own + * @param {number} [port] the port to take; any free one when it is taken, or with 0 * @returns {Promise<{ url: string, close: () => void }>} */ -function serve({ dist, runtime }, bundles = new Map(), packDir = null) { - const page = join(here, 'page.html') +export function serve({ dist, runtime }, { files: bundles, packDir }, own, port = 0) { const ASSETS = '/frame-assets/' const server = createServer(async (request, response) => { - const path = decodeURIComponent(new URL(request.url ?? '/', 'http://localhost').pathname) - /** - * @param {string} root - * @param {string} rest - */ - const safe = (root, rest) => join(root, normalize(rest).replace(/^(\.\.[/\\])+/, '')) - const bundle = bundles.get(path) - const inPack = packDir !== null && path.startsWith(PACK_ROUTE) - const target = - bundle ?? - (path === '/' ? page : inPack ? safe(packDir, path.slice(PACK_ROUTE.length)) : path.startsWith(ASSETS) ? safe(runtime, path.slice(ASSETS.length)) : safe(dist, path)) - if (target !== page && !bundle && !target.startsWith(dist) && !target.startsWith(runtime) && !(inPack && target.startsWith(packDir))) { - response.writeHead(403).end() - return - } try { - const body = await readFile(target) - response.writeHead(200, { - 'Content-Type': MIME[extname(target)] ?? 'application/octet-stream', - 'Content-Length': body.length, - 'Cache-Control': 'no-store' - }) - response.end(body) - } catch { - response.writeHead(404, { 'Content-Type': 'text/plain' }).end('not found') + const path = decodeURIComponent(new URL(request.url ?? '/', 'http://localhost').pathname) + if (await own(path, request, response)) return + /** + * @param {string} root + * @param {string} rest + */ + const safe = (root, rest) => join(root, normalize(rest).replace(/^(\.\.[/\\])+/, '')) + const bundle = bundles.get(path) + const inPack = packDir !== null && path.startsWith(PACK_ROUTE) + const target = + bundle ?? (inPack ? safe(packDir, path.slice(PACK_ROUTE.length)) : path.startsWith(ASSETS) ? safe(runtime, path.slice(ASSETS.length)) : safe(dist, path)) + if (!bundle && !target.startsWith(dist) && !target.startsWith(runtime) && !(inPack && target.startsWith(packDir))) { + response.writeHead(403).end() + return + } + await sendFile(target, response) + } catch (error) { + // A path that does not decode, or a file that went away mid-answer: the server goes on. + if (!response.headersSent) response.writeHead(error instanceof URIError ? 400 : 500).end() + else response.destroy() } }) - return new Promise((resolveServer) => { - server.listen(0, '127.0.0.1', () => { - const { port } = /** @type {import('node:net').AddressInfo} */ (server.address()) - resolveServer({ url: `http://127.0.0.1:${port}`, close: () => server.close() }) + return new Promise((resolveServer, rejectServer) => { + server.on('error', (error) => { + // The port asked for is taken, by another preview or another program: any free one will do. + if (/** @type {NodeJS.ErrnoException} */ (error).code === 'EADDRINUSE' && port !== 0) { + port = 0 + server.listen(0, '127.0.0.1') + } else rejectServer(error) + }) + server.once('listening', () => { + const { port: bound } = /** @type {import('node:net').AddressInfo} */ (server.address()) + resolveServer({ + url: `http://127.0.0.1:${bound}`, + // A connection left open — a browser's keep-alive, a preview tab's presence — would hold it. + close: () => { + server.closeAllConnections() + server.close() + } + }) }) + server.listen(port, '127.0.0.1') }) } +/** + * A file whole, with its type and no caching, or a 404. + * @param {string} target + * @param {ServerResponse} response + */ +async function sendFile(target, response) { + let body + try { + body = await readFile(target) + } catch { + response.writeHead(404, { 'Content-Type': 'text/plain' }).end('not found') + return + } + response.writeHead(200, { + 'Content-Type': MIME[extname(target)] ?? 'application/octet-stream', + 'Content-Length': body.length, + 'Cache-Control': 'no-store' + }) + response.end(body) +} + /** * A Chromium: the one named, else Chrome or Edge installed on the machine, else Playwright's * own. Chrome and Edge first because they are usually there and cost no download. + * @internal * @param {string | undefined} executablePath */ -async function launch(executablePath) { +export async function launch(executablePath) { /** @type {import('playwright-core').LaunchOptions[]} */ const attempts = executablePath ? [{ executablePath }] diff --git a/packages/verify/tests/preview.test.ts b/packages/verify/tests/preview.test.ts new file mode 100644 index 00000000..41df5f8d --- /dev/null +++ b/packages/verify/tests/preview.test.ts @@ -0,0 +1,173 @@ +import { spawn } from 'node:child_process' +import { once } from 'node:events' +import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join, resolve } from 'node:path' +import { BlobReader, BlobWriter, Uint8ArrayReader, Uint8ArrayWriter, ZipReader, ZipWriter } from '@zip.js/zip.js' +import type { H5PPlayerElement } from '@missing-elements/h5p-offline-player' +import type { Browser } from 'playwright-core' +import { afterAll, beforeAll, describe, expect, it } from 'vitest' +import { byteRange, previewPackage } from '../lib/preview.mjs' +import { launch } from '../lib/verify.mjs' + +/** + * `--preview` end to end: its server answers for the package as a static host with `Range` does, + * a real browser plays the package from it by range, and it ends once its last tab has gone. + */ + +const content = resolve(import.meta.dirname, '../../../apps/demo/demo/content') +const quiz = join(content, 'quiz.h5p') +const video = join(content, 'video.h5p') +const bin = resolve(import.meta.dirname, '../bin/h5p-verify.mjs') +/** A stored video that nothing plays, large enough that reading it whole would show. */ +const UNWATCHED = 'content/videos/unwatched.mp4' +const UNWATCHED_SIZE = 32 * 1024 * 1024 +let dir: string +let browser: Browser + +beforeAll(async () => { + dir = await mkdtemp(join(tmpdir(), 'h5p-preview-')) + browser = await launch(undefined) +}) +afterAll(async () => { + await browser?.close() + await rm(dir, { recursive: true, force: true }) +}) + +/** The demo's video package with the unwatched video beside its own; stored entries stay stored. */ +async function withUnwatchedVideo(): Promise { + const reader = new ZipReader(new BlobReader(new Blob([await readFile(video)]))) + const writer = new ZipWriter(new BlobWriter('application/zip')) + for (const entry of await reader.getEntries()) { + if (entry.directory) continue + const bytes = await entry.getData!(new Uint8ArrayWriter()) + await writer.add(entry.filename, new Uint8ArrayReader(bytes), { level: entry.compressionMethod === 0 ? 0 : 6 }) + } + await reader.close() + await writer.add(UNWATCHED, new Uint8ArrayReader(new Uint8Array(UNWATCHED_SIZE)), { level: 0 }) + const path = join(dir, 'unwatched.h5p') + await writeFile(path, new Uint8Array(await (await writer.close()).arrayBuffer())) + return path +} + +/** The command's exit code and what it wrote to stderr. */ +function run(...args: string[]): Promise<{ code: number | null, stderr: string }> { + return new Promise((resolveRun) => { + const child = spawn(process.execPath, [bin, ...args], { stdio: ['ignore', 'ignore', 'pipe'] }) + let stderr = '' + child.stderr.setEncoding('utf8').on('data', (text: string) => (stderr += text)) + child.once('exit', (code) => resolveRun({ code, stderr })) + }) +} + +describe('byteRange', () => { + it('reads one range as a static host does', () => { + expect(byteRange('bytes=0-0', 100)).toEqual({ start: 0, end: 0 }) + expect(byteRange('bytes=10-', 100)).toEqual({ start: 10, end: 99 }) + expect(byteRange('bytes=90-200', 100)).toEqual({ start: 90, end: 99 }) + expect(byteRange('bytes=-22', 100)).toEqual({ start: 78, end: 99 }) + expect(byteRange('bytes=-200', 100)).toEqual({ start: 0, end: 99 }) + // Starting past the end, an open one included, or asking for no bytes at all: a 416. + expect(byteRange('bytes=100-', 100)).toBe(false) + expect(byteRange('bytes=5000-', 100)).toBe(false) + expect(byteRange('bytes=-0', 100)).toBe(false) + expect(byteRange('bytes=0-0', 0)).toBe(false) + // None, one that does not parse, a last byte before the first, several: the whole file. + expect(byteRange(null, 100)).toBeNull() + expect(byteRange('bytes=-', 100)).toBeNull() + expect(byteRange('items=0-1', 100)).toBeNull() + expect(byteRange('bytes=5-3', 100)).toBeNull() + expect(byteRange('bytes=0-1,5-6', 100)).toBeNull() + }) +}) + +describe('h5p-verify --preview', () => { + it('answers for the package as a static host with Range does', async () => { + const bytes = await readFile(quiz) + const preview = await previewPackage({ file: quiz, port: 0 }) + try { + const archive = `${preview.url}/__package/quiz.h5p` + expect(await (await fetch(`${preview.url}/`)).text()).toContain('"src":"/__package/quiz.h5p"') + // What the element's probe asks first. + const first = await fetch(archive, { headers: { Range: 'bytes=0-0' } }) + expect(first.status).toBe(206) + expect(first.headers.get('content-range')).toBe(`bytes 0-0/${bytes.length}`) + expect(first.headers.get('etag')).toMatch(/^"[0-9a-f]+-[0-9a-f]+"$/) + expect(Buffer.from(await first.arrayBuffer())).toEqual(bytes.subarray(0, 1)) + const tail = await fetch(archive, { headers: { Range: 'bytes=-22' } }) + expect(Buffer.from(await tail.arrayBuffer())).toEqual(bytes.subarray(bytes.length - 22)) + const whole = await fetch(archive) + expect(whole.status).toBe(200) + expect(Buffer.from(await whole.arrayBuffer())).toEqual(bytes) + const past = await fetch(archive, { headers: { Range: `bytes=${bytes.length}-` } }) + expect(past.status).toBe(416) + expect(past.headers.get('content-range')).toBe(`bytes */${bytes.length}`) + expect((await fetch(`${preview.url}/__package/other.h5p`)).status).toBe(404) + } finally { + preview.close() + } + expect(await preview.closed).toBe('stopped') + }) + + it('plays the package in a browser by range, reads no video nobody asked for, and ends with its tab', async () => { + const file = await withUnwatchedVideo() + const answers: { range: string | null, bytes: number }[] = [] + const preview = await previewPackage({ file, port: 0, closeGrace: 200, onArchiveRequest: (answer) => answers.push(answer) }) + const page = await browser.newPage() + await page.goto(`${preview.url}/`) + await page.waitForFunction(() => document.querySelector('h5p-player')?.state === 'ready', null, { timeout: 60_000 }) + // Read as a site that honours Range is: in pieces, not downloaded whole first. + expect(await page.evaluate(() => document.querySelector('h5p-player')?.source?.type)).toBe('range-http') + const frame = page.frames().find((candidate) => candidate.url().includes('/h5p/frame/')) + expect(frame).toBeDefined() + expect(await frame!.evaluate(() => document.querySelector('#h5p-root')?.querySelectorAll('*').length ?? 0)).toBeGreaterThan(0) + // A range of the large video, asked for as a media element asks: answered, and read alone. + const asked = await frame!.evaluate(async (path) => { + const config = JSON.parse(document.getElementById('h5p-boot-config')?.textContent ?? 'null') + const response = await fetch(`${config.h5pJsonPath}/${path}`, { headers: { Range: 'bytes=1000000-1000999' } }) + return { status: response.status, length: (await response.arrayBuffer()).byteLength } + }, UNWATCHED) + expect(asked).toEqual({ status: 206, length: 1000 }) + expect(answers.length).toBeGreaterThan(0) + expect(answers.filter((answer) => answer.range === null)).toEqual([]) + expect(answers.reduce((sum, answer) => sum + answer.bytes, 0)).toBeLessThan(UNWATCHED_SIZE / 4) + + await page.close() + expect(await preview.closed).toBe('closed') + }, 90_000) + + it('ends by itself when no tab opens it in time', async () => { + const preview = await previewPackage({ file: quiz, port: 0, openWait: 100 }) + expect(await preview.closed).toBe('unopened') + }) + + it('prints its address with --no-open, and ends with exit code 0 on Ctrl+C', async () => { + const child = spawn(process.execPath, [bin, quiz, '--preview', '--no-open'], { stdio: ['ignore', 'pipe', 'inherit'] }) + try { + let output = '' + child.stdout.setEncoding('utf8').on('data', (text: string) => (output += text)) + // The last line it writes before it waits; the lines above it can arrive in chunks of their own. + await new Promise((resolveStarted, reject) => { + child.stdout.on('data', () => output.includes('Ctrl+C') && resolveStarted()) + child.once('exit', () => reject(new Error(`ended before it started: ${output}`))) + }) + expect(output).toContain('Open that address in a browser.') + const address = /at (http:\/\/127\.0\.0\.1:\d+\/)$/m.exec(output)?.[1] + expect(address).toBeDefined() + expect((await fetch(address!)).status).toBe(200) + child.kill('SIGINT') + // `close` rather than `exit`: its last line is read by then. + const [code] = await once(child, 'close') + expect(code).toBe(0) + expect(output).toContain('The preview ended.') + } finally { + if (child.exitCode === null) child.kill() + } + }, 30_000) + + it("refuses the check's own options, and --no-open without --preview", async () => { + expect(await run(quiz, '--preview', '--json', '--out', dir)).toEqual({ code: 2, stderr: '--preview does not take --out, --json\n' }) + expect(await run(quiz, '--no-open')).toEqual({ code: 2, stderr: '--no-open goes with --preview\n' }) + expect((await run(join(dir, 'missing.h5p'), '--preview', '--no-open')).code).toBe(2) + }) +}) From 6034bf88445660bb42a88458d57861f697a18d76 Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Fri, 23 Oct 2026 08:22:21 +0300 Subject: [PATCH 2/4] fix: preview page takes only an http(s) src; test compares the archive natively - The preview page resolves its src and sets it only when it is http or https, as CodeQL's js/xss-through-dom suggested. - The server test compared the 1.5 MB archive with toEqual, which walks it a byte at a time: 2.6 s locally and past the 5 s timeout on CI. Buffer.equals takes it to 36 ms. Co-Authored-By: Claude Opus 5.5 --- packages/verify/lib/preview.html | 12 +++++++++++- packages/verify/tests/preview.test.ts | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/verify/lib/preview.html b/packages/verify/lib/preview.html index 8343272a..f338b7a8 100644 --- a/packages/verify/lib/preview.html +++ b/packages/verify/lib/preview.html @@ -46,7 +46,17 @@

Preview

// An error once the content is up leaves it running; before that, the package did not open. if (player.state !== 'ready') status.textContent = event.detail.message }) - player.setAttribute('src', settings.src) + // Only a web address becomes `src`: never a `javascript:` or `data:` URL. + try { + const src = new URL(settings.src, window.location.href) + if (src.protocol === 'http:' || src.protocol === 'https:') { + player.setAttribute('src', src.href) + } else { + status.textContent = 'Invalid preview source.' + } + } catch { + status.textContent = 'Invalid preview source.' + } // Held open while this tab shows the preview: the command ends once no tab does. Once its // server has gone the page stops asking, so that it cannot keep a later preview alive. diff --git a/packages/verify/tests/preview.test.ts b/packages/verify/tests/preview.test.ts index 41df5f8d..73bcfb0d 100644 --- a/packages/verify/tests/preview.test.ts +++ b/packages/verify/tests/preview.test.ts @@ -98,7 +98,8 @@ describe('h5p-verify --preview', () => { expect(Buffer.from(await tail.arrayBuffer())).toEqual(bytes.subarray(bytes.length - 22)) const whole = await fetch(archive) expect(whole.status).toBe(200) - expect(Buffer.from(await whole.arrayBuffer())).toEqual(bytes) + // Buffer's own comparison: toEqual walks 1.5 MB a byte at a time, past the timeout on CI. + expect(Buffer.from(await whole.arrayBuffer()).equals(bytes)).toBe(true) const past = await fetch(archive, { headers: { Range: `bytes=${bytes.length}-` } }) expect(past.status).toBe(416) expect(past.headers.get('content-range')).toBe(`bytes */${bytes.length}`) From a9e789cc6c84ae78391322bc95abaf0030f9eb17 Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Fri, 23 Oct 2026 09:31:37 +0300 Subject: [PATCH 3/4] docs: AGENTS.md says why the preview page parses its src Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index 0b2014a9..f5070110 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1727,7 +1727,9 @@ driver along and the normalizer should not. The check's own options are refused with it, not ignored. The browser opener is `open`, `cmd /c start` or `xdg-open`, and the address is printed either way; it has not been tried on - Windows. + Windows. The page sets `src` from the parsed URL's `href`, `http` or `https` only, as every page + that takes a package address from outside does (#71): its own route needs no check, but CodeQL + flagged the text going in, as `js/xss-through-dom`. Not built, deliberately: steps that click through the content and check the xAPI it sends, and a content digest for reviewing what was generated. A check of library versions against the hub's From 5cd8dd46dbf15aaef0faea5c8da7df8393215471 Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Fri, 23 Oct 2026 09:38:45 +0300 Subject: [PATCH 4/4] test: start the preview test's browser in the one test that uses it The file's beforeAll launched Chrome under Vitest's ten-second hook timeout. On CI, beside verify.test.ts's browsers, it took longer, and all six tests were skipped. The browser now starts in the browser test, under its own timeout (120 s, as verify.test.ts gives its browser tests), so the other five do not wait on it. The options test, three node start-ups in a row, gets the 30 s the other command test has. Co-Authored-By: Claude Opus 5.5 --- packages/verify/tests/preview.test.ts | 56 ++++++++++++++------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/packages/verify/tests/preview.test.ts b/packages/verify/tests/preview.test.ts index 73bcfb0d..4429fe90 100644 --- a/packages/verify/tests/preview.test.ts +++ b/packages/verify/tests/preview.test.ts @@ -5,7 +5,6 @@ import { tmpdir } from 'node:os' import { join, resolve } from 'node:path' import { BlobReader, BlobWriter, Uint8ArrayReader, Uint8ArrayWriter, ZipReader, ZipWriter } from '@zip.js/zip.js' import type { H5PPlayerElement } from '@missing-elements/h5p-offline-player' -import type { Browser } from 'playwright-core' import { afterAll, beforeAll, describe, expect, it } from 'vitest' import { byteRange, previewPackage } from '../lib/preview.mjs' import { launch } from '../lib/verify.mjs' @@ -23,14 +22,11 @@ const bin = resolve(import.meta.dirname, '../bin/h5p-verify.mjs') const UNWATCHED = 'content/videos/unwatched.mp4' const UNWATCHED_SIZE = 32 * 1024 * 1024 let dir: string -let browser: Browser beforeAll(async () => { dir = await mkdtemp(join(tmpdir(), 'h5p-preview-')) - browser = await launch(undefined) }) afterAll(async () => { - await browser?.close() await rm(dir, { recursive: true, force: true }) }) @@ -114,28 +110,36 @@ describe('h5p-verify --preview', () => { const file = await withUnwatchedVideo() const answers: { range: string | null, bytes: number }[] = [] const preview = await previewPackage({ file, port: 0, closeGrace: 200, onArchiveRequest: (answer) => answers.push(answer) }) - const page = await browser.newPage() - await page.goto(`${preview.url}/`) - await page.waitForFunction(() => document.querySelector('h5p-player')?.state === 'ready', null, { timeout: 60_000 }) - // Read as a site that honours Range is: in pieces, not downloaded whole first. - expect(await page.evaluate(() => document.querySelector('h5p-player')?.source?.type)).toBe('range-http') - const frame = page.frames().find((candidate) => candidate.url().includes('/h5p/frame/')) - expect(frame).toBeDefined() - expect(await frame!.evaluate(() => document.querySelector('#h5p-root')?.querySelectorAll('*').length ?? 0)).toBeGreaterThan(0) - // A range of the large video, asked for as a media element asks: answered, and read alone. - const asked = await frame!.evaluate(async (path) => { - const config = JSON.parse(document.getElementById('h5p-boot-config')?.textContent ?? 'null') - const response = await fetch(`${config.h5pJsonPath}/${path}`, { headers: { Range: 'bytes=1000000-1000999' } }) - return { status: response.status, length: (await response.arrayBuffer()).byteLength } - }, UNWATCHED) - expect(asked).toEqual({ status: 206, length: 1000 }) - expect(answers.length).toBeGreaterThan(0) - expect(answers.filter((answer) => answer.range === null)).toEqual([]) - expect(answers.reduce((sum, answer) => sum + answer.bytes, 0)).toBeLessThan(UNWATCHED_SIZE / 4) + // Started here, under this test's timeout: beside the other file's browsers on CI, Chrome + // took longer than a hook's ten seconds to start. + const browser = await launch(undefined) + try { + const page = await browser.newPage() + await page.goto(`${preview.url}/`) + await page.waitForFunction(() => document.querySelector('h5p-player')?.state === 'ready', null, { timeout: 60_000 }) + // Read as a site that honours Range is: in pieces, not downloaded whole first. + expect(await page.evaluate(() => document.querySelector('h5p-player')?.source?.type)).toBe('range-http') + const frame = page.frames().find((candidate) => candidate.url().includes('/h5p/frame/')) + expect(frame).toBeDefined() + expect(await frame!.evaluate(() => document.querySelector('#h5p-root')?.querySelectorAll('*').length ?? 0)).toBeGreaterThan(0) + // A range of the large video, asked for as a media element asks: answered, and read alone. + const asked = await frame!.evaluate(async (path) => { + const config = JSON.parse(document.getElementById('h5p-boot-config')?.textContent ?? 'null') + const response = await fetch(`${config.h5pJsonPath}/${path}`, { headers: { Range: 'bytes=1000000-1000999' } }) + return { status: response.status, length: (await response.arrayBuffer()).byteLength } + }, UNWATCHED) + expect(asked).toEqual({ status: 206, length: 1000 }) + expect(answers.length).toBeGreaterThan(0) + expect(answers.filter((answer) => answer.range === null)).toEqual([]) + expect(answers.reduce((sum, answer) => sum + answer.bytes, 0)).toBeLessThan(UNWATCHED_SIZE / 4) - await page.close() - expect(await preview.closed).toBe('closed') - }, 90_000) + await page.close() + expect(await preview.closed).toBe('closed') + } finally { + preview.close() + await browser.close() + } + }, 120_000) it('ends by itself when no tab opens it in time', async () => { const preview = await previewPackage({ file: quiz, port: 0, openWait: 100 }) @@ -170,5 +174,5 @@ describe('h5p-verify --preview', () => { expect(await run(quiz, '--preview', '--json', '--out', dir)).toEqual({ code: 2, stderr: '--preview does not take --out, --json\n' }) expect(await run(quiz, '--no-open')).toEqual({ code: 2, stderr: '--no-open goes with --preview\n' }) expect((await run(join(dir, 'missing.h5p'), '--preview', '--no-open')).code).toBe(2) - }) + }, 30_000) })