gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmainfe209ffeMerge branch 'w56-owner-members' into mainGitoriamain/tests/shots-go34.mjs

8.7 KB

  1. import { requiredPort, requiredPortRange } from './gate-ports.mjs';
  2. // 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
  3. // (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
  4. // 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
  5. // access check. An organization's address lists its repos and /<repo>/… behave like repo pages. A public repo is unchanged.
  6. // node tests/private.mjs (GITORIA_PRIV_PORT required, GITORIA_PRIV_IDENT_PORT required)
  7. import { spawn, execFileSync } from 'node:child_process';
  8. import { request } from 'node:http';
  9. import { mkdirSync, rmSync, writeFileSync, existsSync, readdirSync } from 'node:fs';
  10. import { dirname, join, resolve } from 'node:path';
  11. import { fileURLToPath } from 'node:url';
  12. import { startIdent } from './identkit.mjs';
  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 HERE = dirname(fileURLToPath(import.meta.url));
  16. const APP = resolve(HERE, '..');
  17. const CHROME = requiredPortRange('GITORIA_PRIV_CHROME');
  18. const PORT = requiredPort('GITORIA_PRIV_PORT'), IDENT_PORT = requiredPort('GITORIA_PRIV_IDENT_PORT');
  19. 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')));
  20. const STORE = join(APP, '.scratch/shots-store');
  21. const OUT = process.env.SHOTS_DIR || join(STORE, 'shots');
  22. const SSH_SECRET = 'sshsecret-for-the-private-gate';
  23. const J = JSON.stringify, sleep = (ms) => new Promise(r => setTimeout(r, ms));
  24. let passes = 0, failures = 0;
  25. const check = (label, ok, detail = '') => { console.log(`${ok ? 'ok ' : 'FAIL'} ${label}${ok ? '' : ' — ' + detail}`); if (ok) passes++; else failures++; };
  26. function raw(method, path, { host = `localhost:${PORT}`, headers = {}, body = null, cookie = null } = {}) {
  27. return new Promise((res, rej) => {
  28. const h = { host, ...headers }; if (cookie) h.cookie = cookie;
  29. if (body !== null) h['content-length'] = Buffer.byteLength(body);
  30. 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') })); });
  31. r.on('error', rej); if (body !== null) r.write(body); r.end();
  32. });
  33. }
  34. const get = (path, o = {}) => raw('GET', path, o);
  35. const g404 = (r) => r.status === 404 || /No such repository/.test(r.body);
  36. const at = (label, path = '/', o = {}) => get(path, { host: `${label}.localhost:${PORT}`, ...o });
  37. let server = null, log = '';
  38. function startServer(env) {
  39. log = '';
  40. const p = spawn(join(APP, 'bin/hybriel'), ['project.hl'], { cwd: APP, stdio: ['ignore', 'pipe', 'pipe'],
  41. 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 } });
  42. p.stdout.on('data', d => log += d); p.stderr.on('data', d => log += d);
  43. server = p; return p;
  44. }
  45. 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); }
  46. const stop = async () => { if (server) { const p = server; server = null; p.kill(); await new Promise(r => p.once('exit', r)); } };
  47. let ident;
  48. try {
  49. rmSync(STORE, { recursive: true, force: true }); mkdirSync(STORE, { recursive: true }); mkdirSync(OUT, { recursive: true });
  50. ident = await startIdent({ identDir: IDENT_DIR, workDir: join(STORE, 'ident'), port: IDENT_PORT });
  51. const alice = await ident.signIn('[email protected]'), bob = await ident.signIn('[email protected]'), carol = await ident.signIn('[email protected]');
  52. const BASE = `http://localhost:${PORT}`;
  53. const app = await ident.registerApp(alice, 'gitoria (private gate)', [BASE]);
  54. 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 };
  55. startServer(ENV); await up();
  56. const cookieOf = (r) => (r.headers['set-cookie'] || []).map(c => c.split(';')[0])[0] || null;
  57. const login = async (who) => {
  58. const ck = cookieOf(await get('/'));
  59. const code = await ident.selectorCode(who, app, BASE);
  60. const cb = await get('/login/callback?ident_code=' + code + '&next=%2F', { cookie: ck });
  61. return { cookie: cookieOf(cb) || ck };
  62. };
  63. let n = 0;
  64. const face = async (who, event, payload) => {
  65. const r = await raw('POST', '/__hl/emit', { headers: { 'content-type': 'application/json' }, cookie: who && who.cookie, body: J({ t: 'emit', i: ++n, event, payload }) });
  66. let j = null; try { j = JSON.parse(r.body); } catch {}
  67. return { status: r.status, value: j && j.value, error: j && j.error };
  68. };
  69. const A = await login(alice), B = await login(bob), C = await login(carol);
  70. await face(A, 'gitoriaSaveName', ['Alice']); await face(B, 'gitoriaSaveName', ['Bob']); await face(C, 'gitoriaSaveName', ['Carol']);
  71. const tok = async (who, name) => (await face(who, 'gitoriaMakeToken', [name])).value.token;
  72. const tA = await tok(A, 'a'), tB = await tok(B, 'b'), tC = await tok(C, 'c');
  73. const W = join(STORE, 'work'); mkdirSync(W, { recursive: true });
  74. 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' };
  75. const g = (...a) => execFileSync('git', a, { cwd: W, encoding: 'utf8', env: genv, stdio: ['ignore', 'pipe', 'pipe'] });
  76. 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');
  77. await face(A, 'gitoriaCreateOrg', ['acme', true]);
  78. await face(A, 'gitoriaCreateRepo', ['tool', 'the private tool of acme', 'acme', false]);
  79. await face(A, 'gitoriaCreateRepo', ['second', 'another one', 'acme', false]);
  80. g('push', '-q', `http://u:${tA}@acme.localhost:${PORT}/tool.git`, 'main');
  81. await face(A, 'gitoriaChangeMember', ['acme/tool', 'Bob', true]);
  82. const b = await launchBrowser({ debugPortRange: CHROME });
  83. try {
  84. const page = await b.newPage();
  85. await page.send('Network.enable');
  86. const as = async (who, host) => {
  87. await page.send('Network.deleteCookies', { name: 'gitoriasid', domain: host.split(':')[0] }).catch(() => {});
  88. if (who) { const [ck, cv] = who.cookie.split('='); await page.send('Network.setCookie', { name: ck, value: cv, url: `http://${host}/` }); }
  89. };
  90. const list = [['org-owner', A, 'acme.localhost', '/'], ['repo-owner', A, 'acme.localhost', '/tool'], ['repo-signedout', null, 'acme.localhost', '/tool'], ['org-signedout', null, 'acme.localhost', '/'],
  91. ['settings-owner', A, 'acme.localhost', '/tool/settings'], ['home-create', A, 'localhost', '/']];
  92. for (const [w, h] of [[390, 844], [1280, 900]]) {
  93. await page.send('Emulation.setDeviceMetricsOverride', { width: w, height: h, deviceScaleFactor: 1, mobile: w < 600 });
  94. for (const [name, who, host, path] of list) {
  95. const hp = `${host}:${PORT}`;
  96. await as(who, hp);
  97. await page.goto(`http://${hp}${path}`);
  98. await sleep(2500);
  99. 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; })()`);
  100. const { data } = await page.send('Page.captureScreenshot', { format: 'png', captureBeyondViewport: true });
  101. writeFileSync(join(OUT, `${name}-${w}.png`), Buffer.from(data, 'base64'));
  102. console.log(name, w, JSON.stringify(m));
  103. }
  104. }
  105. } finally { await b.close().catch(() => {}); }
  106. } finally {
  107. await stop().catch(() => {});
  108. try { ident && ident.stop && await ident.stop(); } catch {}
  109. process.exit(0);
  110. }

Branches

Latest commits

  • fe209ffeMerge branch 'w56-owner-members' into mainGitoria
  • fa0754c8|||PR repository owner transfer and members API (#55)mre
  • 8862e537Merge branch 'w55-rename-org' into mainGitoria
  • 014d2b7fOne immutable readers snapshot per move for the left-organization event (#53)mre
  • 1859d506Decide who hears a repo left its organization by its readers before the move (#53)mre
  • 499f25f2Review fixes for the Settings organization move (#53)mre
  • ed3b922b|||PR move a repository into an organization in Settings (#53)mre
  • 74ec2af8Merge branch 'w54-repo-rename-pr' into mainGitoria
  • 9f7ce8d8Preserve rename compatibility and live page titlesmre
  • c3d95b6e|||PR separate names and addresses, rename repositories in Settings (#53)mre
  • ce08920dMerge branch 'w52c-header-phone' into mainGitoria
  • 0bab9e46|||PR header: ident status wraps on phones instead of clippingmre
  • e7af3eceMerge branch 'w52b-approval-followup' into mainGitoria
  • c01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mre
  • 841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria
  • 80307ae2|||PR pulls page cache: live PR numbers, complete scans onlymre
  • 69141f65|||PR pulls page: cache the git part per repo by branch tipsmre
  • 155861f6Merge branch 'w52-person-approval' into mainGitoria
  • b5435de8|||PR person approvals: second Kunibert round (#52)mre
  • 3a8b2fb8|||PR person approvals: Kunibert review fixes (#52)mre