// Conversion linkage, LinkSpin half (2026-11-07, reporting only). Throwaway data, no production data, nothing leaves the machine. // 1) unit: netconv.js + convtrack.js + rotator.js on a temp dir: sign-up, wallet, purchases counted once with values; duplicate // delivery; own account and own browser kept as tests; 30-day window by event time; first account owns the click; an event // after the hit aged out of the rotator; InstantAdPay ad clicks; goals and events in one list without counting anything twice; // per-link counts; postbacks once, never for a test. // 2) two processes on one data dir (a deploy's overlap): the same events raced from both, each counted once, none lost. // 3) browser (Playwright, if present): the landing tag on another origin decorates links to the network, directly and inside a // redirect link (Orlando's go?c=...&u=... shape), keeps existing params, leaves other links alone, survives a page script // that rewrites the link, and a stored id still decorates on a later page. Spin links are left alone. // node qa/netconv.test.mjs Env: PORT (default 8983), PW import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import http from 'node:http'; import { spawn } from 'node:child_process'; import { createRequire } from 'node:module'; const require = createRequire(import.meta.url); const site = path.resolve(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')), '..'); const PORT = Number(process.env.PORT || 8983); const DAY = 86400000; let failed = 0, passed = 0; const out = []; const check = (ok, what, detail) => { out.push((ok ? 'PASS ' : 'FAIL ') + what + (!ok && detail !== undefined ? ' :: ' + (typeof detail === 'string' ? detail : JSON.stringify(detail)) : '')); if (ok) passed++; else failed++; }; const sleep = ms => new Promise(r => setTimeout(r, ms)); const rq = (ip, ua) => ({ url: '/r/x', headers: { 'user-agent': ua || 'Mozilla/5.0 UnitUA', 'x-forwarded-for': ip }, socket: {} }); // ---------- 1) unit ---------- const UD = fs.mkdtempSync(path.join(os.tmpdir(), 'nc-unit-')); const C = require(path.join(site, 'convtrack.js')), R = require(path.join(site, 'rotator.js')), N = require(path.join(site, 'netconv.js')), M = require(path.join(site, 'marks.js')); M.init({ dataDir: UD }); const fired = []; const P = { fire: async (o, ev, ctx) => { fired.push({ o, ev, ctx }); return []; } }; const initAll = () => { R.init({ dataDir: UD, ownHosts: () => ['linkspin.app'], wantsClickId: o => C.goalsOf(o).length > 0, isNetworkHost: h => ['instantadpay.com'].includes(h), goalRows: () => C.goalRows() }); C.init({ dataDir: UD, rotator: R, noTimers: true }); N.init({ dataDir: UD, convtrack: C, rotator: R, postbacks: P, marks: M, noTimers: true }); }; initAll(); const OWNER = 'orlando@u.test'; const rot = (await R.create(OWNER, { name: 'Bridge spin' })).rotation; await R.addDest(OWNER, rot.id, { url: 'https://instantadpay.com/join/opgnetwerk', weight: 1 }); const hit = async ip => { const r = await R.resolve(rot.code, rq(ip)); return { id: r.lsid, url: r.url }; }; const a = await hit('198.51.100.1'); check(new URL(a.url).searchParams.get('lsclk') === a.id, 'a spin link straight at InstantAdPay carries lsclk with no goal set up', a.url); const ev = (k, ev_, click, email, extra) => Object.assign({ k, ev: ev_, click, email, ts: Date.now() }, extra || {}); let r = await N.record(ev('join:a', 'join', a.id, 'lead1@u.test')); check(r.result === 'counted', 'sign-up on a spin click counted', r); check((await N.record(ev('join:a', 'join', a.id, 'lead1@u.test'))).result === 'dup', 'the same sign-up delivered twice counts once'); check((await N.record(ev('join:other-key', 'join', a.id, 'lead1@u.test'))).result === 'dup', 'a second sign-up on one click counts once'); check((await N.record(ev('wallet:a', 'wallet', a.id, 'lead1@u.test'))).result === 'counted', 'wallet link counted'); check((await N.record(ev('purchase:0xa:1', 'purchase', a.id, 'lead1@u.test', { value: 5, tx: '0xa' }))).result === 'counted', 'first purchase counted'); check((await N.record(ev('purchase:0xa:1', 'purchase', a.id, 'lead1@u.test', { value: 5, tx: '0xa' }))).result === 'dup', 'the same purchase delivered twice counts once'); check((await N.record(ev('purchase:0xb:1', 'purchase', a.id, 'lead1@u.test', { value: 20, tx: '0xb' }))).result === 'counted', 'a second package on the same click counts on its own'); let u = N.unified(OWNER, 30); const row = (U, k) => (U.rows.find(x => x.k === k) || { n: 0, value: 0 }); check(row(u, 'join').n === 1 && row(u, 'wallet').n === 1 && row(u, 'purchase').n === 2 && row(u, 'purchase').value === 25, 'Reports: 1 join, 1 wallet, 2 purchases worth $25.00', u.rows); check(u.buyers === 1, 'two packages by one click are one buyer', u.buyers); const link = await R.get(OWNER, rot.id); check(link.conversions === 4, 'the link shows 4 conversions', link.conversions); const lst = await R.stats(OWNER, rot.id); check(lst.conversions.some(x => /^purchase/.test(x.k) && x.n === 2), 'the link stats list both purchases', lst.conversions); const sum = await R.summary(OWNER, 30); check(sum.top[0].conv === 4, 'Reports top links: Conv. 4', sum.top[0]); await N.syncMirror(); check(N.unified(OWNER, 30).rows.find(x => x.k === 'purchase').n === 2 && (await R.get(OWNER, rot.id)).conversions === 4, 'replaying the ledger into the rotator adds nothing'); check(fired.filter(f => f.ev === 'join').length === 1 && fired.filter(f => f.ev === 'purchase').length === 2, 'postbacks fire once per counted event', fired.map(f => f.ev)); check(fired.find(f => f.ev === 'purchase').ctx.payout === 5 && fired.find(f => f.ev === 'purchase').ctx.txid === '0xa', 'the purchase postback carries the value and the tx'); // a LinkSpin site join on the same click stays one join await R.convert(a.id, 'join', {}); check(N.unified(OWNER, 30).rows.find(x => x.k === 'join').n === 1, 'a LinkSpin join on the same click does not add a second join'); // own account, own browser const b = await hit('198.51.100.2'); fired.length = 0; r = await N.record(ev('join:own', 'join', b.id, 'Orlando@U.test')); check(r.result === 'self', "the click owner's own account is not counted", r); r = await N.record(ev('purchase:0xself:1', 'purchase', b.id, 'orlando@u.test', { value: 100 })); check(r.result === 'self' && fired.length === 0, 'its purchases are not counted and fire no postback', r); const c1 = await hit('198.51.100.3'); const skO = C.softKey(rq('192.0.2.7', 'OwnerUA')); C.noteOwner(OWNER, skO); check((await N.record(ev('join:ownsk', 'join', c1.id, 'stranger1@u.test', { sk: skO }))).result === 'self', "a sign-up from a browser the owner signs in from is a test"); check((await N.record(ev('purchase:0xownsk:1', 'purchase', c1.id, 'stranger1@u.test', { value: 9 }))).result === 'self', 'and stays one on that click'); const c2 = await hit('198.51.100.4'); C.markSelfClick(c2.id, OWNER, rq('1.1.1.1'), OWNER); check((await N.record(ev('join:ownclick', 'join', c2.id, 'stranger2@u.test'))).result === 'self', "the owner's own click is a test"); check(row(N.unified(OWNER, 30), 'purchase').value === 25, 'none of that moved the totals'); // window, unknown clicks, first account const d2 = await hit('198.51.100.6'); const hd2 = R.hitById(d2.id).hit; hd2.ts = Date.now() - 31 * DAY; check((await N.record(ev('join:late', 'join', d2.id, 'late2@u.test'))).result === 'expired', 'an event more than 30 days after the click does not count'); const d3 = await hit('198.51.100.7'); R.hitById(d3.id).hit.ts = Date.now() - 31 * DAY; check((await N.record(ev('join:latedelivery', 'join', d3.id, 'late3@u.test', { ts: Date.now() - 2 * DAY }))).result === 'counted', 'the window runs to the event, so a delayed delivery still counts'); check((await N.record(ev('join:nosuch', 'join', 'abcdef0123', 'x@u.test'))).result === 'nomatch', 'an unknown click is refused'); check((await N.record(ev('join:bad', 'nonsense', a.id, 'x@u.test'))).result === 'bad' && (await N.record({ k: 'x' })).result === 'bad', 'bad events are refused'); const e1 = await hit('198.51.100.8'); await N.record(ev('join:first', 'join', e1.id, 'first@u.test')); check((await N.record(ev('purchase:0xsecond:1', 'purchase', e1.id, 'second@u.test', { value: 5 }))).result === 'other', 'a click belongs to the first account on it'); // the hit ages out of the rotator: later events still count on the link const f = await hit('198.51.100.9'); await N.record(ev('join:aged', 'join', f.id, 'aged@u.test')); { const file = path.join(UD, 'rotator.json'); const db = JSON.parse(fs.readFileSync(file, 'utf8')); db.hits = db.hits.filter(h => h.id !== f.id); fs.writeFileSync(file, JSON.stringify(db)); initAll(); } check(!R.hitById(f.id), 'the hit is gone from the rotator'); const before = (await R.get(OWNER, rot.id)).conversions; check((await N.record(ev('purchase:0xaged:1', 'purchase', f.id, 'aged@u.test', { value: 50 }))).result === 'counted', 'a purchase after the hit aged out still counts'); check((await R.get(OWNER, rot.id)).conversions === before + 1, 'and lands on the link'); // InstantAdPay ad click: no rotator, in the ledger, on the campaign C.importClicks([{ id: 'i0123456789a', camp: 7, cname: 'My ad', owner: 'adv@u.test', ts: Date.now(), sk: '', type: 'banner' }]); check((await N.record(ev('join:ad', 'join', 'i0123456789a', 'adlead@u.test'))).result === 'counted', 'an InstantAdPay ad click counts'); await N.record(ev('purchase:0xad:1', 'purchase', 'i0123456789a', 'adlead@u.test', { value: 20 })); const pa = N.partner('adv@u.test', 30); check(pa.byCampaign[7] && pa.byCampaign[7].join === 1 && pa.byCampaign[7].purchase === 1 && pa.byCampaign[7].value === 20, 'the campaign shows its join and purchase', pa.byCampaign); check(row(N.unified('adv@u.test', 30), 'purchase').value === 20, 'ad events are in the one list (not in the rotator)'); check((await N.record(ev('join:adown', 'join', 'i0123456789a', 'adv@u.test'))).result !== 'counted', "the ad owner's own account is not counted"); // goals and events in one list; a goal is not an event and nothing is listed twice const g = C.createGoal(OWNER, { name: 'subscriber', value: '2' }); const px = g.goal.px; const gh = await hit('198.51.100.10'); const fake = { headers: { 'user-agent': 'GoalUA', 'x-forwarded-for': '198.51.100.11' }, socket: {} }; check(C.record({ px, c: gh.id, m: 'url' }, fake, {}).result === 'counted', 'a goal converts on a spin click'); await N.record(ev('join:gh', 'join', gh.id, 'goalman@u.test')); const U2 = N.unified(OWNER, 30); check(row(U2, 'goal:subscriber').n === 1 && row(U2, 'goal:subscriber').value === 2, 'goals are in the one list with their value', U2.rows); const before2 = (await R.get(OWNER, rot.id)); check(before2.conversions >= 2 && before2.convBy['goal: subscriber'] === 1, 'per-link counts include goals', before2.convBy); const s2 = await R.summary(OWNER, 30); check(s2.conversions.filter(x => x.k === 'goal: subscriber').length === 1, 'and the top-links count sees it once'); // ---------- 2) two processes, one data dir ---------- { const worker = path.join(UD, 'worker.cjs'); const DD = fs.mkdtempSync(path.join(os.tmpdir(), 'nc-race-')); fs.writeFileSync(worker, ` const path = require('path'); const site = ${JSON.stringify(site)}; const DD = ${JSON.stringify(DD)}; const C = require(path.join(site, 'convtrack.js')), R = require(path.join(site, 'rotator.js')), N = require(path.join(site, 'netconv.js')), M = require(path.join(site, 'marks.js')); M.init({ dataDir: DD }); R.init({ dataDir: DD, ownHosts: () => [] }); C.init({ dataDir: DD, rotator: R, noTimers: true }); N.init({ dataDir: DD, convtrack: C, rotator: R, marks: M, noTimers: true }); (async () => { const until = Number(process.argv[2]); while (Date.now() < until) await new Promise(r => setTimeout(r, 5)); const out = []; for (let i = 0; i < 40; i++) out.push((await N.record({ k: 'purchase:0xrace:' + i, ev: 'purchase', click: 'abcdef0123', email: 'race@u.test', value: 1, ts: Date.now() })).result); out.push((await N.record({ k: 'join:race', ev: 'join', click: 'abcdef0123', email: 'race@u.test', ts: Date.now() })).result); process.stdout.write(JSON.stringify(out)); })().catch(e => { process.stdout.write(JSON.stringify({ error: e.message })); }); `); const seed = { v: 1, rotations: [{ id: 1, owner: 'race-owner@u.test', name: 'r', code: 'raceee', fallback: '', destinations: [], created: Date.now(), paused: false, nextDest: 1 }], hits: [{ id: 'abcdef0123', r: 1, d: 0, ts: Date.now(), v: 'x', c: '', s: 'direct', dv: 'desktop', bot: 0 }], conv: [] }; fs.writeFileSync(path.join(DD, 'rotator.json'), JSON.stringify(seed)); const until = Date.now() + 1200; const run = () => new Promise(res => { const p = spawn(process.execPath, [worker, String(until)], { stdio: ['ignore', 'pipe', 'inherit'] }); let o = ''; p.stdout.on('data', c => o += c); p.on('close', () => { try { res(JSON.parse(o)); } catch (e) { res({ error: o }); } }); }); const [x, y] = await Promise.all([run(), run()]); const all = [].concat(x, y); check(Array.isArray(x) && Array.isArray(y), 'both copies ran', { x, y }); check(all.filter(v => v === 'counted').length === 41, '41 events raced from two copies: exactly 41 counted', all.filter(v => v === 'counted').length); const rows = fs.readFileSync(path.join(DD, 'netconv.jsonl'), 'utf8').trim().split('\n').map(l => JSON.parse(l)); check(rows.length === 41 && new Set(rows.map(r => r.k)).size === 41, 'the ledger holds each event once, none lost', rows.length); } // ---------- 3) the landing tag in a browser, on another origin ---------- const PW = process.env.PW || 'D:/Projects/MarketingAgent/qa-tester/node_modules/playwright'; if (fs.existsSync(PW)) { const DATA = fs.mkdtempSync(path.join(os.tmpdir(), 'nc-b-')); fs.mkdirSync(path.join(DATA, 'uploads')); const env = Object.assign({}, process.env, { PORT: String(PORT), DATA_DIR: DATA, ADMIN_PASSWORD: 'localtest', SITE_HOST: '127.0.0.1:' + PORT }); for (const k of ['NODE_ENV', 'DATABASE_URL', 'ENGINE_KEY', 'OUTBOUND', 'LS_TESTERS', 'CURTAIN', 'NAS_DB_HOST', 'NAS_DB_USER', 'NAS_DB_PASSWORD', 'NAS_DB_NAME', 'SENDGRID_API_KEY', 'OPENROUTER_API_KEY', 'LS_SYNC_SECRET']) delete env[k]; const srv = spawn(process.execPath, ['server.js'], { cwd: site, env, stdio: 'ignore' }); try { for (let i = 0; i < 40; i++) { try { const r = await fetch('http://127.0.0.1:' + PORT + '/t.js', { signal: AbortSignal.timeout(2000) }); if (r.ok) break; } catch (e) {} await sleep(500); } const tag = await (await fetch('http://127.0.0.1:' + PORT + '/t.js')).text(); check(/"instantadpay\.com"/.test(tag) && !tag.includes('/*ls-net*/'), '/t.js is served with the network host list filled in'); const pw = require(PW); const br = await pw.chromium.launch(); const ctx = await br.newContext(); const page = await ctx.newPage(); const errors = []; page.on('pageerror', e => errors.push(e.message)); const BTN = 'https://go.opgnetwerk.test/go?c=bridge-a&u=https%3A%2F%2Finstantadpay.com%2Fjoin%2Fopgnetwerk&keep=a%20b'; const html = `bridge IAP through a redirect LinkSpin through a redirect direct share domain already has one spin link other site redirect to another site anchornext `; const later = 'nextx'; const noid = html; const bridge = http.createServer((q, s) => { s.writeHead(200, { 'Content-Type': 'text/html' }); s.end(new URL(q.url, 'http://x').pathname === '/next' ? later : noid); }).listen(PORT + 1); const BR = 'http://localhost:' + (PORT + 1); const ID = 'abc123def0'; await page.goto(BR + '/lp?src=banner&lsclk=' + ID); await sleep(700); const hrefOf = sel => page.$eval(sel, a => a.href); const wrap = await hrefOf('#wrap'); const wu = new URL(wrap); check(new URL(wu.searchParams.get('u')).searchParams.get('lsclk') === ID, "Orlando's redirect link: the click id is inside the u address", wrap); check(new URL(wu.searchParams.get('u')).pathname === '/join/opgnetwerk' && wu.searchParams.get('c') === 'x-banner', 'the redirect link keeps its address and its other params', wrap); check(new URL(new URL(await hrefOf('#wrapls')).searchParams.get('u')).searchParams.get('lsclk') === ID, 'the LinkSpin button through the redirect too'); check(await hrefOf('#direct') === 'https://instantadpay.com/join/opgnetwerk?v=play&lsclk=' + ID + '#top', 'a direct link: existing param and #hash stay', await hrefOf('#direct')); check(new URL(await hrefOf('#share')).searchParams.get('lsclk') === ID, 'share domains are network hosts (www stripped)'); check(await hrefOf('#has') === 'https://instantadpay.com/join/x?lsclk=abcdef0123', 'a link already carrying a click id is left alone'); check(await hrefOf("#spin").then(h => h.split("127.0.0.1")[1]) === "/r/abc123", 'spin links are left alone, they make their own click'); check(await hrefOf('#other') === 'https://bix.example/?a=1' && !/lsclk/.test(await hrefOf('#wrapother')), 'other sites are left alone, redirected or not'); check(await hrefOf('#skip') === 'https://instantadpay.com/join/y', 'data-ls-skip is respected'); await page.click('#late-target'); await sleep(600); check(new URL(page.url()).searchParams.get('lsclk') === ID, 'same-site links still carry it, as before'); await page.goto(BR + '/next'); await sleep(600); check(new URL(await hrefOf('#direct')).searchParams.get('lsclk') === ID, 'a later page with no id in the address: the stored id still decorates'); // a foreign clickid is not one of ours: stored as before, never carried to the network const c2x = await br.newContext(); const p2 = await c2x.newPage(); await p2.goto(BR + '/lp?clickid=foreign12345678'); await sleep(600); check(await p2.$eval('#direct', a => a.href) === 'https://instantadpay.com/join/opgnetwerk?v=play#top', "another network's clickid never travels to the network"); // no id at all: the page is untouched const c3x = await br.newContext(); const p3 = await c3x.newPage(); await p3.goto(BR + '/lp'); await sleep(500); check(await p3.$eval('#direct', a => a.href) === 'https://instantadpay.com/join/opgnetwerk?v=play#top', 'no click id: links untouched'); check(errors.length === 0, 'no page errors', errors); await br.close(); bridge.close(); } finally { srv.kill(); } } else out.push('SKIP browser part (no Playwright at ' + PW + ')'); console.log(out.join('\n')); console.log('\n' + passed + ' passed, ' + failed + ' failed'); process.exit(failed ? 1 : 0);