/** * Module-split scenarios for the Statum client. * * Each scenario runs in a FRESH QuickJS context (run.sh invokes this script * once per scenario) and loads a specific subset of the module files in a * specific order, proving: * * 1 core-alone - window.statum, seam, overrides, driver-less render * 2 core+document - render driver installed, handler order pinned * 3 core+doc+markdown - full order, statum.markdown API, test hatch * 4 core+markdown - cherry-pick without the walk (crash-free) * 5 document alone - guarded IIFE: console.error, nothing attached * 6 double register - warns, order unchanged * 7 unknown after - warns, registration position kept * * Usage: qjs tests/split/scenarios.js <1|2|3|4|5|6|7> */ import * as std from 'std'; import { installShim } from './shim.js'; // Repo root, as a CWD-relative path. run.sh (like tests/markdown/run.sh) // always `cd`s to the repo root before invoking qjs, and QuickJS' // std.loadFile resolves relative paths against the process CWD — the // established in-repo idiom (see tests/markdown/spec-test.js). var ROOT = '.'; var scenario = scriptArgs.length > 1 ? scriptArgs[1] : ''; function fail(msg) { std.err.puts('FAIL [' + scenario + '] ' + msg + '\n'); std.exit(1); } function check(cond, msg) { if (!cond) fail(msg); } function load(name) { (0, eval)(std.loadFile(ROOT + '/js/' + name)); } function handlerNames() { return globalThis.statum._bridge.attributeHandlers.map(function (h) { return h.name; }); } installShim(); var log = globalThis.__shim.log; if (scenario === '1') { load('statum-core.js'); var s = globalThis.statum; check(s, 'window.statum is not defined'); check(s.version === '0.1.0', 'version missing'); check(typeof s.config === 'object' && s.config.entrypointUrl === '/_statum/entrypoint', 'config missing'); check(typeof s.registerAttribute === 'function', 'registerAttribute is not a function'); check(s._bridge, '_bridge is not present'); check(typeof s.Error === 'function', 'statum.Error missing'); check(log.errors.length === 0, 'unexpected console.error at load: ' + JSON.stringify(log.errors)); check(log.warns.length === 0, 'unexpected console.warn at load: ' + JSON.stringify(log.warns)); // The three override pairs round-trip a function and null out non-functions. var fn = function () {}; var pairs = ['onConfirmHandler', 'onNotifyHandler', 'onErrorHandler']; pairs.forEach(function (p) { s[p] = fn; check(s[p] === fn, p + ' did not round-trip the function'); s[p] = 42; check(s[p] === null, p + ' did not null out a non-function'); s[p] = 'nope'; check(s[p] === null, p + ' did not null out a non-function (string)'); }); // No render driver installed: a render cycle must not crash. check(s._bridge.attributeHandlers.length === 0, 'handlers registered without modules'); s._bridge.renderAll(); check(log.errors.length === 0, 'renderAll without a driver errored: ' + JSON.stringify(log.errors)); } else if (scenario === '2') { load('statum-core.js'); load('statum-document.js'); var s = globalThis.statum; var names = handlerNames(); check(JSON.stringify(names) === JSON.stringify( ['stm-class.', 'stm-attribute.', 'stm-disabled', 'stm-text', 'stm-html']), 'handler order is ' + JSON.stringify(names)); // The driver is installed: renderAll walks document.body and applies a // binding. Scope comes from the (empty) store, so use a literal expression. var el = globalThis.__shim.makeElement('p'); el.setAttribute('stm-text', "'driven'"); globalThis.__shim.body.appendChild(el); s._bridge.renderAll(); check(el.textContent === 'driven', 'render driver did not apply stm-text (got ' + JSON.stringify(el.textContent) + ')'); check(log.errors.length === 0, 'unexpected console.error: ' + JSON.stringify(log.errors)); } else if (scenario === '3') { load('statum-core.js'); load('statum-document.js'); load('statum-markdown.js'); var s = globalThis.statum; var names = handlerNames(); check(JSON.stringify(names) === JSON.stringify( ['stm-class.', 'stm-attribute.', 'stm-disabled', 'stm-text', 'stm-html', 'stm-markdown', 'stm-h-demotion']), 'full handler order is ' + JSON.stringify(names)); // "directly after" from the ordered list above. check(names.indexOf('stm-markdown') === names.indexOf('stm-html') + 1, 'stm-markdown is not directly after stm-html'); check(names.indexOf('stm-h-demotion') === names.indexOf('stm-markdown') + 1, 'stm-h-demotion is not directly after stm-markdown'); // Programmatic API shares the binding's engine. The engine renders blocks // with a trailing newline (CommonMark serialisation), so '# x' -> '