gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchf2-api-bugs26117255|||PR apps API: review comments on files with spaces in their namemref2-api-bugs/tests/shots-go34.mjs

8.6 KB

  1. // tests/shots-go34.mjs (not a gate: makes the 390 / 1280 screenshots of the GO34 pages into SHOTS_DIR and prints the tap/field measurements) — was tests/private.mjs — GATE OF PRIVATE REPOS AND ORGANIZATIONS (ticket gitoria#34, GO34). Real HTTP against a real local gitoria + its own ident
  2. // (tests/identkit.mjs), the real `git` binary for clone / push, the move tool (tools/move-repo.hl) on a copy. A private repo, and every repo of a
  3. // private organization, does not exist for anybody who is not in it: homepage list, repo page, every code path, API, git over HTTP, the ssh
  4. // access check. An organization's address lists its repos and /<repo>/… behave like repo pages. A public repo is unchanged.
  5. // node tests/private.mjs (GITORIA_PRIV_PORT default 8744, GITORIA_PRIV_IDENT_PORT default 8743)
  6. import { spawn, execFileSync } from 'node:child_process';
  7. import { request } from 'node:http';
  8. import { mkdirSync, rmSync, writeFileSync, existsSync, readdirSync } from 'node:fs';
  9. import { dirname, join, resolve } from 'node:path';
  10. import { fileURLToPath } from 'node:url';
  11. import { startIdent } from './identkit.mjs';
  12. import { launchBrowser } from './cdp.mjs';
  13. if (!process.env.HL_CHROME && existsSync('/opt/google/chrome/chrome')) process.env.HL_CHROME = '/opt/google/chrome/chrome';
  14. const HERE = dirname(fileURLToPath(import.meta.url));
  15. const APP = resolve(HERE, '..');
  16. const PORT = Number(process.env.GITORIA_PRIV_PORT || 8744), IDENT_PORT = Number(process.env.GITORIA_PRIV_IDENT_PORT || 8743);
  17. const IDENT_DIR = process.env.GITORIA_GATE_IDENT_DIR || [resolve(APP, '../ident.worldapi.org'), '/media/STORAGE/projects/WorldAPI/ident.worldapi.org'].find(d => existsSync(join(d, 'project.hl')));
  18. const STORE = join(APP, '.scratch/shots-store');
  19. const OUT = process.env.SHOTS_DIR || join(STORE, 'shots');
  20. const SSH_SECRET = 'sshsecret-for-the-private-gate';
  21. const J = JSON.stringify, sleep = (ms) => new Promise(r => setTimeout(r, ms));
  22. let passes = 0, failures = 0;
  23. const check = (label, ok, detail = '') => { console.log(`${ok ? 'ok ' : 'FAIL'} ${label}${ok ? '' : ' — ' + detail}`); if (ok) passes++; else failures++; };
  24. function raw(method, path, { host = `localhost:${PORT}`, headers = {}, body = null, cookie = null } = {}) {
  25. return new Promise((res, rej) => {
  26. const h = { host, ...headers }; if (cookie) h.cookie = cookie;
  27. if (body !== null) h['content-length'] = Buffer.byteLength(body);
  28. const r = request({ host: '127.0.0.1', port: PORT, method, path, headers: h }, (m) => { const c = []; m.on('data', d => c.push(d)); m.on('end', () => res({ status: m.statusCode, headers: m.headers, body: Buffer.concat(c).toString('latin1') })); });
  29. r.on('error', rej); if (body !== null) r.write(body); r.end();
  30. });
  31. }
  32. const get = (path, o = {}) => raw('GET', path, o);
  33. const g404 = (r) => r.status === 404 || /No such repository/.test(r.body);
  34. const at = (label, path = '/', o = {}) => get(path, { host: `${label}.localhost:${PORT}`, ...o });
  35. let server = null, log = '';
  36. function startServer(env) {
  37. log = '';
  38. const p = spawn(join(APP, 'bin/hybriel'), ['project.hl'], { cwd: APP, stdio: ['ignore', 'pipe', 'pipe'],
  39. env: { ...process.env, GITORIA_PORT: String(PORT), GITORIA_WATCH: '0', GITORIA_PUBLIC_URL: `http://localhost:${PORT}`, GITORIA_SSH_SECRET: SSH_SECRET, GITORIA_TICKETS_URL: 'http://127.0.0.1:1', ...env } });
  40. p.stdout.on('data', d => log += d); p.stderr.on('data', d => log += d);
  41. server = p; return p;
  42. }
  43. async function up() { for (let i = 0; i < 960; i++) { try { if ((await get('/api/repos')).status === 200) return; } catch {} await sleep(250); } throw new Error('gitoria did not come up\n' + log); }
  44. const stop = async () => { if (server) { const p = server; server = null; p.kill(); await new Promise(r => p.once('exit', r)); } };
  45. let ident;
  46. try {
  47. rmSync(STORE, { recursive: true, force: true }); mkdirSync(STORE, { recursive: true }); mkdirSync(OUT, { recursive: true });
  48. ident = await startIdent({ identDir: IDENT_DIR, workDir: join(STORE, 'ident'), port: IDENT_PORT });
  49. const alice = await ident.signIn('[email protected]'), bob = await ident.signIn('[email protected]'), carol = await ident.signIn('[email protected]');
  50. const BASE = `http://localhost:${PORT}`;
  51. const app = await ident.registerApp(alice, 'gitoria (private gate)', [BASE]);
  52. const ENV = { GITORIA_STORAGE: join(STORE, 'mpackdb'), GITORIA_SESSIONS: join(STORE, 'sessions') + '/', GITORIA_GIT: join(STORE, 'git'), IDENT_URL: ident.base, IDENT_EXCHANGE_URL: ident.base, IDENT_API_KEY: app.key, IDENT_API_SECRET: app.secret };
  53. startServer(ENV); await up();
  54. const cookieOf = (r) => (r.headers['set-cookie'] || []).map(c => c.split(';')[0])[0] || null;
  55. const login = async (who) => {
  56. const ck = cookieOf(await get('/'));
  57. const code = await ident.selectorCode(who, app, BASE);
  58. const cb = await get('/login/callback?ident_code=' + code + '&next=%2F', { cookie: ck });
  59. return { cookie: cookieOf(cb) || ck };
  60. };
  61. let n = 0;
  62. const face = async (who, event, payload) => {
  63. const r = await raw('POST', '/__hl/emit', { headers: { 'content-type': 'application/json' }, cookie: who && who.cookie, body: J({ t: 'emit', i: ++n, event, payload }) });
  64. let j = null; try { j = JSON.parse(r.body); } catch {}
  65. return { status: r.status, value: j && j.value, error: j && j.error };
  66. };
  67. const A = await login(alice), B = await login(bob), C = await login(carol);
  68. await face(A, 'gitoriaSaveName', ['Alice']); await face(B, 'gitoriaSaveName', ['Bob']); await face(C, 'gitoriaSaveName', ['Carol']);
  69. const tok = async (who, name) => (await face(who, 'gitoriaMakeToken', [name])).value.token;
  70. const tA = await tok(A, 'a'), tB = await tok(B, 'b'), tC = await tok(C, 'c');
  71. const W = join(STORE, 'work'); mkdirSync(W, { recursive: true });
  72. const genv = { ...process.env, GIT_TERMINAL_PROMPT: '0', GIT_CONFIG_GLOBAL: '/dev/null', GIT_AUTHOR_NAME: 'a', GIT_AUTHOR_EMAIL: 'a@a', GIT_COMMITTER_NAME: 'a', GIT_COMMITTER_EMAIL: 'a@a' };
  73. const g = (...a) => execFileSync('git', a, { cwd: W, encoding: 'utf8', env: genv, stdio: ['ignore', 'pipe', 'pipe'] });
  74. g('init', '-q', '-b', 'main'); writeFileSync(join(W, 'README.md'), '# Hidden tool\n\nA private tool.\n'); g('add', '-A'); g('commit', '-q', '-m', 'init');
  75. await face(A, 'gitoriaCreateOrg', ['acme', true]);
  76. await face(A, 'gitoriaCreateRepo', ['tool', 'the private tool of acme', 'acme', false]);
  77. await face(A, 'gitoriaCreateRepo', ['second', 'another one', 'acme', false]);
  78. g('push', '-q', `http://u:${tA}@acme.localhost:${PORT}/tool.git`, 'main');
  79. await face(A, 'gitoriaChangeMember', ['acme/tool', 'Bob', true]);
  80. const b = await launchBrowser({ debugPortRange: [8750, 8757] });
  81. try {
  82. const page = await b.newPage();
  83. await page.send('Network.enable');
  84. const as = async (who, host) => {
  85. await page.send('Network.deleteCookies', { name: 'gitoriasid', domain: host.split(':')[0] }).catch(() => {});
  86. if (who) { const [ck, cv] = who.cookie.split('='); await page.send('Network.setCookie', { name: ck, value: cv, url: `http://${host}/` }); }
  87. };
  88. const list = [['org-owner', A, 'acme.localhost', '/'], ['repo-owner', A, 'acme.localhost', '/tool'], ['repo-signedout', null, 'acme.localhost', '/tool'], ['org-signedout', null, 'acme.localhost', '/'],
  89. ['settings-owner', A, 'acme.localhost', '/tool/settings'], ['home-create', A, 'localhost', '/']];
  90. for (const [w, h] of [[390, 844], [1280, 900]]) {
  91. await page.send('Emulation.setDeviceMetricsOverride', { width: w, height: h, deviceScaleFactor: 1, mobile: w < 600 });
  92. for (const [name, who, host, path] of list) {
  93. const hp = `${host}:${PORT}`;
  94. await as(who, hp);
  95. await page.goto(`http://${hp}${path}`);
  96. await sleep(2500);
  97. const m = await page.evaluate(`(() => { const out = { sw: document.documentElement.scrollWidth, small: [] }; for (const e of document.querySelectorAll('a, button, input, select')) { const r = e.getBoundingClientRect(); if (r.width > 0 && (r.height < 44) && getComputedStyle(e).display !== 'inline') out.small.push(e.tagName + '#' + e.id + ' ' + Math.round(r.width) + 'x' + Math.round(r.height)); if ((e.tagName === 'INPUT' || e.tagName === 'SELECT') && r.width > 0 && parseFloat(getComputedStyle(e).fontSize) < 16) out.small.push('font<16 ' + e.id); } out.small = out.small.slice(0, 12); return out; })()`);
  98. const { data } = await page.send('Page.captureScreenshot', { format: 'png', captureBeyondViewport: true });
  99. writeFileSync(join(OUT, `${name}-${w}.png`), Buffer.from(data, 'base64'));
  100. console.log(name, w, JSON.stringify(m));
  101. }
  102. }
  103. } finally { await b.close().catch(() => {}); }
  104. } finally {
  105. await stop().catch(() => {});
  106. try { ident && ident.stop && await ident.stop(); } catch {}
  107. process.exit(0);
  108. }

Branches

Latest commits

  • 26117255|||PR apps API: review comments on files with spaces in their namemre
  • b8796cecMerge branch 'f1-broken-json' into mainGitoria
  • 2f2959c5|||PR apps API: broken JSON body gives 400 invalid_json, not 500mre
  • f7c6ca07Merge branch 'ident-chooser-selector' into mainGitoria
  • 691abb8e|||PR tests: find ident identity choice without the removed .choose classmre
  • 16a3530fMerge branch 'heap-revert' into mainGitoria
  • 220a80e0|||PR Remove temporary /__heap route and heap helper; logmre
  • 13f0c342Merge branch 'hl25-records' into mainGitoria
  • a002ecb7|||PR records: HL25 report, heap helper, logmre
  • 2492a55cTemporary keyed /__heap route for the live memory snapshotmre
  • fefaedd2log: tracker PR-only, HL25 startedmre
  • 21aa57calog: parkmre
  • e9f950a8reports: G25c soakmre
  • a7783686log: deploy 9f41ffcmre
  • 9f41ffc1log: W30b mergedmre
  • bef635c1Merge branch 'w30b'mre
  • 15b996a5Org list: keyed rows by @id, newest first, concat instead of hand loopmre
  • c4f6d9e3reports: G25c growth huntmre
  • b88e39f1log: privacy cleanup donemre
  • bfd7826ddocs: neutral example organisation namemre