// BTC4Hits bridge + admin spin links (2026-11-11). Boots a throwaway LinkSpin (temp DATA_DIR, JSON stores, no mail key, // ADMIN_PASSWORD=localtest) and checks: // /from/btc4hits: 200, the sponsor cookie is cleared, angle b4h + forced first-touch source legacy:btc4hits, sign-in copy, // no em dash in the copy; a sign-up through it gets NO welcome-back credits (legacy grant rule is fw/t1 only) // /api/admin/rotations: POST with urls[] makes one spin link with every destination at weight 1, GET lists links + sources, // DELETE removes it; auth required. // node qa/btc4hits-bridge.test.mjs (B4H_PORT to move the port) import { spawn } from 'node:child_process'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const SITE = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); const PORT = Number(process.env.B4H_PORT || 8831), BASE = 'http://127.0.0.1:' + PORT; const sleep = ms => new Promise(r => setTimeout(r, ms)); let pass = 0, fail = 0; const ok = (c, n, x) => { if (c) { pass++; console.log('ok ' + n); } else { fail++; console.log('FAIL ' + n + (x ? '\n ' + x : '')); } }; const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'b4h-ls-')); fs.mkdirSync(path.join(dir, 'uploads'), { recursive: true }); const now = Date.now(); fs.writeFileSync(path.join(dir, 'accounts.json'), JSON.stringify({ v: 2, byEmail: { 'owner@qa.test': { email: 'owner@qa.test', pass: null, sponsorRef: '', code: 'cowner1', username: 'owner', address: null, created: now - 86400000, joinedVia: 'carried', joinedRef: 'network' } }, byAddress: {}, byCode: { cowner1: 'owner@qa.test' }, joins: 0, positions: {} })); const env = Object.assign({}, process.env, { PORT: String(PORT), DATA_DIR: dir, ADMIN_EMAIL: 'admin@qa.test', ADMIN_PASSWORD: 'localtest', DATABASE_URL: '', SENDGRID_API_KEY: '', SENDGRID_KEY: '', OUTBOUND: '', LS_SYNC_SECRET: '', ENGINE_KEY: '', ENGINE_KEY_FILE: '' }); const log = path.join(dir, 'server.log'); const proc = spawn(process.execPath, ['server.js'], { cwd: SITE, env, stdio: ['ignore', fs.openSync(log, 'w'), fs.openSync(log, 'a')] }); const admin = (pth, method, body, auth = true) => fetch(BASE + pth, { method, headers: Object.assign({ 'Content-Type': 'application/json' }, auth ? { Authorization: 'Bearer localtest' } : {}), body: body ? JSON.stringify(body) : undefined }); try { let up = false; for (let i = 0; i < 40 && !up; i++) { try { up = (await fetch(BASE + '/api/config', { signal: AbortSignal.timeout(2000) })).status < 500; } catch (e) { await sleep(1000); } } ok(up, 'local LinkSpin up'); // 1. the bridge page const r = await fetch(BASE + '/from/btc4hits?src=splash', { headers: { Cookie: 'iap.sponsor=someone' } }); const html = await r.text(); const ck = r.headers.getSetCookie ? r.headers.getSetCookie() : []; ok(r.status === 200, '/from/btc4hits answers 200', String(r.status)); ok(ck.some(c => /^iap\.sponsor=;/.test(c) && /Max-Age=0/.test(c)), 'sponsor cookie cleared (no sponsor from the bridge itself)', ck.join(' | ')); ok(ck.some(c => /^iap\.angle=b4h;/.test(c)), 'angle b4h remembered'); ok(ck.some(c => c.startsWith('iap.ref=' + encodeURIComponent('legacy:btc4hits:splash') + ';')), 'forced first-touch source legacy:btc4hits', ck.join(' | ')); ok(/
redirects to one of them', red.status + ' ' + red.headers.get('location'));
const d = await admin('/api/admin/rotations?email=owner@qa.test&id=' + rot.id, 'DELETE');
ok(d.status === 200, 'DELETE removes it');
const g2 = await (await admin('/api/admin/rotations?email=owner@qa.test', 'GET')).json();
ok(!(g2.rotations || []).length, 'gone after DELETE');
ok((await admin('/api/admin/rotations?email=nobody@qa.test', 'GET')).status === 404, 'unknown member -> 404');
} finally { proc.kill(); }
console.log('\n' + pass + ' passed, ' + fail + ' failed');
process.exit(fail ? 1 : 0);