gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit3fb871123fb87112|||PR HM55b record six additional focused toggle bisectsTracker3fb87112/tests/identkit.mjs

6.2 KB

  1. // tests/identkit.mjs — helpers for tickets' tests that need a REAL ident (ticket #7): a
  2. // throw-away ident instance from ident's code (a COPY without .env, storage, sessions — so it
  3. // can never send mail: codes go to IDENT_MAIL_SINK), its accounts, a registered app, and the
  4. // ident side of the two login flows. Never the live ident, never the dev server on :8351.
  5. //
  6. // const id = await startIdent({ identDir, workDir, port }) // copies the code, starts it
  7. // const alice = await id.signIn('[email protected]') // → { cookie } (identsid=…)
  8. // const app = await id.registerApp(alice, 'tickets', [origin]) // → { key, secret }
  9. // const code = await id.selectorCode(alice, app, origin) // one-time code (the selector's path)
  10. // const pid = await id.exchange(app, code) // → the per-app identity id
  11. // id.stop()
  12. import { spawn } from 'node:child_process';
  13. import { cpSync, mkdirSync, readFileSync, rmSync, existsSync, writeFileSync } from 'node:fs';
  14. import { join } from 'node:path';
  15. const sleep = (ms) => new Promise(r => setTimeout(r, ms));
  16. const J = JSON.stringify;
  17. // Check outside the fetch catch so an exited server cannot be mistaken for a retry.
  18. export function checkServerExit(proc, name, log) {
  19. if (proc.exitCode !== null || proc.signalCode !== null)
  20. throw new Error(`${name} did not come up (exit code ${proc.exitCode}, signal ${proc.signalCode})\n${log}`);
  21. }
  22. // the ident code, copied without anything that holds secrets or data
  23. const SKIP = new Set(['.env', 'storage', '.sessions', '.scratch', 'server.log', 'server.pid', 'testapp', '.git']);
  24. export function copyIdent(identDir, to) {
  25. rmSync(to, { recursive: true, force: true });
  26. mkdirSync(to, { recursive: true });
  27. cpSync(identDir, to, { recursive: true, filter: (src) => {
  28. const rel = src.slice(identDir.length).replace(/^\/+/, '');
  29. if (rel === '') return true;
  30. return !SKIP.has(rel.split('/')[0]);
  31. } });
  32. if (existsSync(join(to, '.env'))) throw new Error('identkit: a .env was copied — refusing to start');
  33. }
  34. export async function startIdent({ identDir, workDir, port }) {
  35. const code = join(workDir, 'ident-code');
  36. const data = join(workDir, 'ident-data');
  37. copyIdent(identDir, code);
  38. rmSync(data, { recursive: true, force: true });
  39. mkdirSync(data, { recursive: true });
  40. const sink = join(data, 'mail.txt');
  41. writeFileSync(sink, '');
  42. const base = `http://127.0.0.1:${port}`;
  43. let log = '';
  44. const proc = spawn(join(code, 'bin/hybriel'), ['project.hl'], {
  45. cwd: code,
  46. env: { ...process.env, IDENT_PORT: String(port), IDENT_STORAGE: join(data, 'mpackdb'), IDENT_SESSIONS: join(data, 'sessions') + '/',
  47. IDENT_MAIL_SINK: sink, IDENT_IP_LIMIT: '1000', IDENT_IP_DAY_LIMIT: '1000', IDENT_WATCH: '0', HL_HOST: '127.0.0.1',
  48. SMTP_HOST: '', SMTP_USER: '', SMTP_PASSWORD: '' },
  49. stdio: ['ignore', 'pipe', 'pipe'],
  50. });
  51. proc.stdout.on('data', d => log += d); proc.stderr.on('data', d => log += d);
  52. let up = false;
  53. for (let i = 0; i < 600 && !up; i++) { checkServerExit(proc, 'ident', log); try { const r = await fetch(base + '/'); up = r.ok; } catch {} if (!up) await sleep(250); }
  54. if (!up) throw new Error('ident did not come up (150 s; a cold start compiles first, hybriel#148)\n' + log);
  55. let emitI = 0;
  56. const emit = async (event, payload, cookie) => {
  57. const r = await fetch(base + '/__hl/emit', { method: 'POST', headers: { 'content-type': 'application/json', ...(cookie ? { cookie } : {}) }, body: J({ t: 'emit', i: ++emitI, event, payload }) });
  58. const setCookie = (r.headers.get('set-cookie') || '').split(';')[0];
  59. const t = await r.text();
  60. let j = null; try { j = JSON.parse(t); } catch {}
  61. return { value: j && j.value, raw: t, setCookie };
  62. };
  63. const lastCode = (email) => {
  64. const lines = readFileSync(sink, 'utf8').trim().split('\n').filter(l => l.startsWith(email + ' '));
  65. return lines.length ? lines[lines.length - 1].split(' ')[1] : null;
  66. };
  67. return {
  68. base, sink, lastCode, log: () => log, proc,
  69. // an ident account signed in over the REST carrier (the first login creates it)
  70. async signIn(email) {
  71. const c = await fetch(base + '/api/code', { method: 'POST', headers: { 'content-type': 'application/json' }, body: J({ email }) });
  72. if (c.status !== 200) throw new Error('ident code refused: ' + c.status + ' ' + await c.text());
  73. const v = await emit('verifyCode', [email, lastCode(email), 'Europe/Vienna']);
  74. if (!v.value || !v.value.account || !v.setCookie) throw new Error('ident sign-in failed: ' + v.raw);
  75. return { email, cookie: v.setCookie };
  76. },
  77. async registerApp(who, name, origins) {
  78. const r = await emit('appCreate', [{ name, origins }], who.cookie);
  79. if (!r.value || !r.value.secret) throw new Error('appCreate failed: ' + r.raw);
  80. return { key: r.value.app.apiKey, secret: r.value.secret, raw: r.value };
  81. },
  82. // the selector's path without a browser: the identities (as the page at `origin` asks) → choose → code
  83. async selectorCode(who, app, origin, identityName = null) {
  84. const l = await fetch(base + '/api/selector/identities?key=' + app.key, { headers: { origin, cookie: who.cookie } });
  85. const lj = await l.json();
  86. if (!lj.identities || !lj.identities.length) throw new Error('no identities: ' + J(lj));
  87. const pick = identityName ? lj.identities.find(i => i.name === identityName) : lj.identities[0];
  88. const c = await fetch(base + '/api/selector/choose?key=' + app.key, { method: 'POST', headers: { origin, cookie: who.cookie, 'content-type': 'application/json' }, body: J({ identity: pick.id }) });
  89. const cj = await c.json();
  90. if (!cj.code) throw new Error('choose failed: ' + J(cj));
  91. return cj.code;
  92. },
  93. async exchange(app, code) {
  94. const r = await fetch(base + '/api/exchange', { method: 'POST', headers: { 'content-type': 'application/json' }, body: J({ key: app.key, secret: app.secret, code }) });
  95. const j = await r.json();
  96. if (!j.identity) throw new Error('exchange failed: ' + J(j));
  97. return j.identity;
  98. },
  99. async stop() {
  100. try { proc.kill('SIGTERM'); } catch {}
  101. await new Promise(r => { if (proc.exitCode !== null || proc.signalCode !== null) return r(); proc.once('exit', r); setTimeout(r, 3000); });
  102. },
  103. };
  104. }

Branches

Latest commits

  • 3fb87112|||PR HM55b record six additional focused toggle bisectsTracker
  • 20131f51|||PR HM55b record incomplete episode-toggle bisectTracker
  • 17feb7abHM55 WIP: hm54-fill re-applied on main; browser gate fails at ep3 toggleTracker
  • 28eab08cMerge branch 'records-hm55' into mainTracker
  • d3edaa05|||PR records: HM55 start, PROCEDURES.mdTracker
  • 3059f86cMerge branch 'records-design-rule' into mainTracker
  • 421a4809|||PR records: his-design rule (Apollo 2026-11-13)Tracker
  • d1390692Merge branch 'records-dp75b' into mainTracker
  • a4d1a7d2|||PR records: later steps checked, branch auditTracker
  • e070e16fMerge branch 'records-dp75' into mainTracker
  • 642b4fa8|||PR records: DP75 day62 live, AP62 briefTracker
  • bfc4ea4dMerge branch 'ap62' into mainTracker
  • 2c95ac46|||PR AP62: re-vendor Hybriel set day62 (source 70341ad2, bin a54f5f08)Tracker
  • b9c7c3c8Merge branch 'records-dp74' into mainTracker
  • 85cfb4ae|||PR records: AP61 day61, DP74 liveTracker
  • b1496147Merge branch 'ap61' into mainTracker
  • 2047955bMerge branch 'u12-gateports' into mainTracker
  • c8da40f3|||PR AP61: re-vendor Hybriel set day61 (source a05af47c, bin a11042e5)Tracker
  • 4e018678|||PR deploy.sh: gates use Tracker's ports 8716-8719 and LC_ALL=C; README: 2 backups keptTracker
  • 0b86324aMerge branch 'records-dp73' into mainTracker