LinkSpin tracked links for text ads (Marty 2026-11-15): lstrack.js asks LinkSpin (POST LS_URL/api/partner/vab/links, net.v1 with NETWORK_SSO_SECRET, by network username) for a tracked link on the member's own LinkSpin account when they save their ad; /go/<ad> sends through it while it points at the ad's current link. "Track with LinkSpin" toggle on the ad form (default on), {clickid}/{placement} links not tracked, LinkSpin clicks on My page, counters in admin status. Fails soft (plain link) until LinkSpin ships the endpoint; a 2-minute sweep retries. Smoke 179/179 (17 new LinkSpin checks against a stand-in that starts down).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-15 06:20:32 -06:00
parent b71284e5c9
commit 4efc1239b0
18 changed files with 200 additions and 35 deletions
+51
View File
@@ -108,11 +108,27 @@ function chainBoost(o) {
const TG = []; const TG_PORT = PORT + 4;
listen(TG_PORT, async (q, r, b) => { TG.push({ method: q.url.split('/').pop(), token: q.url.split('/')[1], body: JSON.parse(b || '{}') }); r.setHeader('Content-Type', 'application/json'); r.end('{"ok":true,"result":{}}'); });
// ---- stand-in LinkSpin partner endpoint /api/partner/vab/links (net.v1 signature). Starts DOWN (404, as before LinkSpin ships it). ----
const LS = { up: false, calls: [], badSig: 0, links: new Map(), known: new Set(['one', 'owner', 'four', 'five', 'six', 'seven', 'topdog', 'downline']), stats: {} }; const LS_PORT = PORT + 5;
listen(LS_PORT, async (q, r, b) => {
r.setHeader('Content-Type', 'application/json');
if (!LS.up || q.url !== '/api/partner/vab/links') { r.statusCode = 404; return r.end('{"error":"Not found"}'); }
const want = crypto.createHmac('sha256', SSO_SECRET).update('net.v1.' + q.headers['x-net-ts'] + '\n' + b).digest('hex');
if (q.headers['x-net-sig'] !== want) { LS.badSig++; r.statusCode = 401; return r.end('{"error":"unauthorized"}'); }
const j = JSON.parse(b || '{}'); LS.calls.push(j);
if (!LS.known.has(String(j.username || '').toLowerCase())) return r.end('{"account":false}');
if (j.op === 'stats') return r.end(JSON.stringify({ account: true, stats: LS.stats[j.username] || {} }));
const links = {};
for (const x of j.links || []) { const k = j.username + ':' + x.id; let l = LS.links.get(k); if (!l) { l = { code: crypto.randomBytes(3).toString('hex'), url: x.url }; LS.links.set(k, l); } l.url = x.url; links[x.id] = { code: l.code, link: 'https://spin.test/r/' + l.code + '?s1=' + x.id + '&placement=' + x.id, created: false }; }
r.end(JSON.stringify({ account: true, username: j.username, host: 'spin.test', links, errors: {} }));
});
const ENV = Object.assign({}, process.env, { PORT: String(PORT), DATA_DIR: DATA, ADMIN_TOKEN: ADMIN, NODE_ENV: 'development', DOCSTORE_URL: '',
VISIT_SECONDS: '2', VISITS_PER_IP_DAY: '2', LINK_PROBE: '0', NETWORK_LOOKUP_URL: 'http://127.0.0.1:' + LOOKUP_PORT + '/api/network/member',
NETWORK_SSO_SECRET: SSO_SECRET, IAP_URL: 'https://iap.invalid', LS_URL: 'https://ls.invalid', NETWORK_SITES_HUB: '0',
CONTRACT_ADDRESS: SHOP, CHAIN_RPC: 'http://127.0.0.1:' + CHAIN_PORT, KEEPER_KEY: KEEPER_PK, SHOP_START_BLOCK: '1', SHOP_CONFIRMATIONS: '0', SHOP_POLL_MS: '200', EXPLORER: 'https://explorer.invalid',
HUB_ENABLED: '1', HUB_URL: 'http://127.0.0.1:' + HUB_PORT, HUB_SECRET, HUB_SITE: 'viraladblitz', HUB_TICK_MS: '300', HUB_DOWN_MS: '300', HUB_CACHE_MS: '200',
LS_TRACK_URL: 'http://127.0.0.1:' + LS_PORT, LS_SWEEP_FIRST_MS: '300', LS_SWEEP_MS: '300', LS_RETRY_MS: '300', LS_CACHE_MS: '100',
TG_TOKEN: 'standin-token', TG_CHAT: '-100123', TG_PAY_TOPIC: '632242', TG_API: 'http://127.0.0.1:' + TG_PORT });
process.on('exit', () => { try { srv && srv.kill(); } catch (e) {} });
@@ -385,9 +401,44 @@ const mtop = await anon.json('/api/wire?page=member&u=topdog');
const rm = await anon.json('/api/admin/ad', { id: (await M[7].json('/api/my')).ad.id, status: 'removed', note: 'smoke' }, { 'x-admin-token': ADMIN }); ok(rm.ok, 'admin removes an ad');
mp = await anon.json('/api/wire?page=member&u=owner'); ok(!mp.page.line.some(x => x.owner === '@seven') && mp.page.line.some(x => x.owner === '@topdog'), 'a removed ad leaves the member pages; the next one up (place 8) moves in', mp.page.line.map(x => [x.level, x.owner]));
console.log('14b. LinkSpin tracked links for text ads (lstrack.js)');
my = await M1.json('/api/my'); ok(my.ad.status === 'live' && my.linkspin && my.linkspin.on && my.linkspin.state === 'pending' && my.linkspin.track === true, 'LinkSpin not answering the endpoint yet: the ad is live, tracking pending (fails soft)', my.linkspin);
r = await anon.req('/go/' + ad1 + '?p=ticker'); ok(new URL(r.headers.get('location')).hostname === 'member-1.example-shop.com', 'while LinkSpin is down /go/<ad> sends to the member\'s own link', r.headers.get('location'));
LS.up = true; await sleep(1500);
my = await M1.json('/api/my'); const lsLink = my.linkspin && my.linkspin.link;
ok(my.linkspin.state === 'tracked' && lsLink === 'https://spin.test/r/' + (LS.links.get('one:' + ad1) || {}).code + '?s1=' + ad1 + '&placement=' + ad1, 'LinkSpin up: the retry sweep makes the tracked link on the member\'s own account', my.linkspin);
const ec = LS.calls.find(c => c.op === 'ensure' && c.username === 'one');
ok(ec && ec.links.length === 1 && ec.links[0].id === String(ad1) && ec.links[0].url === my.ad.link && !ec.email && LS.badSig === 0, 'the call: signed net.v1, network username, ad id as the item id, the ad link as destination (no email sent)', ec);
const clicksBefore = my.ad.clicks;
r = await anon.req('/go/' + ad1 + '?p=ticker'); ok(r.status === 302 && r.headers.get('location') === lsLink, '/go/<ad> now sends through the LinkSpin tracked link', r.headers.get('location'));
my = await M1.json('/api/my'); ok(my.ad.clicks === clicksBefore + 1, 'the click still counts here too');
{ const a5 = (await M[5].json('/api/my')); ok(a5.linkspin.state === 'macros' && !LS.calls.some(c => c.username === 'five'), 'an ad link with {clickid}/{placement} is not sent to LinkSpin (macros are filled here)', a5.linkspin); }
LS.stats.one = { [ad1]: { hits: 7, hits30: 3, unique30: 2 } }; await sleep(150);
my = await M1.json('/api/my'); ok(my.linkspin.clicks && my.linkspin.clicks.hits === 7 && my.linkspin.clicks.hits30 === 3 && my.linkspin.clicks.unique30 === 2, 'My page shows the ad\'s clicks as LinkSpin counts them', my.linkspin);
const code1 = (LS.links.get('one:' + ad1) || {}).code;
pa = await M1.json('/api/my/ad', adBody(1, { link: 'https://member-1.example-shop.com/new-offer' }));
ok(pa.ok && pa.linkspin === 'tracked' && LS.links.get('one:' + ad1).url === 'https://member-1.example-shop.com/new-offer' && LS.links.get('one:' + ad1).code === code1, 'editing the ad link updates the same LinkSpin link right away', pa);
LS.up = false;
pa = await M1.json('/api/my/ad', adBody(1, { link: 'https://member-1.example-shop.com/third' }));
r = await anon.req('/go/' + ad1); ok(pa.ok && pa.linkspin === 'pending' && r.headers.get('location') === 'https://member-1.example-shop.com/third', 'edited while LinkSpin is down: saved, and /go/<ad> never sends to the old tracked destination', [pa, r.headers.get('location')]);
LS.up = true; const n0 = LS.calls.length;
pa = await M1.json('/api/my/ad', adBody(1, { link: 'https://member-1.example-shop.com/third', track: false }));
r = await anon.req('/go/' + ad1); my = await M1.json('/api/my');
ok(pa.ok && pa.linkspin === 'off' && my.ad.track === false && my.linkspin.state === 'off' && r.headers.get('location') === 'https://member-1.example-shop.com/third', '"Track with LinkSpin" off: /go/<ad> sends straight to the link', [pa, my.linkspin]);
await sleep(900); ok(!LS.calls.slice(n0).some(c => c.op === 'ensure' && c.username === 'one'), 'tracking off: the sweep leaves that ad alone');
pa = await M1.json('/api/my/ad', adBody(1, { link: 'https://member-1.example-shop.com/third' }));
ok(pa.linkspin === 'off', 'an edit without the toggle keeps tracking off', pa);
pa = await M1.json('/api/my/ad', adBody(1, { link: 'https://member-1.example-shop.com/third', track: true }));
r = await anon.req('/go/' + ad1); ok(pa.linkspin === 'tracked' && r.headers.get('location').startsWith('https://spin.test/r/' + code1), 'switched back on: tracked again, same LinkSpin code', [pa, r.headers.get('location')]);
{ const NL = await walletMember({ hub: { username: 'nolsacct', sponsorId: idOwner } }); const q2 = await NL.json('/api/my/ad', adBody('nl')); const m2 = await NL.json('/api/my');
ok(q2.ok && q2.linkspin === 'no-account' && m2.linkspin.state === 'no-account', 'no LinkSpin account for the username: the ad works with its own link', m2.linkspin); }
{ const s2 = await anon.json('/api/admin/status', undefined, { 'x-admin-token': ADMIN }); ok(s2.linkspin && s2.linkspin.on === true && s2.linkspin.ensured >= 3 && s2.linkspin.failed >= 1 && !JSON.stringify(s2.linkspin).includes(SSO_SECRET), 'admin status: LinkSpin tracking counters, no secret', s2.linkspin); }
await sleep(1000); // the store writes 800 ms after a change; a Windows kill has no SIGINT flush
console.log('15. restart');
await stop(); await boot(); await sleep(500);
my = await OWN.json('/api/my'); ok(my.ad && my.ad.id === OWNAD && my.boosts[0].delivered === 6, 'after a restart: ads, Boosts and delivery are kept', my.boosts);
r = await anon.req('/go/' + ad1); ok(r.headers.get('location').startsWith('https://spin.test/r/'), 'after a restart: the tracked link is kept', r.headers.get('location'));
led = await anon.json('/api/ledger'); ok(led.items.length === 2 && TG.length === 1, 'after a restart: no re-record, no repost');
const again = await view(client({ ip: shared, ua: 'Mozilla/5.0 SharedBrowser/9' }), 'front'); ok(!again.r.verified && again.r.why === 'ip', 'after a restart: the per-connection cap still holds (rebuilt from the visit rows)', again.r);