gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitac5ba6a4ac5ba6a4log: W60, #25 measuredmreac5ba6a4/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

  • ac5ba6a4log: W60, #25 measuredmre
  • 452f6a1amission W60: logic clean-upsmre
  • 1448c899log: cleanup, #49 GPTBot, tracker mentions on #36mre
  • fcf95936log: v1.5.1 provenmre
  • 22105420log: W51b livemre
  • 7e8aabeareport: W51b pull source verificationmre
  • 6c18de76fix: preserve pull sources across containing branchesmre
  • fc36fa2elog: W51b pull source bugmre
  • 56bc1220log: v1.5.1 livemre
  • 845fc0aareport: W51mre
  • 83127391fix: return v1.5.1 app and merge error detailsmre
  • b07d0e97log: W51 v1.5.1 startedmre
  • 0b417d06docs: apps-api v1.5 user tokens (#48, W49)mre
  • d046940alog: vinzenz contract note sentmre
  • db443456log: vinzenz note retry 2mre
  • 802f229alog: kunibert repo ping provenmre
  • 2d2eaa92log: Kunibert connected to kunibert repomre
  • 65d5f80alogmre
  • dab74b0dW49 report, logmre
  • fd4fc028Exercise member status reads with a populated check statemre