'use strict'; // Offline tests for set-github-secrets.sh: stub `gh` and `curl` on PATH, dummy // values only, no network. const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); const { spawnSync } = require('node:child_process'); const script = path.join(__dirname, 'set-github-secrets.sh'); const NAMES = [ 'TANGLED_REPO_DID', 'TANGLED_REPO_REPO_DID', 'R2_ACCOUNT_ID', 'R2_BUCKET', 'R2_ACCESS_KEY_ID', 'R2_SECRET_ACCESS_KEY', 'R2_PUBLIC_URL', 'ATP_APP_PASSWORD', ]; const DUMMY = { SPINDLE_SERVER_OWNER: 'did:plc:dummy-owner', TANGLED_REPO_REPO_DID: 'did:plc:dummy-repo', R2_ACCOUNT_ID: 'dummy-r2-account', R2_BUCKET: 'dummy-r2-bucket', R2_ACCESS_KEY_ID: 'dummy-r2-key-id', R2_SECRET_ACCESS_KEY: 'dummy-r2-secret', R2_PUBLIC_URL: 'https://dummy.example.invalid', ATP_APP_PASSWORD: 'dummy-atp-password', }; function setup() { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'set-gh-secrets-')); const bin = path.join(dir, 'bin'); const home = path.join(dir, 'home'); const log = path.join(dir, 'log'); for (const d of [bin, home, log]) fs.mkdirSync(d); const stub = (name, body) => { fs.writeFileSync(path.join(bin, name), `#!/usr/bin/env bash\n${body}\n`, { mode: 0o755 }); }; // gh: record argv line and stdin per call. stub( 'gh', `echo "$*" >> "${log}/gh-argv"\ncat >> "${log}/gh-stdin"\necho >> "${log}/gh-stdin"` ); stub('curl', `echo "$*" >> "${log}/curl-argv"\necho '{}'`); return { dir, bin, home, log }; } function run(args, env, ctx = setup()) { const r = spawnSync('bash', [script, ...args], { encoding: 'utf8', env: { PATH: `${ctx.bin}:${process.env.PATH}`, HOME: ctx.home, ...env }, }); const read = (f) => { const p = path.join(ctx.log, f); return fs.existsSync(p) ? fs.readFileSync(p, 'utf8') : ''; }; return { ...r, ghArgv: read('gh-argv'), ghStdin: read('gh-stdin'), curlArgv: read('curl-argv'), ctx }; } test('sets the eight names in order, values on stdin only', () => { const r = run([], DUMMY); assert.equal(r.status, 0, r.stderr); const calls = r.ghArgv.trim().split('\n'); assert.equal(calls.length, 8); calls.forEach((line, i) => assert.ok(line.startsWith(`secret set ${NAMES[i]} `), line)); assert.ok(r.ghStdin.includes('dummy-r2-key-id')); const stdinValues = r.ghStdin.split('\n').filter(Boolean); assert.equal(stdinValues[0], DUMMY.SPINDLE_SERVER_OWNER); assert.equal(stdinValues[4], 'dummy-r2-key-id'); for (const v of Object.values(DUMMY)) { assert.ok(!r.ghArgv.includes(v), `argv leaked ${v}`); assert.ok(!r.stdout.includes(v), `stdout leaked ${v}`); assert.ok(!r.stderr.includes(v), `stderr leaked ${v}`); assert.ok(!r.curlArgv.includes(v), `curl argv leaked ${v}`); } }); test('never invokes gh auth', () => { const r = run([], DUMMY); assert.ok(!/(^|\s)auth(\s|$)/.test(r.ghArgv)); }); test('refuses empty and unresolved values before any gh or curl call', () => { const bad = { empty: '', pass: 'pass://vault/item', op: 'op://vault/item/field', secret: 'secret://x/y', dollar: '${R2_KEY}', braces: '{{ r2_key }}', placeholder: 'REPLACE_ME', placeholder2: 'replace-me-with-key', }; for (const name of [ 'SPINDLE_SERVER_OWNER', 'R2_ACCOUNT_ID', 'R2_BUCKET', 'R2_ACCESS_KEY_ID', 'R2_SECRET_ACCESS_KEY', 'R2_PUBLIC_URL', 'ATP_APP_PASSWORD', ]) { for (const [kind, value] of Object.entries(bad)) { const r = run([], { ...DUMMY, [name]: value }); assert.notEqual(r.status, 0, `${name}/${kind} should be refused`); assert.ok(r.stderr.includes(name), `${name}/${kind}: message names the variable`); if (value) assert.ok(!r.stderr.includes(value), `${name}/${kind}: message leaks value`); assert.equal(r.ghArgv, '', `${name}/${kind}: gh called`); assert.equal(r.curlArgv, '', `${name}/${kind}: curl called`); } } }); test('--offline makes no curl or gh call and succeeds with metadata supplied', () => { const r = run(['--offline'], { ...DUMMY, PDS_ENDPOINT: 'https://pds.example.invalid' }); assert.equal(r.status, 0, r.stderr); assert.equal(r.curlArgv, ''); assert.equal(r.ghArgv, ''); assert.match(r.stdout, /offline/); for (const n of NAMES) assert.ok(r.stdout.includes(`${n} (`)); for (const v of Object.values(DUMMY)) assert.ok(!r.stdout.includes(v)); }); test('--offline without the repo DID stops and names what it would fetch', () => { const env = { ...DUMMY }; delete env.TANGLED_REPO_REPO_DID; const r = run(['--offline'], env); assert.notEqual(r.status, 0); assert.match(r.stderr, /TANGLED_REPO_REPO_DID/); assert.equal(r.curlArgv, ''); assert.equal(r.ghArgv, ''); }); test('environment path does not depend on local files (empty HOME)', () => { const ctx = setup(); assert.deepEqual(fs.readdirSync(ctx.home), []); const r = run([], DUMMY, ctx); assert.equal(r.status, 0, r.stderr); assert.equal(r.ghArgv.trim().split('\n').length, 8); assert.deepEqual(fs.readdirSync(ctx.home), []); }); test('falls back to local files when the environment is unset', () => { const ctx = setup(); const cfg = path.join(ctx.home, '.config'); fs.mkdirSync(path.join(cfg, 'entangle'), { recursive: true }); fs.mkdirSync(path.join(cfg, 'spindle'), { recursive: true }); fs.writeFileSync( path.join(cfg, 'peek-releases-r2.env'), ['R2_ACCOUNT_ID', 'R2_BUCKET', 'R2_ACCESS_KEY_ID', 'R2_SECRET_ACCESS_KEY', 'R2_PUBLIC_URL'] .map((k) => `${k}=${DUMMY[k]}`) .join('\n') ); fs.writeFileSync(path.join(cfg, 'entangle', 'env'), `TANGLED_APP_PASSWORD=${DUMMY.ATP_APP_PASSWORD}\n`); fs.writeFileSync(path.join(cfg, 'spindle', 'spindle.conf'), `SPINDLE_SERVER_OWNER=${DUMMY.SPINDLE_SERVER_OWNER}\n`); const r = run([], { TANGLED_REPO_REPO_DID: DUMMY.TANGLED_REPO_REPO_DID }, ctx); assert.equal(r.status, 0, r.stderr); assert.ok(r.ghStdin.includes('dummy-atp-password')); });