gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit0f39e1810f39e181Merge branch 'w64-org-slash' into mainGitoria0f39e181/tests/pull-numbers.mjs

26.2 KB

  1. import { requiredPort } from './gate-ports.mjs';
  2. // tests/pull-numbers.mjs — THE GATE OF PULL REQUEST NUMBERS (gitoria#40). Its own gitoria server and storage, the REAL git binary over HTTP:
  3. // numbers per repo (never reused, in commit-time order for the old ones), the changes syncPulls finds (opened, synchronize with forced,
  4. // closed, reopened, merged with mergeCommit), refs/pull/N/head fetchable and NOT pushable, the contract's pull object, wasHeadOf, "#N" on /pulls.
  5. // node tests/pull-numbers.mjs (GITORIA_PULLS_PORT required)
  6. import { spawn, execFileSync, spawnSync } from 'node:child_process';
  7. import { request as httpRequest } from 'node:http';
  8. import { rmSync, mkdirSync, writeFileSync, readFileSync, existsSync } from 'node:fs';
  9. import { dirname, join, resolve } from 'node:path';
  10. import { fileURLToPath } from 'node:url';
  11. const HERE = dirname(fileURLToPath(import.meta.url));
  12. const APP = resolve(HERE, '..');
  13. const PORT = requiredPort('GITORIA_PULLS_PORT');
  14. const STORE = join(APP, '.scratch', 'pulls-store');
  15. const DB = join(STORE, 'db'), GIT = join(STORE, 'git'), WORK = join(STORE, 'work');
  16. const LOG = join(DB, 'pull-changes.log');
  17. const trace = () => existsSync(join(STORE, 'git-trace')) ? readFileSync(join(STORE, 'git-trace'), 'utf8') : '';
  18. const logCalls = () => trace().split('\n').filter(l => /built-in: git (?:.* )?log /.test(l)).length;
  19. const HOST = (slug) => `${slug}.gitoria.test`;
  20. let failures = 0, passes = 0;
  21. function check(label, ok, detail = '') { console.log(`${ok ? 'ok ' : 'FAIL'} ${label}${ok ? '' : ' — ' + String(detail).slice(0, 400)}`); if (ok) passes++; else failures++; }
  22. const sleep = (ms) => new Promise(r => setTimeout(r, ms));
  23. const FAIL = join(STORE, 'fail'), WRAP = join(STORE, 'bin');
  24. const env = { ...process.env, PATH: WRAP + ':' + process.env.PATH, W53_FAIL_DIR: FAIL, GIT_TRACE: join(STORE, 'git-trace'), GITORIA_STORAGE: DB, GITORIA_GIT: GIT, GITORIA_PORT: String(PORT), GITORIA_WATCH: '0', GITORIA_PUBLIC_URL: `http://gitoria.test:${PORT}`, GITORIA_TICKETS_URL: 'http://127.0.0.1:1' };
  25. const BIN = join(APP, 'bin/hybriel');
  26. function raw(path, host) {
  27. return new Promise((res, rej) => {
  28. const r = httpRequest({ host: '127.0.0.1', port: PORT, path, headers: { host } }, (x) => { let b = ''; x.setEncoding('utf8'); x.on('data', d => b += d); x.on('end', () => res({ status: x.statusCode, body: b })); });
  29. r.on('error', rej); r.end();
  30. });
  31. }
  32. const gitEnv = { ...process.env, GIT_TERMINAL_PROMPT: '0', GIT_AUTHOR_NAME: 'Dev', GIT_AUTHOR_EMAIL: '[email protected]', GIT_COMMITTER_NAME: 'Dev', GIT_COMMITTER_EMAIL: '[email protected]' };
  33. function git(cwd, args, extra = {}) {
  34. const r = spawnSync('git', args, { cwd, encoding: 'utf8', env: { ...gitEnv, ...extra } });
  35. return { ok: r.status === 0, out: (r.stdout || '').trim(), err: (r.stderr || '').trim() };
  36. }
  37. const must = (cwd, args, extra) => { const r = git(cwd, args, extra); if (!r.ok) throw new Error('git ' + args.join(' ') + ': ' + r.err); return r.out; };
  38. const bare = (slug) => join(GIT, slug + '.git');
  39. const refOf = (slug, ref) => git(bare(slug), ['rev-parse', '--verify', '-q', ref]).out;
  40. const changes = () => existsSync(LOG) ? readFileSync(LOG, 'utf8').split('\n').filter(Boolean).map(l => JSON.parse(l)) : [];
  41. let seen = 0;
  42. const newChanges = () => { const all = changes(); const out = all.slice(seen); seen = all.length; return out; };
  43. let server = null, slog = '';
  44. const cleanup = () => { try { server && server.kill('SIGTERM'); } catch {} };
  45. process.on('exit', cleanup);
  46. try {
  47. if (!existsSync(BIN)) throw new Error('bin/hybriel missing');
  48. rmSync(STORE, { recursive: true, force: true }); mkdirSync(DB, { recursive: true }); mkdirSync(GIT, { recursive: true }); mkdirSync(WORK, { recursive: true });
  49. mkdirSync(FAIL); mkdirSync(WRAP);
  50. // One-shot failures affect only this server's git child, never the real git client or another gate.
  51. writeFileSync(join(WRAP, 'git'), `#!/bin/sh
  52. kind="$4"
  53. if [ "$4" = log ] && [ "$5" = '--format=%H%x09%s' ] && [ -f "$W53_FAIL_DIR/replacement" ]; then exit 128; fi
  54. case "$4 $5" in
  55. 'for-each-ref --contains') kind=source ;;
  56. 'for-each-ref --format=%(refname)%09%(objectname)') kind=fingerprint ;;
  57. 'for-each-ref --format=%(refname:short)') kind=branches ;;
  58. 'merge-base --is-ancestor')
  59. kind=ancestor
  60. if [ "\${#7}" = 40 ]; then kind=source-ancestor; fi ;;
  61. esac
  62. if [ -f "$W53_FAIL_DIR/$kind" ]; then
  63. rm "$W53_FAIL_DIR/$kind"
  64. exit 128
  65. fi
  66. exec /usr/bin/git "$@"
  67. `, { mode: 0o700 });
  68. // ---- seed: a user, two repos, a token (cold compile: a minute)
  69. const seed = (slug) => spawnSync(BIN, ['tests/pulls-seed.hl', slug], { cwd: APP, env, encoding: 'utf8' });
  70. const s1 = seed('pr-test');
  71. const TOKEN = (/TOKEN (gtr_[0-9a-f]{40})/.exec(s1.stdout) || [])[1];
  72. check('seed made a token', !!TOKEN, s1.stdout + s1.stderr);
  73. if (!TOKEN) throw new Error('no seed');
  74. const s2 = seed('old-prs');
  75. check('seed made the second repo', s2.status === 0, s2.stdout + s2.stderr);
  76. // ---- old-prs: PRs that exist BEFORE gitoria numbers them (pushed to the bare repo directly), with distinct commit times
  77. const OLD = join(WORK, 'old');
  78. mkdirSync(OLD);
  79. must(OLD, ['init', '-q', '-b', 'main']);
  80. const at = (d) => ({ GIT_AUTHOR_DATE: d, GIT_COMMITTER_DATE: d });
  81. writeFileSync(join(OLD, 'a.txt'), 'a\n'); must(OLD, ['add', '.']); must(OLD, ['commit', '-q', '-m', 'base'], at('2026-01-01T10:00:00Z'));
  82. // the NEWER PR branch is created first: numbers must follow the commit time, not the creation order
  83. must(OLD, ['checkout', '-q', '-b', 'newer', 'main']); writeFileSync(join(OLD, 'n.txt'), 'n\n'); must(OLD, ['add', '.']); must(OLD, ['commit', '-q', '-m', '|||PR newer one'], at('2026-03-01T10:00:00Z'));
  84. must(OLD, ['checkout', '-q', '-b', 'older', 'main']); writeFileSync(join(OLD, 'o.txt'), 'o\n'); must(OLD, ['add', '.']); must(OLD, ['commit', '-q', '-m', '|||PR older one'], at('2026-02-01T10:00:00Z'));
  85. must(OLD, ['push', '-q', bare('old-prs'), 'main', 'newer', 'older']);
  86. // ---- start the server
  87. server = spawn(BIN, ['project.hl'], { cwd: APP, env, stdio: ['ignore', 'pipe', 'pipe'] });
  88. server.stdout.on('data', d => slog += d); server.stderr.on('data', d => slog += d);
  89. for (let i = 0; i < 960; i++) { try { if ((await raw('/api/repos', 'gitoria.test')).status === 200) break; } catch {} await sleep(250); if (i === 959) throw new Error('server did not come up\n' + slog); }
  90. const URL = (slug) => `http://app:${TOKEN}@${HOST(slug)}:${PORT}/${slug}.git`;
  91. const RES = (slug) => ['-c', `http.curloptResolve=${HOST(slug)}:${PORT}:127.0.0.1`];
  92. const push = (cwd, slug, refs, extra = []) => { const t0 = Date.now(); const r = git(cwd, [...RES(slug), 'push', ...extra, URL(slug), ...refs]); return { ...r, ms: Date.now() - t0 }; };
  93. // ---- backfill: /pulls numbers the old ones, oldest commit first, and logs no change
  94. const page = await raw('/pulls', HOST('old-prs'));
  95. const numOf = (html, word) => { const m = new RegExp('class="number"[^>]*>#(\\d+)</span>[\\s\\S]{0,600}?' + word).exec(html); return m ? Number(m[1]) : null; };
  96. check('/pulls of old PRs: the older commit is #1', numOf(page.body, 'older one') === 1, String(numOf(page.body, 'older one')));
  97. check('/pulls of old PRs: the newer commit is #2', numOf(page.body, 'newer one') === 2, String(numOf(page.body, 'newer one')));
  98. check('backfill logs no change', changes().length === 0);
  99. check('backfill made refs/pull/1/head and /2/head', refOf('old-prs', 'refs/pull/1/head') === refOf('old-prs', 'refs/heads/older') && refOf('old-prs', 'refs/pull/2/head') === refOf('old-prs', 'refs/heads/newer'));
  100. // Recover after failures in each git read of a scan without changing the fingerprint again.
  101. const oldHead = refOf('old-prs', 'refs/heads/older');
  102. const tree = must(OLD, ['rev-parse', oldHead + '^{tree}']);
  103. const advanced = must(OLD, ['commit-tree', tree, '-p', oldHead, '-m', 'later source head']);
  104. for (const kind of ['fingerprint', 'branches', 'log', 'source', 'ancestor', 'source-ancestor']) {
  105. if (kind === 'source-ancestor') {
  106. must(OLD, ['push', '-q', bare('old-prs'), advanced + ':refs/heads/cache-source']);
  107. must(bare('old-prs'), ['update-ref', '-d', 'refs/heads/older']);
  108. } else {
  109. must(bare('old-prs'), ['update-ref', 'refs/heads/cache-' + kind, refOf('old-prs', 'refs/heads/main')]);
  110. }
  111. writeFileSync(join(FAIL, kind), 'fail once');
  112. await raw('/pulls', HOST('old-prs'));
  113. check(kind + ' fault was injected into the scan', !existsSync(join(FAIL, kind)));
  114. const beforeRecovery = logCalls();
  115. const recovered = await raw('/pulls', HOST('old-prs'));
  116. check(kind + ' failure is not cached: next view rescans and recovers', logCalls() > beforeRecovery && numOf(recovered.body, 'older one') === 1 && numOf(recovered.body, 'newer one') === 2);
  117. const afterRecovery = logCalls();
  118. await raw('/pulls', HOST('old-prs'));
  119. check(kind + ' recovered scan is cached (exit 1 means not an ancestor)', logCalls() === afterRecovery);
  120. if (kind === 'source-ancestor') {
  121. must(bare('old-prs'), ['update-ref', 'refs/heads/older', oldHead]);
  122. must(bare('old-prs'), ['update-ref', '-d', 'refs/heads/cache-source']);
  123. } else { must(bare('old-prs'), ['update-ref', '-d', 'refs/heads/cache-' + kind]); }
  124. }
  125. // ---- pr-test: first push (main only), then PRs
  126. const W = join(WORK, 'w');
  127. mkdirSync(W);
  128. must(W, ['init', '-q', '-b', 'main']);
  129. writeFileSync(join(W, 'a.txt'), 'a\n'); must(W, ['add', '.']); must(W, ['commit', '-q', '-m', 'base']);
  130. let r = push(W, 'pr-test', ['main']);
  131. check('first push (no PR) works', r.ok, r.err);
  132. check('no PR → no change', newChanges().length === 0);
  133. await raw('/pulls', HOST('pr-test')); // warm the pre-push list
  134. must(W, ['checkout', '-q', '-b', 'f1']); writeFileSync(join(W, 'f1.txt'), '1\n'); must(W, ['add', '.']);
  135. must(W, ['commit', '-q', '-m', '|||PR first thing', '-m', 'Body line one.\n\nFixes other-proj#7 and bare #12.']);
  136. const f1a = must(W, ['rev-parse', 'HEAD']);
  137. r = push(W, 'pr-test', ['f1']);
  138. check('push of a |||PR branch works', r.ok, r.err);
  139. const firstView = await raw('/pulls', HOST('pr-test'));
  140. check('HTTPS push appears on the next /pulls view', firstView.status === 200 && firstView.body.includes('first thing'));
  141. const afterFirst = logCalls();
  142. check('git trace observed the initial log calls', afterFirst > 0);
  143. const secondView = await raw('/pulls', HOST('pr-test'));
  144. check('second /pulls view makes no git log call', logCalls() === afterFirst);
  145. check('warm list preserves the first HTML', secondView.body.match(/<ol id="pulllist"[\s\S]*?<\/ol>/)?.[0] === firstView.body.match(/<ol id="pulllist"[\s\S]*?<\/ol>/)?.[0]);
  146. let ch = newChanges();
  147. check('opened #1', ch.length === 1 && ch[0].action === 'opened' && ch[0].number === 1 && ch[0].after === f1a && ch[0].before === null && ch[0].repoId, JSON.stringify(ch));
  148. check('sender is the pusher', ch.length === 1 && ch[0].sender && ch[0].sender.name === 'Gate Pusher', JSON.stringify(ch));
  149. check('refs/pull/1/head = the head when the change is logged', refOf('pr-test', 'refs/pull/1/head') === f1a);
  150. must(W, ['checkout', '-q', 'main']); must(W, ['checkout', '-q', '-b', 'f2']); writeFileSync(join(W, 'f2.txt'), '2\n'); must(W, ['add', '.']); must(W, ['commit', '-q', '-m', '|||PR|main] second thing']);
  151. const f2a = must(W, ['rev-parse', 'HEAD']);
  152. push(W, 'pr-test', ['f2']);
  153. ch = newChanges();
  154. check('second PR is #2', ch.length === 1 && ch[0].action === 'opened' && ch[0].number === 2, JSON.stringify(ch));
  155. // synchronize (new commit, not forced)
  156. must(W, ['checkout', '-q', 'f1']); writeFileSync(join(W, 'f1b.txt'), 'b\n'); must(W, ['add', '.']); must(W, ['commit', '-q', '-m', 'more']);
  157. const f1b = must(W, ['rev-parse', 'HEAD']);
  158. push(W, 'pr-test', ['f1']);
  159. ch = newChanges();
  160. check('synchronize: before/after, forced false', ch.length === 1 && ch[0].action === 'synchronize' && ch[0].number === 1 && ch[0].before === f1a && ch[0].after === f1b && ch[0].forced === false, JSON.stringify(ch));
  161. check('ref follows the new head', refOf('pr-test', 'refs/pull/1/head') === f1b);
  162. // force push (amend the second commit)
  163. must(W, ['commit', '-q', '--amend', '-m', 'more, amended']);
  164. const f1c = must(W, ['rev-parse', 'HEAD']);
  165. r = push(W, 'pr-test', ['f1'], ['--force']);
  166. check('force push works', r.ok, r.err);
  167. ch = newChanges();
  168. check('synchronize forced true', ch.length === 1 && ch[0].action === 'synchronize' && ch[0].forced === true && ch[0].before === f1b && ch[0].after === f1c, JSON.stringify(ch));
  169. // every earlier head stays referenced (refs/pull/N/heads/<sha>): git gc --prune=now must not drop the heads f1a and f1b (both replaced, f1b by a force-push)
  170. const gc = git(bare('pr-test'), ['gc', '-q', '--prune=now']);
  171. check('git gc --prune=now ran', gc.ok, gc.err);
  172. check('after gc the replaced heads still resolve (f1a, f1b)', git(bare('pr-test'), ['cat-file', '-e', f1a + '^{commit}']).ok && git(bare('pr-test'), ['cat-file', '-e', f1b + '^{commit}']).ok);
  173. check('refs/pull/1/heads/<sha> exist for every head', [f1a, f1b, f1c].every(h => refOf('pr-test', `refs/pull/1/heads/${h}`) === h));
  174. r = push(W, 'pr-test', [`main:refs/pull/1/heads/${'a'.repeat(40)}`]);
  175. check('a push to refs/pull/N/heads/* is refused too', !r.ok, r.err);
  176. // refs/pull/* cannot be pushed (create, update, delete), and the ref stays
  177. const before1 = refOf('pr-test', 'refs/pull/1/head');
  178. for (const [what, refspec] of [['update', 'main:refs/pull/1/head'], ['create', 'main:refs/pull/99/head'], ['create (+force)', '+main:refs/pull/98/head'], ['delete', ':refs/pull/1/head']]) {
  179. r = push(W, 'pr-test', [refspec]);
  180. check(`push refused: ${what} ${refspec}`, !r.ok && /refs\/pull|hidden|deny|refus|rejected/i.test(r.err), r.err);
  181. }
  182. check('refs/pull/1/head unchanged after the attempts', refOf('pr-test', 'refs/pull/1/head') === before1 && refOf('pr-test', 'refs/pull/99/head') === '' && refOf('pr-test', 'refs/pull/98/head') === '');
  183. check('refused pushes made no change', newChanges().length === 0);
  184. // fetch of the head over HTTPS works, and clone does not make branches of it
  185. const F = join(WORK, 'f'); mkdirSync(F); must(F, ['init', '-q']);
  186. r = git(F, [...RES('pr-test'), 'fetch', '-q', URL('pr-test'), 'refs/pull/1/head']);
  187. check('fetch refs/pull/1/head over HTTPS', r.ok && must(F, ['rev-parse', 'FETCH_HEAD']) === f1c, r.err);
  188. // close: delete the branch; reopen: push it again; the number stays
  189. r = push(W, 'pr-test', [':f2']);
  190. ch = newChanges();
  191. check('branch deleted → closed #2', ch.length === 1 && ch[0].action === 'closed' && ch[0].number === 2, JSON.stringify(ch));
  192. push(W, 'pr-test', ['f2']);
  193. ch = newChanges();
  194. check('branch back → reopened #2 (same number)', ch.length === 1 && ch[0].action === 'reopened' && ch[0].number === 2 && ch[0].after === f2a, JSON.stringify(ch));
  195. // the |||PR commit gone from the branch (force-push of f2 to main's commit)
  196. push(W, 'pr-test', ['+main:f2']);
  197. check('force push removing a marker removes its list row on the next view', !(await raw('/pulls', HOST('pr-test'))).body.includes('second thing'));
  198. ch = newChanges();
  199. check('|||PR commit gone from the branch → closed #2', ch.length === 1 && ch[0].action === 'closed' && ch[0].number === 2, JSON.stringify(ch));
  200. // merged: main takes f1 with a merge commit
  201. must(W, ['checkout', '-q', 'main']); must(W, ['merge', '-q', '--no-ff', 'f1', '-m', 'merge f1']);
  202. const mc = must(W, ['rev-parse', 'HEAD']);
  203. await raw('/pulls', HOST('pr-test')); // cached open state
  204. push(W, 'pr-test', ['main']);
  205. const mergedPage = await raw('/pulls', HOST('pr-test'));
  206. check('merged state appears on the next view', /class="merged"[^>]*>merged/.test(mergedPage.body));
  207. ch = newChanges();
  208. check('push into the target → merged #1 with mergeCommit', ch.length === 1 && ch[0].action === 'merged' && ch[0].number === 1 && ch[0].mergeCommit === mc, JSON.stringify(ch));
  209. push(W, 'pr-test', ['main']);
  210. check('merged is final: a second push changes nothing', newChanges().length === 0);
  211. // a new PR gets #3, never a reused number
  212. must(W, ['checkout', '-q', '-b', 'f3']); writeFileSync(join(W, 'f3.txt'), '3\n'); must(W, ['add', '.']); must(W, ['commit', '-q', '-m', '|||PR third']);
  213. const f3a = must(W, ['rev-parse', 'HEAD']);
  214. const t3 = push(W, 'pr-test', ['f3']);
  215. ch = newChanges();
  216. check('next PR is #3 (numbers are never reused)', ch.length === 1 && ch[0].action === 'opened' && ch[0].number === 3, JSON.stringify(ch));
  217. console.log(` (a push that opens a PR took ${t3.ms} ms)`);
  218. // old-prs: a live PR after the backfill is #3
  219. const O2 = join(WORK, 'o2'); must(WORK, ['clone', '-q', bare('old-prs'), O2]);
  220. must(O2, ['checkout', '-q', '-b', 'live', 'main']); writeFileSync(join(O2, 'l.txt'), 'l\n'); must(O2, ['add', '.']); must(O2, ['commit', '-q', '-m', '|||PR live']);
  221. push(O2, 'old-prs', ['live']);
  222. ch = newChanges();
  223. check('old-prs: a new PR after the backfill is #3 and opened', ch.length === 1 && ch[0].number === 3 && ch[0].action === 'opened', JSON.stringify(ch));
  224. // ---- the page: "#N" on every row, no refs/pull anywhere as a branch
  225. const pg = await raw('/pulls', HOST('pr-test'));
  226. check('/pulls shows #1 (merged) and #3 next to their titles (a closed PR is no longer found in git: not listed)', numOf(pg.body, 'first thing') === 1 && numOf(pg.body, 'third') === 3, `${numOf(pg.body, 'first thing')} ${numOf(pg.body, 'third')}`);
  227. const code = await raw('/code', HOST('pr-test'));
  228. const br = await raw('/branch/f1', HOST('pr-test'));
  229. check('no refs/pull in the code and branch pages', !/refs\/pull|pull\/\d+\/head/.test(code.body + br.body + pg.body));
  230. check('the branch list has no pull entries', !/>\s*pull\/|href="[^"]*\/branch\/pull/.test(code.body));
  231. check('git branch -a of the bare repo has no pull ref', !/pull/.test(must(bare('pr-test'), ['for-each-ref', '--format=%(refname)', 'refs/heads', 'refs/remotes'])));
  232. // W63: rewrite the marker itself, rather than a later commit.
  233. must(W, ['checkout', '-q', 'f3']);
  234. must(W, ['commit', '-q', '--amend', '-m', '|||PR third amended']);
  235. const f3b = must(W, ['rev-parse', 'HEAD']);
  236. r = push(W, 'pr-test', ['f3'], ['--force']);
  237. ch = newChanges();
  238. check('W63 marker amend keeps #3 with one forced synchronize', r.ok && ch.length === 1 && ch[0].number === 3 && ch[0].action === 'synchronize' && ch[0].forced === true && ch[0].before === f3a && ch[0].after === f3b, JSON.stringify(ch));
  239. const amendedPage = await raw('/pulls', HOST('pr-test'));
  240. check('W63 list keeps #3 and has no #4', numOf(amendedPage.body, 'third amended') === 3 && !amendedPage.body.includes('>#4</span>'));
  241. const rewritten = spawnSync(BIN, ['tests/pulls-probe.hl', 'pr-test', '3', '3:' + f3a], {cwd: APP, env, encoding: 'utf8'});
  242. const rewrittenPull = (rewritten.stdout.match(/^PULL .*$/gm) || []).map(l => JSON.parse(l.slice(5)))[2];
  243. check('W63 stays open with new title/head and retains old head', rewrittenPull?.state === 'open' && rewrittenPull.title === 'third amended' && rewrittenPull.head === f3b && rewritten.stdout.includes('WAS 3:' + f3a + ' true'), rewritten.stdout);
  244. must(W, ['commit', '-q', '--allow-empty', '-m', 'after rewrite']);
  245. const f3c = must(W, ['rev-parse', 'HEAD']);
  246. push(W, 'pr-test', ['f3']); ch = newChanges();
  247. check('W63 ordinary push after rewrite synchronizes without forced', ch.length === 1 && ch[0].number === 3 && ch[0].action === 'synchronize' && ch[0].forced === false && ch[0].after === f3c, JSON.stringify(ch));
  248. // ---- the contract's pull object + wasHeadOf (a second process on the same tables)
  249. const pr = spawnSync(BIN, ['tests/pulls-probe.hl', 'pr-test', '3', `1:${f1a}`, `1:${f1b}`, `1:${f1c}`, `1:${'0'.repeat(40)}`, `2:${f2a}`, `3:${f1a}`], { cwd: APP, env, encoding: 'utf8' });
  250. const pulls = (pr.stdout.match(/^PULL .*$/gm) || []).map(l => JSON.parse(l.slice(5)));
  251. const was = Object.fromEntries((pr.stdout.match(/^WAS .*$/gm) || []).map(l => { const p = l.split(' '); return [p[1], p[2]]; }));
  252. check('pullObject: 3 pulls and none for number 4', pulls.length === 4 && pulls[3] === null, pr.stdout + pr.stderr);
  253. const p1 = pulls[0] || {}, p3 = pulls[2] || {};
  254. check('pull #1 title/body/state', p1.number === 1 && p1.title === 'first thing' && /Body line one\./.test(p1.body) && p1.state === 'merged' && p1.draft === false, JSON.stringify(p1));
  255. check('pull #1 refs and shas', p1.source === 'f1' && p1.target === 'main' && p1.head === f1c && p1.ref === 'refs/pull/1/head' && p1.baseRef === 'refs/heads/main' && p1.base === mc && /^[0-9a-f]{40}$/.test(p1.mergeBase), JSON.stringify(p1));
  256. check('pull #1 author, url, times', p1.author && p1.author.name === 'Gate Pusher' && /^[0-9a-z]{12}$/.test(p1.author.id || '') && p1.url === `http://pr-test.gitoria.test:${PORT}/pulls/1` && /^\d{4}-\d\d-\d\dT/.test(p1.created) && /^\d{4}-\d\d-\d\dT/.test(p1.updated), JSON.stringify(p1));
  257. check('pull #1 tickets: explicit project#n only (repo not connected)', JSON.stringify(p1.tickets) === JSON.stringify(['other-proj#7']), JSON.stringify(p1.tickets));
  258. check('pull #3 open, source f3, head', p3.state === 'open' && p3.source === 'f3' && p3.head === f3c && p3.title === 'third amended', JSON.stringify(p3));
  259. check('wasHeadOf: every head #1 had is true', was[`1:${f1a}`] === 'true' && was[`1:${f1b}`] === 'true' && was[`1:${f1c}`] === 'true', JSON.stringify(was));
  260. check('wasHeadOf: a sha that never was one is false (also another PR\'s head)', was[`1:${'0'.repeat(40)}`] === 'false' && was[`3:${f1a}`] === 'false', JSON.stringify(was));
  261. check('wasHeadOf: #2 knows its head', was[`2:${f2a}`] === 'true', JSON.stringify(was));
  262. // A manual close must not be paired with its rewritten marker.
  263. must(W, ['checkout', '-q', '-b', 'closed-rewrite', 'main']);
  264. must(W, ['commit', '-q', '--allow-empty', '-m', '|||PR manually closed original']);
  265. push(W, 'pr-test', ['closed-rewrite']); newChanges();
  266. const close = spawnSync(BIN, ['tests/pull-close-process.hl', 'pr-test', '4'], {cwd: APP, env, encoding: 'utf8'});
  267. check('W63 manual close setup', close.stdout.includes('"ok":true'), close.stdout);
  268. newChanges();
  269. must(W, ['commit', '-q', '--allow-empty', '--amend', '-m', '|||PR manually closed rewritten']);
  270. push(W, 'pr-test', ['closed-rewrite'], ['--force']); ch = newChanges();
  271. const closedProbe = spawnSync(BIN, ['tests/pulls-probe.hl', 'pr-test', '5'], {cwd: APP, env, encoding: 'utf8'});
  272. const closedPulls = (closedProbe.stdout.match(/^PULL .*$/gm) || []).map(l => JSON.parse(l.slice(5)));
  273. check('W63 rewritten manually closed PR stays closed; new marker gets new number', closedPulls[3]?.state === 'closed' && ch.length === 1 && ch[0].action === 'opened' && ch[0].number === 5, JSON.stringify(ch));
  274. // Two new markers are ambiguous: close the old PR and open both new ones.
  275. must(W, ['checkout', '-q', '-b', 'ambiguous', 'main']);
  276. must(W, ['commit', '-q', '--allow-empty', '-m', '|||PR ambiguous original']);
  277. push(W, 'pr-test', ['ambiguous']); newChanges();
  278. must(W, ['reset', '--hard', 'main']);
  279. must(W, ['commit', '-q', '--allow-empty', '-m', '|||PR ambiguous new one']);
  280. must(W, ['commit', '-q', '--allow-empty', '-m', '|||PR ambiguous new two']);
  281. push(W, 'pr-test', ['ambiguous'], ['--force']); ch = newChanges();
  282. check('W63 two new markers retain close plus two opens behavior', ch.length === 3 && ch.filter(c => c.action === 'closed' && c.number === 6).length === 1 && ch.filter(c => c.action === 'opened').length === 2, JSON.stringify(ch));
  283. // A failed replacement scan must defer both closure and discovery.
  284. must(W, ['checkout', '-q', '-b', 'scan-failure', 'main']);
  285. must(W, ['commit', '-q', '--allow-empty', '-m', '|||PR scan original']);
  286. push(W, 'pr-test', ['scan-failure']); newChanges();
  287. const inspect = () => spawnSync(BIN, ['tests/pull-close-process.hl', 'pr-test', '9', 'inspect'], {cwd: APP, env, encoding: 'utf8'});
  288. const scanRow = JSON.parse(inspect().stdout.match(/^ROW (.*)$/m)[1]);
  289. must(W, ['commit', '-q', '--allow-empty', '--amend', '-m', '|||PR scan rewritten']);
  290. const scanHead = must(W, ['rev-parse', 'HEAD']);
  291. writeFileSync(join(FAIL, 'replacement'), 'fail replacement log');
  292. push(W, 'pr-test', ['scan-failure'], ['--force']); ch = newChanges();
  293. const deferred = JSON.parse(inspect().stdout.match(/^ROW (.*)$/m)[1]);
  294. check('W63 failed replacement log defers without closing or opening', ch.length === 0 && deferred.state === 'open' && deferred.head === scanRow.head, JSON.stringify(ch));
  295. check('W63 failed replacement log queues resync', existsSync(join(DB, 'pull-resync', scanRow.rid)));
  296. rmSync(join(FAIL, 'replacement'));
  297. await raw('/api/pulls/9', HOST('pr-test')); ch = newChanges();
  298. check('W63 read retries failed scan with same number', ch.length === 1 && ch[0].number === 9 && ch[0].action === 'synchronize' && ch[0].forced && ch[0].after === scanHead, JSON.stringify(ch));
  299. // Two different branches cannot both claim one replacement marker.
  300. for (const branch of ['pair-a', 'pair-b']) {
  301. must(W, ['checkout', '-q', '-b', branch, 'main']);
  302. must(W, ['commit', '-q', '--allow-empty', '-m', '|||PR ' + branch]);
  303. push(W, 'pr-test', [branch]); newChanges();
  304. }
  305. must(W, ['checkout', '-q', '-b', 'common-replacement', 'main']);
  306. must(W, ['commit', '-q', '--allow-empty', '-m', '|||PR common replacement']);
  307. push(W, 'pr-test', ['+HEAD:pair-a', '+HEAD:pair-b']); ch = newChanges();
  308. check('W63 shared replacement closes both old PRs and opens one new PR', ch.length === 3 && ch.filter(c => c.action === 'closed' && [10,11].includes(c.number)).length === 2 && ch.filter(c => c.action === 'opened' && c.number === 12).length === 1, JSON.stringify(ch));
  309. // Rewriting and merging in one push keeps the number and reports merged.
  310. must(W, ['checkout', '-q', '-b', 'rewrite-merge', 'main']);
  311. must(W, ['commit', '-q', '--allow-empty', '-m', '|||PR before combined merge']);
  312. push(W, 'pr-test', ['rewrite-merge']); newChanges();
  313. must(W, ['commit', '-q', '--allow-empty', '--amend', '-m', '|||PR rewritten and merged']);
  314. const mergedRewrite = must(W, ['rev-parse', 'HEAD']);
  315. push(W, 'pr-test', ['+HEAD:rewrite-merge', 'HEAD:main']); ch = newChanges();
  316. check('W63 rewritten marker merged in same push keeps #13 merged', ch.some(c => c.number === 13 && c.action === 'merged' && c.after === mergedRewrite && c.mergeCommit === mergedRewrite) && !ch.some(c => c.number === 13 && c.action === 'closed') && !ch.some(c => c.action === 'opened'), JSON.stringify(ch));
  317. } catch (e) {
  318. check('gate ran to the end', false, String(e && e.stack || e));
  319. } finally {
  320. cleanup();
  321. }
  322. if (failures) console.log('--- server log (tail) ---\n' + slog.split('\n').slice(-40).join('\n'));
  323. console.log(`\n${passes} passed, ${failures} failed`);
  324. process.exit(failures ? 1 : 0);

Branches

Latest commits

  • 0f39e181Merge branch 'w64-org-slash' into mainGitoria
  • 9c72d0bf|||PR org repo addresses work with a trailing slashmre
  • a4a33081Merge branch 'w63-force-push' into mainGitoria
  • 52576cb0force push: defer on a failed scan, one-to-one pairs, docs (#56)mre
  • 5a7b3e68|||PR a force push keeps the same pull request (#56)mre
  • cc1be2d6Merge branch 'w62-pr-close' into mainGitoria
  • 6e03be0apull state: keep the resync marker until the sync ran (#56)mre
  • 93bdd26cpull state: wait for a claimed row, resync when it stays busy (#56)mre
  • e53d284bpull state: stop on a failed branch scan, claim the row on writes (#56)mre
  • d8608224close and reopen: keep a manual close against sync (#56)mre
  • ffb60746|||PR close and reopen pull requests (#56)mre
  • 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