gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit03b5bb0803b5bb08W39 report to reports/, drop scratch repro, mission + logmre03b5bb08/tests/ticketskit.mjs

4.7 KB

  1. // tests/ticketskit.mjs — a throw-away tickets.worldapi.org for the gate (ticket gitoria#10): tickets' code
  2. // COPIED without .env / storage / sessions (never the live one, never its data), started on its own port and
  3. // storage, logged in through the gate's ident, with an API token for the tickets user "gitoria".
  4. // const t = await startTickets({ ticketsDir, workDir, port, ident, identAlice }) // → { base, token, stop(), api(path) }
  5. import { spawn } from 'node:child_process';
  6. import { cpSync, mkdirSync, rmSync, existsSync } from 'node:fs';
  7. import { join } from 'node:path';
  8. const sleep = (ms) => new Promise(r => setTimeout(r, ms));
  9. const J = JSON.stringify;
  10. const SKIP = new Set(['.env', 'storage', '.sessions', '.scratch', 'server.log', 'server.pid', 'testapp', '.git', 'tests', 'import']);
  11. // the server and anything it started: its own process group (spawned detached), by the saved numeric pid
  12. function killTree(proc) { try { process.kill(-proc.pid, 'SIGKILL'); } catch { try { proc.kill('SIGKILL'); } catch {} } }
  13. export async function startTickets({ ticketsDir, workDir, port, ident, who, origins = '' }) {
  14. const code = join(workDir, 'tickets-code');
  15. const data = join(workDir, 'tickets-data');
  16. rmSync(workDir, { recursive: true, force: true });
  17. mkdirSync(code, { recursive: true });
  18. mkdirSync(data, { recursive: true });
  19. cpSync(ticketsDir, code, { recursive: true, filter: (src) => {
  20. const rel = src.slice(ticketsDir.length).replace(/^\/+/, '');
  21. return rel === '' || !SKIP.has(rel.split('/')[0]);
  22. } });
  23. if (existsSync(join(code, '.env'))) throw new Error('ticketskit: a .env was copied — refusing to start');
  24. const base = `http://127.0.0.1:${port}`;
  25. const app = await ident.registerApp(who, 'tickets (gitoria gate)', [base]);
  26. let log = '';
  27. const proc = spawn(join(code, 'bin/hybriel'), ['project.hl'], {
  28. cwd: code,
  29. env: { ...process.env, TICKETS_PORT: String(port), TICKETS_STORAGE: join(data, 'mpackdb'), TICKETS_SESSIONS: join(data, 'sessions') + '/', TICKETS_WATCH: '0',
  30. HL_HOST: '127.0.0.1', IDENT_URL: ident.base, IDENT_EXCHANGE_URL: ident.base, TICKETS_PUBLIC_URL: base, IDENT_API_KEY: app.key, IDENT_API_SECRET: app.secret, TICKETS_CREATOR_IDENTITY: '', TICKETS_CONNECT_ORIGINS: origins },
  31. stdio: ['ignore', 'pipe', 'pipe'], detached: true,
  32. });
  33. proc.stdout.on('data', d => log += d); proc.stderr.on('data', d => log += d);
  34. // any failure while starting kills the server again (a crashed gate left it listening, 2026-10-23)
  35. let token = null, cookie = '';
  36. try {
  37. let up = false;
  38. // tickets on native Hybriel compiles cold for ~25 s (hybriel#148): wait up to 120 s
  39. for (let i = 0; i < 480 && !up; i++) { try { up = (await fetch(base + "/api/projects")).ok; } catch {} if (!up) await sleep(250); }
  40. if (!up) throw new Error('tickets did not come up\n' + log);
  41. // the user "gitoria": the login button's server half, a display name, an API token
  42. const first = await fetch(base + '/');
  43. cookie = (first.headers.get('set-cookie') || '').split(';')[0];
  44. const cb = await fetch(base + '/login/callback?ident_code=' + await ident.selectorCode(who, app, base), { headers: { cookie }, redirect: 'manual' });
  45. if (cb.status !== 302) throw new Error('tickets login failed: ' + cb.status);
  46. // a login gets a NEW session id (tickets mission ST, session fixation): like a browser, keep the cookie the login set
  47. const rotated = (cb.headers.get('set-cookie') || '').split(';')[0];
  48. if (rotated) cookie = rotated;
  49. let n = 0;
  50. const emit = async (event, payload) => {
  51. const r = await fetch(base + '/__hl/emit', { method: 'POST', headers: { 'content-type': 'application/json', cookie }, body: J({ t: 'emit', i: ++n, event, payload }) });
  52. const t = await r.text(); let j = null; try { j = JSON.parse(t); } catch {}
  53. return { raw: t, value: j && j.value };
  54. };
  55. const nm = await emit('saveDisplayName', ['gitoria']);
  56. if (!nm.value || nm.value.name !== 'gitoria') throw new Error('tickets name failed: ' + nm.raw);
  57. const tk = await emit('tokenCreate', ['gitoria gate']);
  58. token = tk.value && tk.value.token;
  59. if (!/^tkt_[0-9a-f]{48}$/.test(token || '')) throw new Error('tickets token failed: ' + tk.raw);
  60. } catch (e) { killTree(proc); throw e; }
  61. return {
  62. base, token, cookie, log: () => log,
  63. async api(path, headers = {}) { const r = await fetch(base + path, { headers }); const t = await r.text(); let j = null; try { j = JSON.parse(t); } catch {} return { status: r.status, json: j }; },
  64. async stop() {
  65. try { process.kill(-proc.pid, 'SIGTERM'); } catch { try { proc.kill('SIGTERM'); } catch {} }
  66. await new Promise(r => { if (proc.exitCode !== null || proc.signalCode !== null) return r(); proc.once('exit', r); setTimeout(r, 3000); });
  67. },
  68. };
  69. }

Branches

Latest commits

  • 03b5bb08W39 report to reports/, drop scratch repro, mission + logmre
  • 154d333fMerge w39-create-api: POST /api/repos with a user token (#39)mre
  • 149dd512Rewrite W39 reportmre
  • e5385075Expect 401 for POST /api/repos without a tokenmre
  • 1a76055cDocument repository creation APImre
  • 4d779b3bAdd create-api gatemre
  • 88563122Add POST /api/repos for user tokensmre
  • b00860e3Create repositories with member checks in createRepoInmre
  • 0a38f236Report repository creation API blockermre
  • b5f72187Record malformed JSON blocker for repository creationmre
  • a2ae8302logmre
  • 6947cd58logmre
  • 2175e48clogmre
  • 40eb3ca1gitoria#38: review package, logmre
  • 06a5a50egitoria#38: deploy b8252ca, logmre
  • b8252ca0gitoria#46: W46b report, logmre
  • 50093aafgitoria#46: statuses keyed by app + context (merge w46b-keyed)mre
  • dd0267edgitoria#46: fix a label in the keyed gatemre
  • b1dcb580gitoria#46: statuses keyed by app + context, required = app + context, old rows still readmre
  • 2287363egitoria#47: W47 report, logmre