scenarios.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * Module-split scenarios for the Statum client.
  3. *
  4. * Each scenario runs in a FRESH QuickJS context (run.sh invokes this script
  5. * once per scenario) and loads a specific subset of the module files in a
  6. * specific order, proving:
  7. *
  8. * 1 core-alone - window.statum, seam, overrides, driver-less render
  9. * 2 core+document - render driver installed, handler order pinned
  10. * 3 core+doc+markdown - full order, statum.markdown API, test hatch
  11. * 4 core+markdown - cherry-pick without the walk (crash-free)
  12. * 5 document alone - guarded IIFE: console.error, nothing attached
  13. * 6 double register - warns, order unchanged
  14. * 7 unknown after - warns, registration position kept
  15. *
  16. * Usage: qjs tests/split/scenarios.js <1|2|3|4|5|6|7>
  17. */
  18. import * as std from 'std';
  19. import { installShim } from './shim.js';
  20. // Repo root, as a CWD-relative path. run.sh (like tests/markdown/run.sh)
  21. // always `cd`s to the repo root before invoking qjs, and QuickJS'
  22. // std.loadFile resolves relative paths against the process CWD — the
  23. // established in-repo idiom (see tests/markdown/spec-test.js).
  24. var ROOT = '.';
  25. var scenario = scriptArgs.length > 1 ? scriptArgs[1] : '';
  26. function fail(msg) {
  27. std.err.puts('FAIL [' + scenario + '] ' + msg + '\n');
  28. std.exit(1);
  29. }
  30. function check(cond, msg) {
  31. if (!cond) fail(msg);
  32. }
  33. function load(name) {
  34. (0, eval)(std.loadFile(ROOT + '/js/' + name));
  35. }
  36. function handlerNames() {
  37. return globalThis.statum._bridge.attributeHandlers.map(function (h) { return h.name; });
  38. }
  39. installShim();
  40. var log = globalThis.__shim.log;
  41. if (scenario === '1') {
  42. load('statum-core.js');
  43. var s = globalThis.statum;
  44. check(s, 'window.statum is not defined');
  45. check(s.version === '0.1.0', 'version missing');
  46. check(typeof s.config === 'object' && s.config.entrypointUrl === '/_statum/entrypoint',
  47. 'config missing');
  48. check(typeof s.registerAttribute === 'function', 'registerAttribute is not a function');
  49. check(s._bridge, '_bridge is not present');
  50. check(typeof s.Error === 'function', 'statum.Error missing');
  51. check(log.errors.length === 0, 'unexpected console.error at load: ' + JSON.stringify(log.errors));
  52. check(log.warns.length === 0, 'unexpected console.warn at load: ' + JSON.stringify(log.warns));
  53. // The three override pairs round-trip a function and null out non-functions.
  54. var fn = function () {};
  55. var pairs = ['onConfirmHandler', 'onNotifyHandler', 'onErrorHandler'];
  56. pairs.forEach(function (p) {
  57. s[p] = fn;
  58. check(s[p] === fn, p + ' did not round-trip the function');
  59. s[p] = 42;
  60. check(s[p] === null, p + ' did not null out a non-function');
  61. s[p] = 'nope';
  62. check(s[p] === null, p + ' did not null out a non-function (string)');
  63. });
  64. // No render driver installed: a render cycle must not crash.
  65. check(s._bridge.attributeHandlers.length === 0, 'handlers registered without modules');
  66. s._bridge.renderAll();
  67. check(log.errors.length === 0, 'renderAll without a driver errored: ' + JSON.stringify(log.errors));
  68. } else if (scenario === '2') {
  69. load('statum-core.js');
  70. load('statum-document.js');
  71. var s = globalThis.statum;
  72. var names = handlerNames();
  73. check(JSON.stringify(names) === JSON.stringify(
  74. ['stm-class.', 'stm-attribute.', 'stm-disabled', 'stm-text', 'stm-html']),
  75. 'handler order is ' + JSON.stringify(names));
  76. // The driver is installed: renderAll walks document.body and applies a
  77. // binding. Scope comes from the (empty) store, so use a literal expression.
  78. var el = globalThis.__shim.makeElement('p');
  79. el.setAttribute('stm-text', "'driven'");
  80. globalThis.__shim.body.appendChild(el);
  81. s._bridge.renderAll();
  82. check(el.textContent === 'driven',
  83. 'render driver did not apply stm-text (got ' + JSON.stringify(el.textContent) + ')');
  84. check(log.errors.length === 0, 'unexpected console.error: ' + JSON.stringify(log.errors));
  85. } else if (scenario === '3') {
  86. load('statum-core.js');
  87. load('statum-document.js');
  88. load('statum-markdown.js');
  89. var s = globalThis.statum;
  90. var names = handlerNames();
  91. check(JSON.stringify(names) === JSON.stringify(
  92. ['stm-class.', 'stm-attribute.', 'stm-disabled', 'stm-text', 'stm-html',
  93. 'stm-markdown', 'stm-h-demotion']),
  94. 'full handler order is ' + JSON.stringify(names));
  95. // "directly after" from the ordered list above.
  96. check(names.indexOf('stm-markdown') === names.indexOf('stm-html') + 1,
  97. 'stm-markdown is not directly after stm-html');
  98. check(names.indexOf('stm-h-demotion') === names.indexOf('stm-markdown') + 1,
  99. 'stm-h-demotion is not directly after stm-markdown');
  100. // Programmatic API shares the binding's engine. The engine renders blocks
  101. // with a trailing newline (CommonMark serialisation), so '# x' -> '<h1>x</h1>\n'.
  102. check(s.markdown, 'statum.markdown is not defined');
  103. check(s.markdown.render('# x') === '<h1>x</h1>\n',
  104. 'markdown.render("# x") = ' + JSON.stringify(s.markdown.render('# x')));
  105. check(typeof s.markdown.renderTo === 'function', 'markdown.renderTo is not a function');
  106. var el = globalThis.__shim.makeElement('div');
  107. s.markdown.renderTo(el, '# y');
  108. check(el.innerHTML === '<h1>y</h1>\n',
  109. 'renderTo innerHTML = ' + JSON.stringify(el.innerHTML));
  110. // The binding path and the programmatic path agree. (Attribute values are
  111. // expressions, hence the quoted string; an unparseable value yields
  112. // undefined and leaves the element untouched by design.)
  113. el.innerHTML = '';
  114. el.setAttribute('stm-markdown', "'# y'");
  115. globalThis.__shim.body.appendChild(el);
  116. // Drive the walk through the driver via a fresh render cycle.
  117. var bridge = s._bridge;
  118. bridge.renderAll();
  119. check(el.innerHTML === '<h1>y</h1>\n',
  120. 'stm-markdown binding innerHTML = ' + JSON.stringify(el.innerHTML));
  121. // Test hatch (the shim sets __STATUM_TEST__).
  122. check(typeof s._testMarkdown === 'function', '_testMarkdown is not exposed');
  123. check(log.errors.length === 0, 'unexpected console.error: ' + JSON.stringify(log.errors));
  124. } else if (scenario === '4') {
  125. // Cherry-pick: markdown without the walk module. 'after: stm-html' cannot
  126. // resolve (document is absent) so it warns and keeps registration order —
  127. // that is the designed degradation, not a failure.
  128. load('statum-core.js');
  129. load('statum-markdown.js');
  130. var s = globalThis.statum;
  131. check(s.markdown.render('# x') === '<h1>x</h1>\n', 'markdown.render failed without statum-document.js');
  132. var names = handlerNames();
  133. check(names.indexOf('stm-markdown') !== -1 && names.indexOf('stm-h-demotion') !== -1,
  134. 'handlers not registered: ' + JSON.stringify(names));
  135. check(names.indexOf('stm-h-demotion') === names.indexOf('stm-markdown') + 1,
  136. 'demotion constraint broke without the walk: ' + JSON.stringify(names));
  137. check(log.errors.length === 0, 'unexpected console.error: ' + JSON.stringify(log.errors));
  138. check(log.warns.length === 1 && log.warns[0].indexOf('stm-html') !== -1,
  139. 'expected one unresolvable-constraint warning, got ' + JSON.stringify(log.warns));
  140. // A render cycle with no driver still runs.
  141. s._bridge.renderAll();
  142. check(log.errors.length === 0, 'renderAll errored without a driver: ' + JSON.stringify(log.errors));
  143. } else if (scenario === '5') {
  144. // document without core: the guarded IIFE must record one console.error,
  145. // not throw, and attach nothing.
  146. load('statum-document.js');
  147. check(log.errors.length === 1, 'expected exactly one console.error, got ' + JSON.stringify(log.errors));
  148. check(log.errors[0].indexOf('statum-document.js requires statum-core.js') !== -1,
  149. 'error message: ' + JSON.stringify(log.errors[0]));
  150. check(!globalThis.statum, 'document attached window.statum without core');
  151. // Same for markdown.
  152. log.errors.length = 0;
  153. load('statum-markdown.js');
  154. check(log.errors.length === 1, 'expected one console.error from markdown, got ' + JSON.stringify(log.errors));
  155. check(log.errors[0].indexOf('statum-markdown.js requires statum-core.js') !== -1,
  156. 'error message: ' + JSON.stringify(log.errors[0]));
  157. check(!globalThis.statum, 'markdown attached window.statum without core');
  158. } else if (scenario === '6') {
  159. load('statum-core.js');
  160. load('statum-document.js');
  161. var before = handlerNames();
  162. globalThis.statum.registerAttribute('stm-text', function () {});
  163. check(log.warns.length === 1, 'double registration did not warn: ' + JSON.stringify(log.warns));
  164. check(log.warns[0].indexOf('already registered') !== -1, 'warn text: ' + JSON.stringify(log.warns[0]));
  165. var after = handlerNames();
  166. check(JSON.stringify(before) === JSON.stringify(after),
  167. 'order changed on double registration: ' + JSON.stringify(after));
  168. } else if (scenario === '7') {
  169. load('statum-core.js');
  170. load('statum-document.js');
  171. var before = handlerNames();
  172. globalThis.statum.registerAttribute('stm-prefill', function () {}, { after: 'stm-nope' });
  173. check(log.warns.length === 1, 'unresolvable constraint did not warn: ' + JSON.stringify(log.warns));
  174. check(log.warns[0].indexOf('stm-nope') !== -1, 'warn names the missing anchor: ' + JSON.stringify(log.warns[0]));
  175. var after = handlerNames();
  176. check(after.length === before.length + 1, 'handler was not registered');
  177. check(after[after.length - 1] === 'stm-prefill',
  178. 'handler did not keep registration position: ' + JSON.stringify(after));
  179. } else {
  180. fail('unknown scenario: ' + scenario);
  181. }
  182. print('scenario ' + scenario + ': OK');