gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain5d144817Merge branch 'records-t40' into mainTicketsmain/tests/e2e-route-marker.mjs

11.7 KB

  1. // tests/e2e-route-marker.mjs (tickets mission 024, from e2e-menu-current.mjs of 023): the menu's current-page marker, the Inbox badge and the MdEditor's real keys in
  2. // LISBETH's real test Chrome over CDP. Real mouse clicks and real key events; no /json/new, no clipboard. ONE existing page tab,
  3. // picked fresh from /json, brought to front, left at about:blank. Run it only under the shared lock:
  4. // timeout 600 flock /media/STORAGE/claude-scratch/atlas/lisbeth.lock node tests/e2e-route-marker.mjs <port> <sid> <shotDir> [cdpPort=9223] [tag=new]
  5. // The app runs on loreana on a COPY of a tickets backup, reached from lisbeth through `ssh -R <port>:127.0.0.1:<port>`.
  6. // Walk at 390 (phone: the ☰ opens the sidebar with the menu) and 1280 (header menu): list → Inbox → a ticket; the marker moves, the
  7. // Inbox badge shows the count (= the rows of the inbox page); in the comment field a list and then a table are typed with real keys.
  8. import { writeFileSync } from 'node:fs';
  9. const [port, sid, shotDir, cdpPort = '9223', tag = 'new'] = process.argv.slice(2);
  10. const BASE = `http://127.0.0.1:${port}`, sleep = ms => new Promise(r => setTimeout(r, ms)), J = JSON.stringify;
  11. const list = await (await fetch(`http://127.0.0.1:${cdpPort}/json`)).json();
  12. const pages = list.filter(t => t.type === 'page');
  13. const tgt = pages.find(t => t.url === 'about:blank') || pages[0];
  14. if (!tgt) { console.log('NO PAGE TAB'); process.exit(2); }
  15. const ws = new WebSocket(tgt.webSocketDebuggerUrl.replace(/ws:\/\/[^/]+/, `ws://127.0.0.1:${cdpPort}`)); await new Promise(r => ws.onopen = r);
  16. let id = 0; const pend = new Map(), errors = [];
  17. ws.onmessage = m => { const d = JSON.parse(m.data); if (d.id && pend.has(d.id)) { const [ok, bad] = pend.get(d.id); pend.delete(d.id); d.error ? bad(new Error(J(d.error))) : ok(d.result); } else if (d.method === 'Runtime.exceptionThrown') errors.push(J(d.params.exceptionDetails).slice(0, 200)); else if (d.method === 'Runtime.consoleAPICalled' && d.params.type === 'error') errors.push(d.params.args.map(a => a.value || a.description).join(' ').slice(0, 200)); };
  18. const send = (method, params = {}) => new Promise((ok, bad) => { const i = ++id; pend.set(i, [ok, bad]); ws.send(J({ id: i, method, params })); });
  19. const ev = async x => { const r = await send('Runtime.evaluate', { expression: x, awaitPromise: true, returnByValue: true }); if (r.exceptionDetails) throw new Error(J(r.exceptionDetails).slice(0, 300)); return r.result.value; };
  20. const waitFor = async (x, label, t = 25000) => { for (const end = Date.now() + t; Date.now() < end;) { if (await ev(x).catch(() => false)) return true; await sleep(150); } console.log(' TIMEOUT', label); return false; };
  21. const rect = async sel => ev(`(() => { const e = document.querySelector(${J(sel)}); if (!e) return null; e.scrollIntoView({ block: 'center' }); const r = e.getBoundingClientRect(); return { x: r.x + r.width / 2, y: r.y + r.height / 2 }; })()`);
  22. const click = async sel => { let r = await rect(sel); if (!r) throw new Error('no ' + sel); await sleep(150); r = await rect(sel); for (const [type, extra] of [['mouseMoved', {}], ['mousePressed', { button: 'left', clickCount: 1 }], ['mouseReleased', { button: 'left', clickCount: 1 }]]) await send('Input.dispatchMouseEvent', { type, x: r.x, y: r.y, ...extra }); };
  23. const shot = async name => { const r = await send('Page.captureScreenshot', { format: 'png' }); writeFileSync(`${shotDir}/e2e-${tag}-${name}.png`, Buffer.from(r.data, 'base64')); };
  24. let pass = 0, fail = 0; const check = (n, ok, d = '') => { ok ? pass++ : fail++; console.log((ok ? 'PASS ' : 'FAIL ') + n + (ok ? '' : ' ' + d)); };
  25. const hydrated = () => waitFor(`!!window.__hl && window.__hl.socket && window.__hl.socket.readyState === 1`, 'hydrated');
  26. const key = async (k, code, vk, text, modifiers = 0) => { await send('Input.dispatchKeyEvent', { type: text !== undefined ? 'keyDown' : 'rawKeyDown', key: k, code, windowsVirtualKeyCode: vk, text, modifiers }); await send('Input.dispatchKeyEvent', { type: 'keyUp', key: k, code, windowsVirtualKeyCode: vk, modifiers }); await sleep(30); };
  27. const enter = () => key('Enter', 'Enter', 13, '\r');
  28. const type = async t => { for (const ch of t) { if (ch === '\n') await enter(); else await key(ch, '', undefined, ch); } };
  29. const view = async W => { if (W === 390) { await send('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 2, mobile: true }); await send('Emulation.setTouchEmulationEnabled', { enabled: true, maxTouchPoints: 5 }); } else { await send('Emulation.setTouchEmulationEnabled', { enabled: false }); await send('Emulation.setDeviceMetricsOverride', { width: 1280, height: 900, deviceScaleFactor: 1, mobile: false }); } await sleep(300); };
  30. const early = async path => { // marker at first paint: read at DOMContentLoaded, before any hydration (the server's HTML)
  31. await send('Page.navigate', { url: 'about:blank' }); await sleep(200);
  32. const done = new Promise(r => { const h = m => { const d = JSON.parse(m.data); if (d.method === 'Page.domContentEventFired') { ws.removeEventListener('message', h); r(); } }; ws.addEventListener('message', h); });
  33. await send('Page.navigate', { url: BASE + path }); await done;
  34. return ev(`[...document.querySelectorAll('main-menu a')].filter(a => a.getAttribute('aria-current') === 'page').map(a => a.getAttribute('href')).filter((h, i, l) => l.indexOf(h) === i).join()`);
  35. };
  36. const go = async (path, ready) => { await send('Page.navigate', { url: BASE + path }); await waitFor(`!!document.querySelector(${J(ready)})`, 'ready ' + ready + ' on ' + path); await hydrated(); await sleep(1500); };
  37. // the menu the user sees: at 390 the ☰ opens the sidebar (a real tap), from 60rem the header carries it
  38. const menuScope = W => W === 390 ? '#sidebar main-menu' : 'application-header main-menu';
  39. const openMenu = async W => { if (W !== 390) return; if (await ev(`document.querySelector('#sidebar').matches(':popover-open')`)) return; await click('button.menu-toggle'); await waitFor(`document.querySelector('#sidebar').matches(':popover-open')`, 'sidebar open'); await sleep(300); };
  40. const menuState = W => ev(`[...document.querySelectorAll(${J(menuScope(W) + ' a')})].map(a => ({ href: a.getAttribute('href'), aria: a.getAttribute('aria-current'), cls: a.className, label: a.firstChild && a.firstChild.nodeValue, badge: [...a.querySelectorAll('span.count')].map(x => x.textContent), shown: a.getBoundingClientRect().width > 0 }))`);
  41. const markedIs = async (W, href) => { await waitFor(`[...document.querySelectorAll(${J(menuScope(W) + ' a')})].some(a => a.getAttribute('aria-current') === 'page')`, 'a marker'); const m = await menuState(W); return { ok: m.length === 2 && m.every(x => x.shown) && m.filter(x => x.aria === 'page').map(x => x.href).join() === href && m.filter(x => x.cls === 'current').length === 1, m }; };
  42. await send('Page.enable'); await send('Runtime.enable'); await send('DOM.enable'); await send('Network.enable');
  43. await send('Target.activateTarget', { targetId: tgt.id }).catch(() => {});
  44. await send('Page.bringToFront');
  45. await send('Network.setCookie', { name: 'hlsid', value: sid, url: BASE, httpOnly: true });
  46. await send('Storage.clearDataForOrigin', { origin: BASE, storageTypes: 'service_workers,cache_storage' }).catch(() => {});
  47. try {
  48. for (const W of [390, 1280]) {
  49. await view(W); const T = `${W}: `;
  50. { const v = await early('/'); check(T + 'first paint (DOMContentLoaded, no hydration) of /: marker already on All tickets', v === '/', J(v)); }
  51. { const v = await early('/inbox'); check(T + 'first paint of /inbox: marker already on Inbox', v === '/inbox', J(v)); }
  52. await go('/', '#tickets li'); await openMenu(W);
  53. let r = await markedIs(W, '/'); const badge = r.m[1].badge[0];
  54. check(T + 'list: All tickets has aria-current="page" + class current, Inbox not', r.ok, J(r.m));
  55. check(T + `list: the Inbox badge is its own span.count (${badge}), the label is the plain "Inbox"`, /^[1-9]\d*$/.test(badge || '') && r.m[1].label === 'Inbox' && r.m[0].badge.length === 0, J(r.m));
  56. await shot(`${W}-1-list`);
  57. // real click on the Inbox item
  58. await click(menuScope(W) + ' a[href="/inbox"]'); await waitFor(`location.pathname === '/inbox' && !!document.querySelector('#pendingbox, #reviewbox')`, 'inbox page'); await hydrated(); await sleep(1500);
  59. const rows = await ev(`document.querySelectorAll('#pending li, #review li').length`);
  60. await openMenu(W); r = await markedIs(W, '/inbox');
  61. check(T + 'inbox (clicked): the marker moved to Inbox, All tickets unmarked', r.ok, J(r.m));
  62. check(T + `inbox: the badge (${r.m[1].badge[0]}) = the rows of the page (${rows})`, rows > 0 && r.m[1].badge[0] === String(rows), J(r.m));
  63. await shot(`${W}-2-inbox`);
  64. // real click on a ticket of the inbox
  65. await send('Input.dispatchKeyEvent', { type: 'rawKeyDown', key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27 }); await sleep(300);
  66. await click('#pending li a.subject, #review li a.subject'); await waitFor(`!!document.querySelector('#subject')`, 'ticket page'); await hydrated(); await sleep(1500);
  67. const path = await ev('location.pathname');
  68. await openMenu(W); r = await markedIs(W, '/');
  69. check(T + `ticket (${path}, clicked from the inbox): the marker is on All tickets (no item path matches), Inbox unmarked, badge kept`, r.ok && /^[1-9]\d*$/.test(r.m[1].badge[0] || ''), J(r.m));
  70. await shot(`${W}-3-ticket`);
  71. await send('Input.dispatchKeyEvent', { type: 'rawKeyDown', key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27 }); await sleep(300);
  72. // comments push redraw: post a comment with real keys, it appears in the thread; edit it, the new text is drawn
  73. await waitFor(`!!document.querySelector('#commentform md-editor article')`, 'comment editor');
  74. const n0 = await ev(`document.querySelectorAll('li.comment').length`);
  75. await click('#commentform md-editor article'); await waitFor(`document.querySelector('#commentform md-editor article').contains(document.activeElement)`, 'editor focused'); await sleep(300);
  76. const stamp = 'e2e024 ' + W + ' ' + Date.now();
  77. await type(stamp); await sleep(300);
  78. await click('#commentform button.comment-post');
  79. const posted = await waitFor(`document.querySelectorAll('li.comment').length === ${n0 + 1} && document.body.textContent.includes(${J(stamp)})`, 'comment appears');
  80. check(T + `ticket with ${n0} comments: a posted comment appears in the thread live (${n0} → ${n0 + 1})`, posted, await ev(`document.querySelectorAll('li.comment').length`));
  81. await shot(`${W}-4-comment-posted`);
  82. await click('li.comment:last-of-type button.comment-edit-start');
  83. await waitFor(`!!document.querySelector('li.comment form.comment-edit md-editor article')`, 'edit form'); await sleep(400);
  84. await click('li.comment form.comment-edit md-editor article'); await sleep(200);
  85. await key('End', 'End', 35, undefined, 0); await type(' EDITED'); await sleep(200);
  86. await click('li.comment form.comment-edit button.comment-save');
  87. const edited = await waitFor(`document.body.textContent.includes(${J(stamp + ' EDITED')}) && !document.querySelector('form.comment-edit')`, 'edited text drawn');
  88. check(T + 'edit: the saved text is redrawn in the thread, edit form closed', edited, '');
  89. await shot(`${W}-5-comment-edited`);
  90. const mm = await markedIs(W, '/'); check(T + 'marker still on All tickets after the comments redraws', mm.ok || (W === 390 && (await ev(`document.querySelectorAll('main-menu a[aria-current="page"]').length`)) > 0), J(mm.m));
  91. }
  92. check('no console errors during the walk', errors.length === 0, errors.slice(0, 3).join(' | '));
  93. } catch (e) { console.log('E2E ERROR', e.stack || e); fail++; }
  94. console.log(`\nSUMMARY ${tag}: ${pass} passed, ${fail} failed`);
  95. await send('Emulation.clearDeviceMetricsOverride'); await send('Emulation.setTouchEmulationEnabled', { enabled: false });
  96. await send('Page.navigate', { url: 'about:blank' }); await sleep(300);
  97. ws.close(); process.exit(fail ? 1 : 0);

Branches

Latest commits

  • 5d144817Merge branch 'records-t40' into mainTickets
  • 44ee6f06|||PR records: #43 #45 livemre
  • b8fdeee9Merge branch 'p39-routes' into mainTickets
  • a0187a0a|||PR Remove redundant route-order loopmre
  • 369e4b9cMerge branch 'k40-lists' into mainTickets
  • 9af8e780|||PR Use keyed list mutations for live ticket updatesmre
  • 610d8e8aMerge branch 'records-t39' into mainTickets
  • 32bcdf24|||PR records: GO P39 K40mre
  • 4123c6d1Merge branch 'records-t38' into mainTickets
  • cd3641a1|||PR records: day72 deploy live, tickets closedmre
  • 7d44d852Merge branch 'records-t37' into mainTickets
  • 926f7061|||PR records: merges, D36 deploymre
  • bf58534eMerge branch 'l33-final' into mainTickets
  • 759a9ac3|||PR thin login (tickets#46, #45): merge main, route entry cleanupmre
  • 239436d8Merge remote-tracking branch 'origin/main' into l33-finalmre
  • 5822cb3aMerge branch 'o34-clean' into mainTickets
  • 0870653c|||PR offline note from __hlOnline, no polling (tickets#41)mre
  • 14cf96b4Merge branch 'records-t36' into mainTickets
  • 07749fa1|||PR records: day72 + #40 mergedmre
  • ecd7e226|||PR Consolidate login and move handler logic into topic libraries (partial)mre