// Network rate on LinkSpin (Marty, 2026-10-27, approved 4:1): views off LinkSpin's own pages cost 1/synRateDivisor of the // on-site rate. JSON store, NAS stubbed, throwaway data dir. node qa/nas-ratio.test.cjs // Covers: N=4 NAS views (1 credit per 20 banner views, fractions exact), partner-zone views (2 credits per 40, counted in // synImps, not in Views here), on-site batches unchanged, budget is the limit, a lowered rate tops NAS up once, N=1 = old rule. 'use strict'; delete process.env.DATABASE_URL; const fs = require('fs'); const os = require('os'); const path = require('path'); const ok = [], bad = []; const t = (n, c, extra) => { (c ? ok : bad).push(n + (c || extra === undefined ? '' : ' -> ' + extra)); }; const DATA = fs.mkdtempSync(path.join(os.tmpdir(), 'ls-nas-ratio-')); const nas = require('../nas.js'); const rows = {}; const calls = { topUp: [], deactivate: 0 }; nas.enabled = () => true; nas.impressionsPerCredit = type => type === 'banner' ? 5 : 10; nas.readServed = async id => { const r = rows[id]; return r ? { assigned: r.assigned, remaining: r.remaining, served: Math.max(0, Math.min(r.assigned, r.assigned - r.remaining)), clicks: 0 } : null; }; nas.deactivate = async id => { const r = rows[id]; const served = Math.max(0, r.assigned - r.remaining); r.remaining = 0; calls.deactivate++; return { ok: true, served }; }; nas.topUp = async (id, add) => { rows[id].assigned += add; rows[id].remaining += add; calls.topUp.push([id, add]); return { ok: true }; }; nas.restart = async (id, base, add) => { rows[id].assigned = base + add; rows[id].remaining = add; return { ok: true }; }; nas.updateCreative = async () => ({ ok: true }); const ads = require('../ads.js'); const zones = require('../zones.js'); const ADV = 'ratio@qa.test', PUB = 'pub@qa.test', FILE = path.join(DATA, 'campaigns.json'); const banner = (id, x) => Object.assign({ id, owner: ADV, memberId: 0, type: 'banner', name: 'QA ' + id, targetUrl: 'https://example.com/', imageUrl: 'https://example.com/b.png', width: 728, height: 90, budget: 1000, spent: 0, accrued: 0, imps: 0, batchImps: 0, clicks: 0, status: 'active', created: Date.now() }, x || {}); const seed = camps => { fs.writeFileSync(FILE, JSON.stringify({ v: 1, nextId: 9100, campaigns: camps, burnsPending: [] })); ads.init({ dataDir: DATA, chain: { creditBalance: async () => 0 } }); }; const C = id => JSON.parse(fs.readFileSync(FILE, 'utf8')).campaigns.find(c => c.id === id); const used = c => (c.spent || 0) + (c.accrued || 0); const req = { headers: { referer: 'https://pubsite.org/page', 'user-agent': 'Mozilla/5.0 (QA)', 'x-forwarded-for': '203.0.113.9' }, socket: {} }; (async () => { t('default divisor is 4', ads.synRateDivisor() === 4, String(ads.synRateDivisor())); // partner zones seed([banner(9001)]); fs.writeFileSync(path.join(DATA, 'zone-rates.json'), JSON.stringify({ nasOn: 0 })); zones.init({ dataDir: DATA, ads, siteHost: 'linkspin.test' }); await ads.addEarned(ADV, 100000); const z = (await zones.create(PUB, { name: 'QA zone', site: 'pubsite.org', size: '728x90' })).zone; for (let i = 0; i < 39; i++) await zones.serve(z.id, req, {}); let c = C(9001); t('N=4: 39 partner-zone views charge nothing yet', used(c) === 0, String(used(c))); await zones.serve(z.id, req, {}); c = C(9001); t('the 40th completes one batch: 2 credits (1/4 of 2 per 10)', used(c) === 2, String(used(c))); t('partner-zone views counted in synImps, not in Views here', c.synImps === 40 && c.imps === 0, JSON.stringify({ syn: c.synImps, imps: c.imps })); for (let i = 0; i < 10; i++) await ads.serve('banner', { width: 728, height: 90 }); c = C(9001); t('on-site: 10 views still charge 2 credits', used(c) === 4 && c.imps === 10, JSON.stringify({ used: used(c), imps: c.imps })); // NAS rows[9002] = { assigned: 20000, remaining: 20000 }; seed([C(9001), banner(9002, { nasAdId: 9002, nasServed: 0 })]); rows[9002].remaining -= 1010; await ads.reconcileNas(); t('N=4: 1,010 network views charge 50 credits (1 per 20)', used(C(9002)) === 50, String(used(C(9002)))); rows[9002].remaining -= 10; await ads.reconcileNas(); t('10 more complete the 51st credit (fraction carried)', used(C(9002)) === 51, String(used(C(9002)))); // budget is the limit rows[9003] = { assigned: 200, remaining: 200 }; seed([C(9001), C(9002), banner(9003, { budget: 10, nasAdId: 9003, nasServed: 0 })]); rows[9003].remaining -= 200; await ads.reconcileNas(); const x = C(9003); t('budget is the limit: 10 credits, ad out, NAS stopped', used(x) === 10 && x.status === 'out' && calls.deactivate >= 1, JSON.stringify({ used: used(x), st: x.status })); // old rule, then the rate lowered ads.setRates({ synRateDivisor: 1 }); rows[9004] = { assigned: 1000, remaining: 1000 }; seed([C(9001), C(9002), C(9003), banner(9004, { nasAdId: 9004, nasServed: 0 })]); rows[9004].remaining -= 500; await ads.reconcileNas(); t('N=1 (old rule): 500 network views charge 100 credits', used(C(9004)) === 100, String(used(C(9004)))); ads.setRates({ synRateDivisor: 4 }); const b0 = calls.topUp.length; await ads.reconcileNas(); await ads.reconcileNas(); const tu = calls.topUp.slice(b0).filter(q => q[0] === 9004); t('rate lowered to 1/4: NAS topped up once to what 900 credits now buy (18,000)', tu.length === 1 && rows[9004].remaining === 18000, JSON.stringify({ tu, row: rows[9004] })); console.log('PASS ' + ok.length); for (const b of bad) console.log('FAIL ' + b); try { fs.rmSync(DATA, { recursive: true, force: true }); } catch (e) {} process.exit(bad.length ? 1 : 0); })().catch(e => { console.error(e); process.exit(1); });