// DownlineHydra tracked program links (/api/partner/dh/links, Marty 2026-11-15). // Boots server.js on its own port with a throwaway seeded data dir (JSON store, no DB, no mail key, no chain keys) and checks: // the NETWORK_SSO_SECRET signature, account by email then by network username, one link per program on the member's own account // with the "DownlineHydra" source, the program id as sub-id, the destination following a changed referral URL (same code), the // clean link host, https-only destinations, the redirect keeping the referral ID intact, and clicks per program in the stats. // node qa/partner-dhlinks.test.mjs (from the site dir; PORT default 8973) import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import crypto from 'node:crypto'; import { spawn } from 'node:child_process'; const PORT = Number(process.env.PORT || 8973); const BASE = 'http://127.0.0.1:' + PORT; const SEC = crypto.randomBytes(32).toString('hex'); let pass = 0, fail = 0; const ok = (c, what, x) => { if (c) { pass++; console.log(' ok ' + what); } else { fail++; console.log(' FAIL ' + what + (x !== undefined ? ' -> ' + JSON.stringify(x).slice(0, 400) : '')); } }; const DATA = fs.mkdtempSync(path.join(os.tmpdir(), 'dh-qa-')); const now = Date.now(); const acct = (email, username, memberId) => ({ email, username, code: username.slice(0, 7), memberId, created: now - 86400000, sponsorRef: '' }); const accounts = { v: 2, byEmail: { 'one@dh.test': acct('one@dh.test', 'dhone', 920001), 'two@dh.test': acct('two@dh.test', 'dhtwo', 920002) }, byAddress: {}, byCode: {}, joins: 2, positions: {} }; for (const a of Object.values(accounts.byEmail)) accounts.byCode[a.code] = a.email; fs.writeFileSync(path.join(DATA, 'accounts.json'), JSON.stringify(accounts)); fs.writeFileSync(path.join(DATA, 'site.json'), JSON.stringify({ linkDomains: 'spin-links.test.org' })); const env = Object.assign({}, process.env, { PORT: String(PORT), DATA_DIR: DATA, NETWORK_SSO_SECRET: SEC, ADMIN_PASSWORD: 'x'.repeat(16), ADMIN_EMAIL: 'admin@dh.test' }); for (const k of ['NODE_ENV', 'DATABASE_URL', 'ENGINE_KEY', 'LS_SYNC_SECRET', 'OUTBOUND', 'LS_TESTERS', 'CURTAIN', 'HUB_ENABLED', 'HUB_URL']) delete env[k]; const site = path.resolve(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')), '..'); const srv = spawn(process.execPath, ['server.js'], { cwd: site, env, stdio: ['ignore', 'ignore', 'ignore'] }); const sleep = ms => new Promise(r => setTimeout(r, ms)); async function call(body, o = {}) { const raw = JSON.stringify(body); const ts = String(o.ts || Date.now()); const sig = crypto.createHmac('sha256', o.sec || SEC).update('net.v1.' + ts + '\n' + raw).digest('hex'); const r = await fetch(BASE + '/api/partner/dh/links', { method: 'POST', body: raw, headers: { 'Content-Type': 'application/json', 'X-Net-Ts': ts, 'X-Net-Sig': sig } }); return Object.assign({ _status: r.status }, await r.json().catch(() => ({}))); } try { for (let i = 0; i < 60; i++) { try { if ((await fetch(BASE + '/api/config')).ok) break; } catch (e) {} await sleep(500); } const L1 = [{ program: 'ctb19', name: 'Ads Earn Network', url: 'https://www.adsearnnetwork.com/?one19' }, { program: 'mh3', name: 'My Mailer', url: 'https://mymailer.example.org/r/one' }]; let r = await call({ email: 'one@dh.test', op: 'ensure', links: L1 }, { sec: 'y'.repeat(64) }); ok(r._status === 401, 'a wrong signature is refused'); r = await call({ email: 'one@dh.test', op: 'ensure', links: L1 }, { ts: Date.now() - 600000 }); ok(r._status === 401, 'an old timestamp is refused'); r = await call({ email: 'nobody@dh.test', op: 'ensure', links: L1 }); ok(r._status === 200 && r.account === false, 'no LinkSpin account: account false, nothing made', r); r = await call({ email: 'one@dh.test', op: 'ensure', links: L1 }); const c19 = r.links && r.links.ctb19 && r.links.ctb19.code; ok(r.account && r.host === 'spin-links.test.org' && /^[a-z0-9]{6}$/.test(c19 || '') && r.links.ctb19.created && r.links.mh3.created, 'ensure: one new link per program on the member\'s account, clean host', r); ok(r.links.ctb19.link === 'https://spin-links.test.org/r/' + c19 + '?s1=ctb19&placement=ctb19', 'the link carries the program id as sub-id (s1) and placement', r.links.ctb19.link); const src = JSON.parse(fs.readFileSync(path.join(DATA, 'sources.json'), 'utf8')); const mine = (src.items || src.sources || []).filter(x => x.owner === 'one@dh.test'); ok(mine.length === 1 && mine[0].template === 'downlinehydra' && mine[0].name === 'DownlineHydra' && r.sourceId === mine[0].id, 'the account has one "DownlineHydra" source (network template)', mine); const rot = JSON.parse(fs.readFileSync(path.join(DATA, 'rotator.json'), 'utf8')).rotations.filter(x => x.owner === 'one@dh.test'); ok(rot.length === 2 && rot.every(x => x.sourceId === mine[0].id && /^dh:/.test(x.partner)) && rot.find(x => x.partner === 'dh:ctb19').destinations[0].url === 'https://www.adsearnnetwork.com/?one19', 'the links are tagged with that source and point at the referral URL', rot); r = await call({ email: 'one@dh.test', op: 'ensure', links: [{ program: 'ctb19', name: 'Ads Earn Network', url: 'https://www.adsearnnetwork.com/?one19new' }] }); const rot2 = JSON.parse(fs.readFileSync(path.join(DATA, 'rotator.json'), 'utf8')).rotations.filter(x => x.owner === 'one@dh.test'); ok(r.links.ctb19.code === c19 && !r.links.ctb19.created && rot2.length === 2 && rot2.find(x => x.partner === 'dh:ctb19').destinations[0].url.endsWith('?one19new'), 'a changed referral URL updates the same link (same code, no new link)', r); r = await call({ email: 'one@dh.test', op: 'ensure', links: [{ program: 'bad', url: 'http://plain.example.org/' }, { program: 'bad id!', url: 'https://x.example.org/' }] }); ok(r.errors && r.errors.bad && r.errors['bad id!'] && !Object.keys(r.links).length, 'http destinations and bad program ids are refused', r); r = await call({ username: 'dhtwo', op: 'ensure', links: [{ program: 'ctb19', url: 'https://www.adsearnnetwork.com/?two19' }] }); ok(r.account && r.username === 'dhtwo' && r.links.ctb19 && r.links.ctb19.code !== c19, 'found by network username when the email is not given; a separate link on that account', r); // a click through the link keeps the referral ID and counts for the program const hit = await fetch('https://spin-links.test.org/r/'.replace('https://spin-links.test.org', BASE) + c19 + '?s1=ctb19&placement=ctb19', { redirect: 'manual', headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/130.0 Safari/537.36' } }); const loc = hit.headers.get('location') || ''; ok(hit.status >= 300 && hit.status < 400 && loc.startsWith('https://www.adsearnnetwork.com/?one19new'), 'the redirect lands on the program with the member\'s referral ID intact', [hit.status, loc]); await sleep(300); r = await call({ email: 'one@dh.test', op: 'stats' }); ok(r.account && r.stats.ctb19 && r.stats.ctb19.hits === 1 && r.stats.ctb19.hits30 === 1 && r.stats.ctb19.unique30 === 1 && r.stats.mh3.hits === 0 && r.stats.ctb19.link.includes(c19), 'stats: clicks per program for this account only', r.stats); const rep = JSON.parse(fs.readFileSync(path.join(DATA, 'rotator.json'), 'utf8')).hits.find(h => h.r === rot.find(x => x.partner === 'dh:ctb19').id); ok(rep && rep.sr === mine[0].id && rep.q && rep.q.s1 === 'ctb19' && rep.q.placement === 'ctb19', 'the click is recorded under the DownlineHydra source with the program sub-id (Reports split it by program)', rep); } catch (e) { fail++; console.error(e); } finally { srv.kill(); await sleep(300); try { fs.rmSync(DATA, { recursive: true, force: true }); } catch (e) {} } console.log('\n' + pass + ' passed, ' + fail + ' failed'); process.exit(fail ? 1 : 0);