// LinkSpin -> network hub activity (Marty 2026-11-16: "LinkSpin never reports activity", DownlineHydra "Wake it"). // node qa/networksso-activity.test.mjs (needs ../../network-hub beside this repo; skips otherwise) // Boots the REAL hub (JSON store, throwaway dir) and drives networksso.js in this process the way server.js does: // 1. a signed-in member's real page load (/links, Sec-Fetch-Dest: document) records site 'linkspin' on the hub // 2. a second page load the same day sends nothing more (once a day) // 3. API calls, assets, bots and signed-out page loads never ping // 4. seen() (what /api/me, /api/my/ping and the admin portal call): a tab kept open, a browser with no Sec-Fetch headers, // and a wallet-only session all record the day's visit 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'; import { createRequire } from 'node:module'; const HERE = path.resolve(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')), '..'); const HUB_DIR = [process.env.HUB_DIR, path.resolve(HERE, '../../network-hub')].filter(Boolean).find(d => fs.existsSync(path.join(d, 'lib', 'activity.js'))); if (!HUB_DIR) { console.log('SKIP activity test: needs ../../network-hub'); process.exit(0); } const require = createRequire(import.meta.url); let fails = 0; const ok = (c, label, extra) => { console.log((c ? 'PASS ' : 'FAIL ') + label + (extra ? ' ' + extra : '')); if (!c) fails++; }; const sleep = ms => new Promise(r => setTimeout(r, ms)); const KEY = crypto.randomBytes(24).toString('hex'); const HP = Number(process.env.ACT_HUB_PORT || 8871); const HUB = 'http://127.0.0.1:' + HP; const TMP = fs.mkdtempSync(path.join(os.tmpdir(), 'ls-act-')); let hubLog = ''; const hub = spawn(process.execPath, ['server.js'], { cwd: HUB_DIR, stdio: ['ignore', 'pipe', 'pipe'], env: Object.assign({}, process.env, { PORT: String(HP), DATA_DIR: TMP, DATABASE_URL: '', HUB_ADMIN_SECRET: '', HUB_FORUM_URL: '', HUB_FORUM_API_KEY: '', HUB_SITES: JSON.stringify({ linkspin: { secret: KEY } }) }) }); hub.stdout.on('data', d => hubLog += d); hub.stderr.on('data', d => hubLog += d); const done = code => { try { hub.kill(); } catch (e) {} try { fs.rmSync(TMP, { recursive: true, force: true }); } catch (e) {} process.exit(code); }; async function call(route, body) { const raw = JSON.stringify(body); const ts = String(Date.now()); const r = await fetch(HUB + route, { method: 'POST', body: raw, headers: { 'Content-Type': 'application/json', 'X-Hub-Site': 'linkspin', 'X-Hub-Ts': ts, 'X-Hub-Sig': crypto.createHmac('sha256', KEY).update(ts + '\n' + raw).digest('hex') } }); return { status: r.status, body: await r.json().catch(() => null) }; } const lsAt = async id => { const r = await call('/v1/activity/person', { id }); return r.body && r.body.sites && r.body.sites.linkspin ? r.body.sites.linkspin : null; }; try { for (let i = 0; i < 40; i++) { try { if ((await fetch(HUB + '/health')).ok) break; } catch (e) {} await sleep(250); } // members on the hub (joined from LinkSpin), one with a wallet const a = (await call('/v1/join', { email: 'ann@example.com', username: 'annls' })).body.person; const b = (await call('/v1/join', { email: 'ben@example.com', username: 'benls' })).body.person; const c = (await call('/v1/join', { email: 'cat@example.com', username: 'catls' })).body.person; const W = '0x' + '7'.repeat(40); const wr = await call('/v1/wallet', { email: 'cat@example.com', wallet: W }); ok(a && a.id && b && b.id && c && c.id && wr.status === 200, 'the hub is up with three LinkSpin members (one with a wallet)', JSON.stringify(wr.body).slice(0, 160)); process.env.HUB_URL = HUB; process.env.HUB_SECRET = KEY; process.env.HUB_SITE = 'linkspin'; process.env.HUB_SSO = ''; process.env.HUB_ACTIVITY = ''; const nsso = require(path.join(HERE, 'networksso.js')); const sessions = { 'tok-ann': { email: 'ann@example.com' }, 'tok-ben': { email: 'ben@example.com' } }; nsso.init({ self: 'linkspin', peer: 'iap', peerBase: () => 'http://127.0.0.1:1', isProd: false, dataDir: TMP, db: null, defaultPath: '/ads', member: async req => { const m = /sid=([^;]+)/.exec(req.headers.cookie || ''); return m ? sessions[m[1]] || null : null; } }); const UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36'; const req = (url, sid, h) => ({ method: 'GET', url, headers: Object.assign({ 'user-agent': UA, 'sec-fetch-dest': 'document', 'sec-fetch-mode': 'navigate' }, sid ? { cookie: 'sid=' + sid } : {}, h || {}) }); const res = () => ({ writeHead() { this.redirected = true; }, end() {} }); const load = async (url, sid, h) => { const r = res(); const answered = await nsso.page(req(url, sid, h), r, new URL(url, 'http://x')); return answered || !!r.redirected; }; // 1. a signed-in page load records 'linkspin' ok((await lsAt(a.id)) === null, 'before: ann has no LinkSpin activity on the hub'); ok((await load('/links', 'tok-ann')) === false, 'the page load is served normally (no redirect with the hub sign-in off)'); await sleep(500); const r1 = await lsAt(a.id); ok(r1 && r1.lastKind === 'visit' && Date.now() - r1.lastAt < 10000, 'a signed-in LinkSpin page load records site linkspin on the hub', JSON.stringify(r1)); // 2. once a day ok(nsso._pinged.has('ann@example.com'), 'ann is marked pinged for today'); await load('/ads', 'tok-ann'); await sleep(300); ok((await lsAt(a.id)).lastAt === r1.lastAt, 'a second page load the same day sends nothing more'); // 3. never for API calls, assets, bots or signed-out loads await load('/api/me', 'tok-ben'); await load('/assets/site.css', 'tok-ben'); await load('/links', 'tok-ben', { 'user-agent': 'curl/8.0' }); await load('/links', 'tok-ben', { 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors' }); await load('/links', null); await sleep(400); ok((await lsAt(b.id)) === null && !nsso._pinged.has('ben@example.com'), 'API calls, assets, bots, fetches and signed-out loads never ping'); // 4. seen(): /api/me and the heartbeat (no Sec-Fetch headers needed), the admin portal, a wallet-only session ok(nsso.seen({ email: 'Ben@Example.com' }) === true, 'seen() (the /api/me and /api/my/ping hook) takes a signed-in session'); ok(nsso.seen({ address: W }) === true && nsso.seen(null) === false && nsso.seen({}) === false, 'seen(): a wallet-only session pings by wallet; nobody signed in pings nothing'); await sleep(600); const rb = await lsAt(b.id), rc = await lsAt(c.id); ok(rb && rb.lastKind === 'visit', 'a Control Room tab kept open (no page load) still records the day on the hub', JSON.stringify(rb)); ok(rc && rc.lastKind === 'visit', 'a wallet-only session records the day for the person who owns the wallet', JSON.stringify(rc)); const n = nsso._pinged.size; nsso.seen({ email: 'ben@example.com' }); ok(nsso._pinged.size === n, 'seen() keeps the once-a-day rule'); const srv = fs.readFileSync(path.join(HERE, 'server.js'), 'utf8'); ok(/networksso\.seen\(\{ email: s\.email \|\| \(acct && acct\.email\)/.test(srv) && /touchSeen\(s\.email\)\.catch\(\(\) => \{\}\); networksso\.seen\(s\)/.test(srv) && /adminFromRequest\(req\); if \(adm && adm\.email\) networksso\.seen/.test(srv), 'server.js calls seen() from /api/me, the dashboard heartbeat and the admin portal'); } catch (e) { ok(false, 'test crashed', String(e && e.stack || e).slice(0, 400)); } if (fails) console.log('hub log:\n' + hubLog.slice(-1500)); console.log(fails ? fails + ' failed' : 'all passed'); done(fails ? 1 : 0);