Files
linkspin/qa/partner-spins.test.mjs
T

57 lines
6.5 KiB
JavaScript

// Banner Spins partner endpoint (/api/partner/bl/spins) for Downline Banners and Viral Banner Blitz (2026-11-14).
// 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, Downline Banners' 468x60 answer unchanged, { site: 'vbb' } = 125x125 + 250x250 spins with a
// "Viral Banner Blitz" source, the network-username fallback for vbb only, and the host in the answer.
// node qa/partner-spins.test.mjs (from the site dir; PORT default 8971)
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 || 8971); 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(), 'ps-qa-'));
const W1 = '0x' + '1'.repeat(40), W2 = '0x' + '2'.repeat(40), now = Date.now();
const acct = (email, username, memberId, address) => Object.assign({ email, username, code: username.slice(0, 7), memberId, created: now - 86400000, sponsorRef: '' }, address ? { address } : {});
const accounts = { v: 2, byEmail: { 'one@ps.test': acct('one@ps.test', 'psone', 910001, W1), 'two@ps.test': acct('two@ps.test', 'pstwo', 910002) }, byAddress: { [W1]: 'one@ps.test' }, 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));
const spin = (id, owner, size, code, extra) => Object.assign({ id, owner, name: 'Spin ' + id, code, size, mode: 'weight', fallback: '', fallbackImage: '', banners: [{ id: 1, image: '/x.png', link: 'https://t.example.org/', active: true, weight: 10 }], nextBanner: 2, paused: false, created: now }, extra || {});
fs.writeFileSync(path.join(DATA, 'bannerspin.json'), JSON.stringify({ spins: [spin(1, 'one@ps.test', '468x60', 'qwhone'), spin(2, 'one@ps.test', '125x125', 'qwhtwo'), spin(3, 'one@ps.test', '250x250', 'qwhthr'),
spin(4, 'one@ps.test', '300x250', 'qwhfou'), spin(5, 'one@ps.test', '125x125', 'qwhfiv', { adminOff: true }), spin(6, 'two@ps.test', '125x125', 'qwhsix')], clicks: [], conv: [] }));
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@ps.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/bl/spins', { 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); }
let r = await call({ wallet: W1 }, { sec: 'y'.repeat(64) }); ok(r._status === 401, 'a wrong signature is refused');
r = await call({ wallet: W1 }, { ts: Date.now() - 600000 }); ok(r._status === 401, 'an old timestamp is refused');
r = await call({ wallet: W1 });
ok(r.account && r.spins.length === 1 && r.spins[0].id === 1 && r.spins[0].size === '468x60' && r.username === 'psone', 'Downline Banners (no site): the 468x60 spins only, as before', r);
ok(r.host === 'spin-links.test.org', 'the answer names the link host', r.host);
r = await call({ wallet: W1, spinId: 1 });
ok(r.use && r.use.img === 'https://spin-links.test.org/i/qwhone.gif' && /\/c\/qwhone\?clickid=\{clickid\}&placement=\{spot\}&s1=\{type\}$/.test(r.use.link), 'Downline Banners use: image + click link with the macros', r.use);
r = await call({ site: 'vbb', wallet: W1 });
ok(r.account && r.spins.map(x => x.id).join(',') === '2,3' && r.spins.every(x => ['125x125', '250x250'].includes(x.size)), 'vbb: 125x125 + 250x250 spins only (no 468x60, 300x250 or admin-off)', r.spins);
r = await call({ site: 'vbb', wallet: W1, spinId: 1 }); ok(r._status === 404 && /125x125/.test(r.error), 'vbb: a 468x60 spin cannot be used', r);
r = await call({ site: 'vbb', wallet: W1, spinId: 3 });
ok(r.use && r.use.size === '250x250' && r.use.img === 'https://spin-links.test.org/i/qwhthr.gif' && r.use.tagged === true && r.use.sourceId, 'vbb use: the spin, tagged with a new source', r.use);
const src = JSON.parse(fs.readFileSync(path.join(DATA, 'sources.json'), 'utf8')); const mineSrc = (src.sources || src.items || Object.values(src).flat()).filter(x => x && x.owner === 'one@ps.test');
ok(mineSrc.some(x => x.template === 'viralbannerblitz' && x.name === 'Viral Banner Blitz') && mineSrc.some(x => x.template === 'downlinebanners'), 'the account has a Viral Banner Blitz source (and its Downline Banners one)', mineSrc.map(x => x.template));
r = await call({ site: 'vbb', wallet: W1, spinId: 3 }); ok(r.use && r.use.tagged === false && !r.use.otherSource, 'a second use reuses the source (no second tag)', r.use);
r = await call({ site: 'vbb', wallet: W2, username: 'pstwo' }); ok(r.account && r.username === 'pstwo' && r.spins.length === 1 && r.spins[0].id === 6, 'vbb: no account holds the wallet, found by network username', r);
r = await call({ wallet: W2, username: 'pstwo' }); ok(r.account === false, 'Downline Banners: wallet only (no username fallback)', r);
r = await call({ site: 'vbb', wallet: W2 }); ok(r.account === false, 'vbb without a username: account false', r);
} 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);