gitoria

components

All repositories: gitoria

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

8.1 KB

  1. // tests/md-paste-files.mjs — tickets#49: a REAL Ctrl+V of a clipboard that holds text AND a picture (a spreadsheet, an office app) pastes the
  2. // text, in every editor, files "on" or "off"; a picture alone still goes to the files where files are "on". A real headless Chrome, a real click
  3. // into the visual editor, the clipboard filled through the browser's Clipboard API, then a real Ctrl+V key (rawKeyDown with the paste command).
  4. // node tests/md-paste-files.mjs <base-url> <path> <editor-article-selector> <value-field-selector> [pre-click-selector] [sid-cookie] [images: on|off]
  5. // exit 0 = all passed, 1 = a check failed. Chrome debug port: PASTE_CHROME_PORT (default 8745).
  6. import { spawn } from 'node:child_process';
  7. import { rmSync, mkdtempSync } from 'node:fs';
  8. import { tmpdir } from 'node:os';
  9. import { join } from 'node:path';
  10. const [BASE, PATH, SEL, VAL, PRE = '', SID = '', FILES = 'off'] = process.argv.slice(2);
  11. const DP = Number(process.env.PASTE_CHROME_PORT || 8745), J = JSON.stringify, sleep = ms => new Promise(r => setTimeout(r, ms));
  12. const prof = mkdtempSync(join(process.env.PASTE_PROFILE_DIR || tmpdir(), 'pf-'));
  13. const ch = spawn('/opt/google/chrome/chrome', ['--headless=new', '--no-sandbox', '--remote-debugging-port=' + DP, '--user-data-dir=' + prof, 'about:blank'], { stdio: 'ignore' });
  14. let failed = 0; const check = (n, ok, d) => { console.log((ok ? 'ok ' : 'FAIL ') + n + (ok ? '' : ' ' + d)); if (!ok) failed++; };
  15. try {
  16. let v; for (let i = 0; i < 60; i++) { try { v = await (await fetch(`http://127.0.0.1:${DP}/json/version`)).json(); break; } catch { await sleep(200); } }
  17. const bws = new WebSocket(v.webSocketDebuggerUrl); await new Promise(r => bws.onopen = r);
  18. let bid = 0; const bp = new Map(); bws.onmessage = m => { const d = JSON.parse(m.data); if (d.id && bp.has(d.id)) { bp.get(d.id)(d); bp.delete(d.id); } };
  19. const bsend = (method, params = {}) => new Promise((res, rej) => { const i = ++bid; bp.set(i, d => d.error ? rej(new Error(J(d.error))) : res(d.result)); bws.send(J({ id: i, method, params })); });
  20. await bsend('Browser.grantPermissions', { permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'], origin: BASE });
  21. for (const W of [1280, 390]) {
  22. const { targetId } = await bsend('Target.createTarget', { url: 'about:blank' });
  23. const ws = new WebSocket(`ws://127.0.0.1:${DP}/devtools/page/${targetId}`); await new Promise(r => ws.onopen = r);
  24. let id = 0; const pend = new Map();
  25. ws.onmessage = m => { const d = JSON.parse(m.data); if (d.id && pend.has(d.id)) { pend.get(d.id)(d); pend.delete(d.id); } };
  26. const send = (method, params = {}) => new Promise((res, rej) => { const i = ++id; pend.set(i, d => d.error ? rej(new Error(method + J(d.error))) : res(d.result)); ws.send(J({ id: i, method, params })); });
  27. const ev = async e => { const r = await send('Runtime.evaluate', { expression: e, returnByValue: true, awaitPromise: true, userGesture: true }); if (r.exceptionDetails) throw new Error(J(r.exceptionDetails).slice(0, 300)); return r.result.value; };
  28. const press = async (key, code, vk, mod, commands) => { await send('Input.dispatchKeyEvent', { type: 'rawKeyDown', modifiers: mod, key, code, windowsVirtualKeyCode: vk, commands }); await send('Input.dispatchKeyEvent', { type: 'keyUp', modifiers: mod, key, code, windowsVirtualKeyCode: vk }); };
  29. const click = async sel => { const r = await ev(`(()=>{const e=document.querySelector(${J(sel)}); e.scrollIntoView({block:'center'}); const r=e.getBoundingClientRect(); return {x:r.x+r.width/2,y:r.y+Math.min(r.height/2,20)}})()`); await sleep(250); for (const [type, x] of [['mouseMoved', {}], ['mousePressed', { button: 'left', clickCount: 1 }], ['mouseReleased', { button: 'left', clickCount: 1 }]]) await send('Input.dispatchMouseEvent', { type, x: r.x, y: r.y, ...x }); await sleep(600); };
  30. try {
  31. await send('Page.enable'); await send('Runtime.enable'); await send('Network.enable');
  32. await send('Emulation.setDeviceMetricsOverride', { width: W, height: W < 600 ? 844 : 900, deviceScaleFactor: 1, mobile: W < 600 });
  33. if (SID) await send('Network.setCookie', { name: 'hlsid', value: SID, url: BASE, httpOnly: true });
  34. await send('Page.bringToFront'); await send('Page.navigate', { url: BASE + PATH });
  35. for (let i = 0; i < 120; i++) { try { if (await ev(`document.readyState==='complete' && !!document.querySelector(${J(SEL.split('|')[0])})`)) break; } catch {} await sleep(300); }
  36. await sleep(1500);
  37. for (const p of PRE ? PRE.split('|') : []) await click(p);
  38. await click(SEL);
  39. const clip = async (o) => ev(`(async()=>{ const items={}; const o=${J(o)}; if (o.p!==undefined) items['text/plain']=new Blob([o.p],{type:'text/plain'}); if (o.h!==undefined) items['text/html']=new Blob([o.h],{type:'text/html'}); if (o.img) { const cv=document.createElement('canvas'); cv.width=cv.height=8; cv.getContext('2d').fillRect(0,0,8,8); items['image/png']=await new Promise(r=>cv.toBlob(r)); } await navigator.clipboard.write([new ClipboardItem(items)]); return 1; })()`);
  40. const value = () => ev(`document.querySelector(${J(VAL)}).value`);
  41. const empty = async () => { await click(SEL); await press('a', 'KeyA', 65, 2, ['selectAll']); await press('Delete', 'Delete', 46, 0, ['deleteBackward']); await sleep(300); };
  42. const L = `[${W}] `;
  43. await empty();
  44. await clip({ p: 'PASTE SPREADSHEET TEXT', img: 1 }); await press('v', 'KeyV', 86, 2, ['paste']); await sleep(1200);
  45. let val = await value();
  46. check(L + 'text + picture on the clipboard: a real Ctrl+V pastes the text and the field has it', val.includes('PASTE SPREADSHEET TEXT'), J(val));
  47. check(L + 'text + picture: the text is in the visible editor', (await ev(`document.querySelector(${J(SEL)}).innerText`)).includes('PASTE SPREADSHEET TEXT'), '');
  48. check(L + 'text + picture: no picture was taken as a file', !val.includes('!['), J(val));
  49. await empty();
  50. await clip({ p: 'plain only', h: '<p>plain only</p>' }); await press('v', 'KeyV', 86, 2, ['paste']); await sleep(900);
  51. check(L + 'text + html still pastes', (await value()).includes('plain only'), J(await value()));
  52. // a file manager copy: text that is only the names / file:// paths of the files, plus the files: the files are about (as before)
  53. for (const nm of ['image.png', 'file:///home/someone/Pictures/image.png\n']) {
  54. await empty();
  55. await clip({ p: nm, img: 1 }); await press('v', 'KeyV', 86, 2, ['paste']);
  56. if (FILES === 'on') {
  57. let got = false; for (let i = 0; i < 40 && !got; i++) { await sleep(300); got = (await value()).includes('![') || (await ev(`document.querySelectorAll('li.picked, ul.files li').length`)) > 0; }
  58. check(L + 'file manager copy (text = ' + J(nm) + ' + the file): the file is attached', got, J(await value()));
  59. } else { await sleep(1200); check(L + 'file manager copy, files "off": no name text is inserted', (await value()) === '', J(await value())); }
  60. }
  61. await empty();
  62. await clip({ p: 'image.png and more words', img: 1 }); await press('v', 'KeyV', 86, 2, ['paste']); await sleep(1200);
  63. check(L + 'text that is more than the file names + a picture: the text is pasted', (await value()).includes('and more words'), J(await value()));
  64. if (FILES === 'on') {
  65. await empty();
  66. await clip({ img: 1 }); await press('v', 'KeyV', 86, 2, ['paste']);
  67. let got = false; for (let i = 0; i < 40 && !got; i++) { await sleep(300); got = (await value()).includes('![') || (await ev(`document.querySelectorAll('li.picked, ul.files li').length`)) > 0; }
  68. check(L + 'a picture alone still goes to the files (files "on")', got, J(await value()));
  69. } else {
  70. await empty();
  71. await clip({ img: 1 }); await press('v', 'KeyV', 86, 2, ['paste']); await sleep(1200);
  72. check(L + 'a picture alone, files "off": nothing is inserted', (await value()) === '', J(await value()));
  73. }
  74. } finally { await bsend('Target.closeTarget', { targetId }).catch(() => {}); }
  75. }
  76. } catch (e) { console.log('FAIL error ' + e.message); failed++; }
  77. finally { try { process.kill(ch.pid); } catch {} await sleep(500); rmSync(prof, { recursive: true, force: true }); }
  78. console.log(failed ? `${failed} failed` : '0 failed'); process.exit(failed ? 1 : 0);

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