| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- /**
- * 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 (headless: state/directives/boot),
- * overrides, driver-less render, NO registerAttribute
- * 2 core headless boot- stubbed fetch -> entrypoint state applied, no walk
- * 3 core+document - registry installed, handler order pinned
- * (stm-prefill first), statum.document API,
- * statum.forms gone
- * 4 core+doc+markdown - full order, statum.markdown API, test hatch
- * 5 core+markdown - cherry-pick without the walk (crash-free; queued
- * registration)
- * 6 document alone - guarded IIFE: console.error, nothing attached
- * 7 double register - warns, order unchanged
- * 8 unknown anchor - warns, registration position kept
- *
- * Usage: qjs tests/split/scenarios.js <1..8>
- */
- 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; });
- }
- function installEntryStub() {
- var snap = { slot: { key: 'k-s', type: 's', scope: 'page' }, public: { v: 42 } };
- globalThis.__shim.setFetch(function () {
- return globalThis.__shim.okResponse([{
- type: 'set',
- snapshot_frame: { content: JSON.stringify(snap), signature: 'sig-s-1' }
- }]);
- });
- }
- async function spin(n) {
- for (var i = 0; i < (n || 30); i++) await Promise.resolve();
- }
- 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 === 'undefined',
- 'registerAttribute leaked into headless core (it is document API now)');
- check(s._bridge, '_bridge is not present');
- check(typeof s.Error === 'function', 'statum.Error missing');
- check(typeof s._bridge.registerAttribute === 'function',
- 'bridge.registerAttribute (the pre-document queue) is not a function');
- 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, and the
- // pre-document registration queue stays empty.
- s._bridge.renderAll();
- check(log.errors.length === 0, 'renderAll without a driver errored: ' + JSON.stringify(log.errors));
- } else if (scenario === '2') {
- // Headless boot: stub the entrypoint fetch, init, and prove state was
- // applied with no DOM module loaded.
- load('statum-core.js');
- var s = globalThis.statum;
- installEntryStub();
- await s.init();
- await spin();
- check(s.read('s') && s.read('s').v === 42,
- 'headless init did not apply entrypoint state: ' + JSON.stringify(s.state()));
- check(s._bridge.entrypointLoaded(), 'entrypointLoaded is not exposed as true after boot');
- check(log.errors.length === 0, 'headless boot errored: ' + JSON.stringify(log.errors));
- check(log.warns.length === 0, 'headless boot warned: ' + JSON.stringify(log.warns));
- } else if (scenario === '3') {
- load('statum-core.js');
- load('statum-document.js');
- var s = globalThis.statum;
- check(typeof s.registerAttribute === 'function',
- 'registerAttribute is not public on the document module');
- var names = handlerNames();
- check(JSON.stringify(names) === JSON.stringify(
- ['stm-prefill', 'stm-class.', 'stm-attribute.', 'stm-disabled', 'stm-text', 'stm-html']),
- 'handler order is ' + JSON.stringify(names));
- check(s.document && typeof s.document.reset === 'function', 'statum.document.reset missing');
- check(typeof s.document.markDirty === 'function' && typeof s.document.isDirty === 'function',
- 'statum.document.markDirty/isDirty missing');
- check(s.forms === undefined, 'statum.forms must be gone');
- check(s._bridge.formsDirty instanceof WeakSet, 'bridge.formsDirty missing');
- // 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 === '4') {
- 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-prefill', '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' -> '<h1>x</h1>\n'.
- check(s.markdown, 'statum.markdown is not defined');
- check(s.markdown.render('# x') === '<h1>x</h1>\n',
- 'markdown.render("# x") = ' + JSON.stringify(s.markdown.render('# x')));
- check(typeof s.markdown.renderTo === 'function', 'markdown.renderTo is not a function');
- var el = globalThis.__shim.makeElement('div');
- s.markdown.renderTo(el, '# y');
- check(el.innerHTML === '<h1>y</h1>\n',
- 'renderTo innerHTML = ' + JSON.stringify(el.innerHTML));
- // The binding path and the programmatic path agree. (Attribute values are
- // expressions, hence the quoted string; an unparseable value yields
- // undefined and leaves the element untouched by design.)
- el.innerHTML = '';
- el.setAttribute('stm-markdown', "'# y'");
- globalThis.__shim.body.appendChild(el);
- // Drive the walk through the driver via a fresh render cycle.
- var bridge = s._bridge;
- bridge.renderAll();
- check(el.innerHTML === '<h1>y</h1>\n',
- 'stm-markdown binding innerHTML = ' + JSON.stringify(el.innerHTML));
- // Test hatch (the shim sets __STATUM_TEST__).
- check(typeof s._testMarkdown === 'function', '_testMarkdown is not exposed');
- check(log.errors.length === 0, 'unexpected console.error: ' + JSON.stringify(log.errors));
- } else if (scenario === '5') {
- // Cherry-pick: markdown without the walk module. The registration lands in
- // core's pre-document QUEUE (no warning — it is the designed headless
- // degradation), the API works, and a render cycle with no driver still runs.
- load('statum-core.js');
- load('statum-markdown.js');
- var s = globalThis.statum;
- check(s.markdown.render('# x') === '<h1>x</h1>\n', 'markdown.render failed without statum-document.js');
- check(s._bridge.attributeHandlers === undefined,
- 'attributeHandlers must be absent without statum-document.js');
- check(log.errors.length === 0, 'unexpected console.error: ' + JSON.stringify(log.errors));
- check(log.warns.length === 0, 'queued registration must not warn: ' + JSON.stringify(log.warns));
- s._bridge.renderAll();
- check(log.errors.length === 0, 'renderAll errored without a driver: ' + JSON.stringify(log.errors));
- } else if (scenario === '6') {
- // document without core: the guarded IIFE must record one console.error,
- // not throw, and attach nothing.
- load('statum-document.js');
- check(log.errors.length === 1, 'expected exactly one console.error, got ' + JSON.stringify(log.errors));
- check(log.errors[0].indexOf('statum-document.js requires statum-core.js') !== -1,
- 'error message: ' + JSON.stringify(log.errors[0]));
- check(!globalThis.statum, 'document attached window.statum without core');
- // Same for markdown.
- log.errors.length = 0;
- load('statum-markdown.js');
- check(log.errors.length === 1, 'expected one console.error from markdown, got ' + JSON.stringify(log.errors));
- check(log.errors[0].indexOf('statum-markdown.js requires statum-core.js') !== -1,
- 'error message: ' + JSON.stringify(log.errors[0]));
- check(!globalThis.statum, 'markdown attached window.statum without core');
- } else if (scenario === '7') {
- load('statum-core.js');
- load('statum-document.js');
- var before = handlerNames();
- globalThis.statum.registerAttribute('stm-text', function () {});
- check(log.warns.length === 1, 'double registration did not warn: ' + JSON.stringify(log.warns));
- check(log.warns[0].indexOf('already registered') !== -1, 'warn text: ' + JSON.stringify(log.warns[0]));
- var after = handlerNames();
- check(JSON.stringify(before) === JSON.stringify(after),
- 'order changed on double registration: ' + JSON.stringify(after));
- } else if (scenario === '8') {
- load('statum-core.js');
- load('statum-document.js');
- var before = handlerNames();
- globalThis.statum.registerAttribute('stm-widget', function () {}, { after: 'stm-nope' });
- check(log.warns.length === 1, 'unresolvable constraint did not warn: ' + JSON.stringify(log.warns));
- check(log.warns[0].indexOf('stm-nope') !== -1, 'warn names the missing anchor: ' + JSON.stringify(log.warns[0]));
- var after = handlerNames();
- check(after.length === before.length + 1, 'handler was not registered');
- check(after[after.length - 1] === 'stm-widget',
- 'handler did not keep registration position: ' + JSON.stringify(after));
- } else {
- fail('unknown scenario: ' + scenario);
- }
- print('scenario ' + scenario + ': OK');
|