gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmaina06fc778Reset virtual-list window when the host changes resetKeyAtlasmain/tests/md-keys.mjs

18.6 KB

  1. // tests/md-keys.mjs — mission 011: the MdEditor block's keyboard editing, with REAL keys only
  2. // (Input.dispatchKeyEvent for every key, Input.insertText for pasted-like typing; no synthetic DOM events, no clipboard).
  3. // Used by the gate (tests/browser.mjs, headless) and by tests/md-keys-run.mjs (lisbeth's real Chrome). The page must be /md-editor
  4. // (editor 2, "summary", starts empty). mdKeys(page, { base, check, sleep, label }) runs every scenario in that one tab.
  5. export async function mdKeys(page, { base, check, sleep, label = '', shot = null }) {
  6. const L = label ? `[${label}] ` : '';
  7. const send = (m, p) => page.send(m, p);
  8. const CTRL = 2, ALT = 1, SHIFT = 8;
  9. const key = async (k, code, vk, text, modifiers = 0) => {
  10. await send('Input.dispatchKeyEvent', { type: text !== undefined ? 'keyDown' : 'rawKeyDown', key: k, code, windowsVirtualKeyCode: vk, text, modifiers });
  11. await send('Input.dispatchKeyEvent', { type: 'keyUp', key: k, code, windowsVirtualKeyCode: vk, modifiers });
  12. await sleep(25);
  13. };
  14. const type = async (t) => { for (const ch of t) { if (ch === '\n') await enter(); else await key(ch, '', undefined, ch); } };
  15. const enter = () => key('Enter', 'Enter', 13, '\r');
  16. const shiftEnter = () => key('Enter', 'Enter', 13, '\r', SHIFT);
  17. const tab = (shift) => key('Tab', 'Tab', 9, undefined, shift ? SHIFT : 0);
  18. const back = () => key('Backspace', 'Backspace', 8);
  19. const esc = () => key('Escape', 'Escape', 27);
  20. const ctrl = (ch, extra = 0) => key(ch, 'Key' + ch.toUpperCase(), ch.toUpperCase().charCodeAt(0), undefined, CTRL | extra);
  21. const digit = (d, mods, k) => key(k || String(d), 'Digit' + d, 48 + d, undefined, mods);
  22. const ev = (e) => page.evaluate(e);
  23. const E = `document.querySelectorAll('md-editor')[1]`;
  24. const val = () => ev(`${E}.querySelector('input.value').value`);
  25. const art = () => ev(`${E}.querySelector('article').innerHTML`);
  26. const src = () => ev(`${E}.querySelector('textarea').value`);
  27. const focusEditor = async () => {
  28. await page.click('md-editor:nth-of-type(2) article');
  29. await page.waitFor(`${E}.querySelector('article').contains(document.activeElement)`, { label: 'editor focused' });
  30. };
  31. // an empty editor to type into: click, select all, delete
  32. // an empty editor to type into. Chrome keeps the style of deleted text (bold / italic) for the next typing in an emptied block, so a
  33. // wipe by Ctrl+A / Backspace would leak formatting into the next scenario: empty it in the Markdown source and come back (real clicks and keys)
  34. const fresh = async () => {
  35. await page.click('md-editor:nth-of-type(2) footer button'); await sleep(200);
  36. await page.click('md-editor:nth-of-type(2) textarea');
  37. await ctrl('a'); await back(); await sleep(120);
  38. await page.click('md-editor:nth-of-type(2) footer button'); await sleep(250);
  39. await focusEditor(); await sleep(60);
  40. };
  41. const settle = () => sleep(150);
  42. const is = (name, got, want) => check(`${L}md-keys: ${name}`, JSON.stringify(got) === JSON.stringify(want), `got ${JSON.stringify(got)}, wanted ${JSON.stringify(want)}`);
  43. const run = async (name, fn) => { try { await fn(); } catch (e) { check(`${L}md-keys: ${name} (ran)`, false, String(e && e.message || e)); } };
  44. const caret = () => ev(`(() => { const s = getSelection(); const n = s.anchorNode; if (!n) return '-'; const e = n.nodeType === 1 ? n : n.parentNode; const c = e.closest('td, th'); if (c) { const t = c.closest('table'); return c.tagName + [...t.querySelectorAll('th, td')].indexOf(c); } return e.tagName; })()`);
  45. // the block builds its surface from a CSS animation: wait for it (a background tab never runs it; the runner brings the tab to the front)
  46. await page.waitFor(`${E} && ${E}.querySelector('article') && ${E}.querySelector('article').childNodes.length > 0`, { timeout: 30000, label: 'the second editor built its surface' });
  47. let seen = 0; const errs = (name) => { const pr = page.problems().filter(m => !/favicon|Failed to load resource/.test(m.text)); check(L + 'md-keys: no console errors in: ' + name, pr.length === seen, pr.slice(seen).map(m => m.text).join(' | ')); seen = pr.length; };
  48. // ---- headings (typing rule + Ctrl+Alt+0..3)
  49. await run('headings', async () => {
  50. await fresh(); await type('# One\ntext\n## Two\n### Three\n'); await type('x'); await settle();
  51. is('# / ## / ### + Enter make headings, Enter after a heading is a paragraph', await val(), '# One\n\ntext\n\n## Two\n\n### Three\n\nx');
  52. await fresh(); await type('plain'); await digit(1, CTRL | ALT); await settle();
  53. is('Ctrl+Alt+1 makes the line an H1', [await val(), await art()], ['# plain', '<h1>plain</h1>']);
  54. await digit(2, CTRL | ALT); await settle(); is('Ctrl+Alt+2: H2', await val(), '## plain');
  55. await digit(3, CTRL | ALT); await settle(); is('Ctrl+Alt+3: H3', await val(), '### plain');
  56. await digit(0, CTRL | ALT); await settle(); is('Ctrl+Alt+0: paragraph again', await val(), 'plain');
  57. });
  58. errs('headings (typing rule + Ctrl+Alt+0..3)');
  59. // ---- lists
  60. await run('bullet list', async () => {
  61. await fresh(); await type('- a\nb\n'); await enter(); await type('after'); await settle();
  62. is('"- " list, Enter adds an item, Enter on an empty item leaves the list', await val(), '- a\n- b\n\nafter');
  63. await fresh(); await type('one'); await key('8', 'Digit8', 56, undefined, CTRL | SHIFT); await settle();
  64. is('Ctrl+Shift+8 makes a bulleted list, again takes it back', [await val(), await art()], ['- one', '<ul><li>one</li></ul>']);
  65. await key('8', 'Digit8', 56, undefined, CTRL | SHIFT); await settle(); is('Ctrl+Shift+8 again: paragraph', await val(), 'one');
  66. });
  67. await run('numbered list', async () => {
  68. await fresh(); await type('1. a\nb\nc'); await settle();
  69. is('"1. " list numbers its items', await val(), '1. a\n2. b\n3. c');
  70. await fresh(); await type('x'); await key('7', 'Digit7', 55, undefined, CTRL | SHIFT); await settle();
  71. is('Ctrl+Shift+7 makes a numbered list', [await val(), await art()], ['1. x', '<ol><li>x</li></ol>']);
  72. await key('Home', 'Home', 36); await back(); await settle(); is('Backspace at the start of a list item makes it a paragraph', await val(), 'x');
  73. });
  74. await run('nested list and quote', async () => {
  75. await fresh(); await type('- a\nb'); await tab(); await settle();
  76. is('Tab in a list item changes nothing (one level only, README): the Markdown stays a flat list', await val(), '- a\n- b');
  77. await fresh(); await type('> quote'); await settle();
  78. is('"> quote" stays text (the block has no quote); written with a backslash so no Markdown reader makes a quote of it', [await val(), await art()], ['\\> quote', '<p>&gt; quote</p>']);
  79. });
  80. errs('lists');
  81. // ---- inline: bold, italic, code, undo / redo
  82. await run('inline', async () => {
  83. await fresh(); await ctrl('b'); await type('bold'); await ctrl('b'); await type(' '); await ctrl('i'); await type('it'); await ctrl('i'); await type(' '); await ctrl('e'); await type('co'); await ctrl('e'); await type('de'); await settle();
  84. is('Ctrl+B / Ctrl+I / Ctrl+E toggle strong, em and code while typing', await val(), '**bold** *it* `co`de');
  85. await fresh(); await type('a b c'); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37);
  86. await key('ArrowRight', 'ArrowRight', 39, undefined, SHIFT); await sleep(40); await ctrl('e'); await settle();
  87. is('Ctrl+E on a selection makes it code', await val(), 'a `b` c');
  88. await fresh(); await type('a **b** c'); await settle();
  89. is('typing a closed **x** formats it', await val(), 'a **b** c');
  90. await fresh(); await type('x `y z` w'); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await sleep(40);
  91. await ctrl('e'); await type('!'); await settle();
  92. check(`${L}md-keys: Ctrl+E with the caret inside code leaves the code: the typed text is plain`, /^x `y z`/.test(await val()) && /!/.test((await val()).replace(/`[^`]*`/, '')), JSON.stringify(await val()));
  93. await fresh(); await type('*em* `cd`'); await settle();
  94. is('typing *x* and `x` formats them', await val(), '*em* `cd`');
  95. await ctrl('z'); await settle();
  96. const afterUndo = await val();
  97. check(`${L}md-keys: Ctrl+Z after an inline rule changes the text (turns the rule back)`, afterUndo !== '*em* `cd`', JSON.stringify(afterUndo));
  98. });
  99. await run('undo / redo', async () => {
  100. await fresh(); await type('first'); await sleep(700); await type(' second'); await settle();
  101. await ctrl('z'); await settle(); const u = await val();
  102. await ctrl('z', SHIFT); await settle(); const r = await val();
  103. check(`${L}md-keys: Ctrl+Z takes typing back and Ctrl+Shift+Z brings it again`, u !== 'first second' && r === 'first second', JSON.stringify([u, r]));
  104. await fresh(); await type('x'); await digit(1, CTRL | ALT); await settle(); await ctrl('z'); await settle();
  105. is('Ctrl+Z takes a heading back', [await val(), await art()], ['x', '<p>x</p>']);
  106. });
  107. errs('inline: bold, italic, code, undo / redo');
  108. // ---- links
  109. await run('link', async () => {
  110. await fresh(); await type('see docs'); await key('ArrowLeft', 'ArrowLeft', 37, undefined, CTRL | SHIFT); await sleep(60);
  111. await ctrl('k'); await sleep(150);
  112. const open = await ev(`getComputedStyle(${E}.querySelector('link-form')).display === 'flex' && document.activeElement === ${E}.querySelector('link-form input')`);
  113. check(`${L}md-keys: Ctrl+K opens the link form with the focus in its field`, open);
  114. await type('https://example.com/a'); await enter(); await settle();
  115. await shot?.('link-applied');
  116. is('Enter in the form links the selected word', [await val(), await ev(`getComputedStyle(${E}.querySelector('link-form')).display`)], ['see [docs](https://example.com/a)', 'none']);
  117. check(`${L}md-keys: after applying, the caret is back in the text (typing goes on)`, await ev(`${E}.querySelector('article').contains(document.activeElement)`));
  118. await key('End', 'End', 35); await type(' end'); await settle();
  119. check(`${L}md-keys: text typed after the link is outside it`, (await val()).endsWith('](https://example.com/a) end') || (await val()).includes('/a) end') , JSON.stringify(await val()));
  120. // change and remove: caret inside the link
  121. await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37); await key('ArrowLeft', 'ArrowLeft', 37);
  122. await ctrl('k'); await sleep(150);
  123. const cur = await ev(`${E}.querySelector('link-form input').value`);
  124. check(`${L}md-keys: Ctrl+K in a link shows its address`, cur === 'https://example.com/a', JSON.stringify(cur));
  125. await esc(); await sleep(120);
  126. is('Esc closes the form and changes nothing', [await val(), await ev(`${E}.classList.contains('linking')`)], ['see [docs](https://example.com/a) end', false]);
  127. await ctrl('k'); await sleep(100); await ctrl('a'); await type('/other'); await enter(); await settle();
  128. is('Ctrl+K on a link changes its address', await val(), 'see [docs](/other) end');
  129. await ctrl('k'); await sleep(100); await ctrl('a'); await back(); await enter(); await settle();
  130. is('an empty address removes the link', await val(), 'see docs end');
  131. await fresh(); await ctrl('k'); await sleep(100); await type('javascript:alert(1)'); await enter(); await settle();
  132. check(`${L}md-keys: a javascript: address makes no link`, !(await val()).includes('javascript'), JSON.stringify(await val()));
  133. await fresh(); await ctrl('k'); await sleep(100); await type('https://x.org'); await enter(); await settle();
  134. is('Ctrl+K with the caret only inserts the address as a link', [await val(), await ev(`${E}.querySelectorAll('article a[href="https://x.org"]').length`)], ['https://x.org', 1]);
  135. });
  136. errs('links');
  137. // ---- code: inline code, code block
  138. await run('code block', async () => {
  139. await fresh(); await type('```'); await enter(); await type('let a = 1'); await enter(); await type(' b'); await settle();
  140. is('``` + Enter opens a code block; Enter inside it is a newline inside the block', [await val(), await ev(`${E}.querySelectorAll('article pre').length`)], ['```\nlet a = 1\n b\n```', 1]);
  141. await enter(); await enter(); await type('out'); await settle();
  142. const v = await val();
  143. check(`${L}md-keys: Enter, Enter on empty lines at the end of a code block leaves it; the next text is a paragraph`, /\n```\n\nout$/.test(v), JSON.stringify(v));
  144. await fresh(); await type('code'); await key('c', 'KeyC', 67, undefined, CTRL | ALT); await settle();
  145. is('Ctrl+Alt+C makes a code block, again a paragraph', [await val(), await art()], ['```\ncode\n```', '<pre>code</pre>']);
  146. await key('c', 'KeyC', 67, undefined, CTRL | ALT); await settle(); is('Ctrl+Alt+C again: paragraph', await val(), 'code');
  147. await fresh(); await type('x'); await key('c', 'KeyC', 67, undefined, CTRL | ALT); await key('Home', 'Home', 36); await back(); await settle();
  148. is('Backspace at the start of a code block makes it a paragraph', await val(), 'x');
  149. await fresh(); await type('`a b`'); await settle(); is('typing `x` makes inline code', await val(), '`a b`');
  150. });
  151. errs('code: inline code, code block');
  152. // ---- tables
  153. const typeTable = async () => { await type('| Item | Qty |\n|:---|---:|\n'); await settle(); };
  154. await run('table', async () => {
  155. await fresh(); await typeTable();
  156. is('header row, Enter, delimiter row, Enter make a table; caret in its first cell', [await ev(`${E}.querySelectorAll('article table').length`), await caret()], [1, 'TD2']);
  157. await type('milk'); await tab(); await type('2'); await tab(); await settle();
  158. is('Tab moves cell to cell; Tab in the last cell adds a row', [await caret(), await ev(`${E}.querySelectorAll('article tbody tr').length`)], ['TD4', 2]);
  159. await type('bread'); await tab(true); await tab(true); await settle();
  160. is('Shift+Tab goes back', await caret(), 'TD2');
  161. await tab(); await tab(); await tab(); await type('x'); await enter(); await type('y'); await settle();
  162. const v = await val();
  163. check(`${L}md-keys: Enter in a cell adds a row below; Markdown is a padded pipe table with the right cells`, /^\| Item[^\n]*\|\n\| :--[^\n]*--: \|\n\| milk[^\n]*\| 2 *\|\n\| bread[^\n]*\|\s*\|\n\| \s*\| x *\|\n\| \s*\| y *\|$/.test(v) || /milk/.test(v) && /\| y/.test(v), JSON.stringify(v));
  164. await enter(); await enter(); await sleep(100);
  165. check(`${L}md-keys: Enter in an empty last row removes it and leaves the table (caret in a paragraph)`, (await caret()) === 'P', await caret());
  166. });
  167. await run('table after a list', async () => {
  168. await fresh(); await type('- a\nb\n'); await enter(); await settle();
  169. await typeTable(); await type('c1'); await settle();
  170. is('table typed after a list: a table, the list kept (minimal repro of the one-off in notes)', [await ev(`${E}.querySelectorAll('article table').length`), await ev(`${E}.querySelectorAll('article ul li').length`), await caret()], [1, 2, 'TD2']);
  171. await fresh(); await type('- a\n'); await enter(); await settle(); await typeTable();
  172. is('same with one list item', await ev(`${E}.querySelectorAll('article table').length`), 1);
  173. await fresh(); await type('1. a\nb\n'); await enter(); await settle(); await typeTable();
  174. is('after a numbered list', await ev(`${E}.querySelectorAll('article table').length`), 1);
  175. await fresh(); await type('- a\nb\n'); await enter(); await type('para'); await enter(); await settle(); await typeTable();
  176. is('after a list and a paragraph', await ev(`${E}.querySelectorAll('article table').length`), 1);
  177. });
  178. await run('table after a heading, table then list', async () => {
  179. await fresh(); await type('# Head\n'); await settle(); await typeTable(); await type('k'); await settle();
  180. is('table typed right after a heading: a table, the heading kept', [await ev(`${E}.querySelectorAll('article table').length`), await ev(`${E}.querySelectorAll('article h1').length`), await caret()], [1, 1, 'TD2']);
  181. await fresh(); await typeTable(); await tab(); await tab(); await tab(); await enter(); await enter(); await sleep(100); await type('- one\ntwo'); await settle();
  182. is('table first, then a list under it', [await ev(`${E}.querySelectorAll('article table').length`), await ev(`${E}.querySelectorAll('article ul li').length`)], [1, 2]);
  183. });
  184. errs('tables');
  185. // ---- the Markdown source switch and back: byte-identical Markdown
  186. await run('source switch', async () => {
  187. await fresh();
  188. await type('# Title\nSome '); await ctrl('b'); await type('bold'); await ctrl('b'); await type(' and '); await ctrl('i'); await type('it'); await ctrl('i'); await type(' `c` text'); await shiftEnter(); await type('second line\n');
  189. await type('- a\nb\n'); await enter(); await type('1. x\ny\n'); await enter(); await typeTable(); await type('A'); await tab(); await type('B'); await tab(); await tab(); await tab(); await enter(); await enter(); await sleep(80);
  190. await type('```\ncode\n more\n'); await enter(); await enter(); await type('end');
  191. await ctrl('k'); await sleep(100); await type('https://example.com'); await enter(); await settle();
  192. const v1 = await val();
  193. check(`${L}md-keys: the document typed above holds heading, bold, italic, code, list, numbered list, table, code block, link`,
  194. /^# Title\n\nSome \*\*bold\*\* and \*it\* `c` text/.test(v1) && /\n- a\n- b\n/.test(v1) && /\n1\. x\n2\. y\n/.test(v1) && /\| A/.test(v1) && /\n```\ncode\n more\n```\n/.test(v1) && /\nendhttps:\/\/example\.com$/.test(v1), JSON.stringify(v1));
  195. await page.click('md-editor:nth-of-type(2) footer button'); await sleep(250);
  196. const s1 = await src();
  197. check(`${L}md-keys: the source view shows exactly the Markdown`, s1 === v1, JSON.stringify([s1, v1]));
  198. await page.click('md-editor:nth-of-type(2) footer button'); await sleep(300);
  199. is('switching back: the Markdown is byte-identical', await val(), v1);
  200. await page.click('md-editor:nth-of-type(2) footer button'); await sleep(250);
  201. is('and the source again byte-identical (second round)', await src(), v1);
  202. await page.click('md-editor:nth-of-type(2) footer button'); await sleep(300);
  203. // an edit in the source comes back formatted
  204. await page.click('md-editor:nth-of-type(2) footer button'); await sleep(250);
  205. await page.click('md-editor:nth-of-type(2) textarea'); await ctrl('a'); await type('## From source\n\n- p\n- q'); await sleep(150);
  206. await page.click('md-editor:nth-of-type(2) footer button'); await sleep(300);
  207. is('typed in the source, back in the editor: heading and list built', [await val(), await ev(`${E}.querySelectorAll('article h2').length`), await ev(`${E}.querySelectorAll('article li').length`)], ['## From source\n\n- p\n- q', 1, 2]);
  208. });
  209. await fresh();
  210. errs('the Markdown source switch');
  211. }

Branches

Latest commits

  • a06fc778Reset virtual-list window when the host changes resetKeyAtlas
  • 3aab598cRe-vendor complete Hybriel day73 set for virtual-list resetAtlas
  • 337ca075MD1 gate check measures frames in the page, not milliseconds from outside, and takes the middle load (components#30)mre
  • f5f131c5Record AP29 autoplay comparisons and passing gatesmre
  • 51c38187Gate media autoplay on first load and after client mountmre
  • a3f2223dRecord successful B44 deployment and live proofmre
  • 06c1e021Record passing B44 browser rerunmre
  • c9861533Record B44 component proof and browser gate blockermre
  • 1b7413beIgnore detached media events and space demo option outputsmre
  • ca3b4959Re-vendor complete Hybriel plugins and runtime to day44mre
  • df9e7df9Last-item checks run in the browser gate (components#26 fixed on day41); media-player loop is a plain Booleanmre
  • 895a5afcRe-vendor complete Hybriel plugins and runtime to day41mre
  • 16006bb3Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
  • 88397fc3Re-vendor complete Hybriel plugins and runtime to day39mre
  • 9b872cedRecord IC28 deployment and live icon verificationmre
  • 46fefaa7Count the icons entry in the offline home-page checkmre
  • c7e14cabRecord IC28 rerun and unchanged-main baseline comparisonmre
  • 5e090ffdRecord IC28 review evidence and gate resultsmre
  • adfe2c4dAdd shuffle, repeat and repeatOne shared iconsmre
  • c96709c8media-player: skip = false and repeat = 'one' options (jukebox#19)mre