gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit3a8bc0d23a8bc0d2N5: empty Table-button table is written as a real pipe table (MdEditor 4bd1112, plain copy); gate tests/table-button.mjs; reportmre3a8bc0d2/tests/identkit.mjs

6.7 KB

  1. // tests/identkit.mjs — helpers for notes' 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. // the ident code, copied without anything that holds secrets or data
  18. const SKIP = new Set(['.env', 'storage', '.sessions', '.scratch', 'server.log', 'server.pid', 'testapp', '.git']);
  19. export function copyIdent(identDir, to) {
  20. rmSync(to, { recursive: true, force: true });
  21. mkdirSync(to, { recursive: true });
  22. cpSync(identDir, to, { recursive: true, filter: (src) => {
  23. const rel = src.slice(identDir.length).replace(/^\/+/, '');
  24. if (rel === '') return true;
  25. return !SKIP.has(rel.split('/')[0]);
  26. } });
  27. if (existsSync(join(to, '.env'))) throw new Error('identkit: a .env was copied — refusing to start');
  28. }
  29. export async function startIdent({ identDir, workDir, port }) {
  30. const code = join(workDir, 'ident-code');
  31. const data = join(workDir, 'ident-data');
  32. copyIdent(identDir, code);
  33. rmSync(data, { recursive: true, force: true });
  34. mkdirSync(data, { recursive: true });
  35. const sink = join(data, 'mail.txt');
  36. writeFileSync(sink, '');
  37. const base = `http://127.0.0.1:${port}`;
  38. // something already answers on the port: our sign-ins would go to ITS mail sink and the code would never be found
  39. // ("email and code must be strings"). Refuse at once, with the real reason.
  40. let taken = false; try { await fetch(base + '/'); taken = true; } catch {}
  41. if (taken) throw new Error('identkit: port ' + port + ' is already in use — another process answers there (NOTES_GATE_IDENT_PORT)');
  42. let log = '';
  43. const proc = spawn(join(code, 'bin/hybriel'), ['project.hl'], {
  44. cwd: code,
  45. env: { ...process.env, IDENT_PORT: String(port), IDENT_STORAGE: join(data, 'mpackdb'), IDENT_SESSIONS: join(data, 'sessions') + '/',
  46. IDENT_MAIL_SINK: sink, IDENT_IP_LIMIT: '1000', IDENT_IP_DAY_LIMIT: '1000', IDENT_WATCH: '0', HL_HOST: '127.0.0.1',
  47. SMTP_HOST: '', SMTP_USER: '', SMTP_PASSWORD: '' },
  48. stdio: ['ignore', 'pipe', 'pipe'],
  49. });
  50. proc.stdout.on('data', d => log += d); proc.stderr.on('data', d => log += d);
  51. let up = false;
  52. // a compiling Hybriel (hybriel#148) starts cold in ~20 s: wait up to NOTES_COLD_MS (default 120 s)
  53. for (const end = Date.now() + Number(process.env.NOTES_COLD_MS || 120000); !up && Date.now() < end;) {
  54. if (proc.exitCode !== null || proc.signalCode !== null) throw new Error('ident exited before it came up (port ' + port + ' taken?)\n' + log);
  55. try { const r = await fetch(base + '/'); up = r.ok; } catch {} if (!up) await sleep(250);
  56. }
  57. if (!up) throw new Error('ident did not come up\n' + log);
  58. let emitI = 0;
  59. const emit = async (event, payload, cookie) => {
  60. const r = await fetch(base + '/__hl/emit', { method: 'POST', headers: { 'content-type': 'application/json', ...(cookie ? { cookie } : {}) }, body: J({ t: 'emit', i: ++emitI, event, payload }) });
  61. const setCookie = (r.headers.get('set-cookie') || '').split(';')[0];
  62. const t = await r.text();
  63. let j = null; try { j = JSON.parse(t); } catch {}
  64. return { value: j && j.value, raw: t, setCookie };
  65. };
  66. const lastCode = (email) => {
  67. if (!existsSync(sink)) return null;
  68. const lines = readFileSync(sink, 'utf8').trim().split('\n').filter(l => l.startsWith(email + ' '));
  69. return lines.length ? lines[lines.length - 1].split(' ')[1] : null;
  70. };
  71. return {
  72. base, sink, lastCode, emit, log: () => log, proc,
  73. // an ident account signed in over the REST carrier (the first login creates it)
  74. async signIn(email) {
  75. const c = await fetch(base + '/api/code', { method: 'POST', headers: { 'content-type': 'application/json' }, body: J({ email }) });
  76. if (c.status !== 200) throw new Error('ident code refused: ' + c.status + ' ' + await c.text());
  77. const code = lastCode(email);
  78. if (!code) throw new Error('ident sign-in: no code for ' + email + ' in the mail sink ' + sink + ' — is this our ident answering on ' + base + '?');
  79. const v = await emit('verifyCode', [email, code, 'Europe/Vienna']);
  80. if (!v.value || !v.value.account || !v.setCookie) throw new Error('ident sign-in failed: ' + v.raw);
  81. return { email, cookie: v.setCookie, identities: (v.value.identities || []) };
  82. },
  83. async registerApp(who, name, origins) {
  84. const r = await emit('appCreate', [{ name, origins }], who.cookie);
  85. if (!r.value || !r.value.secret) throw new Error('appCreate failed: ' + r.raw);
  86. return { key: r.value.app.apiKey, secret: r.value.secret, raw: r.value };
  87. },
  88. // the selector's path without a browser: the identities (as the page at `origin` asks) → choose → code
  89. async selectorCode(who, app, origin, identityName = null) {
  90. const l = await fetch(base + '/api/selector/identities?key=' + app.key, { headers: { origin, cookie: who.cookie } });
  91. const lj = await l.json();
  92. if (!lj.identities || !lj.identities.length) throw new Error('no identities: ' + J(lj));
  93. const pick = identityName ? lj.identities.find(i => i.name === identityName) : lj.identities[0];
  94. 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 }) });
  95. const cj = await c.json();
  96. if (!cj.code) throw new Error('choose failed: ' + J(cj));
  97. return cj.code;
  98. },
  99. async exchange(app, code) {
  100. const r = await fetch(base + '/api/exchange', { method: 'POST', headers: { 'content-type': 'application/json' }, body: J({ key: app.key, secret: app.secret, code }) });
  101. const j = await r.json();
  102. if (!j.identity) throw new Error('exchange failed: ' + J(j));
  103. return j.identity;
  104. },
  105. async stop() {
  106. try { proc.kill('SIGTERM'); } catch {}
  107. await new Promise(r => { if (proc.exitCode !== null || proc.signalCode !== null) return r(); proc.once('exit', r); setTimeout(r, 3000); });
  108. },
  109. };
  110. }

Branches

Latest commits

  • 3a8bc0d2N5: empty Table-button table is written as a real pipe table (MdEditor 4bd1112, plain copy); gate tests/table-button.mjs; reportmre
  • c16bd443N5: step 1 report (tables pass locally; his DASHBOARD note has no delimiter row)mre
  • 6091320anotes: MdEditor 7437e32 (surface drawn at once when the page is claimed); gate measures claim-to-text on 5 note loads at 1280 and 390mre
  • 0aa9277enotes: move each component's styles into its own Style block (shell and header selector in main.hl, list, editor frame and delete question in notes.hl); styles.hl keeps the theme inherit, the accent and the message colour; .gitignore ignores a bin symlink too; tests/shots.mjs for the pixel comparemre
  • 9ba01de3notes: write the standby service out in full so the boot gate can read the main servicemre
  • 98cab40anotes: standby service on its port plus 10 and a deploy that hands over through it without a gapmre
  • 1fadb473notes gate: the editor table now has 1 px cell lines from the notes thememre
  • 8762115cnotes: take the components e54f83a (paste as Markdown, text typed before the editor is built is kept, Table button); table lines come from the notes thememre
  • ac23e194ident#31: the selector's sign-in returns to the page the user started on (return attribute = the login callback with next), gate checkmre
  • 2804f0d3Merge branch 'iw-ident-widget'mre
  • 52d8d242notes: e2e script checks Log out and the selector in the header instead of the user menumre
  • e5abf7b7notes header: the selector alone top right plus Log out as before the theme missions; the user icon is gonemre
  • a2e343aeident selector back in the header (creator 2026-10-24): selector top right signed in and out on phone and desktop, no second Log in button, new gate checks (WIP: gates not re-run after the last edits)mre
  • 7a889b82notes: MdEditor with the Table button, typed pipe rows and the theme's table look (components mt-tables); a table note is named by its header words; gate checks that fail on mainmre
  • b96c7f70theme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
  • 6140e6a0sessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
  • cb7e6469deploy: security gate (tests/security.mjs)mre
  • 774bf34bnotes security: CSRF guard on /api/notes, new session at login, Secure cookie, uploadMax 0, login rate limit, nosniff, face payload check; tests/security.mjs; SN reportmre
  • ef18a68fsync components.hybriel 214c318: MdEditor on a phone, two even toolbar rows of 44 px targets, label 15 px, table head 16 px (E2)mre
  • c37488b1MN report: alignment updatemre