e23d9ecbdb
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
67 lines
6.8 KiB
JavaScript
67 lines
6.8 KiB
JavaScript
// Network sites as traffic sources (2026-11-14). Boots server.js on its own port with a throwaway seeded data dir (JSON store, no DB)
|
|
// and checks: the admin account gets one fixed network link per site (nwiap, nwls, nwdlb, nwvbb, nwvab, nwsp, nwec) and one source per
|
|
// network site; /r/<code>?src=&s1=&s2= lands on the right page with ?ref=<member>; the click counts under the sending site's source;
|
|
// a member's own link with ?src=<site> gets that site's source made on first use; an unknown src changes nothing; a reboot makes
|
|
// nothing twice.
|
|
// node qa/netsrc.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 { spawn } from 'node:child_process';
|
|
|
|
const PORT = Number(process.env.PORT || 8973); const BASE = 'http://127.0.0.1:' + PORT;
|
|
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(), 'ns-qa-')); const now = Date.now(); const ADMIN = 'admin@ns.test', MEM = 'mem@ns.test';
|
|
fs.writeFileSync(path.join(DATA, 'sources.json'), JSON.stringify({ v: 1, items: [{ id: 'aaaaaaaaaa', owner: MEM, name: 'My mailer', template: 'custom', site: 'mailer.example', params: [], postbackUrl: '', events: ['join'], enabled: true, created: now }] }));
|
|
fs.writeFileSync(path.join(DATA, 'rotator.json'), JSON.stringify({ v: 1, rotations: [
|
|
{ id: 1, owner: MEM, name: 'Mine', code: 'memlnk', fallback: '', destinations: [{ id: 1, url: 'https://offer.example.org/', weight: 1, active: true }], created: now, paused: false, nextDest: 2, sourceId: 'aaaaaaaaaa' }], hits: [], conv: [] }));
|
|
const env = Object.assign({}, process.env, { PORT: String(PORT), DATA_DIR: DATA, ADMIN_PASSWORD: 'x'.repeat(16), ADMIN_EMAIL: ADMIN });
|
|
for (const k of ['NODE_ENV', 'DATABASE_URL', 'ENGINE_KEY', 'LS_SYNC_SECRET', 'OUTBOUND', 'LS_TESTERS', 'CURTAIN', 'HUB_ENABLED', 'HUB_URL', 'NETWORK_LINKS_OWNER', 'NETWORK_SSO_SECRET']) delete env[k];
|
|
const site = path.resolve(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')), '..');
|
|
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
|
let srv = null;
|
|
async function boot() { srv = spawn(process.execPath, ['server.js'], { cwd: site, env, stdio: ['ignore', 'ignore', 'ignore'] }); for (let i = 0; i < 60; i++) { try { if ((await fetch(BASE + '/api/config')).ok) break; } catch (e) {} await sleep(500); } await sleep(3800); }
|
|
async function stop() { if (srv) srv.kill(); srv = null; await sleep(600); }
|
|
const go = async (p, ref) => { const r = await fetch(BASE + p, { redirect: 'manual', headers: Object.assign({ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) Chrome/130 netsrc-qa' }, ref ? { Referer: ref } : {}) }); return { status: r.status, loc: r.headers.get('location') || '' }; };
|
|
const read = f => JSON.parse(fs.readFileSync(path.join(DATA, f), 'utf8'));
|
|
const srcOf = (owner, tpl) => read('sources.json').items.find(x => x.owner === owner && x.template === tpl);
|
|
try {
|
|
await boot();
|
|
const rots = read('rotator.json').rotations.filter(r => r.owner === ADMIN);
|
|
ok(['nwiap', 'nwls', 'nwdlb', 'nwvbb', 'nwvab', 'nwsp', 'nwec'].every(c => rots.some(r => r.code === c)) && rots.length === 7, 'the admin account has the seven network links', rots.map(r => r.code));
|
|
const mineSrc = read('sources.json').items.filter(x => x.owner === ADMIN);
|
|
ok(['instantadpay', 'linkspin', 'downlinebanners', 'viralbannerblitz', 'viraladblitz', 'stackpol', 'earnerscorner'].every(t => mineSrc.some(x => x.template === t)) && mineSrc.length === 7, 'and one source per network site', mineSrc.map(x => x.template));
|
|
|
|
let r = await go('/r/nwdlb?src=iap&s1=bob&s2=how');
|
|
ok(r.status === 302 && /^https:\/\/downlinebanners\.com\/how\?ref=bob$/.test(r.loc), 'nwdlb: lands on /how with ?ref=bob', r);
|
|
r = await go('/r/nwiap?src=vbb&s1=amy');
|
|
ok(r.status === 302 && /^https:\/\/instantadpay\.com\/\?ref=amy&lsclk=[a-f0-9]{10}$/.test(r.loc), 'nwiap: home page with ?ref=amy and the click id (InstantAdPay reports joins against it)', r);
|
|
r = await go('/r/nwec?src=dlb');
|
|
ok(r.status === 302 && /^https:\/\/earnerscorner\.com\/(\?|$)/.test(r.loc), 'nwec: the forum home', r);
|
|
r = await go('/r/nwvab?src=vab&s1=bob&s2=boost');
|
|
ok(r.status === 302 && /^https:\/\/viraladblitz\.com\/boost\?ref=bob$/.test(r.loc), 'nwvab: ViralAdBlitz page with the member ref', r);
|
|
r = await go('/r/nwvbb?src=linkspin', 'https://linkspin.app/contract');
|
|
ok(r.status === 302 && /^https:\/\/viralbannerblitz\.com\/\?ref=(&|$)/.test(r.loc), 'nwvbb with no member: an empty ref', r);
|
|
await sleep(300);
|
|
const hits = read('rotator.json').hits; const byCode = c => { const rr = read('rotator.json').rotations.find(x => x.code === c); return hits.filter(h => h.r === rr.id); };
|
|
const h1 = byCode('nwdlb')[0], h2 = byCode('nwiap')[0], h3 = byCode('nwec')[0], h4 = byCode('nwvbb')[0];
|
|
ok(h1 && h1.sr === srcOf(ADMIN, 'instantadpay').id && h1.s === 'instantadpay.com' && h1.q.s1 === 'bob', 'the DLB click counts under the InstantAdPay source (no referrer: the site name)', h1);
|
|
ok(h2 && h2.sr === srcOf(ADMIN, 'viralbannerblitz').id, 'the IAP click counts under the Viral Banner Blitz source', h2);
|
|
ok(h3 && h3.sr === srcOf(ADMIN, 'downlinebanners').id, 'the Earners Corner click counts under the Downline Banners source', h3);
|
|
ok(h4 && h4.sr === srcOf(ADMIN, 'linkspin').id && h4.s === 'linkspin.app', 'a real referrer is kept as the referrer', h4);
|
|
|
|
ok(!srcOf(MEM, 'stackpol'), 'the member has no Stack POL source yet');
|
|
r = await go('/r/memlnk?src=stackpol');
|
|
ok(r.status === 302 && /^https:\/\/offer\.example\.org\//.test(r.loc), 'a member link with ?src=stackpol still redirects', r);
|
|
await go('/r/memlnk?src=nosuchsite'); await go('/r/memlnk'); await sleep(300);
|
|
const ms = srcOf(MEM, 'stackpol'); const mh = read('rotator.json').hits.filter(h => h.r === 1);
|
|
ok(ms && ms.name === 'Stack POL', 'the member got a Stack POL source on first use', ms);
|
|
ok(mh.length === 3 && mh[0].sr === ms.id && mh[1].sr === 'aaaaaaaaaa' && mh[2].sr === 'aaaaaaaaaa', 'that click counts under it; an unknown src or none keeps the attached source', mh.map(h => h.sr));
|
|
ok(read('sources.json').items.filter(x => x.owner === MEM).length === 2, 'an unknown src makes no source');
|
|
|
|
await stop(); await boot();
|
|
ok(read('rotator.json').rotations.filter(x => x.owner === ADMIN).length === 7 && read('sources.json').items.filter(x => x.owner === ADMIN).length === 7, 'a reboot makes nothing twice');
|
|
} catch (e) { fail++; console.error(e); }
|
|
finally { await stop(); try { fs.rmSync(DATA, { recursive: true, force: true }); } catch (e) {} }
|
|
console.log('\n' + pass + ' passed, ' + fail + ' failed'); process.exit(fail ? 1 : 0);
|