import test from 'node:test'; import assert from 'node:assert/strict'; import { mkdtempSync, writeFileSync, chmodSync, rmSync } from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { spawnSync } from 'node:child_process'; const helper = fileURLToPath(new URL('../../../scripts/gitea-api.sh', import.meta.url)); for (const [method, status, expected] of [['GET', '200', 0], ['POST', '201', 0], ['GET', '403', 1]]) { test(`real helper ${method} HTTP ${status} preserves exit ${expected} without credentials`, t => { const dir = mkdtempSync(path.join(os.tmpdir(), 'ledger-helper-')); t.after(() => rmSync(dir, { recursive: true, force: true })); function tool(name, content) { const p = path.join(dir, name); writeFileSync(p, '#!/usr/bin/env bash\n' + content); chmodSync(p, 0o755); } // Stub node before the helper can read credential material. No auth file exists. tool('node', 'printf %s https://git.mosaicstack.dev\n'); tool('git', 'printf %s https://git.mosaicstack.dev/mosaicstack/stack.git\n'); tool('curl', 'while (($#)); do if [[ "$1" == -o ]]; then shift; out="$1"; fi; shift; done\nprintf "[]" > "$out"\nprintf %s "$FAKE_HTTP"\n'); const r = spawnSync('bash', [helper, method, 'repos/mosaicstack/stack/issues', ...(method === 'POST' ? ['{}'] : [])], { encoding: 'utf8', env: { ...process.env, PATH: `${dir}:${process.env.PATH}`, MOSAIC_GITEA_CREDENTIAL_FILE: `${dir}/nonexistent`, FAKE_HTTP: status } }); assert.equal(r.status, expected, r.stderr); assert.equal(r.stdout, '[]'); assert.match(r.stderr, new RegExp(`HTTP ${status}`)); }); }