gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain0f6d1592Merge branch 'f18b-shell-session' into mainIdentmain/tests/docs.mjs

8.9 KB

  1. // tests/docs.mjs — GATE ident#45: /docs shows docs/IDENT.md, the only source of the text, in ident's shell.
  2. // 1. signed out: GET /docs is 200 and carries every heading of docs/IDENT.md; in headless Chrome at 390 px the page renders in
  3. // the shell, every heading is there, nothing makes the page wider than the screen (code blocks scroll inside themselves), the
  4. // console is clean.
  5. // 2. signed in (real sign-in with the mail sink): the main menu has ONE "Docs" entry; a real click on it opens /docs; same checks
  6. // at 390 and at 1280 px.
  7. // On code without the page the first check fails (/docs is the 404 page). Own server, own storage, real headless Chrome on loreana.
  8. // Shots go to DOCS_GATE_SHOTS when set. Run: node tests/docs.mjs
  9. import { spawn } from 'node:child_process';
  10. import { rmSync, mkdirSync, writeFileSync, existsSync, readFileSync } from 'node:fs';
  11. import { dirname, join, resolve } from 'node:path';
  12. import { fileURLToPath } from 'node:url';
  13. import { launchBrowser } from './cdp.mjs';
  14. if (!process.env.HL_CHROME && existsSync('/opt/google/chrome/chrome')) process.env.HL_CHROME = '/opt/google/chrome/chrome';
  15. const APP = resolve(dirname(fileURLToPath(import.meta.url)), '..');
  16. const G = join(APP, '.scratch/docs-gate');
  17. rmSync(G, { recursive: true, force: true });
  18. mkdirSync(join(G, 'sess'), { recursive: true });
  19. const PORT = Number(process.env.DOCS_GATE_PORT || 8782), BASE = `http://127.0.0.1:${PORT}`, SINK = join(G, 'mail.txt');
  20. const SHOTS = process.env.DOCS_GATE_SHOTS || '';
  21. if (SHOTS) mkdirSync(SHOTS, { recursive: true });
  22. const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
  23. let passed = 0, failed = 0;
  24. const check = (n, ok, d = '') => { if (ok) { passed++; console.log('ok ' + n); } else { failed++; console.log('FAIL ' + n + (d ? ' — ' + d : '')); } };
  25. // the headings of the source, as a reader sees them (Markdown marks removed)
  26. const plain = (s) => s.replace(/\[([^\]]*)\]\([^)]*\)/g, '$1').replace(/[*`_]/g, '').replace(/\s+/g, ' ').trim();
  27. const headings = readFileSync(join(APP, 'docs/IDENT.md'), 'utf8').split('\n').filter((l) => /^#{1,6} /.test(l)).map((l) => plain(l.replace(/^#+ /, '')));
  28. let log = '';
  29. const srv = spawn(join(APP, 'bin/hybriel'), ['project.hl'], { cwd: APP, env: { ...process.env, SMTP_HOST: '', IDENT_PORT: String(PORT), IDENT_STORAGE: join(G, 'store'), IDENT_SESSIONS: join(G, 'sess'), IDENT_MAIL_SINK: SINK, IDENT_IP_LIMIT: '1000', IDENT_IP_DAY_LIMIT: '1000', IDENT_WATCH: '0' }, stdio: ['ignore', 'pipe', 'pipe'] });
  30. srv.stdout.on('data', (d) => log += d); srv.stderr.on('data', (d) => log += d);
  31. let browser;
  32. async function finish() {
  33. try { await browser.close(); } catch {}
  34. try { process.kill(srv.pid); } catch {}
  35. writeFileSync(join(G, 'server.log'), log);
  36. console.log(`\n${passed} passed, ${failed} failed`);
  37. process.exit(failed ? 1 : 0);
  38. }
  39. const viewport = async (p, w, h) => { await p.send('Emulation.setDeviceMetricsOverride', { width: w, height: h, deviceScaleFactor: 1, mobile: w < 600 }); await sleep(300); };
  40. const shot = async (p, name, full = false) => {
  41. if (!SHOTS) return;
  42. const opts = { format: 'png' };
  43. if (full) { const m = await p.send('Page.getLayoutMetrics'); opts.captureBeyondViewport = true; opts.clip = { x: 0, y: 0, width: m.cssContentSize.width, height: Math.min(m.cssContentSize.height, 6000), scale: 1 }; }
  44. const { data } = await p.send('Page.captureScreenshot', opts); writeFileSync(join(SHOTS, name + '.png'), Buffer.from(data, 'base64'));
  45. };
  46. // what a reader of the page sees: the headings, the width, the code blocks
  47. const pageState = (p) => p.evaluate(`(() => {
  48. const hs = [...document.querySelectorAll('main docs-page h1, main docs-page h2, main docs-page h3, main docs-page h4')].map((h) => h.textContent.replace(/\\s+/g, ' ').trim());
  49. const pres = [...document.querySelectorAll('docs-page pre')];
  50. const preBad = pres.filter((x) => { const r = x.getBoundingClientRect(); return r.right > window.innerWidth + 1 || r.left < -1 || getComputedStyle(x).overflowX !== 'auto'; }).length;
  51. return { hs, pres: pres.length, preBad, sw: document.documentElement.scrollWidth, iw: window.innerWidth, shell: !!document.querySelector('application-header') };
  52. })()`);
  53. const pageChecks = async (p, label) => {
  54. const s = await pageState(p);
  55. const missing = headings.filter((h) => !s.hs.includes(h));
  56. check(label + ': renders in the shell (header)', s.shell);
  57. check(label + ': every heading of docs/IDENT.md is on the page (' + headings.length + ')', headings.length > 5 && missing.length === 0, 'missing ' + JSON.stringify(missing) + ' page ' + JSON.stringify(s.hs.slice(0, 4)));
  58. check(label + ': the code blocks are there and scroll inside themselves', s.pres > 0 && s.preBad === 0, JSON.stringify({ pres: s.pres, bad: s.preBad }));
  59. check(label + ': nothing makes the page wider than the screen', s.sw <= s.iw, s.sw + '/' + s.iw);
  60. };
  61. try {
  62. for (let i = 0; i < 480; i++) { try { await fetch(BASE + '/'); break; } catch {} await sleep(250); }
  63. // ---- 1. signed out, plain HTTP
  64. const r = await fetch(BASE + '/docs');
  65. const html = await r.text();
  66. check('GET /docs answers 200 signed out', r.status === 200, String(r.status));
  67. const htmlText = html.replace(/<[^>]*>/g, '').replace(/&amp;/g, '&').replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/\s+/g, ' ');
  68. const notInHtml = headings.filter((h) => !htmlText.includes(h));
  69. check('the server HTML carries every heading of docs/IDENT.md', notInHtml.length === 0, JSON.stringify(notInHtml));
  70. browser = await launchBrowser({ debugPortRange: (process.env.DOCS_GATE_CHROME || '8783-8786').split('-').map(Number) });
  71. const p = await browser.newPage();
  72. const connected = () => p.waitFor('!!window.__hl && window.__hl.socket && window.__hl.socket.readyState === 1');
  73. await viewport(p, 390, 844);
  74. await p.goto(BASE + '/docs'); await connected(); await sleep(800);
  75. await pageChecks(p, 'signed out 390');
  76. await shot(p, 'docs-out-390-top');
  77. await p.evaluate('document.querySelector("docs-page pre").scrollIntoView()'); await sleep(300);
  78. await shot(p, 'docs-out-390-code');
  79. check('signed out 390: console clean', p.problems().length === 0, p.dumpConsole());
  80. // ---- 2. signed in: the menu's Docs entry
  81. const email = '[email protected]';
  82. const code = async () => { for (let i = 0; i < 30; i++) { if (existsSync(SINK)) { const l = readFileSync(SINK, 'utf8').split('\n').filter((x) => x.startsWith(email + ' ')); if (l.length) return l.at(-1).split(' ')[1]; } await sleep(100); } };
  83. await viewport(p, 1280, 900);
  84. await p.goto(BASE + '/');
  85. await p.waitForSelector('#email'); await connected();
  86. await p.type('#email', email, { clear: true });
  87. await p.click('#sendcode');
  88. await p.waitForSelector('#code'); await connected(); await sleep(300);
  89. await p.type('#code', await code(), { clear: true });
  90. await p.click('#verify');
  91. await p.waitForSelector('#menusignoutall'); await connected(); await sleep(500);
  92. await shot(p, 'header-in-1280');
  93. const entries = await p.evaluate('[...document.querySelectorAll("application-header main-menu a")].map((a) => a.textContent.trim() + " " + a.getAttribute("href"))');
  94. check('signed in 1280: the header menu has one Docs entry after Apps (and the Access entry, ident#27)', JSON.stringify(entries) === JSON.stringify(['Identities /', 'Inbox /inbox', 'Apps /apps', 'Access /access', 'Docs /docs']), JSON.stringify(entries));
  95. await p.click('application-header main-menu a[href="/docs"]');
  96. await p.waitFor('location.pathname === "/docs" && !!document.querySelector("docs-page h1")'); await sleep(800);
  97. await pageChecks(p, 'signed in 1280');
  98. await shot(p, 'docs-in-1280-top');
  99. await p.evaluate('document.querySelector("docs-page pre").scrollIntoView()'); await sleep(300);
  100. await shot(p, 'docs-in-1280-code');
  101. check('signed in 1280: the Docs entry is marked as the current page', await p.evaluate('(() => { const a = document.querySelector("application-header main-menu a[href=\\"/docs\\"]"); return !!a && (a.getAttribute("aria-current") === "page" || a.matches(".current, .active")); })()'));
  102. await viewport(p, 390, 844);
  103. await p.goto(BASE + '/docs'); await connected(); await sleep(800);
  104. await pageChecks(p, 'signed in 390');
  105. await shot(p, 'docs-in-390-top');
  106. await shot(p, 'docs-in-390-full', true);
  107. // the phone's menu (☰ → the sidebar): the same Docs entry
  108. await p.click('application-header > button.menu-toggle');
  109. await p.waitFor('document.querySelector("#sidebar").matches(":popover-open")'); await sleep(400);
  110. await shot(p, 'menu-in-390');
  111. const side = await p.evaluate('[...document.querySelectorAll("#sidebar main-menu a")].map((a) => a.textContent.trim() + " " + a.getAttribute("href"))');
  112. check('signed in 390: the phone menu has one Docs entry after Apps (and the Access entry, ident#27)', JSON.stringify(side) === JSON.stringify(['Identities /', 'Inbox /inbox', 'Apps /apps', 'Access /access', 'Docs /docs']), JSON.stringify(side));
  113. check('signed in: console clean', p.problems().length === 0, p.dumpConsole());
  114. } catch (e) {
  115. check('no exception', false, e.stack || String(e));
  116. }
  117. await finish();

Branches

Latest commits

  • 0f6d1592Merge branch 'f18b-shell-session' into mainIdent
  • 19868218|||PR ident#39 shell reads its session member instead of a cross-file emitmre
  • 5d1b3fedMerge branch 'v61-day61' into mainIdent
  • 83faeb15|||PR ident: vendor Hybriel day61mre
  • b2572b34Merge branch 'v59-day59' into mainIdent
  • 67cf016d|||PR ident: vendor Hybriel day59mre
  • 600c043dMerge branch 'v58-day58' into mainIdent
  • 365f8c80|||PR ident: vendor Hybriel day58mre
  • 585126e5Merge branch 'f16-setf' into mainIdent
  • 953bf03a|||PR ident setF mirror dropped (hybriel#370)mre
  • a1419d9cMerge branch 'f15-navigate' into mainIdent
  • 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
  • e36f4516Merge branch 'f14-online' into mainIdent
  • 2c5eac91|||PR ident#38 online state without probe timermre
  • bf1980f2Merge branch 'v56-day56' into mainIdent
  • 62ae0adf|||PR ident: vendor Hybriel day56mre
  • 0d18abc4Merge branch 'f13-avatar' into mainIdent
  • 2a99730a|||PR ident#37 avatar in hlmre
  • fa4ed25bMerge branch 'f11-upsert' into mainIdent
  • 8f3d9bc9|||PR ident#42 keyed upsert and removemre